[GIS] GDAL/OGR to extract pixel values

gdalogrpython

I want to use GDAL/OGR to extract pixel values from a raster file. The extraction should be based on polygons, stored in a shapefile. My thought was to iterate over the features in the shapefile, extract the pixel values of those pixels that have there center point within the polygon and calculate a percentile from those values. When this is done I want to write the calculated percentile back to the polygon and go on with the next polygon.

Iterating over the polygons is easy, but what I do not get is how to extract the pixel values from the pixels, covered by the polygon.

I'm using Python.

Could somebody help me at this point?

Best Answer

Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.

Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3

In that blog post I rasterize vector data to subset pixels from an image.

The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.

You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.

Next, you can do the rest of the calculations.