Skip to main content

ridge_core/
lib.rs

1#![doc = include_str!("../README.md")]
2//!
3//! ## Implementation notes
4//!
5//! A port of the Python `ridge_map` package (with the parts of `SRTM.py`,
6//! `numpy` and `scipy.ndimage` it relies on) to Rust, minus matplotlib:
7//! rendering is delegated to [`svg`] (server-side export) or to the web
8//! frontend, which consumes [`geometry::RidgeScene`] JSON.
9
10pub mod colormap;
11pub mod geometry;
12pub mod grid;
13pub mod preprocess;
14pub mod rotate;
15pub mod srtm;
16pub mod svg;
17
18pub use geometry::{RidgeRow, RidgeScene};
19pub use srtm::{Tile, TileSource};
20
21use serde::{Deserialize, Serialize};
22
23/// Default bounding box from upstream ridge_map: The White Mountains, NH.
24pub const DEFAULT_BBOX: Bbox = Bbox {
25    lon0: -71.928864,
26    lat0: 43.758201,
27    lon1: -70.957947,
28    lat1: 44.465151,
29};
30
31/// Geographic bounding box, `(long, lat, long, lat)` of the bottom-left and
32/// top-right corners, exactly like upstream `RidgeMap.__init__`.
33#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
34pub struct Bbox {
35    pub lon0: f64,
36    pub lat0: f64,
37    pub lon1: f64,
38    pub lat1: f64,
39}
40
41impl Bbox {
42    pub fn new(lon0: f64, lat0: f64, lon1: f64, lat1: f64) -> Self {
43        Self {
44            lon0,
45            lat0,
46            lon1,
47            lat1,
48        }
49    }
50
51    /// Bottom and top latitude (upstream `lats` property).
52    pub fn lats(&self) -> (f64, f64) {
53        (self.lat0, self.lat1)
54    }
55
56    /// Left and right longitude (upstream `longs` property).
57    pub fn longs(&self) -> (f64, f64) {
58        (self.lon0, self.lon1)
59    }
60
61    /// Figure aspect ratio (height / width) used by upstream `plot_map`.
62    pub fn ratio(&self) -> f64 {
63        (self.lat1 - self.lat0) / (self.lon1 - self.lon0)
64    }
65
66    pub fn is_valid(&self) -> bool {
67        self.lon1 > self.lon0
68            && self.lat1 > self.lat0
69            && self.lon0 >= -180.0
70            && self.lon1 <= 180.0
71            && self.lat0 >= -60.0
72            && self.lat1 <= 60.0
73    }
74}
75
76impl Default for Bbox {
77    fn default() -> Self {
78        DEFAULT_BBOX
79    }
80}
81
82#[derive(Debug, thiserror::Error)]
83pub enum Error {
84    #[error("invalid bounding box: {0:?}")]
85    InvalidBbox(Bbox),
86    #[error("elevation data contains no valid points for this bbox")]
87    EmptyData,
88    #[error("srtm tile error: {0}")]
89    Srtm(String),
90    #[error("io error: {0}")]
91    Io(#[from] std::io::Error),
92}
93
94pub type Result<T> = std::result::Result<T, Error>;