Skip to main content

geolith_tiles_builder/tile/
postprocess.rs

1//! Per-tile post-processing for the protomaps schema: building height
2//! quantization, then same-attribute feature merging.
3
4use crate::layers::Layer;
5use crate::layers::buildings;
6use crate::tile::feature::{PropValue, TileFeature};
7use crate::tile::protomaps_merge;
8
9/// Post-process the features of one tile at zoom `zoom`.
10///
11/// Building heights are quantized first, as the protomaps basemap does, so
12/// that neighbouring buildings of similar height share attributes and can
13/// merge; then [`protomaps_merge::merge_tile`] merges each layer.
14#[must_use]
15pub fn postprocess_tile(mut features: Vec<TileFeature>, zoom: u8) -> Vec<TileFeature> {
16    for feature in &mut features {
17        if feature.layer == Layer::Buildings {
18            quantize_building_height(feature, zoom);
19        }
20    }
21    protomaps_merge::merge_tile(zoom, features)
22}
23
24fn quantize_building_height(feature: &mut TileFeature, zoom: u8) {
25    for (key, value) in std::sync::Arc::make_mut(&mut feature.properties) {
26        if key == "height"
27            && let PropValue::F64(h) = value
28        {
29            *h = buildings::quantize_height(*h, zoom);
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::tile::feature::GeomType;
38    use crate::tile::mvt;
39    use std::sync::Arc;
40
41    fn make_building(x: i32, height: f64) -> TileFeature {
42        TileFeature {
43            layer: Layer::Buildings,
44            id: 1,
45            geom_type: GeomType::Polygon,
46            geometry: mvt::encode_polygon(&[vec![(x, 0), (x + 400, 0), (x + 400, 400), (x, 400)]]),
47            properties: Arc::new(vec![
48                ("kind".into(), PropValue::Str("building".into())),
49                ("height".into(), PropValue::F64(height)),
50            ]),
51            sort_key: 400,
52        }
53    }
54
55    fn heights(features: &[TileFeature]) -> Vec<PropValue> {
56        let mut out: Vec<PropValue> = features
57            .iter()
58            .flat_map(|f| f.properties.iter().filter(|(k, _)| k == "height"))
59            .map(|(_, v)| v.clone())
60            .collect();
61        out.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}")));
62        out
63    }
64
65    #[test]
66    fn heights_are_quantized_below_z15_and_kept_at_z15() {
67        let at_12 = postprocess_tile(vec![make_building(0, 17.0)], 12);
68        assert_eq!(heights(&at_12), [PropValue::F64(20.0)]);
69        let at_15 = postprocess_tile(vec![make_building(0, 17.0)], 15);
70        assert_eq!(heights(&at_15), [PropValue::F64(17.0)]);
71    }
72
73    /// Quantizing first is what lets buildings of similar height merge:
74    /// 17 m and 22 m both round to 20 m at z12 (20 m steps) but to 15 m and
75    /// 20 m at z14 (5 m steps).
76    #[test]
77    fn quantization_runs_before_merging() {
78        let pair = || vec![make_building(0, 17.0), make_building(400, 22.0)];
79        let at_12 = postprocess_tile(pair(), 12);
80        assert_eq!(at_12.len(), 1);
81        assert_eq!(heights(&at_12), [PropValue::F64(20.0)]);
82        let at_14 = postprocess_tile(pair(), 14);
83        assert_eq!(at_14.len(), 2);
84        assert_eq!(
85            heights(&at_14),
86            [PropValue::F64(15.0), PropValue::F64(20.0)]
87        );
88    }
89}