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 user defined bins.

Parameters
  • agg (xarray.DataArray) – 2D NumPy, CuPy, NumPy-backed Dask, or Cupy-backed Dask array of values to be reclassified.

  • bins (array-like object) – Values or ranges of values to be changed.

  • new_values (array-like object) – New values for each bin.

  • name (str, default='reclassify') – Name of output aggregate array.

Returns

reclass_agg – 2D aggregate array of reclassified allocations. All other input attributes are preserved.

Return type

xarray.DataArray, of the same type as agg

References

Examples

import numpy as np
import xarray as xr
import dask.array as da
from xrspatial.classify import reclassify

elevation = np.array([
    [np.nan,  1.,  2.,  3.,  4.],
    [ 5.,  6.,  7.,  8.,  9.],
    [10., 11., 12., 13., 14.],
    [15., 16., 17., 18., 19.],
    [20., 21., 22., 23., np.inf]
])
data = xr.DataArray(elevation, attrs={'res': (10.0, 10.0)})
bins = [10, 20, 30]
new_values = [1, 2, 3]
data_reclassify = reclassify(data, bins=bins, new_values=new_values)
>>> print(data)
<xarray.DataArray (dim_0: 5, dim_1: 5)>
array([[nan,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.],
       [10., 11., 12., 13., 14.],
       [15., 16., 17., 18., 19.],
       [20., 21., 22., 23., inf]])
Dimensions without coordinates: dim_0, dim_1
Attributes:
    res:      (10.0, 10.0)

>>> print(data_reclassify)
<xarray.DataArray 'reclassify' (dim_0: 5, dim_1: 5)>
array([[nan,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  2.,  2.,  2.,  2.],
       [ 2.,  2.,  2.,  2.,  2.],
       [ 2.,  3.,  3.,  3., nan]], dtype=float32)
Dimensions without coordinates: dim_0, dim_1
Attributes:
    res:      (10.0, 10.0)