xrspatial.classify.reclassify

xrspatial.classify.reclassify(agg: xarray.core.dataarray.DataArray, bins: List[int], new_values: List[int], name: Optional[str] = 'reclassify')xarray.core.dataarray.DataArray[source]

Reclassifies data for array (agg) into new values based on bins.

agg: xarray.DataArray

2D array of values to be reclassified. NumPy, CuPy, NumPy-backed Dask, or Cupy-backed Dask array.

bins: array-like object

Values or ranges of values to be changed.

new_values: array-like object

New values for each bin.

name: str, optional (default = “reclassify”)

Name of output aggregate.

xarray.DataArray, reclassified aggregate.

2D array of new values. All input attributes are preserved.

Adapted from PySAL:

Imports >>> import numpy as np >>> import xarray as xr >>> from xrspatial.classify import reclassify

Create Initial DataArray >>> np.random.seed(1) >>> agg = xr.DataArray(np.random.randint(2, 8, (4, 4)), >>> dims = [“lat”, “lon”]) >>> height, width = agg.shape >>> _lon = np.linspace(0, width - 1, width) >>> _lat = np.linspace(0, height - 1, height) >>> agg[“lon”] = _lon >>> agg[“lat”] = _lat >>> print(agg) <xarray.DataArray (lat: 4, lon: 4)> array([[7, 5, 6, 2],

[3, 5, 7, 2], [2, 3, 6, 7], [6, 3, 4, 6]])

Coordinates:
  • lon (lon) float64 0.0 1.0 2.0 3.0

  • lat (lat) float64 0.0 1.0 2.0 3.0

Reclassify >>> bins = list(range(2, 8)) >>> new_val = list(range(20, 80, 10)) >>> reclassify_agg = reclassify(agg, bins, new_val) >>> print(reclassify_agg) <xarray.DataArray ‘reclassify’ (lat: 4, lon: 4)> array([[70., 50., 60., 20.],

[30., 50., 70., 20.], [20., 30., 60., 70.], [60., 30., 40., 60.]], dtype=float32)

Coordinates:
  • lon (lon) float64 0.0 1.0 2.0 3.0

  • lat (lat) float64 0.0 1.0 2.0 3.0