geolith-tiles-builder 0.4.2

Builds tiles from OpenStreetMap and Overture Maps: protomaps-schema vector PMTiles, Terrain-RGB, raster archives and 3D Tiles from Gaussian splats
Documentation
//! Bathymetry layer — underwater depth contours from Overture `base/bathymetry`.

use crate::layers::Layer;
use crate::source::input::InputFeature;
use crate::tile::feature::PropValue;

use super::buildings::ProcessedFeature;

/// Process an Overture base/bathymetry feature into a bathymetry layer feature.
///
/// Returns `None` if the feature has no usable geometry.
pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
    let min_zoom = feature
        .get_i64("cartography.min_zoom")
        .map(|z| z.clamp(0, 15) as u8)
        .unwrap_or(5);

    let max_zoom = feature
        .get_i64("cartography.max_zoom")
        .map(|z| z.clamp(0, 15) as u8)
        .unwrap_or(8)
        .min(15);

    // Overture writes `depth` as INTEGER, which the reader decodes as I64.
    // Reading it only as F64 dropped it from every tile.
    let depth = feature
        .get_i64("depth")
        .map(|d| d as f64)
        .or_else(|| feature.get_f64("depth"));

    // Since 2026-09-23 levels nest: the feature at depth `d` covers all water
    // deeper than `d`, so a deeper level sits inside every shallower one and
    // must paint after it. Without a cartography hint, order by depth
    // (0..=3000 m in the release) so shallow levels come first.
    let sort_rank = feature
        .get_i64("cartography.sort_key")
        .map(|s| s.clamp(0, 1000) as i32)
        .or_else(|| depth.map(|d| (d.abs() / 10.0).clamp(0.0, 1000.0) as i32))
        .unwrap_or(200);

    let mut properties = Vec::with_capacity(2);
    properties.push(("kind".into(), PropValue::Str("bathymetry".into())));

    if let Some(depth) = depth {
        properties.push(("depth".into(), PropValue::F64(depth)));
    }

    Some(ProcessedFeature {
        id: feature.id,
        geometry: feature.geometry.clone(),
        layer: Layer::Bathymetry,
        min_zoom,
        max_zoom,
        sort_rank,
        properties,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::source::input::DataSource;
    use std::collections::HashMap;
    use std::sync::Arc;

    fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
        let mut properties = HashMap::new();
        for (k, v) in props {
            properties.insert(k.to_string(), v.clone());
        }
        InputFeature {
            id: 1,
            geometry: Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
                geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
                vec![],
            ))),
            properties,
            bbox: None,
            source: DataSource::Overture {
                theme: String::new(),
                otype: String::new(),
            },
        }
    }

    #[test]
    fn default_bathymetry() {
        let feat = make_feature(&[]);
        let result = process(&feat).unwrap();
        assert_eq!(result.layer, Layer::Bathymetry);
        assert_eq!(result.min_zoom, 5);
        assert_eq!(result.max_zoom, 8);
        assert_eq!(result.sort_rank, 200);
        assert!(
            result
                .properties
                .iter()
                .any(|(k, v)| k == "kind" && *v == PropValue::Str("bathymetry".into()))
        );
    }

    #[test]
    fn depth_property_included() {
        let feat = make_feature(&[("depth", PropValue::F64(-500.0))]);
        let result = process(&feat).unwrap();
        assert!(
            result
                .properties
                .iter()
                .any(|(k, v)| k == "depth" && *v == PropValue::F64(-500.0))
        );
    }

    /// Overture's `depth` column is INTEGER; the reader decodes it as I64.
    #[test]
    fn integer_depth_reaches_the_tile() {
        let feat = make_feature(&[("depth", PropValue::I64(1500))]);
        let result = process(&feat).unwrap();
        assert!(
            result
                .properties
                .iter()
                .any(|(k, v)| k == "depth" && *v == PropValue::F64(1500.0))
        );
    }

    /// Nested levels (2026-09-23): 0 m covers the whole ocean, 3000 m only the
    /// abyss inside it. Deeper levels must sort after shallower ones so they
    /// paint on top; the 10 levels in the release map to ranks 0..=300.
    #[test]
    fn deeper_levels_sort_after_shallower_ones() {
        let rank = |d: i64| {
            process(&make_feature(&[("depth", PropValue::I64(d))]))
                .unwrap()
                .sort_rank
        };
        let levels = [0, 10, 50, 100, 500, 1000, 1500, 2000, 2500, 3000];
        let ranks: Vec<i32> = levels.iter().map(|&d| rank(d)).collect();
        assert_eq!(ranks, vec![0, 1, 5, 10, 50, 100, 150, 200, 250, 300]);
    }

    #[test]
    fn a_cartography_sort_key_overrides_depth_ordering() {
        let feat = make_feature(&[
            ("depth", PropValue::I64(3000)),
            ("cartography.sort_key", PropValue::I64(7)),
        ]);
        assert_eq!(process(&feat).unwrap().sort_rank, 7);
    }

    #[test]
    fn cartography_hints_respected() {
        let feat = make_feature(&[
            ("cartography.min_zoom", PropValue::I64(3)),
            ("cartography.max_zoom", PropValue::I64(10)),
            ("cartography.sort_key", PropValue::I64(150)),
        ]);
        let result = process(&feat).unwrap();
        assert_eq!(result.min_zoom, 3);
        assert_eq!(result.max_zoom, 10);
        assert_eq!(result.sort_rank, 150);
    }

    #[test]
    fn zoom_clamped_to_valid_range() {
        let feat = make_feature(&[
            ("cartography.min_zoom", PropValue::I64(-5)),
            ("cartography.max_zoom", PropValue::I64(20)),
        ]);
        let result = process(&feat).unwrap();
        assert_eq!(result.min_zoom, 0);
        assert_eq!(result.max_zoom, 15);
    }

    #[test]
    fn defaults_bounded_to_low_zoom_to_prevent_explosion() {
        let feat = make_feature(&[]);
        let result = process(&feat).unwrap();
        assert!(
            result.max_zoom <= 10,
            "bathymetry default max_zoom must stay low to prevent planet-scale stripe_clip explosion"
        );
    }
}