use crate::layers::Layer;
use crate::source::input::InputFeature;
use crate::tile::feature::PropValue;
use super::buildings::ProcessedFeature;
pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
let min_zoom = feature
.get_i64("cartography.min_zoom")
.map(|z| z.clamp(0, 15) as u8)
.unwrap_or(5);
let max_zoom = feature
.get_i64("cartography.max_zoom")
.map(|z| z.clamp(0, 15) as u8)
.unwrap_or(8)
.min(15);
let depth = feature
.get_i64("depth")
.map(|d| d as f64)
.or_else(|| feature.get_f64("depth"));
let sort_rank = feature
.get_i64("cartography.sort_key")
.map(|s| s.clamp(0, 1000) as i32)
.or_else(|| depth.map(|d| (d.abs() / 10.0).clamp(0.0, 1000.0) as i32))
.unwrap_or(200);
let mut properties = Vec::with_capacity(2);
properties.push(("kind".into(), PropValue::Str("bathymetry".into())));
if let Some(depth) = depth {
properties.push(("depth".into(), PropValue::F64(depth)));
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Bathymetry,
min_zoom,
max_zoom,
sort_rank,
properties,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::input::DataSource;
use std::collections::HashMap;
use std::sync::Arc;
fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
let mut properties = HashMap::new();
for (k, v) in props {
properties.insert(k.to_string(), v.clone());
}
InputFeature {
id: 1,
geometry: Arc::new(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![],
))),
properties,
bbox: None,
source: DataSource::Overture {
theme: String::new(),
otype: String::new(),
},
}
}
#[test]
fn default_bathymetry() {
let feat = make_feature(&[]);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Bathymetry);
assert_eq!(result.min_zoom, 5);
assert_eq!(result.max_zoom, 8);
assert_eq!(result.sort_rank, 200);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("bathymetry".into()))
);
}
#[test]
fn depth_property_included() {
let feat = make_feature(&[("depth", PropValue::F64(-500.0))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "depth" && *v == PropValue::F64(-500.0))
);
}
#[test]
fn integer_depth_reaches_the_tile() {
let feat = make_feature(&[("depth", PropValue::I64(1500))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "depth" && *v == PropValue::F64(1500.0))
);
}
#[test]
fn deeper_levels_sort_after_shallower_ones() {
let rank = |d: i64| {
process(&make_feature(&[("depth", PropValue::I64(d))]))
.unwrap()
.sort_rank
};
let levels = [0, 10, 50, 100, 500, 1000, 1500, 2000, 2500, 3000];
let ranks: Vec<i32> = levels.iter().map(|&d| rank(d)).collect();
assert_eq!(ranks, vec![0, 1, 5, 10, 50, 100, 150, 200, 250, 300]);
}
#[test]
fn a_cartography_sort_key_overrides_depth_ordering() {
let feat = make_feature(&[
("depth", PropValue::I64(3000)),
("cartography.sort_key", PropValue::I64(7)),
]);
assert_eq!(process(&feat).unwrap().sort_rank, 7);
}
#[test]
fn cartography_hints_respected() {
let feat = make_feature(&[
("cartography.min_zoom", PropValue::I64(3)),
("cartography.max_zoom", PropValue::I64(10)),
("cartography.sort_key", PropValue::I64(150)),
]);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 3);
assert_eq!(result.max_zoom, 10);
assert_eq!(result.sort_rank, 150);
}
#[test]
fn zoom_clamped_to_valid_range() {
let feat = make_feature(&[
("cartography.min_zoom", PropValue::I64(-5)),
("cartography.max_zoom", PropValue::I64(20)),
]);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 0);
assert_eq!(result.max_zoom, 15);
}
#[test]
fn defaults_bounded_to_low_zoom_to_prevent_explosion() {
let feat = make_feature(&[]);
let result = process(&feat).unwrap();
assert!(
result.max_zoom <= 10,
"bathymetry default max_zoom must stay low to prevent planet-scale stripe_clip explosion"
);
}
}