[GIS] GDAL (Python) Aggregate Raster Into Lower Resolution

gdalpythonraster

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:

$ gdal_calc.py -A input.tif --calc="A == 2" --outfile equals2.tif

And then to aggregate the averages of a resolution, use gdalwarp with -r average, which does:

average resampling, computes the average of all non-NODATA contributing pixels. (GDAL >= 1.10.0)

$ gdalwarp -tr 30 30 -r average equals2.tif equals2-averaged_30m.tif

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:

gdal.ReprojectImage(src_ds, dst_ds, None, None, gdal.GRA_Average)