Using Virtual Rasters to Generate Contours in QGIS

Every now and again, I am asked to make maps. It’s not my strongest suit, but it sometimes comes with the territory. My latest task, as mentioned in my previous post, involves building support for MBTiles databases into a mobile situational awareness tool. This is done so that the devices can have a persistent local basemap in the field. The need arose to ensure that the basemaps were high contrast to assist with visibility in bright sunlight. Something like this:

One of the requirements was to have topographic-map-like contours to indicate changes in elevation. Existing map services didn’t provide what we needed so it was necessary to build a custom map, which meant generating contour lines. It had been years since I had last done that with Esri tools, but I didn’t have any extension licenses available, so I turned to QGIS to get the job done this time.

My area of interest was a portion of Virginia. Since I couldn’t find any pre-generated contours for the state, I turned to elevation models. There are numerous places to get such data, but I downloaded some DEMs from Radford University since they are already carved up by county. They are perhaps a bit dated, but they sufficed for this particular testing round.

It was easy to find guidance on how to generate contours in QGIS. So I ran the process on a couple of adjacent counties and noticed that the edges didn’t line up, which was not surprising. My first thought was that I would need to merge the DEMs but, luckily, I stumbled across the virtual raster tool in QGIS. This tool provides a nice UI for building a GDAL virtual raster from a series of separate rasters specified by the user. This can be a bit cumbersome to do manually and this GUI tool was a real time saver. It can be found in QGIS Dufour here:


To make my life easier, I moved all of my DEMs into one folder so I could just point the tool at the folder. I filled in the name of the output file and took the defaults for everything else.

Notice that the dialog shows me the GDAL command that I am building with the UI. Advanced users can even edit it here. This is a really nice feature that can help you get comfortable with GDAL. I am not a GDAL expert, nor am I particularly adept with raster operations so I found this to be a huge help and I plan to use it more.

The tool doesn’t change any data; it merely writes a text file so it works very quickly. The resulting virtual raster was done in a few seconds.

With the data now “merged,” I was able to continue with data generation. For my purposes, 10-meter contours were more than sufficient. I generated a shapefile, but any QGIS-supported format is valid as an output. It should be noted that the “Attribute name” choice is not checked by default. Check this if you want to attach the elevation value to each line. Also notice that QGIS is again giving us the relevant GDAL command as we build it. This is very powerful as it gives you the option to use QGIS to prototype GDAL operations and then script them outside of QGIS, if you desire.

This process took a little longer, thanks to Fauquier County, but still finished in about 90 seconds. The resulting contours were contiguous across counties, so my needs were met.

I’m now in the process of styling the map in TileMill so that I can generate the databases. It’s good to occasionally take off my developer hat and put on that of a user. I’ve known for quite a while that QGIS stands toe-to-toe with any other desktop GIS software but this work got me to use some tools that I rarely ever touch. I was impressed with not only the speed, but also how smoothly the work flowed. My pedestrian laptop didn’t engage in nearly the same level of huffing and puffing that it does when I have to use other software. That may be a hidden “win” in that users can extend the useful life of their hardware by using tools that don’t tax it as much while producing the same results.