Skip to main content

Crate terrand

Crate terrand 

Source
Expand description

terrand — Pure-Rust terrain analysis on ndarray.

Provides DEM (Digital Elevation Model) analysis without GDAL or any C dependencies. All operations take ndarray::Array2<f64> as input and return ndarray arrays as output, making them composable with the rest of the Rust scientific computing ecosystem.

§Modules

ModuleOperations
slopeSlope in degrees, radians, or percent
aspectSlope direction as compass bearing
curvatureProfile, plan, and general curvature
hillshadeShaded-relief illumination
roughnessTRI, TPI, and surface roughness
hydrologyFill, flow direction, accumulation, watershed, basin, stream order
viewshedLine-of-sight visibility analysis
contourMarching-squares contour generation

§Quick start

use ndarray::Array2;
use terrand::{slope, aspect, hillshade, CellSize};

// A synthetic DEM with a uniform east-facing slope.
let dem = Array2::from_shape_fn((100, 100), |(_, c)| c as f64 * 10.0);
let cell = CellSize::square(30.0).unwrap();

let s = slope(&dem, cell);
let a = aspect(&dem, cell);
let hs = hillshade(&dem, cell, 315.0, 45.0);

§Parallelism

Enable the parallel feature to use Rayon for multi-threaded computation on all per-cell operations:

[dependencies]
terrand = { package = "terrand-rs", version = "0.1", features = ["parallel"] }

§NaN and nodata handling

NaN handling is operation-specific. Surface-analysis kernels generally propagate NaN through their 3x3 arithmetic on normal-size grids, but their small-grid fallbacks return documented flat values. Hydrology treats NaN as nodata for elevation rasters: fill leaves NaN cells unchanged, while flow_direction encodes NaN cells as direction 0. Contour generation treats NaN cells as holes and skips quads containing them.

§Algorithms

Slope, aspect, and hillshade use the Horn (1981) 3x3 weighted gradient kernel with GDAL-compatible linear extrapolation at grid edges. Curvature uses Horn first-order gradients with the same edge extrapolation, plus finite-difference second derivatives clamped at grid edges.

Re-exports§

pub use aspect::aspect;
pub use aspect::ASPECT_FLAT;
pub use cell_size::CellSize;
pub use curvature::general_curvature;
pub use curvature::plan_curvature;
pub use curvature::profile_curvature;
pub use error::Error;
pub use error::Result;
pub use hillshade::hillshade;
pub use hydrology::basin;
pub use hydrology::fill;
pub use hydrology::flow_accumulation;
pub use hydrology::flow_direction;
pub use hydrology::snap_pour_point;
pub use hydrology::stream_order_strahler;
pub use hydrology::watershed;
pub use roughness::roughness;
pub use roughness::tpi;
pub use roughness::tri;
pub use slope::slope;
pub use slope::slope_percent;
pub use slope::slope_radians;
pub use viewshed::viewshed;
pub use viewshed::ViewshedConfig;
pub use viewshed::EARTH_RADIUS_M;

Modules§

aspect
Aspect (slope direction) computation from a Digital Elevation Model.
cell_size
Cell-size metadata for regular DEM grids.
contour
Contour line generation using the marching squares algorithm.
curvature
Surface curvature computation from a Digital Elevation Model.
error
Error types returned by fallible terrand operations.
hillshade
Hillshade illumination from a Digital Elevation Model.
hydrology
Hydrological analysis on Digital Elevation Models.
roughness
Terrain roughness indices computed from a 3x3 neighborhood.
slope
Slope computation from a Digital Elevation Model using the Horn algorithm.
viewshed
Viewshed analysis: determine which cells are visible from an observer point.