Skip to main content

geolith_tiles_builder/layers/
mod.rs

1//! Tile layer definitions and feature processing.
2//!
3//! Each layer maps Overture features to protomaps-schema MVT layers.
4
5pub use geolith::layer::Layer;
6
7pub mod bathymetry;
8pub mod boundaries;
9pub mod buildings;
10pub mod earth;
11pub mod landcover;
12pub mod landuse;
13pub mod places;
14pub mod pois;
15pub mod roads;
16pub mod transit;
17pub mod water;
18
19use crate::source::input::InputFeature;
20use buildings::ProcessedFeature;
21
22/// Dispatch a feature to the correct layer processor.
23///
24/// Routes based on the target layer. Each processor internally checks
25/// the feature's [`DataSource`] to decide which processing path to take.
26/// Returns `None` if the feature should be skipped by the layer processor.
27pub fn dispatch(feature: &InputFeature, target_layer: Layer) -> Option<ProcessedFeature> {
28    match target_layer {
29        Layer::Earth => earth::process(feature),
30        Layer::Water => water::process(feature),
31        Layer::Boundaries => boundaries::process(feature),
32        Layer::Landcover => landcover::process(feature),
33        Layer::Landuse => landuse::process(feature),
34        Layer::Roads => roads::process(feature),
35        Layer::Transit => transit::process(feature),
36        Layer::Buildings => buildings::process(feature),
37        Layer::Places => places::process(feature),
38        Layer::Pois => pois::process(feature),
39        Layer::Bathymetry => bathymetry::process(feature),
40        _ => None,
41    }
42}
43
44/// Dispatch a feature and return every feature the layer produces.
45///
46/// Most layers yield at most one feature. Buildings may add an address
47/// point, and Overture road segments are split where bridge, tunnel, or
48/// link flags cover only part of the line.
49#[must_use]
50pub fn dispatch_all(feature: &InputFeature, target_layer: Layer) -> Vec<ProcessedFeature> {
51    match target_layer {
52        Layer::Buildings => buildings::process_all(feature),
53        Layer::Roads => roads::process_all(feature),
54        _ => dispatch(feature, target_layer).into_iter().collect(),
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::source::input::DataSource;
62    use crate::tile::feature::PropValue;
63    use std::collections::HashMap;
64    use std::sync::Arc;
65
66    fn overture(otype: &str, geometry: geo::Geometry<f64>, props: &[(&str, &str)]) -> InputFeature {
67        InputFeature {
68            id: 1,
69            geometry: Arc::new(geometry),
70            properties: props
71                .iter()
72                .map(|(k, v)| (k.to_string(), PropValue::Str(v.to_string())))
73                .collect::<HashMap<_, _>>(),
74            bbox: None,
75            source: DataSource::Overture {
76                theme: String::new(),
77                otype: otype.into(),
78            },
79        }
80    }
81
82    fn line() -> geo::Geometry<f64> {
83        geo::Geometry::LineString(geo::LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]))
84    }
85
86    #[test]
87    fn dispatch_all_splits_a_partially_bridged_road() {
88        let feat = overture(
89            "segment",
90            line(),
91            &[("class", "primary"), ("road_flags.is_bridge", "0.2:0.5")],
92        );
93        let out = dispatch_all(&feat, Layer::Roads);
94        assert_eq!(out.len(), 3);
95        assert!(out.iter().all(|p| p.layer == Layer::Roads));
96        assert_eq!(
97            dispatch(&feat, Layer::Roads).map(|p| p.layer),
98            Some(Layer::Roads)
99        );
100    }
101
102    #[test]
103    fn dispatch_all_routes_buildings_through_process_all() {
104        let poly = geo::Geometry::Polygon(geo::Polygon::new(
105            geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
106            vec![],
107        ));
108        let feat = overture("building", poly, &[]);
109        // No address fields, so exactly the building itself.
110        let out = dispatch_all(&feat, Layer::Buildings);
111        assert_eq!(out.len(), 1);
112        assert_eq!(out[0].layer, Layer::Buildings);
113    }
114
115    #[test]
116    fn dispatch_all_yields_at_most_one_feature_for_other_layers() {
117        let feat = overture("water", line(), &[("subtype", "river")]);
118        let out = dispatch_all(&feat, Layer::Water);
119        assert_eq!(out.len(), 1);
120        assert_eq!(out[0].layer, Layer::Water);
121
122        let dropped = overture("infrastructure", line(), &[("subtype", "power")]);
123        assert!(dispatch_all(&dropped, Layer::Transit).is_empty());
124    }
125}