An Embarrassment of Contours
This post discusses how a large collection of contour shapefiles were merged together, dissolved and then a subset of contours were selected and exported.
Here’s an example of a relatively quick and easy GIS job that frequently comes across my desktop. Woody from SOLA contacted me with a question about some data he had. It was 0.5 m contour data (i.e. the vertical distance between contours was 0.5 m) but he was only interested in the 5 m contours. 0.5 m contour data is not that easy to come by, let alone 5 m, and this sounded like a nice dataset.
Once I got a copy of the data (which he got from a surveyor) I could see that he had 43 shapefiles, each roughly between 1 and 5 Mb in size, with a contour interval of 0.5 m – sweet. Sounds like an easy job – just enough to keep me out of trouble for a little while. I looked over the attribute tables to ensure they all had the same attributes and also checked to see that they were all in the same coordinate system. Here’s what they looked like added to a map:
The data cover an area of around 4,000 hectares in the Canterbury high country.
Before we go too far, note the coordinates at lower right: . These stand out to a NOT being in the standard coordinate system (New Zealand Transverse Mercator) and I suspected the data are in one of the surveyor’s circuit coordinate systems, probably the Timaru one. How did I know that they weren’t NZTM? Sadly, lots of time spent working with those coordinate values… No major problem for what we’re wanting, but it could be an issue later on he wants to use these data with other layers and this fits with one key GIS principle: Spatial Analyst, know thy data.
So, first things first – I really don’t want to have to work with 43 separate files so I used the Merge tool to, well, merge them all into one layer. Here’s the output:
Now I’ve just got one layer to worry about. Here’s a quick look at the table to see what we’re working with:
I’ve got my ELEVATION attribute which is important – this holds the raw data that make contours useful. We’ve also got this LAYER attribute which looks it might be useful for mapping – this might be an easy way to symbolise the index and minor contours.
Given that I’ve got 37,391 contour features, I’m wondering if there’s a way I can simplify this layer and when I sort the ELEVATION values in ascending order, I noted that each elevation value has multiple features with the same value. For example, there are 33 features with a value of 530.5.
This layer is a prime candidate for a Dissolve
The Dissolve tool will collapse all features with the same attribute value into one feature. So instead of having 33 features with a value of 530.5 we’ll end up with one. The only potential downside of this is that all those individual features will be treated as one feature, so if I select any one of the contours on the map, all 33 pieces of it will get selected. Probably okay in this case, but something to be aware of.
Here’s how I set up the tool:
By ticking both LAYER and ELEVATION, the tool will compress all those features with the same value into one feature. After running the tool, here’s my table:
With a bit more inspection, it turns out the the “Major” value in the LAYER is exactly what I need – all of the “Major” values are divisible by 5.
So that thoughtful surveyor has added an attribute that suddenly makes this job a lot easier.
All I really have to do is run a query (Select by Attribute) to find all the records where the LAYER attribute is equal to “Major”
Then I can export these records to a new shapefile by right-clicking on the layer name and going to Data > Export Data. This results in a new shapefile with just the 5 m contours in it. A key idea here is that if I’ve got any records selected when I run most geoprocessing tools, it will only work on those selected records – that’s an important factor that should always be borne in mind. Here are those features overlain on the original data (5 m contours in black):
So just to summarise, I started with 43 separate shapefiles, all with the same attributes and coordinates system. Then I merged them together and dissolved the features based on the elevation values and type of contour. To get just the 5 m contours I used a query to select them and the exported those selected features. Here’s a flow diagram that summarises this process graphically:
Job done. Now I can go off and see what other sorts of spatial trouble I can get myself into. It usually seems to find me…