xrspatial.interpolate.idw#

xrspatial.interpolate.idw(x, y=None, z=None, template=None, power=2.0, k=None, fill_value=nan, name='idw', *, column=None)[source]#

Inverse Distance Weighting interpolation.

Supports NumPy, CuPy, Dask with NumPy, and Dask with CuPy backed templates; the output backend matches the template. The k-nearest mode (k is not None) runs on the CPU backends only (NumPy and Dask with NumPy); GPU templates require k=None and raise NotImplementedError otherwise.

Parameters:
  • x (array-like) – Coordinates and values of scattered input points. Alternatively, pass a GeoDataFrame of Point geometries as the first argument and leave y/z unset; template and column must then be keywords.

  • y (array-like) – Coordinates and values of scattered input points. Alternatively, pass a GeoDataFrame of Point geometries as the first argument and leave y/z unset; template and column must then be keywords.

  • z (array-like) – Coordinates and values of scattered input points. Alternatively, pass a GeoDataFrame of Point geometries as the first argument and leave y/z unset; template and column must then be keywords.

  • template (xr.DataArray) – 2-D DataArray whose grid defines the output raster.

  • power (float, default 2.0) – Distance weighting exponent.

  • k (int or None, default None) – Number of nearest neighbours. None uses all points (numba JIT); an integer uses scipy.spatial.cKDTree (CPU only).

  • fill_value (float, default np.nan) – Value for pixels with zero total weight.

  • name (str, default 'idw') – Name of the output DataArray.

  • column (str, optional) – When the first argument is a GeoDataFrame, the column whose values are interpolated. Defaults to the first numeric column.

Returns:

Interpolated raster (float64) on the same grid, coords, dims, and attrs as template. Input points containing NaN/inf in any of x, y, z are dropped before interpolation; pixels with zero total weight take fill_value.

Return type:

xr.DataArray

Raises:

MemoryError – When k is set on a numpy-backed template and the (grid_pixels, k) distance and index arrays from the cKDTree query would exceed 80% of available memory.

Examples

>>> import numpy as np, xarray as xr
>>> from xrspatial.interpolate import idw
>>> template = xr.DataArray(
...     np.zeros((2, 2)),
...     dims=["y", "x"],
...     coords={"y": [1.0, 0.0], "x": [0.0, 1.0]},
... )
>>> idw([0.0, 1.0], [0.0, 1.0], [0.0, 10.0], template).values
array([[ 5., 10.],
       [ 0.,  5.]])