xrspatial.zonal.apply#
- xrspatial.zonal.apply(zones, values: DataArray, func: Callable, nodata: int | None = 0, column: str | None = None, rasterize_kw: dict | None = None, name: str | None = None) DataArray[source]#
Apply a function to the values agg within zones in zones agg. Returns a new DataArray with the function applied.
- Parameters:
zones (xr.DataArray, GeoDataFrame, or list of (geometry, value) pairs) –
Zone definitions. Can be:
A 2D xarray DataArray of integers.
A
geopandas.GeoDataFrame(requires column).A list of
(shapely geometry, zone_id)pairs.
When vector input is provided,
rasterize()is called internally using values as the template grid. Userasterize_kw={'dtype': int, 'fill': 0}to produce integer zones (required byapply);rasterizerejects an integer dtype with the default NaN fill (#2504).values (xr.DataArray) – values.data is either a 2D or 3D array of integers or floats. The input value raster.
func (callable function to apply.)
nodata (int, default=0) – Nodata value in zones raster. Cells with nodata does not belong to any zone, and thus excluded from calculation. Set to None to apply func to all cells.
column (str, optional) – Column name in the GeoDataFrame that contains zone IDs. Required when zones is a GeoDataFrame.
rasterize_kw (dict, optional) – Extra keyword arguments forwarded to
rasterize()when zones is vector input.name (str, optional) – Output
xr.DataArray.nameproperty. Defaults toNone, which is the same across every backend (without it the dask backends inherit an internal task name instead).
- Returns:
result – A new DataArray with the same shape, dims, coords, and attrs as values, with func applied to cells within zones.
- Return type:
xr.DataArray
Examples
>>> import numpy as np >>> import xarray as xr >>> from xrspatial.zonal import apply >>> zones_val = np.array([ [1, 1, 0, 2], [0, 2, 1, 2]]) >>> zones = xr.DataArray(zones_val) >>> values_val = np.array([ [2, -1, 5, 3], [3, np.nan, 20, 10]]) >>> agg = xr.DataArray(values_val) >>> func = lambda x: 0 >>> result = apply(zones, agg, func) >>> result array([[0, 0, 5, 0], [3, np.nan, 0, 0]])