I have a (continent-scale) raster with 30m resolution in an Albers equal area projection. Each 30m cell has a value 1-34.
I would like to aggregate information from this raster into a 1km grid such that each 1km grid cell contains a floating-point value which is the number of 30m cells with a value of 2 divided by the total number of 30m cells which fit into the 1km cell.
Is there a way of doing this with GDAL? Or an efficient way to hook GDAL and Python together to perform the calculation (given the large size of the raster, not all of it fits into RAM at once)?
Best Answer
Command-line
First, convert cells equal to 2 to 1, and not equal to 0. Create a second file using
gdal_calc.py
:And then to aggregate the averages of a resolution, use
gdalwarp
with-r average
, which does:Python/GDAL
Within the Python/GDAL API, similar work can be done. The Numpy processing will be similar, except that you might need to process chunks of the input raster if it is larger than your RAM. Look at the source code for
gdal_calc.py
to get an idea how this can be done, if necessary.The second step of aggregating is accomplished with GDALReprojectImage, and might look something like: