I have a DEM file that I created a slope map of using gdaldem slope. This works fine and if I view and colorize it, I can now see how steep a hill is but now I'd love to create a shapefile out of that. So I'd like to have polygons with increments of let's say 5% for each level of steepness.

I though I would go ahead and create gdal_contour of that slope-DEM file and using the -p option to create polygons rather than lines, but the problem is, that this takes FOREVER. Even a small sample file of a few square-kilometers (and about 20MB in file-size) is taking a couple of hours.

My command was: gdal_contour -i 5 -amin 30 -p input.tif output.shp So increments of 5% starting at 30% and polygons.

How can I approach my problem?

Additional Info: For @gabriels anwser. Here's what that looks. So you can basically see each "pixel". enter image description here

  • 1
    One question per Question, please.
    – Vince
    Jan 14, 2021 at 12:10
  • I just removed the second question! thx
    – Georg
    Jan 14, 2021 at 12:22

1 Answer 1


Problem is that contour lines may not be closed, so it's hard to create the polygons.

Simplest GDAL way seems to me classify the raster and then polygonize it.

gdal_calc.py --calc='(A<=30)*30+(A>30)*A//5*5' --outfile=classified.tif -A input.tif

The calc expression turns all values less than 30 to 30, and greater values are rounded to the floor mutiplier of 5. Then, polygonize the classified raster:

gdal_polygonize.py classified.tif output.shp

  • That looks pretty promising!!! Thank you! One question. The result has no "aliasing" (not sure about the correct word). It looks very edgy... I'll update my question and add a screenshot so you can see what I mean. Is there any way around that?
    – Georg
    Jan 15, 2021 at 13:54
  • You are welcome. Yes, it's the price we pay to obtain all valid geometries. You can subsample the source image with a bilinear method to get some interpolation before the classification. Here a non pure GDAL alternative: gis.stackexchange.com/a/350386/133276. Jan 15, 2021 at 21:50
  • Ok I see... So there is no other way to do that efficiently? Cause even when upscaling the image, in the end it will still always be "rectangular" shapes, right?
    – Georg
    Jan 19, 2021 at 6:40
  • Polygonize the raster is always rectangular. Smooth are contour lines, but you must create closed lines to polygonize them. Jan 19, 2021 at 9:33
  • Ok, but the -p for gdal_contour should do that, right? But just very slow?!?
    – Georg
    Jan 20, 2021 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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