Open Source, qgis

Generating contours using GDAL ( via shell or QGIS)

I tell you. It always amazes me how much cool stuff you can do with great open source GIS software these days. GDAL is one of those great open source projects that I have just found a great use for (apart from just opening every raster type under the sun in QGIS).

GDAL has the ability to generate contours from a DEM, something that I have always wanted to try for my town but have never been able due to lack of a good DEMs.

Recently we purchased a set of DEMs that cover a large area as part of a study. Each DEM uses a grid size of 1mx1m. Prefect for generating contours.

Using the GdalTools QGIS plugin.

First make sure that you have the latest version of the GdalTools plugin installed (GdalTools should be installed by default with QGIS. If it’s not, search “Gdal” in the plugin installer). Enable the plugin once it’s installed.

Load the DEM into QGIS using the Load Raster icon.

DEM loaded in QGIS

Now head up to the menu Raster->Extraction->Contour

Raster menu in QGIS

Select the settings that you need. For this DEM I am going to generate 250mm contours.

Contour dialog.

Take note of the text area at the bottom of the dialog as that is the exact command sent to GDAL in order to generate the contours. If you take a copy of that you can run it on the command line for batch processing later.

Hit ok.

250mm contours from the DEM

BAM! :)

Using the command line/shell.

Using QGIS for a one off DEM is fine and dandy but what if you have 3000 DEMs that you need to process. To hell with doing that by hand!

Remember the contour tool in QGIS told us the exact command line args to use, so creating a shell script for automating the process is pretty easy.

for f in *.asc
  echo "Processing $f"
 gdal_contour -a ELEV -i 0.25 $f $f-250mm.shp

The above code will loop though the current directory and process all the DEMs generating 250mm contours for each one. It saves each new contour file as {filename}-250mm.shp. You will need to change *.asc to whatever format your DEM is in.

Copy the above code into a file somewhere and call it This can then be called from the command line using


Running sh on Windows

If you’re a windows user you will need to run OsGeo4W Shell in order to use sh.

Loading OsGeo4w shell.

Once the shell is loaded you can just call:


Output from running

Final remarks

I ran the above sh script on a folder with about 2500 DEMs and it took around 4 hours to complete the whole folder. Of course performance will vary but it seems pretty quick considering.

Once again the possibly to use open source GIS tools to get my work done is amazing.  No expensive software here.

So far I’m not aware of any line smoothing/generalizing abilities using GDAL/OGR.  Although you can import the contours into GRASS and use that:

You can also generate the contours using GDAL via Python but that is a topic for another day.

19 thoughts on “Generating contours using GDAL ( via shell or QGIS)

  1. > If you’re a windows user you will need to run OsGeo4W Shell in order to use sh

    Even if you don’t use the msys rxvt (with the blue M icon) instead of the msys DOS shell, at least run sh interactively.
    Type sh and press enter. Now you have a linux-like prompt, and even if you don’t learn all the awesomeness of it, at least your commands will be saved in the history so you can access them in the future.

  2. Hi
    Nice article.
    Few weeks ago I released Generalizer – plugin for QGIS to smoothing and generalizeing lines, so you don’t have to use GRASS. For now plugin is marked as experimental and I don’t have time to write help to algorithms. I will try to do that in 1.0 version.

    1. Hi,

      Thanks for bringing my attention to that plugin. It’s fantastic! I have tried all the different options and they all work great. I will update the blog post tomorrow to talk about using your plugin to smooth out the lines. I will also need to find out the best smoothing option for general viewing of contours.

  3. One thing I am missing in GDAL/OGR is to produce contour lines on vector point data, e.g. surveyed terrain points. Or maybe I have missed out something running through the documentations?

    1. I don’t think GDAL/OGR can do this directly. I know there is a plugin for QGIS to do this (my post). If you wanted to do it using GDAL you would have to:

      1: Use gdal_rasterize to convert the vector points to raster points.
      2: Run gdal_grid to interpolate a grid for the points to create a DEM.
      3: Use gdal_contour on the DEM from step 2.

      Not a quick one step process but if you wrote it into a script I think it would work well.

      1. Re: step 2
        I don’t know about converting from vector points, but if it were vector contours I’ve seen Frank Warmerdam mention that the normal procedure is to use

        Not sure how that would compare with gdal_contour

  4. Hang on, gdal_grid seems to take vector files as an input, so either you would do:
    1. gdal_rasterize
    3 gdal_contour,
    or you would do
    1. gdal_grid
    2. gdal_contour.

  5. For some reason I haven’t found out yet, I can open gdaltools, fill in all parameters, but it won’t run. So the fact that the exact command sent to GDAL is given is great.

    Just to get an idea about how fast this runs, how big are your DEMs (in terms of pixels or MB)?

  6. Very interesting the loop to run tons of rasters. However, I have tried it but I got the message that sh is neither and internal or external command. Any idea?

  7. hello,
    sorry but iam all new to qgis i was able to create the contour lines but no corect labeling how do i make the labeling follow the lines like in the example above?
    tanks a lot

  8. I’m trying to run a shell script to automate creation of hillshades using your post as a model. I get an error that windows doesn’t recognize the program when I load the shell script in the osgeo4w shell. Any ideas on what I am doing wrong. Below is the code contained within the shell script.

    for f in *.tif
    echo “Processing $f”
    gdaldem hillshade $f $f-hillshade” -z 1.0 -s 1.0 -az 315.0 -alt 45.0 -compute_edges -of GTiff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s