xrspatial.classify.binary#

xrspatial.classify.binary(agg, values, name='binary')[source]#

Binarize a data array based on a set of values. Data that equals to a value in the set will be set to 1. In contrast, data that does not equal to any value in the set will be set to 0. Note that NaNs and infinite values will be set to NaNs.

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

  • values (array-like object) – Values to keep in the binarized array.

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

Returns:

binarized_agg – 2D aggregate array of binarized data array. All other input attributes are preserved. If agg is a Dataset, returns a Dataset with each variable classified independently.

Return type:

xr.DataArray or xr.Dataset

Examples

Binary works with NumPy backed xarray DataArray

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial.classify import binary

>>> data = np.array([
    [np.nan,  1.,  2.,  3.,  4.],
    [5.,  6.,  7.,  8.,  9.],
    [10., 11., 12., 13., 14.],
    [15., 16., 17., 18., np.inf],
], dtype=np.float32)
>>> agg = xr.DataArray(data)
>>> values = [1, 2, 3]
>>> agg_binary = binary(agg, values)
>>> print(agg_binary)
<xarray.DataArray 'binary' (dim_0: 4, dim_1: 5)>
array([[np.nan,  1.,  1.,  1.,  0.],
       [0.,  0.,  0.,  0.,  0.],
       [0.,  0.,  0.,  0.,  0.],
       [0.,  0.,  0.,  0.,  np.nan]], dtype=float32)
Dimensions without coordinates: dim_0, dim_1