Skip to main content

d8_clipped

Function d8_clipped 

Source
pub fn d8_clipped(
    d8: &mut Array2<u8>,
    nodata: u8,
    onland: &Array2<bool>,
) -> Vec<usize>
Expand description

Updates a D8 flow direction using a mask that indicates onland and ocean and return changed flattened indices.

This function will ensure your downstream traces calculated from a D8 will stop at the coast. Any cell whose direction leads us to the ocean will become a sink. Any cell in the ocean will be nodata

An alternative approach (which doesn’t work) is to first mask the DEM with nodata and then calculate the D8. Unfortunately this results in downstream traces that get to the coast (nodata) and then head along the coast, sometimes for ages before coming to a natural sink.

§Parameters

d8: A 2D u8 array representing the directions nodata: The nodata for the d8 (probably 255) onland: A 2D bool array representing the land

§Returns

nothing, but updates the d8

§Example

use ndarray::Array2;
let mut d8 = Array2::from_shape_vec(
    (3, 3),
    vec![
        4, 2, 1,
        2, 2, 1,
        1, 128, 4,
    ],
).expect("Failed to create D8");
let onland = Array2::from_shape_vec(
    (3, 3),
    vec![
        true, true, false,
        true, true, false,
        true, true, false,
    ],
).expect("Failed to create onland");
let newd8 = Array2::from_shape_vec(
    (3, 3),
    vec![
        4, 0, 255,
        2, 0, 255,
        1, 128, 255,
    ],
).expect("Failed to create D8");
let changed = hydro_analysis::d8_clipped(&mut d8, 255, &onland);
assert_eq!(d8, newd8);
assert_eq!(changed, vec![1, 4]);