Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there a way to use the gdal_contour utility with-in the python console of qgis ? Thank you

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Renaud, if you are looking to just make contours and not specifically via the console, look to Nathan W's blog post on the core GdalTools plugin currently available in QGIS:

Generating contours using GDAL ( via shell or QGIS)

If you are looking to generate contours via a Python script or PyQGIS plugin, look to these resources:

GdalTools Plugin

(QGIS source)/python/plugins/GdalTools/tools/GdalTools_utils.py for functions for finding the gdal binaries and setting up environmental variables.

(QGIS source)/python/plugins/GdalTools/tools/doContour.py for how the user interface for the plugin could be manipulated via Python. You would first trigger() the Raster->Extraction->Contour menu's QAction, then manipulate the shown dialog, eventually clicking OK to start the generation of contours.

Doing the same as GdalTools in a script/plugin will leverage Python's subprocess module or Qt's QProcess class. Both are used to run an external process, such as gdal_contour in this case.

Python Binding

svn.osgeo.org/gdal/trunk/autotest/alg/contour.py for how the gdal test suite tests the osgeo.gdal.ContourGenerate() Python binding, which can be used directly in Python code instead of using Python to run the gdal_contours binary.

C public callable GDALContourGenerate(), as a reference when using the above Python binding.

Attempting to generate contours just via the Python console in QGIS would probably be more laborious than writing a script/plugin to do so. If you really need to use the console, Python's subprocess module would probably be the fastest means.

share|improve this answer
dakcarto, Thank you very much for this detailed answer. I'am using the python console to automotize Map generation. Your python binding ressource seems the more appropriate.However, I cannot find the gdal.ContourGenerate() method. I am working on a ubuntu machine where osgeo.version_info gives 2.7.3. I don't know if the problem comes from here. I will give the Python's subprocess module a try, and will let you posted. Thanks again –  Renaud Jun 18 '12 at 12:36
something very simple like : cmands = "gdal_contour -a MAG -i " + str(inc) + " " + fileName_raster + " " + contour_dir and subprocess(cmands,shell=True) works fine for me. Thank you for the tip ! –  Renaud Jun 18 '12 at 13:02
Good to hear you have a workable solution. On my Mac, with osgeo version 2.6.1, doing 'import osgeo' then 'osgeo.gdal.ContourGenerate()' generates the following error: 'TypeError: Required argument 'srcBand' (pos 1) not found'. This tells me that the gdal.ContourGenerate() call is available in my older version of osgeo package, but, of course, is missing needed arguments as noted in the C docs. Just in case you wanted to try that route. –  dakcarto Jun 18 '12 at 19:05
I don't understand what's wrong with my module :>>> import osgeo >>> osgeo.gdal.ContourGenerate() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'gdal' –  Renaud Jun 19 '12 at 7:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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