use geo::Centroid;
use std::sync::Arc;
use geolith::names::push_names;
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::LandPolygons => process_land_polygons(feature),
DataSource::NaturalEarth { .. } => process_ne(feature),
DataSource::Osm { .. } => process_osm(feature),
_ => None,
}
}
fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
if feature.get_str("subtype").is_some_and(|s| s != "land") {
return None;
}
let (kind, min_zoom) = if feature.can_be_polygon() {
("earth", 6u8)
} else if feature.is_point() {
match feature.get_str("class") {
Some("island") => ("island", 6),
Some("islet") => ("island", 12),
_ => return None,
}
} else {
return None;
};
let mut properties = Vec::with_capacity(2);
properties.push(("kind".into(), PropValue::Str(kind.into())));
push_names(feature, &mut properties);
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Earth,
min_zoom,
max_zoom: 15,
sort_rank: 200,
properties,
})
}
fn process_land_polygons(feature: &InputFeature) -> Option<ProcessedFeature> {
let properties = vec![("kind".into(), PropValue::Str("earth".into()))];
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Earth,
min_zoom: 6,
max_zoom: 15,
sort_rank: 200,
properties,
})
}
fn process_ne(feature: &InputFeature) -> Option<ProcessedFeature> {
let source_layer = feature.ne_source_layer()?;
let (min_zoom, max_zoom) = match source_layer {
"ne_50m_land" => (0u8, 4u8),
"ne_10m_land" => (5, 5),
_ => return None,
};
let properties = vec![("kind".into(), PropValue::Str("earth".into()))];
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Earth,
min_zoom,
max_zoom,
sort_rank: 200,
properties,
})
}
fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
if feature.can_be_line() && !feature.can_be_polygon() && feature.has_tag("natural", "cliff") {
let mut properties = Vec::with_capacity(3);
properties.push(("kind".into(), PropValue::Str("cliff".into())));
properties.push(("min_zoom".into(), PropValue::I64(13)));
push_names(feature, &mut properties);
return Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Earth,
min_zoom: 12,
max_zoom: 15,
sort_rank: 200,
properties,
});
}
if feature.can_be_polygon() && feature.has_tag("place", "island") {
let label_geom = match feature.geometry.as_ref() {
geo::Geometry::Polygon(p) => p.centroid().map(|c| Arc::new(geo::Geometry::Point(c))),
geo::Geometry::MultiPolygon(mp) => {
mp.centroid().map(|c| Arc::new(geo::Geometry::Point(c)))
}
_ => None,
};
if let Some(geom) = label_geom {
let mut properties = Vec::with_capacity(2);
properties.push(("kind".into(), PropValue::Str("island".into())));
push_names(feature, &mut properties);
return Some(ProcessedFeature {
id: feature.id,
geometry: geom,
layer: Layer::Earth,
min_zoom: 6,
max_zoom: 15,
sort_rank: 200,
properties,
});
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::input::{DataSource, OsmElementType};
use std::collections::HashMap;
fn polygon_geom() -> Arc<geo::Geometry<f64>> {
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![],
)))
}
fn multipoly_geom() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::MultiPolygon(geo::MultiPolygon(vec![
geo::Polygon::new(
geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
vec![],
),
geo::Polygon::new(
geo::LineString::from(vec![(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 2.0)]),
vec![],
),
])))
}
fn line_geom() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
])))
}
fn point_geom() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0)))
}
fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
make_with(
props,
polygon_geom(),
DataSource::Overture {
theme: String::new(),
otype: String::new(),
},
)
}
fn make_with(
props: &[(&str, PropValue)],
geometry: Arc<geo::Geometry<f64>>,
source: DataSource,
) -> InputFeature {
let mut properties = HashMap::new();
for (k, v) in props {
properties.insert(k.to_string(), v.clone());
}
InputFeature {
id: 1,
geometry,
properties,
bbox: None,
source,
}
}
fn osm_src() -> DataSource {
DataSource::Osm {
element_type: OsmElementType::Way,
relations: vec![],
}
}
fn ne_src(layer: &str) -> DataSource {
DataSource::NaturalEarth {
source_layer: layer.to_string(),
}
}
#[test]
fn default_land_is_earth() {
let feat = make_feature(&[]);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Earth);
assert_eq!(result.min_zoom, 6);
assert_eq!(result.sort_rank, 200);
}
fn kind(r: &ProcessedFeature) -> Option<&PropValue> {
r.properties
.iter()
.find(|(k, _)| k == "kind")
.map(|(_, v)| v)
}
fn overture_src() -> DataSource {
DataSource::Overture {
theme: "base".into(),
otype: "land".into(),
}
}
#[test]
fn overture_land_polygon_is_earth_at_z6() {
let feat = make_feature(&[
("subtype", PropValue::Str("land".into())),
("class", PropValue::Str("island".into())),
]);
let r = process(&feat).unwrap();
assert_eq!(kind(&r), Some(&PropValue::Str("earth".into())));
assert_eq!(r.min_zoom, 6);
}
#[test]
fn overture_non_land_subtypes_are_not_earth() {
for subtype in [
"tree", "shrub", "forest", "grass", "physical", "rock", "wetland", "sand", "reef",
"glacier", "desert", "crater",
] {
let feat = make_feature(&[("subtype", PropValue::Str(subtype.into()))]);
assert!(process(&feat).is_none(), "{subtype}");
}
}
#[test]
fn overture_island_point_is_island_label_at_z6() {
let feat = make_with(
&[
("subtype", PropValue::Str("land".into())),
("class", PropValue::Str("island".into())),
],
point_geom(),
overture_src(),
);
let r = process(&feat).unwrap();
assert_eq!(kind(&r), Some(&PropValue::Str("island".into())));
assert_eq!(r.min_zoom, 6);
}
#[test]
fn overture_islet_point_is_island_label_at_z12() {
let feat = make_with(
&[
("subtype", PropValue::Str("land".into())),
("class", PropValue::Str("islet".into())),
],
point_geom(),
overture_src(),
);
let r = process(&feat).unwrap();
assert_eq!(kind(&r), Some(&PropValue::Str("island".into())));
assert_eq!(r.min_zoom, 12);
}
#[test]
fn overture_other_land_points_and_lines_are_dropped() {
let point = make_with(
&[
("subtype", PropValue::Str("land".into())),
("class", PropValue::Str("land".into())),
],
point_geom(),
overture_src(),
);
assert!(process(&point).is_none());
let line = make_with(
&[("subtype", PropValue::Str("land".into()))],
line_geom(),
overture_src(),
);
assert!(process(&line).is_none());
}
#[test]
fn overture_uses_names_primary() {
let feat = make_feature(&[
("subtype", PropValue::Str("land".into())),
("names.primary", PropValue::Str("Madagascar".into())),
]);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Madagascar".into()))
);
}
fn has(r: &ProcessedFeature, key: &str, value: &str) -> bool {
r.properties
.iter()
.any(|(k, v)| k == key && *v == PropValue::Str(value.into()))
}
#[test]
fn overture_island_takes_name_en_from_the_common_names() {
let feat = make_feature(&[
("subtype", PropValue::Str("land".into())),
("names.primary", PropValue::Str("الجزيرة".into())),
("names.common.en", PropValue::Str("The Island".into())),
]);
let r = process(&feat).unwrap();
assert!(has(&r, "script", "Arabic"));
assert!(has(&r, "name:en", "The Island"));
}
#[test]
fn osm_cliff_takes_name_en() {
let feat = make_with(
&[
("natural", PropValue::Str("cliff".into())),
("name", PropValue::Str("Falaise".into())),
("name:en", PropValue::Str("The Cliff".into())),
],
line_geom(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has(&r, "name", "Falaise"));
assert!(has(&r, "name:en", "The Cliff"));
assert!(r.properties.iter().all(|(k, _)| k != "script"));
}
#[test]
fn overture_falls_back_to_name() {
let feat = make_feature(&[
("subtype", PropValue::Str("land".into())),
("name", PropValue::Str("Bali".into())),
]);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Bali".into()))
);
}
#[test]
fn land_polygons_z6() {
let feat = make_with(&[], polygon_geom(), DataSource::LandPolygons);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 6);
assert_eq!(r.max_zoom, 15);
assert_eq!(r.layer, Layer::Earth);
assert!(
r.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("earth".into()))
);
}
#[test]
fn ne_50m_land_z0_4() {
let feat = make_with(&[], polygon_geom(), ne_src("ne_50m_land"));
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 0);
assert_eq!(r.max_zoom, 4);
}
#[test]
fn ne_10m_land_z5() {
let feat = make_with(&[], polygon_geom(), ne_src("ne_10m_land"));
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 5);
assert_eq!(r.max_zoom, 5);
}
#[test]
fn ne_unknown_layer_returns_none() {
let feat = make_with(&[], polygon_geom(), ne_src("ne_50m_admin"));
assert!(process(&feat).is_none());
}
#[test]
fn osm_cliff_line_z12() {
let feat = make_with(
&[("natural", PropValue::Str("cliff".into()))],
line_geom(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 12);
assert!(
r.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("cliff".into()))
);
assert!(r.properties.iter().any(|(k, _)| k == "min_zoom"));
}
#[test]
fn osm_cliff_line_with_name() {
let feat = make_with(
&[
("natural", PropValue::Str("cliff".into())),
("name", PropValue::Str("Big Cliff".into())),
],
line_geom(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Big Cliff".into()))
);
}
#[test]
fn osm_island_polygon_emits_centroid_label() {
let feat = make_with(
&[
("place", PropValue::Str("island".into())),
("name", PropValue::Str("Tiny".into())),
],
polygon_geom(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 6);
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
assert!(
r.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("island".into()))
);
}
#[test]
fn osm_island_multipolygon_emits_centroid() {
let feat = make_with(
&[("place", PropValue::Str("island".into()))],
multipoly_geom(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
}
#[test]
fn osm_island_point_returns_none() {
let feat = make_with(
&[("place", PropValue::Str("island".into()))],
point_geom(),
osm_src(),
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_unrelated_returns_none() {
let feat = make_with(
&[("highway", PropValue::Str("primary".into()))],
line_geom(),
osm_src(),
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_cliff_polygon_not_matched_as_line() {
let feat = make_with(
&[("natural", PropValue::Str("cliff".into()))],
polygon_geom(),
osm_src(),
);
assert!(process(&feat).is_none());
}
#[test]
fn other_source_returns_none() {
let feat = make_with(&[], polygon_geom(), DataSource::IndiaBoundary);
assert!(process(&feat).is_none());
let feat = make_with(&[], polygon_geom(), DataSource::WaterPolygons);
assert!(process(&feat).is_none());
}
}