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
//! Tile layer definitions and feature processing.
//!
//! Each layer maps Overture features to protomaps-schema MVT layers.

pub use geolith::layer::Layer;

pub mod bathymetry;
pub mod boundaries;
pub mod buildings;
pub mod earth;
pub mod landcover;
pub mod landuse;
pub mod places;
pub mod pois;
pub mod roads;
pub mod transit;
pub mod water;

use crate::source::input::InputFeature;
use crate::tile::feature::PropValue;
use buildings::ProcessedFeature;

/// A private property of a boundaries or places feature: the layer processor
/// sets it to [`FOREIGN`] when the source says the feature belongs to
/// another country's admin division (or is a claim name). The Survey of
/// India mask reads it to drop such lines and labels inside India, then
/// removes it: it never reaches a tile.
pub const OWNER_KEY: &str = "__owner";

/// The value of [`OWNER_KEY`] for an admin feature of another country.
pub const FOREIGN: &str = "foreign";

/// Mark `properties` as belonging to a division that is not India's.
pub fn mark_foreign(properties: &mut Vec<(String, PropValue)>) {
    properties.push((OWNER_KEY.into(), PropValue::Str(FOREIGN.into())));
}

/// `feature` without its private properties, and whether it was marked
/// [`FOREIGN`].
#[must_use]
pub fn take_owner(mut feature: ProcessedFeature) -> (ProcessedFeature, bool) {
    let foreign = feature
        .properties
        .iter()
        .any(|(k, v)| k == OWNER_KEY && *v == PropValue::Str(FOREIGN.into()));
    feature.properties.retain(|(k, _)| k != OWNER_KEY);
    (feature, foreign)
}

/// Dispatch a feature to the correct layer processor.
///
/// Routes based on the target layer. Each processor internally checks
/// the feature's `DataSource` to decide which processing path to take.
/// Returns `None` if the feature should be skipped by the layer processor.
pub fn dispatch(feature: &InputFeature, target_layer: Layer) -> Option<ProcessedFeature> {
    match target_layer {
        Layer::Earth => earth::process(feature),
        Layer::Water => water::process(feature),
        Layer::Boundaries => boundaries::process(feature),
        Layer::Landcover => landcover::process(feature),
        Layer::Landuse => landuse::process(feature),
        Layer::Roads => roads::process(feature),
        Layer::Transit => transit::process(feature),
        Layer::Buildings => buildings::process(feature),
        Layer::Places => places::process(feature),
        Layer::Pois => pois::process(feature),
        Layer::Bathymetry => bathymetry::process(feature),
        _ => None,
    }
}

/// Dispatch a feature and return every feature the layer produces.
///
/// Most layers yield at most one feature. Buildings may add an address
/// point, and Overture road segments are split where bridge, tunnel, or
/// link flags cover only part of the line.
#[must_use]
pub fn dispatch_all(feature: &InputFeature, target_layer: Layer) -> Vec<ProcessedFeature> {
    match target_layer {
        Layer::Buildings => buildings::process_all(feature),
        Layer::Roads => roads::process_all(feature),
        _ => dispatch(feature, target_layer).into_iter().collect(),
    }
}

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

    fn overture(otype: &str, geometry: geo::Geometry<f64>, props: &[(&str, &str)]) -> InputFeature {
        InputFeature {
            id: 1,
            geometry: Arc::new(geometry),
            properties: props
                .iter()
                .map(|(k, v)| (k.to_string(), PropValue::Str(v.to_string())))
                .collect::<HashMap<_, _>>(),
            bbox: None,
            source: DataSource::Overture {
                theme: String::new(),
                otype: otype.into(),
            },
        }
    }

    fn line() -> geo::Geometry<f64> {
        geo::Geometry::LineString(geo::LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]))
    }

    #[test]
    fn dispatch_all_splits_a_partially_bridged_road() {
        let feat = overture(
            "segment",
            line(),
            &[("class", "primary"), ("road_flags.is_bridge", "0.2:0.5")],
        );
        let out = dispatch_all(&feat, Layer::Roads);
        assert_eq!(out.len(), 3);
        assert!(out.iter().all(|p| p.layer == Layer::Roads));
        assert_eq!(
            dispatch(&feat, Layer::Roads).map(|p| p.layer),
            Some(Layer::Roads)
        );
    }

    #[test]
    fn dispatch_all_routes_buildings_through_process_all() {
        let poly = 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![],
        ));
        let feat = overture("building", poly, &[]);
        // No address fields, so exactly the building itself.
        let out = dispatch_all(&feat, Layer::Buildings);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].layer, Layer::Buildings);
    }

    #[test]
    fn dispatch_all_yields_at_most_one_feature_for_other_layers() {
        let feat = overture("water", line(), &[("subtype", "river")]);
        let out = dispatch_all(&feat, Layer::Water);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].layer, Layer::Water);

        let dropped = overture("infrastructure", line(), &[("subtype", "power")]);
        assert!(dispatch_all(&dropped, Layer::Transit).is_empty());
    }
}