7

I am trying to downsample a 1 square km raster dataset to a much larger (.5 degree x .66 degree) dataset by summing all of the pixel values within this large grid cell.

gdal_warp does not contain a summation resampling method so I'm wondering if anyone has figured this out before.

0

2 Answers 2

9

Depending on the version of GDAL, there are a few different resample options available; see gdalwarp.

GDAL 1.10 or later using -r average

average resampling, computes the weighted average of all non-NODATA contributing pixels

This isn't tested, but should look something like:

gdalwarp -t_srs EPSG:4326 -tr 0.5 0.66 -r average fine_one_sq_km.tif coarse_average.tif

Then to get the sum, multiply the average by the number of pixels of the fine resolution raster in one pixel of the coarse resolution raster, which hopefully is constant (you could assume it is).

GDAL 3.1 or later using -r sum

compute the weighted sum of all non-NODATA contributing pixels

This should look like this:

gdalwarp -t_srs EPSG:4326 -tr 0.5 0.66 -r sum fine_one_sq_km.tif coarse_sum.tif

Otherwise, scipy.ndimage.measurements.sum_labels (or sum for older versions) can be used to aggregate multidimensional sums. But this may rely on perfect matchings between grids.

5
  • That would work but I would have to create a labeling matrix the same size as my larger resolution grid. I can do that but I was hoping there was something in numpy/scipy/gdal that looked like: gdalwarp ... -r sum ...
    – DavBar
    Jun 30, 2015 at 3:04
  • Multiplying the average by the number of 1 square kilometers cells could work for this (accuracy isn't my top priority). Thanks!
    – DavBar
    Jun 30, 2015 at 3:06
  • 1
    Yes: avg = sum / count, so sum = avg * count.
    – Mike T
    Jun 30, 2015 at 3:12
  • ...I guess I over thought the problem...
    – DavBar
    Jun 30, 2015 at 14:31
  • 5
    Note: careful with rasters with null values. The average is calculated without considering nulls, so then to multiply that value by the number of input pixels contributing to a larger output pixel would give you an incorrect sum of the input. Setting regions of no data to 0 in the source dataset is probably a sensible idea in most cases where summation is meaningful, so that it influences the average value appropriately. Having a sum resampling method would actually be an excellent contribution to gdalwarp, since it would consider no data appropriately and automatically. Apr 10, 2019 at 22:08
2

Apparently, gdalwarp got a new sum method in GDAL release 3.1.0, see release notes. Adapting from the above solution:

gdalwarp -t_srs EPSG:4326 -tr 0.5 0.66 -r sum fine_one_sq_km.tif coarse_sum.tif

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.