3

I am using the DEM files in the Cornell CUGIR repository to build contour layers in QGIS. The problem I have is that while it is easy to do, it requires many manual steps in QGIS that are both tedious and prone to error. I would like to know if there are any tools out there to automate the process.

Starting with a directory of DEM files, on a Unix box (in my case a Mac), here is what I would like to do:

  1. convert the DEM files from metric to english units (meters -> feet)
  2. create a contour layer with 100 ft intervals including labels for the elevation
  3. create a contour layer with 20 ft intervals including labels for the elevation
  4. apply the same style to all the created contour shape files

The tool I envision would either find all the applicable DEM files in a directory or take the list of files as a command line argument. I will write this myself if there isn't one out there somewhere already.

2
  • 1
    If some of the steps you do involves the use of tools from the Processing plugin, you could consider creating a model.
    – Joseph
    Commented Apr 13, 2016 at 14:03
  • 2
    or you could use a bash script and gdal for everything up to the styling
    – Ian Turton
    Commented Apr 13, 2016 at 14:04

1 Answer 1

1

The gdal command line is great for this kind of thing. Using an ASTGTM DEM file, with heights in metres, to generate a shapefile with 20 foot contours (6.096m):

gdal_contour -a elev -i 6.096 astdem.tif astdem.shp
  • -a elev: create a field called 'elev' with the contour height in metres;
  • -i 6.096: create contours at 6.096m intervals (20 feet);
  • astdem.tif - input DEM file;
  • astdem.shp - output shapefile.

Create a new field with feet:

ogrinfo astdem.shp -sql "ALTER TABLE astdem ADD COLUMN el_ft integer(5)"
ogrinfo astdem.shp -dialect SQLite -sql "UPDATE astdem SET el_ft = round(elev * 3.2804,0)"

In QGIS, create nice labels and styles for your 20' and 100' contours (the el_ft field) and save the style file as a .qml. For each new contour shapefile you just copy and rename the .qml file with the same filename but .qml extension and it will automatically load in QGIS with the right style.

You could do this as a bash script, processing all the tif files in the directory:

!#/bin/bash
for DEM in $(ls *.tif); do
  gdal_contour -a elev -i 6.096 $DEM $DEM.shp;
  ogrinfo $DEM.shp -sql "ALTER TABLE $DEM ADD COLUMN el_ft integer(5)";
  ogrinfo $DEM.shp -dialect SQLite -sql "UPDATE '$DEM' SET el_ft = round(elev * 3.2804,0)";
  cp mylabels.qml $DEM.qml;
done
5
  • This does 95% of what I was looking for and I can easily customize what you have provided for my needs. Thank you!
    – craigmcg
    Commented Apr 13, 2016 at 23:45
  • One problem so far with this solution: the contour interval is 20 feet, but instead of showing up at 20, 40, 60 ft, they are showing up at 19,39,59 etc.
    – craigmcg
    Commented Apr 14, 2016 at 12:57
  • Adding a round() seems to do the trick: ogrinfo $DEM.shp -dialect SQLite -sql "UPDATE '$DEM' SET el_ft = round(elev * 3.2804,0)";
    – craigmcg
    Commented Apr 14, 2016 at 13:40
  • 1
    Ah, good point - my test data was coming up with nicely rounded feet all by itself purely by chance (sea-level data), didn't test it with anything else.
    – Simbamangu
    Commented Apr 14, 2016 at 15:53
  • The answer above still needs one more edit in the first code block above (just to be a little nit-picky :-),
    – craigmcg
    Commented Apr 14, 2016 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.