use geo::Centroid;
use crate::layers::Layer;
use crate::source::input::{DataSource, InputFeature};
use crate::tile::feature::PropValue;
use super::buildings::ProcessedFeature;
pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
match &feature.source {
DataSource::Overture { .. } => process_overture(feature),
DataSource::NaturalEarth { .. } => process_ne(feature),
_ => None,
}
}
fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
let subtype = feature.get_str("subtype").unwrap_or("unknown");
let kind = match subtype {
"barren" => "barren",
"crop" => "farmland",
"forest" => "forest",
"grass" => "grassland",
"mangrove" => "mangrove",
"moss" => "moss",
"shrub" => "scrub",
"snow" => "glacier",
"urban" => "urban_area",
"wetland" => "wetland",
other => other,
};
let sort_key: i32 = match subtype {
"barren" => 0,
"snow" => 1,
"crop" => 2,
"shrub" => 3,
"grass" => 4,
"forest" => 5,
"mangrove" => 6,
"moss" => 7,
"wetland" => 8,
"urban" => 9,
_ => 10,
};
let min_zoom = feature
.get_f64("cartography.min_zoom")
.or_else(|| feature.get_f64("carto.min_zoom"))
.map(|v| v as u8)
.unwrap_or(0);
let max_zoom = feature
.get_f64("cartography.max_zoom")
.or_else(|| feature.get_f64("carto.max_zoom"))
.map(|v| (v as u8).min(7))
.unwrap_or(7);
let properties = vec![
("kind".into(), PropValue::Str(kind.into())),
("sort_key".into(), PropValue::I64(sort_key as i64)),
];
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Landcover,
min_zoom,
max_zoom,
sort_rank: sort_key,
properties,
})
}
fn process_ne(feature: &InputFeature) -> Option<ProcessedFeature> {
let source_layer = feature.ne_source_layer()?;
if source_layer != "ne_10m_glaciated_areas" {
return None;
}
let centroid = match feature.geometry.as_ref() {
geo::Geometry::Polygon(p) => p.centroid(),
geo::Geometry::MultiPolygon(mp) => mp.centroid(),
_ => return None,
};
let c = centroid?;
if c.y() > -60.0 {
return None;
}
let properties = vec![
("kind".into(), PropValue::Str("glacier".into())),
("sort_key".into(), PropValue::I64(1)),
];
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Landcover,
min_zoom: 0,
max_zoom: 7,
sort_rank: 1,
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 forest_mapped_correctly() {
let feat = make_feature(&[("subtype", PropValue::Str("forest".into()))]);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Landcover);
assert_eq!(result.max_zoom, 7);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("forest".into()))
);
}
#[test]
fn crop_becomes_farmland() {
let feat = make_feature(&[("subtype", PropValue::Str("crop".into()))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("farmland".into()))
);
}
#[test]
fn snow_becomes_glacier() {
let feat = make_feature(&[("subtype", PropValue::Str("snow".into()))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("glacier".into()))
);
}
#[test]
fn urban_becomes_urban_area() {
let feat = make_feature(&[("subtype", PropValue::Str("urban".into()))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("urban_area".into()))
);
}
#[test]
fn sort_key_ordering() {
let barren = make_feature(&[("subtype", PropValue::Str("barren".into()))]);
let forest = make_feature(&[("subtype", PropValue::Str("forest".into()))]);
let r_barren = process(&barren).unwrap();
let r_forest = process(&forest).unwrap();
assert!(r_barren.sort_rank < r_forest.sort_rank);
}
fn ne_feature(layer: &str, geometry: geo::Geometry<f64>) -> InputFeature {
InputFeature {
id: 1,
geometry: Arc::new(geometry),
properties: HashMap::new(),
bbox: None,
source: DataSource::NaturalEarth {
source_layer: layer.to_string(),
},
}
}
fn poly_at(cx: f64, cy: f64) -> geo::Geometry<f64> {
geo::Geometry::Polygon(geo::Polygon::new(
geo::LineString::from(vec![
(cx - 0.5, cy - 0.5),
(cx + 0.5, cy - 0.5),
(cx + 0.5, cy + 0.5),
(cx - 0.5, cy + 0.5),
(cx - 0.5, cy - 0.5),
]),
vec![],
))
}
#[test]
fn unsupported_source_returns_none() {
let mut f = make_feature(&[]);
f.source = DataSource::LandPolygons;
assert!(process(&f).is_none());
}
#[test]
fn overture_all_subtypes_mapped() {
let cases: &[(&str, &str, i32)] = &[
("barren", "barren", 0),
("crop", "farmland", 2),
("forest", "forest", 5),
("grass", "grassland", 4),
("mangrove", "mangrove", 6),
("moss", "moss", 7),
("shrub", "scrub", 3),
("snow", "glacier", 1),
("urban", "urban_area", 9),
("wetland", "wetland", 8),
];
for (subtype, kind, sort) in cases {
let f = make_feature(&[("subtype", PropValue::Str((*subtype).into()))]);
let r = process(&f).unwrap();
assert_eq!(kind_of(&r), *kind, "subtype={subtype}");
assert_eq!(r.sort_rank, *sort);
assert_eq!(r.layer, Layer::Landcover);
}
}
#[test]
fn overture_unknown_subtype_passthrough() {
let f = make_feature(&[("subtype", PropValue::Str("custom_kind".into()))]);
let r = process(&f).unwrap();
assert_eq!(kind_of(&r), "custom_kind");
assert_eq!(r.sort_rank, 10);
}
#[test]
fn overture_missing_subtype_unknown() {
let f = make_feature(&[]);
let r = process(&f).unwrap();
assert_eq!(kind_of(&r), "unknown");
assert_eq!(r.sort_rank, 10);
}
#[test]
fn overture_min_zoom_from_cartography() {
let f = make_feature(&[
("subtype", PropValue::Str("forest".into())),
("cartography.min_zoom", PropValue::F64(3.0)),
("cartography.max_zoom", PropValue::F64(5.0)),
]);
let r = process(&f).unwrap();
assert_eq!(r.min_zoom, 3);
assert_eq!(r.max_zoom, 5);
}
#[test]
fn overture_min_zoom_from_carto_alias() {
let f = make_feature(&[
("subtype", PropValue::Str("forest".into())),
("carto.min_zoom", PropValue::F64(2.0)),
("carto.max_zoom", PropValue::F64(4.0)),
]);
let r = process(&f).unwrap();
assert_eq!(r.min_zoom, 2);
assert_eq!(r.max_zoom, 4);
}
#[test]
fn overture_max_zoom_capped_at_7() {
let f = make_feature(&[
("subtype", PropValue::Str("forest".into())),
("cartography.max_zoom", PropValue::F64(15.0)),
]);
let r = process(&f).unwrap();
assert_eq!(r.max_zoom, 7);
}
#[test]
fn overture_default_zoom_when_missing() {
let f = make_feature(&[("subtype", PropValue::Str("forest".into()))]);
let r = process(&f).unwrap();
assert_eq!(r.min_zoom, 0);
assert_eq!(r.max_zoom, 7);
}
#[test]
fn overture_sort_key_in_properties() {
let f = make_feature(&[("subtype", PropValue::Str("forest".into()))]);
let r = process(&f).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "sort_key" && *v == PropValue::I64(5))
);
}
fn kind_of(f: &ProcessedFeature) -> &str {
f.properties
.iter()
.find_map(|(k, v)| match (k.as_str(), v) {
("kind", PropValue::Str(s)) => Some(s.as_str()),
_ => None,
})
.unwrap()
}
#[test]
fn ne_glaciated_antarctica_emits_glacier() {
let f = ne_feature("ne_10m_glaciated_areas", poly_at(0.0, -75.0));
let r = process(&f).expect("antarctica glacier");
assert_eq!(kind_of(&r), "glacier");
assert_eq!(r.layer, Layer::Landcover);
assert_eq!(r.min_zoom, 0);
assert_eq!(r.max_zoom, 7);
assert_eq!(r.sort_rank, 1);
}
#[test]
fn ne_glaciated_arctic_skipped() {
for lat in [80.0, -50.0] {
let f = ne_feature("ne_10m_glaciated_areas", poly_at(0.0, lat));
assert!(process(&f).is_none(), "lat {lat}");
}
}
#[test]
fn ne_wrong_layer_skipped() {
let f = ne_feature("ne_50m_land", poly_at(0.0, -75.0));
assert!(process(&f).is_none());
}
#[test]
fn ne_multipolygon_antarctica_works() {
let mp = geo::Geometry::MultiPolygon(geo::MultiPolygon::new(vec![geo::Polygon::new(
geo::LineString::from(vec![
(-1.0, -75.0),
(1.0, -75.0),
(1.0, -73.0),
(-1.0, -73.0),
(-1.0, -75.0),
]),
vec![],
)]));
let f = ne_feature("ne_10m_glaciated_areas", mp);
let r = process(&f).unwrap();
assert_eq!(kind_of(&r), "glacier");
}
#[test]
fn ne_non_polygon_geometry_skipped() {
let f = ne_feature(
"ne_10m_glaciated_areas",
geo::Geometry::Point(geo::Point::new(0.0, -75.0)),
);
assert!(process(&f).is_none());
}
}