Contour Lines in Qgis

What with the GSoC project and all the other general discussion about analysis tools in QGis, I got somehow sidetracked into pondering how to do contour maps in QGis. There's the hard way: writing a bunch of C++ code to implement a contouring algorithm, then hooking that into QGis and into the GUI for raster drawing properties. Ah, no.

So quick hackery time. Take a raster data set into R, use R's contour() function to get a list of contour lines and heights, then convert that into a SpatialLinesDataFrame using functions in the "sp" package, then convert that to a shapefile of lines using functions in the "rgdal" package. Load resulting shapefile into QGis and look at the pretty contour lines. Style to taste. See attached screenshot (if it uploads ok).

It's far from perfect - when R draws contour plots it puts the labels on the contour lines in a very nice way. Fiddling with line labelling in QGis couldn't get me that effect. Eventually I might package this up as a plugin, but for now I'll just have to leave you to bash your own code together to do this!

It did get me thinking about other "transformative" plugins though - from polygons to point-centroids, from points to voronoi polygons and so forth. Maybe these can be done via GRASS, but it might interesting to implement them using Shapely...

Okay I think that qualifies as rambling enough...

contours.png68.37 KB

Nice Hack


It is always fun to see a new way to cobble together new functionality out of separate pieces of software. Maybe I don't fully understand the QGIS long term plans, but... Shouldn't analysis-type operations be delegated to the software that can already perform these tasks like GRASS ? Especially since QGIS and GRASS have a nice tight coupling as it is, I would expect that the contouring routines in GRASS are good enough, and quite possibly more robust than a cobbled together approach. Don't get me wrong, I am not criticizing your post, rather it seems that some of that effort could be better spent on the making the areas where QGIS currently excels better (data viz and management, etc.). I like the idea of more plug-ins, but the ones you have listed are already implemented in GRASS, PostGIS, ... many other places.

My 4c (after inflation of the weak US dollar),



Sure, let's use what we can - I used the contouring routines in R, but to the user it shouldn't matter if it's using GRASS behind the scenes. The data still has to get to the other app and then back to QGis somehow. Perhaps the R method is slightly more convoluted since it needs to write an OGR data set, but then again using r.contour in Grass means writing a new grass map layer, and hence you need a Grass mapset set up.

I didn't have Grass support on the Qgis I had to hand, but I did have R and Rpy working, so I thought I'd have a play with that, just to see what a contour map as a line-vector map would feel like. I think the approach of having a direct rendering method in Qgis would be so much better, of course.

There's plenty of free and open source code for contouring floating around, so it could in theory be integrated into QGis, together with nicely labelled heights on the contour lines, and thick and thin lines and so on....


Please Implement This!

This is a feature that I would love to see go into QGIS. I waste a great deal of effort and space by generating shapefiles from my DEM files - one shapefile for 20 and/or 40 foot contours, one shapefile for 100 and or 200 foot contours - then have to load up the separate layers and manage them independently in QGIS.

I would be nice if I could just select a checkbox on the display properties for a DEM file in QGIS, "Display Contour Lines", and specify the contour interval and line properties.

Here's how I currently generate my contour shapefiles on the command line:

gdal_contour -i 100 -3d -snodata 0 mydem.tif mydem_contour100

This generates a 100 foot contour shapefile, where each line has an attribute specifying its elevation, 0 is the "no data" value in the DEM.