Downsampling raster (average) with regard to minimum filled pixel

66 Views Asked by At

I want to downsample raster by scale of 3 (from 5m to 15m cellsize). I used rasterio with resample average method. However this generalize area with plenty of NA pixels and created undesirable result. Now I want to add condition to this resample method by only calculate the average on at least 5 out of 9 pixels filled with valid number, maximum 4 pixels are NA.

I used rasterio code as follow for the simple average resampling.

import rasterio
from rasterio.enums import Resampling

scale_factor = 1/3

with rasterio.open(rast) as dataset:

    # resample data to target shape
    profile = dataset.profile.copy()
    data = dataset.read(
        out_shape=(
            dataset.count,
            int(dataset.height * scale_factor),
            int(dataset.width * scale_factor)
        ),
        resampling=Resampling.average
    )

    # scale image transform
    transform = dataset.transform * dataset.transform.scale(
        (dataset.width / data.shape[-1]),
        (dataset.height / data.shape[-2])
    )

    profile.update({"height": data.shape[-2],
                    "width": data.shape[-1],
                   "transform": transform})
    


with rasterio.open(output, "w", **profile) as dataset:
    dataset.write(data)

is there any way to add such condition to the resampling process using rasterio? I am thinking of using numpy but still not sure about how to set the condition. I attach the picture for example of what I am trying to achieve.

enter image description here

0

There are 0 best solutions below