Skip to main content

geolith_tiles_builder/layers/
bathymetry.rs

1//! Bathymetry layer — underwater depth contours from Overture `base/bathymetry`.
2
3use crate::layers::Layer;
4use crate::source::input::InputFeature;
5use crate::tile::feature::PropValue;
6
7use super::buildings::ProcessedFeature;
8
9/// Process an Overture base/bathymetry feature into a bathymetry layer feature.
10///
11/// Returns `None` if the feature has no usable geometry.
12pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
13    let min_zoom = feature
14        .get_i64("cartography.min_zoom")
15        .map(|z| z.clamp(0, 15) as u8)
16        .unwrap_or(5);
17
18    let max_zoom = feature
19        .get_i64("cartography.max_zoom")
20        .map(|z| z.clamp(0, 15) as u8)
21        .unwrap_or(8)
22        .min(15);
23
24    // Overture writes `depth` as INTEGER, which the reader decodes as I64.
25    // Reading it only as F64 dropped it from every tile.
26    let depth = feature
27        .get_i64("depth")
28        .map(|d| d as f64)
29        .or_else(|| feature.get_f64("depth"));
30
31    // Since 2026-09-23 levels nest: the feature at depth `d` covers all water
32    // deeper than `d`, so a deeper level sits inside every shallower one and
33    // must paint after it. Without a cartography hint, order by depth
34    // (0..=3000 m in the release) so shallow levels come first.
35    let sort_rank = feature
36        .get_i64("cartography.sort_key")
37        .map(|s| s.clamp(0, 1000) as i32)
38        .or_else(|| depth.map(|d| (d.abs() / 10.0).clamp(0.0, 1000.0) as i32))
39        .unwrap_or(200);
40
41    let mut properties = Vec::with_capacity(2);
42    properties.push(("kind".into(), PropValue::Str("bathymetry".into())));
43
44    if let Some(depth) = depth {
45        properties.push(("depth".into(), PropValue::F64(depth)));
46    }
47
48    Some(ProcessedFeature {
49        id: feature.id,
50        geometry: feature.geometry.clone(),
51        layer: Layer::Bathymetry,
52        min_zoom,
53        max_zoom,
54        sort_rank,
55        properties,
56    })
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use crate::source::input::DataSource;
63    use std::collections::HashMap;
64    use std::sync::Arc;
65
66    fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
67        let mut properties = HashMap::new();
68        for (k, v) in props {
69            properties.insert(k.to_string(), v.clone());
70        }
71        InputFeature {
72            id: 1,
73            geometry: Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
74                geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
75                vec![],
76            ))),
77            properties,
78            bbox: None,
79            source: DataSource::Overture {
80                theme: String::new(),
81                otype: String::new(),
82            },
83        }
84    }
85
86    #[test]
87    fn default_bathymetry() {
88        let feat = make_feature(&[]);
89        let result = process(&feat).unwrap();
90        assert_eq!(result.layer, Layer::Bathymetry);
91        assert_eq!(result.min_zoom, 5);
92        assert_eq!(result.max_zoom, 8);
93        assert_eq!(result.sort_rank, 200);
94        assert!(
95            result
96                .properties
97                .iter()
98                .any(|(k, v)| k == "kind" && *v == PropValue::Str("bathymetry".into()))
99        );
100    }
101
102    #[test]
103    fn depth_property_included() {
104        let feat = make_feature(&[("depth", PropValue::F64(-500.0))]);
105        let result = process(&feat).unwrap();
106        assert!(
107            result
108                .properties
109                .iter()
110                .any(|(k, v)| k == "depth" && *v == PropValue::F64(-500.0))
111        );
112    }
113
114    /// Overture's `depth` column is INTEGER; the reader decodes it as I64.
115    #[test]
116    fn integer_depth_reaches_the_tile() {
117        let feat = make_feature(&[("depth", PropValue::I64(1500))]);
118        let result = process(&feat).unwrap();
119        assert!(
120            result
121                .properties
122                .iter()
123                .any(|(k, v)| k == "depth" && *v == PropValue::F64(1500.0))
124        );
125    }
126
127    /// Nested levels (2026-09-23): 0 m covers the whole ocean, 3000 m only the
128    /// abyss inside it. Deeper levels must sort after shallower ones so they
129    /// paint on top; the 10 levels in the release map to ranks 0..=300.
130    #[test]
131    fn deeper_levels_sort_after_shallower_ones() {
132        let rank = |d: i64| {
133            process(&make_feature(&[("depth", PropValue::I64(d))]))
134                .unwrap()
135                .sort_rank
136        };
137        let levels = [0, 10, 50, 100, 500, 1000, 1500, 2000, 2500, 3000];
138        let ranks: Vec<i32> = levels.iter().map(|&d| rank(d)).collect();
139        assert_eq!(ranks, vec![0, 1, 5, 10, 50, 100, 150, 200, 250, 300]);
140    }
141
142    #[test]
143    fn a_cartography_sort_key_overrides_depth_ordering() {
144        let feat = make_feature(&[
145            ("depth", PropValue::I64(3000)),
146            ("cartography.sort_key", PropValue::I64(7)),
147        ]);
148        assert_eq!(process(&feat).unwrap().sort_rank, 7);
149    }
150
151    #[test]
152    fn cartography_hints_respected() {
153        let feat = make_feature(&[
154            ("cartography.min_zoom", PropValue::I64(3)),
155            ("cartography.max_zoom", PropValue::I64(10)),
156            ("cartography.sort_key", PropValue::I64(150)),
157        ]);
158        let result = process(&feat).unwrap();
159        assert_eq!(result.min_zoom, 3);
160        assert_eq!(result.max_zoom, 10);
161        assert_eq!(result.sort_rank, 150);
162    }
163
164    #[test]
165    fn zoom_clamped_to_valid_range() {
166        let feat = make_feature(&[
167            ("cartography.min_zoom", PropValue::I64(-5)),
168            ("cartography.max_zoom", PropValue::I64(20)),
169        ]);
170        let result = process(&feat).unwrap();
171        assert_eq!(result.min_zoom, 0);
172        assert_eq!(result.max_zoom, 15);
173    }
174
175    #[test]
176    fn defaults_bounded_to_low_zoom_to_prevent_explosion() {
177        let feat = make_feature(&[]);
178        let result = process(&feat).unwrap();
179        assert!(
180            result.max_zoom <= 10,
181            "bathymetry default max_zoom must stay low to prevent planet-scale stripe_clip explosion"
182        );
183    }
184}