0

I'm looking for a function to resample a raster that can consider a minimum number of valid pixels in order to compute the new pixel value. E.g. If I have to resample a raster of 300m/px to 1Km/px I would consider a window of 3x3 pixels to compute the average value. But I would like to set a control to be sure that at least 5 pixels inside my matrix have valid data.

Example of results

I'm trying using GDAL (in PyQGIS) but neither GDAL Translate nor GDAL Warp have this option

1

It could be done with r.neighbors in grass :

  1. Sum number of cells using a 3x3

  2. Count number of cells without Nan/Null value using a 3x3

  3. Divide first generated raster by second one

Example :

Set a random region 3x3 cells

g.region rows=3 cols=3

Initialize with random cell raster

r.random.cells output=random_cells distance=0 ncells=5

Random raster created

Sum cells (3x3 is default but you could use -c option to set a different cell research)

r.neighbors input=random_cells output=sums method=sum

Count cells

r.neighbors input=random_cells output=counts method=count

Change region resolution to output desired raster resampling :

g.region res=1

Divide sum by count

r.mapcalc "outresamp = sums / counts"

Optinoal : copy colors from random cells to newly resampling raster :

r.colors map=outresamp raster=random_cells

Resampling raster excluding nan values

https://grass.osgeo.org/grass78/manuals/r.neighbors.html

Alternatively, you could use interpolation with r.fillnulls or r.resamp* methods

0

Thanks Sylvain for your answer.

At the end I understood that there isn't a way to solve this issue in only one step. So, I wrote this PyQGIS code to do all these steps: https://github.com/fgianoli/CopernicusGlobalLand/blob/master/CGL_resampler.py

Basically, I did a reclass of the input raster in 0-1 (where 1 are my valid values), then I did a resample using mode -so 0 is where at least 5 pixels inside my kernel aren't valid - and I have multiplied this results with the result of 3x3 resampling using average. In this way I have obtained my results.

enter image description here

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.