Method to resample 2d numpy array to a coarser grid and weighted by pixel area in python (analogy to grass r.resmap.stats -w)

grass-gisnumpypythonrasterresampling

What is the optimal method in Python, when I need to resample 2d numpy array (grid) to coarser resolution (weighted by pixel area, the case when the new resolution is not the exact multiple of the original resolution) ?

In other words, for many years I enjoy GRASS GIS module r.resamp.stats -w when resampling raster data to a coarser resolution. Flag -w is important here, because the algorithm include the weight according to pixel area:

With the -w switch, the aggregate uses the values from all input cells
which intersect the output cell, weighted according to the proportion
of the source cell which lies inside the output cell. This is slower,
but produces a more accurate result.

E.g. resampling of 6m DEM (left) to 20m DEM (right) with weighted resampling is correct (image from GRASS GIS manual). What would be the analogy of this GRASS module in Python numpy or scipy?

resampling DEM from 6m to 20m grid

Best Answer

If you're willing to use OpenCV then you could use cv.resize() with the cv.INTER_AREA flag

Related Question