Is there a way to use the gdal_contour utility within the Python console of QGIS?

2 Answers 2


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.

  • 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, 2012 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, 2012 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, 2012 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, 2012 at 7:53

For those coming across this question more recently, since at least QGIS 2.18 (probably earlier), you have been able to use the GDAL Contour processing tool via the Python Console.

In QGIS 3, to create 10m contours from an input file:

inputFilename = 'C:/temp/dem.tif'
resultContour = processing.run("gdal:contour", { 'BAND' : 1, 'CREATE_3D' : False, 'EXTRA' : '', 'FIELD_NAME' : 'ELEV', 'IGNORE_NODATA' : False, 'INPUT' : inputFilename, 'INTERVAL' : 10, 'NODATA' : None, 'OFFSET' : 0, 'OUTPUT' : 'TEMPORARY_OUTPUT' })['OUTPUT']
## Or alternatively do further processing on resultContour

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.