xrspatial.hydro.flow_direction_d8.flow_direction_d8#

xrspatial.hydro.flow_direction_d8.flow_direction_d8(agg: DataArray, name: str = 'flow_direction', boundary: str = 'nan') DataArray[source]#

Compute D8 flow direction for each cell.

Determines which of the 8 neighbors has the steepest downhill gradient from the center cell. Uses the standard power-of-two D8 direction encoding:

32  64  128
16   0    1
 8   4    2
Parameters:
  • agg (xarray.DataArray or xr.Dataset) – 2D NumPy, CuPy, NumPy-backed Dask, or CuPy-backed Dask xarray DataArray of elevation values. If a Dataset is passed, the operation is applied to each data variable independently.

  • name (str, default='flow_direction') – Name of output DataArray.

  • boundary (str, default='nan') – How to handle edges where the kernel extends beyond the raster. 'nan' - fill missing neighbours with NaN (default). 'nearest' - repeat edge values. 'reflect' - mirror at boundary. 'wrap' - periodic / toroidal.

Returns:

2D array of D8 flow direction codes. Valid values are {0, 1, 2, 4, 8, 16, 32, 64, 128} for interior cells (0 indicates a pit or flat with no downhill neighbor). Edge cells and cells with NaN in their 3x3 window are NaN.

Return type:

xarray.DataArray or xr.Dataset

References

Jenson, S.K. and Domingue, J.O. (1988). Extracting Topographic Structure from Digital Elevation Data for Geographic Information System Analysis. Photogrammetric Engineering and Remote Sensing, 54(11), 1593-1600.