geolith_tiles_builder/tile/
postprocess.rs1use crate::layers::Layer;
5use crate::layers::buildings;
6use crate::tile::feature::{PropValue, TileFeature};
7use crate::tile::protomaps_merge;
8
9#[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 #[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}