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::Osm { .. } => process_osm(feature),
_ => None,
}
}
fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
if feature.overture_otype() == Some("land") {
return process_overture_land(feature);
}
let kind = feature
.get_str("class")
.and_then(class_kind)
.or_else(|| feature.get_str("subtype").and_then(subtype_kind))
.unwrap_or("other");
let mut properties = Vec::with_capacity(2);
properties.push(("kind".into(), PropValue::Str(kind.into())));
if let Some(name) = feature.get_str("names.primary").or(feature.get_str("name")) {
properties.push(("name".into(), PropValue::Str(name.to_string())));
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Landuse,
min_zoom: 8,
max_zoom: 15,
sort_rank: 189,
properties,
})
}
fn process_overture_land(feature: &InputFeature) -> Option<ProcessedFeature> {
if !feature.can_be_polygon() {
return None;
}
let kind = match feature.get_str("class")? {
"wood" | "forest" => "wood",
"grassland" | "grass" | "meadow" | "fell" => "grassland",
"scrub" | "heath" | "shrub" | "shrubbery" => "scrub",
"wetland" => "wetland",
"bare_rock" | "scree" | "stone" | "rock" | "shingle" => "bare_rock",
"beach" => "beach",
"sand" => "sand",
_ => return None,
};
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Landuse,
min_zoom: 7,
max_zoom: 15,
sort_rank: 189,
properties: vec![("kind".into(), PropValue::Str(kind.into()))],
})
}
fn subtype_kind(subtype: &str) -> Option<&'static str> {
Some(match subtype {
"agriculture" | "horticulture" => "farmland",
"golf" => "golf_course",
"cemetery" => "cemetery",
"education" => "school",
"medical" => "hospital",
"military" => "military",
"park" => "park",
"residential" => "residential",
"pedestrian" => "pedestrian",
"protected" => "nature_reserve",
"managed" => "grass",
_ => return None,
})
}
fn class_kind(class: &str) -> Option<&'static str> {
let kind = match class {
"park" | "national_park" => "park",
"garden" => "garden",
"golf_course" => "golf_course",
"dog_park" => "dog_park",
"playground" => "playground",
"pitch" => "pitch",
"nature_reserve" => "nature_reserve",
"industrial" => "industrial",
"railway" => "railway",
"commercial" => "commercial",
"residential" => "residential",
"military" => "military",
"cemetery" => "cemetery",
"hospital" => "hospital",
"school" | "kindergarten" => "school",
"university" | "college" => "university",
"forest" | "wood" => "forest",
"farmland" | "farmyard" | "orchard" => "farmland",
"grass" | "meadow" | "village_green" | "recreation_ground" => "grass",
"allotments" => "allotments",
"beach" => "beach",
"glacier" => "glacier",
"scrub" => "scrub",
"sand" => "sand",
"wetland" => "wetland",
"bare_rock" => "bare_rock",
"pier" => "pier",
"dam" => "dam",
"zoo" => "zoo",
"pedestrian" => "pedestrian",
"aerodrome" | "runway" => "aerodrome",
"grave_yard" => "cemetery",
"airfield" => "airfield",
"naval_base" => "naval_base",
_ => return None,
};
Some(kind)
}
fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
let kind_and_zoom = osm_landuse_classify(feature)?;
let mut properties = Vec::with_capacity(3);
properties.push(("kind".into(), PropValue::Str(kind_and_zoom.0.into())));
if let Some(name) = feature.get_str("name") {
properties.push(("name".into(), PropValue::Str(name.to_string())));
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Landuse,
min_zoom: kind_and_zoom.1,
max_zoom: 15,
sort_rank: 189,
properties,
})
}
fn osm_landuse_classify(feature: &InputFeature) -> Option<(&'static str, u8)> {
if let Some(aeroway) = feature.get_str("aeroway") {
return match aeroway {
"aerodrome" => Some(("aerodrome", 10)),
"runway" | "taxiway" => Some(("aerodrome", 11)),
"apron" | "helipad" => Some(("aerodrome", 13)),
_ => None,
};
}
if let Some(amenity) = feature.get_str("amenity") {
return match amenity {
"hospital" | "clinic" => Some(("hospital", 11)),
"school" | "kindergarten" => Some(("school", 11)),
"university" | "college" => Some(("university", 11)),
"grave_yard" => Some(("cemetery", 11)),
"parking" => Some(("parking", 13)),
_ => None,
};
}
if let Some(leisure) = feature.get_str("leisure") {
return match leisure {
"park" => Some(("park", 6)),
"garden" => Some(("garden", 11)),
"golf_course" => Some(("golf_course", 11)),
"dog_park" => Some(("dog_park", 11)),
"playground" => Some(("playground", 13)),
"pitch" => Some(("pitch", 13)),
"nature_reserve" => Some(("nature_reserve", 6)),
"stadium" => Some(("stadium", 13)),
"swimming_pool" => Some(("swimming_pool", 14)),
_ => None,
};
}
if let Some(landuse) = feature.get_str("landuse") {
return match landuse {
"industrial" => Some(("industrial", 9)),
"railway" => Some(("railway", 11)),
"commercial" => Some(("commercial", 9)),
"residential" => Some(("residential", 9)),
"military" => Some(("military", 8)),
"cemetery" => Some(("cemetery", 11)),
"forest" => Some(("forest", 6)),
"farmland" => Some(("farmland", 8)),
"farmyard" => Some(("farmland", 11)),
"orchard" | "vineyard" => Some(("farmland", 11)),
"grass" | "meadow" | "village_green" | "recreation_ground" => Some(("grass", 11)),
"allotments" => Some(("allotments", 11)),
"quarry" => Some(("quarry", 11)),
"landfill" => Some(("landfill", 11)),
"brownfield" | "greenfield" => Some(("other", 11)),
"retail" => Some(("commercial", 11)),
_ => None,
};
}
if let Some(natural) = feature.get_str("natural") {
return match natural {
"wood" => Some(("forest", 6)),
"scrub" => Some(("scrub", 9)),
"sand" | "beach" => Some(("sand", 11)),
"wetland" => Some(("wetland", 9)),
"bare_rock" | "scree" => Some(("bare_rock", 11)),
"glacier" => Some(("glacier", 6)),
"heath" => Some(("grass", 11)),
_ => None,
};
}
if feature.has_tag("highway", "pedestrian") && feature.can_be_polygon() {
return Some(("pedestrian", 13));
}
if let Some(man_made) = feature.get_str("man_made") {
return match man_made {
"pier" => Some(("pier", 13)),
"bridge" => Some(("bridge", 13)),
_ => None,
};
}
if feature.has_tag("waterway", "dam") {
return Some(("dam", 11));
}
if feature.has_tag("tourism", "zoo") {
return Some(("zoo", 11));
}
if feature.has_tag("boundary", "national_park") {
return Some(("park", 4));
}
if feature.has_tag("boundary", "protected_area") {
let protect_class = feature.get_str("protect_class").unwrap_or("");
return match protect_class {
"2" => Some(("park", 4)),
"3" | "4" | "5" => Some(("nature_reserve", 6)),
_ => Some(("nature_reserve", 8)),
};
}
if feature.can_be_polygon()
&& (feature.has_tag("place", "neighbourhood") || feature.has_tag("place", "suburb"))
{
return Some(("residential", 11));
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::input::{DataSource, OsmElementType};
use std::collections::HashMap;
use std::sync::Arc;
fn make_overture_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: "base".into(),
otype: "land_use".into(),
},
}
}
fn make_osm_feature(tags: &[(&str, &str)]) -> InputFeature {
let mut properties = HashMap::new();
for (k, v) in tags {
properties.insert(k.to_string(), PropValue::Str((*v).to_string()));
}
InputFeature {
id: 42,
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::Osm {
element_type: OsmElementType::Way,
relations: vec![],
},
}
}
fn osm_point(tags: &[(&str, &str)]) -> InputFeature {
let mut f = make_osm_feature(tags);
f.geometry = Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0)));
f
}
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 unsupported_source_returns_none() {
let mut f = make_overture_feature(&[]);
f.source = DataSource::LandPolygons;
assert!(process(&f).is_none());
}
#[test]
fn overture_classes_all_mapped() {
let cases: &[(&str, &str)] = &[
("park", "park"),
("national_park", "park"),
("garden", "garden"),
("golf_course", "golf_course"),
("dog_park", "dog_park"),
("playground", "playground"),
("pitch", "pitch"),
("nature_reserve", "nature_reserve"),
("industrial", "industrial"),
("railway", "railway"),
("commercial", "commercial"),
("residential", "residential"),
("military", "military"),
("cemetery", "cemetery"),
("hospital", "hospital"),
("school", "school"),
("kindergarten", "school"),
("university", "university"),
("college", "university"),
("forest", "forest"),
("wood", "forest"),
("farmland", "farmland"),
("farmyard", "farmland"),
("orchard", "farmland"),
("grass", "grass"),
("meadow", "grass"),
("village_green", "grass"),
("recreation_ground", "grass"),
("allotments", "allotments"),
("beach", "beach"),
("glacier", "glacier"),
("scrub", "scrub"),
("sand", "sand"),
("wetland", "wetland"),
("bare_rock", "bare_rock"),
("pier", "pier"),
("dam", "dam"),
("zoo", "zoo"),
("pedestrian", "pedestrian"),
("aerodrome", "aerodrome"),
("runway", "aerodrome"),
("grave_yard", "cemetery"),
("airfield", "airfield"),
("naval_base", "naval_base"),
("totally_unknown_xyz", "other"),
];
for (class, expected) in cases {
let f = make_overture_feature(&[("class", PropValue::Str((*class).into()))]);
let r = process(&f).expect("processed");
assert_eq!(kind_of(&r), *expected, "class={class}");
assert_eq!(r.layer, Layer::Landuse);
assert_eq!(r.min_zoom, 8);
assert_eq!(r.max_zoom, 15);
assert_eq!(r.sort_rank, 189);
}
}
#[test]
fn overture_real_subtype_class_pairs() {
let cases = [
("agriculture", "farmland", "farmland"),
("education", "school", "school"),
("medical", "hospital", "hospital"),
("golf", "golf_course", "golf_course"),
("horticulture", "orchard", "farmland"),
("managed", "grass", "grass"),
("developed", "industrial", "industrial"),
("cemetery", "grave_yard", "cemetery"),
];
for (subtype, class, expected) in cases {
let f = make_overture_feature(&[
("subtype", PropValue::Str(subtype.into())),
("class", PropValue::Str(class.into())),
]);
assert_eq!(
kind_of(&process(&f).unwrap()),
expected,
"{subtype}/{class}"
);
}
}
#[test]
fn overture_unknown_class_falls_back_to_subtype() {
let cases = [
("agriculture", "farmland"),
("horticulture", "farmland"),
("golf", "golf_course"),
("cemetery", "cemetery"),
("education", "school"),
("medical", "hospital"),
("military", "military"),
("park", "park"),
("residential", "residential"),
("pedestrian", "pedestrian"),
("protected", "nature_reserve"),
("managed", "grass"),
("recreation", "other"),
];
for (subtype, expected) in cases {
let f = make_overture_feature(&[
("subtype", PropValue::Str(subtype.into())),
("class", PropValue::Str("not_a_known_class".into())),
]);
assert_eq!(kind_of(&process(&f).unwrap()), expected, "{subtype}");
}
}
fn overture_land(class: &str, geometry: Arc<geo::Geometry<f64>>) -> InputFeature {
let mut f = make_overture_feature(&[
("subtype", PropValue::Str("forest".into())),
("class", PropValue::Str(class.into())),
]);
f.source = DataSource::Overture {
theme: "base".into(),
otype: "land".into(),
};
f.geometry = geometry;
f
}
#[test]
fn overture_land_vegetation_classes() {
let poly = make_overture_feature(&[]).geometry;
let cases = [
("wood", "wood"),
("forest", "wood"),
("grassland", "grassland"),
("meadow", "grassland"),
("fell", "grassland"),
("heath", "scrub"),
("shrubbery", "scrub"),
("wetland", "wetland"),
("scree", "bare_rock"),
("shingle", "bare_rock"),
("beach", "beach"),
("sand", "sand"),
];
for (class, expected) in cases {
let r = process(&overture_land(class, poly.clone())).unwrap();
assert_eq!(kind_of(&r), expected, "{class}");
assert_eq!(r.min_zoom, 7, "{class}");
assert_eq!(r.layer, Layer::Landuse);
}
}
#[test]
fn overture_land_non_vegetation_classes_are_dropped() {
let poly = make_overture_feature(&[]).geometry;
for class in ["island", "land", "peak", "cliff", "reef", "glacier"] {
assert!(
process(&overture_land(class, poly.clone())).is_none(),
"{class}"
);
}
}
#[test]
fn overture_land_points_are_dropped() {
let point = Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0)));
assert!(process(&overture_land("forest", point)).is_none());
}
#[test]
fn overture_land_without_class_is_dropped() {
let mut f = overture_land("forest", make_overture_feature(&[]).geometry);
f.properties.remove("class");
assert!(process(&f).is_none());
}
#[test]
fn overture_missing_subtype_is_other() {
let f = make_overture_feature(&[]);
let r = process(&f).unwrap();
assert_eq!(kind_of(&r), "other");
}
#[test]
fn overture_uses_names_primary() {
let f = make_overture_feature(&[
("subtype", PropValue::Str("park".into())),
("names.primary", PropValue::Str("Yosemite".into())),
]);
let r = process(&f).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Yosemite".into()))
);
}
#[test]
fn overture_falls_back_to_name() {
let f = make_overture_feature(&[
("subtype", PropValue::Str("park".into())),
("name", PropValue::Str("Hyde Park".into())),
]);
let r = process(&f).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Hyde Park".into()))
);
}
#[test]
fn overture_no_name_omits_property() {
let f = make_overture_feature(&[("subtype", PropValue::Str("park".into()))]);
let r = process(&f).unwrap();
assert!(!r.properties.iter().any(|(k, _)| k == "name"));
}
#[test]
fn osm_aeroway_aerodrome() {
let r = process(&make_osm_feature(&[("aeroway", "aerodrome")])).unwrap();
assert_eq!(kind_of(&r), "aerodrome");
assert_eq!(r.min_zoom, 10);
}
#[test]
fn osm_aeroway_runway_and_taxiway() {
for v in ["runway", "taxiway"] {
let r = process(&make_osm_feature(&[("aeroway", v)])).unwrap();
assert_eq!(kind_of(&r), "aerodrome");
assert_eq!(r.min_zoom, 11);
}
}
#[test]
fn osm_aeroway_apron_and_helipad() {
for v in ["apron", "helipad"] {
let r = process(&make_osm_feature(&[("aeroway", v)])).unwrap();
assert_eq!(kind_of(&r), "aerodrome");
assert_eq!(r.min_zoom, 13);
}
}
#[test]
fn osm_aeroway_unknown_returns_none() {
assert!(process(&make_osm_feature(&[("aeroway", "windsock")])).is_none());
}
#[test]
fn osm_amenity_kinds() {
let cases: &[(&str, &str, u8)] = &[
("hospital", "hospital", 11),
("clinic", "hospital", 11),
("school", "school", 11),
("kindergarten", "school", 11),
("university", "university", 11),
("college", "university", 11),
("grave_yard", "cemetery", 11),
("parking", "parking", 13),
];
for (tag, kind, mz) in cases {
let r = process(&make_osm_feature(&[("amenity", tag)])).unwrap();
assert_eq!(kind_of(&r), *kind);
assert_eq!(r.min_zoom, *mz);
}
}
#[test]
fn osm_amenity_unknown_none() {
assert!(process(&make_osm_feature(&[("amenity", "bench")])).is_none());
}
#[test]
fn osm_leisure_kinds() {
let cases: &[(&str, &str, u8)] = &[
("park", "park", 6),
("garden", "garden", 11),
("golf_course", "golf_course", 11),
("dog_park", "dog_park", 11),
("playground", "playground", 13),
("pitch", "pitch", 13),
("nature_reserve", "nature_reserve", 6),
("stadium", "stadium", 13),
("swimming_pool", "swimming_pool", 14),
];
for (tag, kind, mz) in cases {
let r = process(&make_osm_feature(&[("leisure", tag)])).unwrap();
assert_eq!(kind_of(&r), *kind);
assert_eq!(r.min_zoom, *mz);
}
}
#[test]
fn osm_leisure_unknown_none() {
assert!(process(&make_osm_feature(&[("leisure", "marina")])).is_none());
}
#[test]
fn osm_landuse_kinds() {
let cases: &[(&str, &str, u8)] = &[
("industrial", "industrial", 9),
("railway", "railway", 11),
("commercial", "commercial", 9),
("residential", "residential", 9),
("military", "military", 8),
("cemetery", "cemetery", 11),
("forest", "forest", 6),
("farmland", "farmland", 8),
("farmyard", "farmland", 11),
("orchard", "farmland", 11),
("vineyard", "farmland", 11),
("grass", "grass", 11),
("meadow", "grass", 11),
("village_green", "grass", 11),
("recreation_ground", "grass", 11),
("allotments", "allotments", 11),
("quarry", "quarry", 11),
("landfill", "landfill", 11),
("brownfield", "other", 11),
("greenfield", "other", 11),
("retail", "commercial", 11),
];
for (tag, kind, mz) in cases {
let r = process(&make_osm_feature(&[("landuse", tag)])).unwrap();
assert_eq!(kind_of(&r), *kind, "landuse={tag}");
assert_eq!(r.min_zoom, *mz);
}
}
#[test]
fn osm_landuse_unknown_none() {
assert!(process(&make_osm_feature(&[("landuse", "garage")])).is_none());
}
#[test]
fn osm_natural_kinds() {
let cases: &[(&str, &str, u8)] = &[
("wood", "forest", 6),
("scrub", "scrub", 9),
("sand", "sand", 11),
("beach", "sand", 11),
("wetland", "wetland", 9),
("bare_rock", "bare_rock", 11),
("scree", "bare_rock", 11),
("glacier", "glacier", 6),
("heath", "grass", 11),
];
for (tag, kind, mz) in cases {
let r = process(&make_osm_feature(&[("natural", tag)])).unwrap();
assert_eq!(kind_of(&r), *kind, "natural={tag}");
assert_eq!(r.min_zoom, *mz);
}
}
#[test]
fn osm_natural_unknown_none() {
assert!(process(&make_osm_feature(&[("natural", "spring")])).is_none());
}
#[test]
fn osm_pedestrian_area_polygon() {
let r = process(&make_osm_feature(&[("highway", "pedestrian")])).unwrap();
assert_eq!(kind_of(&r), "pedestrian");
assert_eq!(r.min_zoom, 13);
}
#[test]
fn osm_pedestrian_line_skipped() {
let mut f = make_osm_feature(&[("highway", "pedestrian")]);
f.geometry = Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
])));
assert!(process(&f).is_none());
assert!(process(&osm_point(&[("place", "neighbourhood")])).is_none());
}
#[test]
fn osm_man_made_pier() {
let r = process(&make_osm_feature(&[("man_made", "pier")])).unwrap();
assert_eq!(kind_of(&r), "pier");
assert_eq!(r.min_zoom, 13);
}
#[test]
fn osm_man_made_bridge() {
let r = process(&make_osm_feature(&[("man_made", "bridge")])).unwrap();
assert_eq!(kind_of(&r), "bridge");
}
#[test]
fn osm_man_made_unknown_none() {
assert!(process(&make_osm_feature(&[("man_made", "tower")])).is_none());
}
#[test]
fn osm_waterway_dam() {
let r = process(&make_osm_feature(&[("waterway", "dam")])).unwrap();
assert_eq!(kind_of(&r), "dam");
assert_eq!(r.min_zoom, 11);
}
#[test]
fn osm_tourism_zoo() {
let r = process(&make_osm_feature(&[("tourism", "zoo")])).unwrap();
assert_eq!(kind_of(&r), "zoo");
}
#[test]
fn osm_boundary_national_park() {
let r = process(&make_osm_feature(&[("boundary", "national_park")])).unwrap();
assert_eq!(kind_of(&r), "park");
assert_eq!(r.min_zoom, 4);
}
#[test]
fn osm_boundary_protected_area_class_2() {
let r = process(&make_osm_feature(&[
("boundary", "protected_area"),
("protect_class", "2"),
]))
.unwrap();
assert_eq!(kind_of(&r), "park");
assert_eq!(r.min_zoom, 4);
}
#[test]
fn osm_boundary_protected_area_class_3_4_5() {
for class in ["3", "4", "5"] {
let r = process(&make_osm_feature(&[
("boundary", "protected_area"),
("protect_class", class),
]))
.unwrap();
assert_eq!(kind_of(&r), "nature_reserve");
assert_eq!(r.min_zoom, 6);
}
}
#[test]
fn osm_boundary_protected_area_no_class() {
let other_class = [("boundary", "protected_area"), ("protect_class", "1")];
for tags in [&[("boundary", "protected_area")][..], &other_class] {
let r = process(&make_osm_feature(tags)).expect("a protected area");
assert_eq!(kind_of(&r), "nature_reserve", "{tags:?}");
assert_eq!(r.min_zoom, 8, "{tags:?}");
}
}
#[test]
fn osm_place_neighbourhood_polygon() {
for place in ["neighbourhood", "suburb"] {
let r = process(&make_osm_feature(&[("place", place)])).expect(place);
assert_eq!(kind_of(&r), "residential", "{place}");
}
}
#[test]
fn osm_no_matching_tags_none() {
assert!(process(&make_osm_feature(&[("random", "thing")])).is_none());
}
#[test]
fn osm_name_included() {
let r = process(&make_osm_feature(&[
("leisure", "park"),
("name", "Central Park"),
]))
.unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Central Park".into()))
);
}
#[test]
fn osm_no_name_omits_property() {
let r = process(&make_osm_feature(&[("leisure", "park")])).unwrap();
assert!(!r.properties.iter().any(|(k, _)| k == "name"));
}
#[test]
fn osm_output_has_layer_and_max_zoom_15() {
let r = process(&make_osm_feature(&[("landuse", "industrial")])).unwrap();
assert_eq!(r.layer, Layer::Landuse);
assert_eq!(r.max_zoom, 15);
assert_eq!(r.sort_rank, 189);
assert_eq!(r.id, 42);
}
}