Skip to content

Resampling

resample(raster, resolution, resampling_method='bilinear')

Resamples raster according to given resolution.

Parameters:

Name Type Description Default
raster DatasetReader

The raster to be resampled.

required
resolution Number

Target resolution i.e. cell size of the output raster.

required
resampling_method Literal[nearest, bilinear, cubic, average, gauss, max, min]

Resampling method. Most suitable method depends on the dataset and context. Nearest, bilinear and cubic are some common choices. This parameter defaults to bilinear.

'bilinear'

Returns:

Type Description
ndarray

The resampled raster data.

dict

The updated metadata.

Raises:

Type Description
NumericValueSignException

Resolution is not a positive value.

Source code in eis_toolkit/raster_processing/resampling.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@beartype
def resample(
    raster: rasterio.io.DatasetReader,
    resolution: Number,
    resampling_method: Literal["nearest", "bilinear", "cubic", "average", "gauss", "max", "min"] = "bilinear",
) -> Tuple[np.ndarray, dict]:
    """Resamples raster according to given resolution.

    Args:
        raster: The raster to be resampled.
        resolution: Target resolution i.e. cell size of the output raster.
        resampling_method: Resampling method. Most suitable
            method depends on the dataset and context. Nearest, bilinear and cubic are some
            common choices. This parameter defaults to bilinear.

    Returns:
        The resampled raster data.
        The updated metadata.

    Raises:
        NumericValueSignException: Resolution is not a positive value.
    """
    if resolution <= 0:
        raise NumericValueSignException(f"Expected a positive value for resolution: {resolution})")

    method = RESAMPLE_METHOD_MAP[resampling_method]
    out_image, out_meta = _resample(raster, resolution, method)
    return out_image, out_meta