xrspatial.resample.resample#
- xrspatial.resample.resample(agg: DataArray, scale_factor: float | tuple[float, float] | None = None, target_resolution: float | tuple[float, float] | None = None, method: str = 'nearest', nodata: float | None = None, name: str = 'resample') DataArray[source]#
Change raster resolution without changing its CRS.
Exactly one of scale_factor or target_resolution must be given.
- Parameters:
agg (xarray.DataArray or xarray.Dataset) – Input raster. 2-D
(y, x)or 3-D(band, y, x). For 3-D inputs each band is resampled independently and the leading non-spatial coordinate is preserved. If a Dataset is passed, the operation is applied to each data variable independently (via the@supports_datasetdecorator).scale_factor (float or (float, float), optional) – Multiplicative factor applied to the number of pixels.
0.5halves the pixel count (doubles the cell size);2.0doubles the pixel count (halves the cell size). A two-element tuple sets(scale_y, scale_x)independently.target_resolution (float or (float, float), optional) – Desired cell size in the same units as the raster coordinates. A scalar sets both axes to the same resolution; a 2-tuple sets
(res_y, res_x)independently.method (str, default
'nearest') – Resampling algorithm. Interpolation methods ('nearest','bilinear','cubic') work for both upsampling and downsampling. Aggregation methods ('average','min','max','median','mode') only support downsampling (scale_factor <= 1).nodata (float, optional) – Sentinel value in the input that should be treated as missing. Input pixels equal to nodata are replaced with NaN before resampling. When
None, falls back toagg.attrs['_FillValue']thenagg.attrs['nodata']. The output uses NaN as the sentinel regardless of the input convention.name (str, default
'resample') – Name for the output DataArray.
- Returns:
Resampled raster with updated coordinates and
resattribute. Output dtype matches the input float dtype (float32 or float64); integer inputs return float32 since NaN-sentinel resampling requires a float type.- Return type:
xarray.DataArray
- Raises:
ValueError – If
agghas a zero-length spatial dimension; if neither or both ofscale_factorandtarget_resolutionare given; if either is a sequence whose length is not 2; if any component is zero, negative, NaN, or infinite; ifmethodis not inALL_METHODS; if the spatial coordinates ofaggare not strictly monotonic and evenly spaced (resampleonly supports regular monotonic rasters); or ifnodatadoes not round-trip exactly into an integeragg.dtype(a fractional or out-of-range sentinel that would wrap on the cast).