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 crate::tile::feature::PropValue;
21use buildings::ProcessedFeature;
22
23/// A private property of a boundaries or places feature: the layer processor
24/// sets it to [`FOREIGN`] when the source says the feature belongs to
25/// another country's admin division (or is a claim name). The Survey of
26/// India mask reads it to drop such lines and labels inside India, then
27/// removes it: it never reaches a tile.
28pub const OWNER_KEY: &str = "__owner";
29
30/// The value of [`OWNER_KEY`] for an admin feature of another country.
31pub const FOREIGN: &str = "foreign";
32
33/// Mark `properties` as belonging to a division that is not India's.
34pub fn mark_foreign(properties: &mut Vec<(String, PropValue)>) {
35    properties.push((OWNER_KEY.into(), PropValue::Str(FOREIGN.into())));
36}
37
38/// `feature` without its private properties, and whether it was marked
39/// [`FOREIGN`].
40#[must_use]
41pub fn take_owner(mut feature: ProcessedFeature) -> (ProcessedFeature, bool) {
42    let foreign = feature
43        .properties
44        .iter()
45        .any(|(k, v)| k == OWNER_KEY && *v == PropValue::Str(FOREIGN.into()));
46    feature.properties.retain(|(k, _)| k != OWNER_KEY);
47    (feature, foreign)
48}
49
50/// Dispatch a feature to the correct layer processor.
51///
52/// Routes based on the target layer. Each processor internally checks
53/// the feature's `DataSource` to decide which processing path to take.
54/// Returns `None` if the feature should be skipped by the layer processor.
55pub fn dispatch(feature: &InputFeature, target_layer: Layer) -> Option<ProcessedFeature> {
56    match target_layer {
57        Layer::Earth => earth::process(feature),
58        Layer::Water => water::process(feature),
59        Layer::Boundaries => boundaries::process(feature),
60        Layer::Landcover => landcover::process(feature),
61        Layer::Landuse => landuse::process(feature),
62        Layer::Roads => roads::process(feature),
63        Layer::Transit => transit::process(feature),
64        Layer::Buildings => buildings::process(feature),
65        Layer::Places => places::process(feature),
66        Layer::Pois => pois::process(feature),
67        Layer::Bathymetry => bathymetry::process(feature),
68        _ => None,
69    }
70}
71
72/// Dispatch a feature and return every feature the layer produces.
73///
74/// Most layers yield at most one feature. Buildings may add an address
75/// point, and Overture road segments are split where bridge, tunnel, or
76/// link flags cover only part of the line.
77#[must_use]
78pub fn dispatch_all(feature: &InputFeature, target_layer: Layer) -> Vec<ProcessedFeature> {
79    match target_layer {
80        Layer::Buildings => buildings::process_all(feature),
81        Layer::Roads => roads::process_all(feature),
82        _ => dispatch(feature, target_layer).into_iter().collect(),
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::source::input::DataSource;
90    use crate::tile::feature::PropValue;
91    use std::collections::HashMap;
92    use std::sync::Arc;
93
94    fn overture(otype: &str, geometry: geo::Geometry<f64>, props: &[(&str, &str)]) -> InputFeature {
95        InputFeature {
96            id: 1,
97            geometry: Arc::new(geometry),
98            properties: props
99                .iter()
100                .map(|(k, v)| (k.to_string(), PropValue::Str(v.to_string())))
101                .collect::<HashMap<_, _>>(),
102            bbox: None,
103            source: DataSource::Overture {
104                theme: String::new(),
105                otype: otype.into(),
106            },
107        }
108    }
109
110    fn line() -> geo::Geometry<f64> {
111        geo::Geometry::LineString(geo::LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]))
112    }
113
114    #[test]
115    fn dispatch_all_splits_a_partially_bridged_road() {
116        let feat = overture(
117            "segment",
118            line(),
119            &[("class", "primary"), ("road_flags.is_bridge", "0.2:0.5")],
120        );
121        let out = dispatch_all(&feat, Layer::Roads);
122        assert_eq!(out.len(), 3);
123        assert!(out.iter().all(|p| p.layer == Layer::Roads));
124        assert_eq!(
125            dispatch(&feat, Layer::Roads).map(|p| p.layer),
126            Some(Layer::Roads)
127        );
128    }
129
130    #[test]
131    fn dispatch_all_routes_buildings_through_process_all() {
132        let poly = geo::Geometry::Polygon(geo::Polygon::new(
133            geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
134            vec![],
135        ));
136        let feat = overture("building", poly, &[]);
137        // No address fields, so exactly the building itself.
138        let out = dispatch_all(&feat, Layer::Buildings);
139        assert_eq!(out.len(), 1);
140        assert_eq!(out[0].layer, Layer::Buildings);
141    }
142
143    #[test]
144    fn dispatch_all_yields_at_most_one_feature_for_other_layers() {
145        let feat = overture("water", line(), &[("subtype", "river")]);
146        let out = dispatch_all(&feat, Layer::Water);
147        assert_eq!(out.len(), 1);
148        assert_eq!(out[0].layer, Layer::Water);
149
150        let dropped = overture("infrastructure", line(), &[("subtype", "power")]);
151        assert!(dispatch_all(&dropped, Layer::Transit).is_empty());
152    }
153}