I have a raster, and I'm trying to create a polygon and export to a shapefile that is showing one solid layer between lets say 1000m and 3000m in elevation.

So it seems like I need to filter out all the data outside of those elevations, but I'm not sure how to go about that.

How do I do this using GDAL in Python?

  • 3
    You could try gdal_calc -A your_raster -calc"logical_or(A>1000,A<3000) " based on this post gis.stackexchange.com/questions/204387/… that should make a binary raster with 0 and 1, polygonize the binary raster - it's less intensive than a continuous raster. Commented May 9, 2018 at 4:11
  • What does the A represent?
    – m0ngr31
    Commented May 9, 2018 at 4:52
  • 1
    A is your raster, specified by -A.. this allows you to have multiple rasters in the calculation -A first_raster -B second_raster etc.. then in the calc you can specify A operator B (-calc "A+B" for example), read about it gdal.org/gdal_calc.html Commented May 9, 2018 at 5:19
  • 1
    Is your gdal raster binary? How are you exporting to a shapefile? This might help docs.qgis.org/2.8/en/docs/training_manual/complete_analysis/… get you back on track. Commented May 9, 2018 at 5:59
  • 2
    Feel free to put in your own answer with screen shots and links based on any direction you've gleaned from my comments and I'll upvote it tomorrow when I get back in. Commented May 9, 2018 at 6:09

1 Answer 1


After playing around with the links Michael gave me above, this ended up being the solution:

gdal_calc.py -A idaho.tif --outfile=idaho-filtered.tif --calc="1*logical_and(A>=1000,A<3000)" --NoDataValue=0
gdal_polygonize.py idaho-filtered.tif -f "ESRI Shapefile" idaho-filtered.shp idaho-filtered

Here's the results...

Original: enter image description here

After gdal_calc: enter image description here

Shapefile: enter image description here

  • 1
    Excellent, especially the section about --NoDataValue=0 to eliminate the polygons you're not interested in. If you have the time can you put in a screen shot of the original idaho.tif, binary idaho-filtered.tif and the resulting polygon. Commented May 9, 2018 at 22:23
  • Thank you! I had a lot of trouble finding how to do that! Commented Dec 30, 2019 at 18:18

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.