I have an SRTM DEM and I want contour lines generated for specific elevations. This can easily be done using r.contour.level/step etc. but the contours generated are smooth and cover the cells only partially (red lines in image).

Are there any methods by which I could generate a contour line that encloses the cell boundaries above a desired elevation.

Image description: Red line is the 3950m contour generated by QGIS. The yellow line is one I need: a contour enclosing all cells >= 3950m).


EDIT: The goal is to extract the surface area of elevation ranges (50-100,100-150 etc.) in my raster. I have a surface area raster, and the next step is to calculate zonal statistics of the raster based on a defined polygon area of interest as my contours are occur every 50m, they seemed ideal for the mask. BUT Using a smooth contour vector gives me incomplete zonal statistics for every elevation polygon. This is could be because the 3950 contour polygon does not "completely" enclose pixels >= 3950. In the example below, the the zonal stats only gave me data for 5 pixels. The correct data should include 12 pixels (which I manually checked to be >= 3950m) as enclosed by the yellow line.

  • Maybe create a temp raster as a copy of your DEM and then delete from the temp raster all those cells where ele<3950 (script this with python if you have more ranges that need to be processed) and then see if that helps you any further.
    – til_b
    Commented Apr 4, 2014 at 8:54

2 Answers 2


Hmmm, interesting problem! Although I'm not quite sure why you'd need to do it.

I would approach it by first converting the raster to vector, either in GRASS or QGIS (it sounds like you're using both). Following that I would round the z values in the vector to your desired contour levels and then merge these together.

If you've a lot of files to process, it should be possible to automate your workflow with python.


I think this is a job for the QGIS raster calculator, have a look at "Using a Mask", here:


In your case you will only need something like (my_raster@1 >= 3950), don't use the second part of the expression shown.

You could then polygonise the output raster.


  • 1
    @N: thanks for the link! This would be the right answer if I did not have to calculate zonal stats and only extract an elevational range throughout the raster. Since I have peak areas which are isolated at different elevations from the surrounding landscape, I will have to create vector polygons for individual peak areas. This I have already completed.
    – csheth
    Commented Apr 4, 2014 at 10:50

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.