Classification

The classification tools let you reclassify the values in an xarray DataArray into a new set of values based on set bins.

Importing Packages

[1]:
import numpy as np
import xarray as xr

import datashader as ds
from datashader.transfer_functions import shade
from datashader.transfer_functions import stack
from datashader.transfer_functions import dynspread
from datashader.transfer_functions import set_background
from datashader.colors import Elevation

import xrspatial

Generate Terrain Data

To test out the classification functions, we’ll need some rasterized data. We can generate an artificial digital elevation model (dem), or terrain, with xarray-spatial’s generate_terrain, with the help of datashader’s Canvas for the aggregation of values into a raster.

[2]:
from xrspatial import generate_terrain
from xrspatial import hillshade

W = 800
H = 600

template_terrain = xr.DataArray(np.zeros((H, W)))
x_range = (-20e6, 20e6)
y_range = (-20e6, 20e6)

terrain = generate_terrain(
    template_terrain, x_range=x_range, y_range=y_range, seed=1, zfactor=1000
)
stack(
    shade(hillshade(terrain), cmap=["grey", "white"]),
    shade(terrain, cmap=Elevation, alpha=128),
)
/home/giancastro/miniconda3/envs/xrspatial/lib/python3.8/site-packages/numba/np/ufunc/parallel.py:365: NumbaWarning: The TBB threading layer requires TBB version 2019.5 or later i.e., TBB_INTERFACE_VERSION >= 11005. Found TBB_INTERFACE_VERSION = 9107. The TBB threading layer is disabled.
  warnings.warn(problem)
[2]: