geolith 0.2.2

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! Output layer identifiers for the protomaps basemap schema.

use serde::{Deserialize, Serialize};

/// Output layer identifiers matching the protomaps basemap schema.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum Layer {
    /// Natural earth polygons, landmass.
    Earth = 0,
    /// Ocean, lakes, rivers.
    Water = 1,
    /// Country and admin boundaries.
    Boundaries = 2,
    /// Land cover (forest, grass, urban, etc).
    Landcover = 3,
    /// Land use (parks, industrial, etc).
    Landuse = 4,
    /// Roads and paths.
    Roads = 5,
    /// Rail, ferries, airports.
    Transit = 6,
    /// Building footprints.
    Buildings = 7,
    /// Place labels (countries, cities, towns).
    Places = 8,
    /// Points of interest.
    Pois = 9,
    /// Underwater depth contours.
    Bathymetry = 10,
}

impl Layer {
    /// Layer name as it appears in MVT output.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Earth => "earth",
            Self::Water => "water",
            Self::Boundaries => "boundaries",
            Self::Landcover => "landcover",
            Self::Landuse => "landuse",
            Self::Roads => "roads",
            Self::Transit => "transit",
            Self::Buildings => "buildings",
            Self::Places => "places",
            Self::Pois => "pois",
            Self::Bathymetry => "bathymetry",
        }
    }

    /// All layers in order.
    pub const ALL: [Layer; 11] = [
        Self::Earth,
        Self::Water,
        Self::Boundaries,
        Self::Landcover,
        Self::Landuse,
        Self::Roads,
        Self::Transit,
        Self::Buildings,
        Self::Places,
        Self::Pois,
        Self::Bathymetry,
    ];

    /// Whether this layer skips per-feature Douglas-Peucker simplification.
    ///
    /// Roads and transit match Planetiler's `setPixelTolerance(0)` and
    /// `setMinPixelSize(0)` — features are never simplified or dropped
    /// during the per-feature render phase. Simplification only happens
    /// during post-process line merging.
    #[inline]
    #[must_use]
    pub const fn skips_simplification(self) -> bool {
        matches!(self, Self::Roads | Self::Transit)
    }

    /// Per-layer simplification tolerance in pixel units.
    ///
    /// Matches Planetiler's `setPixelTolerance()` per layer:
    /// - Earth, Water, Landcover, Bathymetry: `0.2` (`Earth.PIXEL_TOLERANCE`)
    /// - Roads, Transit: `0.0` (skip — handled by `skips_simplification`)
    /// - All others: `0.1` (Planetiler default)
    #[inline]
    #[must_use]
    pub const fn pixel_tolerance(self) -> f64 {
        match self {
            Self::Earth | Self::Water | Self::Landcover | Self::Bathymetry => 0.2,
            Self::Roads | Self::Transit => 0.0,
            _ => 0.1,
        }
    }

    /// Minimum polygon area in pixel² to keep at a given zoom level.
    ///
    /// Matches Planetiler's `setMinPixelSize()` and post-process area
    /// thresholds. Landuse uses aggressive filtering to avoid clutter at
    /// low zoom (Planetiler uses `setMinPixelSize(2.0)` plus post-process
    /// area thresholds of 400–800 pixel² at z0–6).
    #[inline]
    #[must_use]
    pub const fn min_pixel_area(self, zoom: u8) -> f64 {
        match self {
            Self::Landuse => match zoom {
                0..=5 => 800.0,
                6 => 600.0,
                _ => 4.0, // setMinPixelSize(2.0) → area = 2² = 4
            },
            Self::Landcover => 1.0,
            Self::Earth | Self::Water | Self::Bathymetry => 1.0,
            _ => 1.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn name_returns_canonical_layer_string_for_every_variant() {
        assert_eq!(Layer::Earth.name(), "earth");
        assert_eq!(Layer::Water.name(), "water");
        assert_eq!(Layer::Boundaries.name(), "boundaries");
        assert_eq!(Layer::Landcover.name(), "landcover");
        assert_eq!(Layer::Landuse.name(), "landuse");
        assert_eq!(Layer::Roads.name(), "roads");
        assert_eq!(Layer::Transit.name(), "transit");
        assert_eq!(Layer::Buildings.name(), "buildings");
        assert_eq!(Layer::Places.name(), "places");
        assert_eq!(Layer::Pois.name(), "pois");
        assert_eq!(Layer::Bathymetry.name(), "bathymetry");
    }

    /// `ALL` is the render order of the protomaps basemap schema, and each
    /// layer's `u8` value is its position in it: the sort key stores that
    /// value, so a reorder would change the on-disk feature order.
    #[test]
    fn all_follows_the_protomaps_render_order_and_matches_the_discriminants() {
        const PROTOMAPS_ORDER: [&str; 11] = [
            "earth",
            "water",
            "boundaries",
            "landcover",
            "landuse",
            "roads",
            "transit",
            "buildings",
            "places",
            "pois",
            "bathymetry",
        ];
        let names: Vec<&str> = Layer::ALL.iter().map(|l| l.name()).collect();
        assert_eq!(names, PROTOMAPS_ORDER);
        for (position, layer) in Layer::ALL.iter().enumerate() {
            assert_eq!(usize::from(*layer as u8), position, "{layer:?}");
        }
    }

    #[test]
    fn skips_simplification_only_for_roads_and_transit() {
        assert!(Layer::Roads.skips_simplification());
        assert!(Layer::Transit.skips_simplification());
        assert!(!Layer::Earth.skips_simplification());
        assert!(!Layer::Water.skips_simplification());
        assert!(!Layer::Boundaries.skips_simplification());
        assert!(!Layer::Landcover.skips_simplification());
        assert!(!Layer::Landuse.skips_simplification());
        assert!(!Layer::Buildings.skips_simplification());
        assert!(!Layer::Places.skips_simplification());
        assert!(!Layer::Pois.skips_simplification());
        assert!(!Layer::Bathymetry.skips_simplification());
    }

    #[test]
    fn pixel_tolerance_per_layer() {
        // Earth-style layers use 0.2 to preserve coastline detail.
        assert!((Layer::Earth.pixel_tolerance() - 0.2).abs() < 1e-12);
        assert!((Layer::Water.pixel_tolerance() - 0.2).abs() < 1e-12);
        assert!((Layer::Landcover.pixel_tolerance() - 0.2).abs() < 1e-12);
        assert!((Layer::Bathymetry.pixel_tolerance() - 0.2).abs() < 1e-12);
        // Roads/Transit skip per-feature simplify (tolerance 0).
        assert!((Layer::Roads.pixel_tolerance() - 0.0).abs() < 1e-12);
        assert!((Layer::Transit.pixel_tolerance() - 0.0).abs() < 1e-12);
        // All others use Planetiler's 0.1 default.
        assert!((Layer::Boundaries.pixel_tolerance() - 0.1).abs() < 1e-12);
        assert!((Layer::Landuse.pixel_tolerance() - 0.1).abs() < 1e-12);
        assert!((Layer::Buildings.pixel_tolerance() - 0.1).abs() < 1e-12);
        assert!((Layer::Places.pixel_tolerance() - 0.1).abs() < 1e-12);
        assert!((Layer::Pois.pixel_tolerance() - 0.1).abs() < 1e-12);
    }

    #[test]
    fn min_pixel_area_landuse_low_zoom_threshold() {
        assert!((Layer::Landuse.min_pixel_area(0) - 800.0).abs() < 1e-9);
        assert!((Layer::Landuse.min_pixel_area(3) - 800.0).abs() < 1e-9);
        assert!((Layer::Landuse.min_pixel_area(5) - 800.0).abs() < 1e-9);
        assert!((Layer::Landuse.min_pixel_area(6) - 600.0).abs() < 1e-9);
        assert!((Layer::Landuse.min_pixel_area(7) - 4.0).abs() < 1e-9);
        assert!((Layer::Landuse.min_pixel_area(15) - 4.0).abs() < 1e-9);
    }

    #[test]
    fn min_pixel_area_default_for_other_layers() {
        for &layer in &[
            Layer::Earth,
            Layer::Water,
            Layer::Bathymetry,
            Layer::Landcover,
            Layer::Boundaries,
            Layer::Roads,
            Layer::Transit,
            Layer::Buildings,
            Layer::Places,
            Layer::Pois,
        ] {
            for zoom in 0..=15 {
                assert!(
                    (layer.min_pixel_area(zoom) - 1.0).abs() < 1e-9,
                    "{:?} at zoom {} should be 1.0",
                    layer,
                    zoom
                );
            }
        }
    }
}