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.
2 Answers 2
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.
-
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– DavBar2015年06月30日 03:04:51 +00:00Commented 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– DavBar2015年06月30日 03:06:59 +00:00Commented Jun 30, 2015 at 3:06
-
1Yes: avg = sum / count, so sum = avg * count.Mike T– Mike T2015年06月30日 03:12:48 +00:00Commented Jun 30, 2015 at 3:12
-
...I guess I over thought the problem...DavBar– DavBar2015年06月30日 14:31:43 +00:00Commented Jun 30, 2015 at 14:31
-
5Note: 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.alphabetasoup– alphabetasoup2019年04月10日 22:08:15 +00:00Commented Apr 10, 2019 at 22:08
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