I've got a shapefile with many polygons, each of which exactly overlays a contiguous region of raster cells; an example in yellow:

enter image description here

For some of these, I want to use the polygon as a sort of analysis mask, so that I can reclassify only those cells that are overlaid by the polygon according to one of the polygon's attributes. I have to do this thousands of times to the same raster, so I'm looking for a Python-automated way to do it, using (hopefully) any of GDAL/OGR, SAGA, QGIS, or numpy arrays.


Figured this out myself, posting for posterity.

The command line utility gdal_rasterize is able to "burn-in" values into a raster, given a polygon as the area over which to do so. It accepts SQL queries, which can be used to specify certain polygons in a whole layer. From the GDAL documentation (http://www.gdal.org/gdal_rasterize.html), one can call something like

gdal_rasterize -a ROOF_H -where 'class="A"' -l footprints footprints.shp city_dem.tif

That burns the attribute value in the field ROOF_H from footprints.shp into city_dem.tif, wherever there are polygons satisfying 'class="A"'.

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.