[gdal-dev] zonal statistics with gdal

Anton Korosov anton.korosov at nersc.no
Wed Jun 20 00:47:26 PDT 2012


Hi,

though be prepared that converting large images into shape is quite 
slow. I would go opposite way: convert a shape to a raster [0, 1]; 
extract band from the image as another raster [2]; and then extract 
statistics using Numpy [e.g. 3].

[0] http://stackoverflow.com/questions/2220749/rasterizing-a-gdal-layer
[1] 
http://trac.osgeo.org/gdal/wiki/FAQRaster#HowcanIcreateablankrasterbasedonavectorfilesextentsforusewithgdal_rasterizeGDAL1.8.0
[2] http://gdal.org/gdal_tutorial.html#gdal_tutorial_read
[3] zoneMean = rasterFromBand[rasterFromShape = zoneIndex].mean()

Good luck!
Anton
On 06/19/2012 07:15 PM, Alexander Bruy wrote:
> Hi,
>
> you can use GDAL Python bindigs for this [0, 1]. The process may look
> as open shape and raster, then convert pixel coordinates into map (shape)
> coordinates and process only pixels inside your polygon.
>
> [0] http://gdal.org/gdal_tutorial.html
> [1] http://gdal.org/ogr/ogr_apitut.html
>
> 2012/6/19 jdmorgan<jdmorgan at unca.edu>:
>> Hello,
>>
>> Is there a way to do something along the lines of zonal statistics with
>> gdal.  Basically, I have a county shape file and a classified raster
>> dataset.  I would like to count the number of raster pixels/county (FIPS).
>> Is this possible with GDAL/OGR?  If so can someone point me in the right
>> direction?  If so, awesome as I plan to do some automation with python.
>>
>> Thanks,
>>
>> Derek
>
> Hope this helps



More information about the gdal-dev mailing list