use crate::layers::Layer;
use crate::layers::buildings;
use crate::tile::feature::{PropValue, TileFeature};
use crate::tile::protomaps_merge;
#[must_use]
pub fn postprocess_tile(mut features: Vec<TileFeature>, zoom: u8) -> Vec<TileFeature> {
for feature in &mut features {
if feature.layer == Layer::Buildings {
quantize_building_height(feature, zoom);
}
}
protomaps_merge::merge_tile(zoom, features)
}
fn quantize_building_height(feature: &mut TileFeature, zoom: u8) {
for (key, value) in std::sync::Arc::make_mut(&mut feature.properties) {
if key == "height"
&& let PropValue::F64(h) = value
{
*h = buildings::quantize_height(*h, zoom);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tile::feature::GeomType;
use crate::tile::mvt;
use std::sync::Arc;
fn make_building(x: i32, height: f64) -> TileFeature {
TileFeature {
layer: Layer::Buildings,
id: 1,
geom_type: GeomType::Polygon,
geometry: mvt::encode_polygon(&[vec![(x, 0), (x + 400, 0), (x + 400, 400), (x, 400)]]),
properties: Arc::new(vec![
("kind".into(), PropValue::Str("building".into())),
("height".into(), PropValue::F64(height)),
]),
sort_key: 400,
}
}
fn heights(features: &[TileFeature]) -> Vec<PropValue> {
let mut out: Vec<PropValue> = features
.iter()
.flat_map(|f| f.properties.iter().filter(|(k, _)| k == "height"))
.map(|(_, v)| v.clone())
.collect();
out.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}")));
out
}
#[test]
fn heights_are_quantized_below_z15_and_kept_at_z15() {
let at_12 = postprocess_tile(vec![make_building(0, 17.0)], 12);
assert_eq!(heights(&at_12), [PropValue::F64(20.0)]);
let at_15 = postprocess_tile(vec![make_building(0, 17.0)], 15);
assert_eq!(heights(&at_15), [PropValue::F64(17.0)]);
}
#[test]
fn quantization_runs_before_merging() {
let pair = || vec![make_building(0, 17.0), make_building(400, 22.0)];
let at_12 = postprocess_tile(pair(), 12);
assert_eq!(at_12.len(), 1);
assert_eq!(heights(&at_12), [PropValue::F64(20.0)]);
let at_14 = postprocess_tile(pair(), 14);
assert_eq!(at_14.len(), 2);
assert_eq!(
heights(&at_14),
[PropValue::F64(15.0), PropValue::F64(20.0)]
);
}
}