pub fn d8_pointer(
dem: &Array2<f64>,
nodata: f64,
resx: f64,
resy: f64,
) -> (Array2<u8>, u8)Expand description
Calculates the D8 flow direction from a digital elevation model (DEM).
More-or-less the contents of whitebox d8_pointer
This function computes the D8 flow direction for each cell in the provided DEM:
| . | . | . |
|---|---|---|
| 64 | 128 | 1 |
| 32 | 0 | 2 |
| 16 | 8 | 4 |
Grid cells that have no lower neighbours are assigned a flow direction of zero. In a DEM that has been pre-processed to remove all depressions and flat areas, this condition will only occur along the edges of the grid.
Grid cells possessing the NoData value in the input DEM are assigned the NoData value in the output image.
§Parameters
dem: A 2D array representing the digital elevation model (DEM)nodata: The nodata in the DEMresx: The resolution of the DEM in the x-direction in metersresy: The resolution of the DEM in the y-direction in meters
§Returns
- A tuple containing:
- An
Array2<u8>representing the D8 flow directions for each cell. - A
u8nodata value (255)
§Example
use anyhow::Result;
use ndarray::{Array2, array};
use hydro_analysis::{d8_pointer};
let dem = Array2::from_shape_vec(
(3, 3),
vec![
11.0, 12.0, 10.0,
12.0, 13.0, 12.0,
10.5, 12.0, 11.0,
],
).expect("Failed to create DEM");
let d8: Array2<u8> = array![
[0, 2, 0],
[8, 1, 128],
[0, 32, 0],
];
let nodata = -9999.0;
let resx = 8.0;
let resy = 8.0;
let (res, _nd) = d8_pointer(&dem, nodata, resx, resy);
assert_eq!(res, d8);Examples found in repository?
examples/d8_pointer.rs (line 15)
5fn main() -> Result<()> {
6 let filled: Array2<f64> = array![
7 [2.0, 3.0, 3.0, 7.0],
8 [3.0, 5.0, 4.0, 2.0],
9 [5.0, 6.0, 8.0, 0.0],
10 ];
11 let nd: f64 = -100.0;
12 let resx: f64 = 8.0;
13 let resy: f64 = 8.0;
14 println!("Running d8 on {filled}");
15 let (d8, _d8_nd) = d8_pointer(&filled, nd, resx, resy);
16 println!("d8 is {d8}");
17
18 Ok(())
19}