use geo::{Area, 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::WaterPolygons => process_water_polygons(feature),
DataSource::NaturalEarth { .. } => process_ne(feature),
DataSource::Osm { .. } => process_osm(feature),
_ => None,
}
}
#[must_use]
fn area_based_min_zoom(geometry: &geo::Geometry<f64>, kind_floor: u8) -> u8 {
let area = match geometry {
geo::Geometry::Polygon(p) => p.unsigned_area(),
geo::Geometry::MultiPolygon(mp) => mp.unsigned_area(),
_ => return kind_floor.max(13),
};
let zoom: u8 = if area > 0.1 {
3 } else if area > 0.01 {
5 } else if area > 0.001 {
7 } else if area > 0.0001 {
9 } else if area > 0.00001 {
11 } else {
13 };
zoom.max(kind_floor)
}
fn water_kind(value: &str) -> Option<(&'static str, u8)> {
Some(match value {
"ocean" => ("ocean", 0u8),
"sea" => ("sea", 3),
"lake" => ("lake", 6),
"pond" => ("pond", 10),
"reservoir" => ("lake", 6),
"river" => ("river", 9),
"stream" => ("stream", 13),
"canal" => ("canal", 10),
"drain" => ("drain", 15),
"ditch" => ("ditch", 15),
"reef" => ("reef", 11),
"swimming_pool" => ("swimming_pool", 14),
"fountain" => ("fountain", 14),
"dock" => ("dock", 12),
"fjord" => ("fjord", 6),
"strait" => ("strait", 6),
"bay" => ("bay", 6),
"basin" => ("basin", 12),
"playa" => ("playa", 6),
"spring" => ("spring", 14),
"waterfall" => ("waterfall", 13),
"hot_spring" => ("spring", 14),
"water_well" => ("water", 15),
"fishpond" => ("pond", 10),
"oxbow" | "lagoon" => ("lake", 6),
"moat" => ("canal", 10),
_ => return None,
})
}
fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
let (kind, base_min_zoom) = feature
.get_str("class")
.and_then(water_kind)
.or_else(|| feature.get_str("subtype").and_then(water_kind))
.unwrap_or(("water", 6));
let min_zoom = match kind {
"lake" | "pond" | "basin" | "playa" | "water" => {
area_based_min_zoom(feature.geometry.as_ref(), base_min_zoom)
}
_ => base_min_zoom,
};
let mut properties = Vec::with_capacity(3);
properties.push(("kind".into(), PropValue::Str(kind.into())));
push_names(feature, &mut properties);
if let Some(detail) = feature.get_str("class") {
properties.push(("kind_detail".into(), PropValue::Str(detail.to_string())));
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Water,
min_zoom,
max_zoom: 15,
sort_rank: 200,
properties,
})
}
fn process_water_polygons(feature: &InputFeature) -> Option<ProcessedFeature> {
let properties = vec![("kind".into(), PropValue::Str("ocean".into()))];
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Water,
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 (kind, min_zoom, max_zoom) = match source_layer {
"ne_50m_ocean" => ("ocean", 0u8, 4u8),
"ne_50m_lakes" => ("lake", 0, 4),
"ne_10m_ocean" => ("ocean", 5, 5),
"ne_10m_lakes" => ("lake", 5, 5),
_ => return None,
};
let resolved_kind = if kind == "lake" {
match feature.get_str("featurecla") {
Some("Playa") => "playa",
Some("Reservoir") => "lake",
Some("Alkaline Lake") => "lake",
_ => "lake",
}
} else {
kind
};
let mut properties = Vec::with_capacity(2);
properties.push(("kind".into(), PropValue::Str(resolved_kind.into())));
push_names(feature, &mut properties);
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Water,
min_zoom,
max_zoom,
sort_rank: 200,
properties,
})
}
fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
if let Some(waterway) = feature.get_str("waterway") {
return process_osm_waterway(feature, waterway);
}
if feature.has_tag("natural", "water") {
let water_type = feature.get_str("water").unwrap_or("");
let (kind, min_zoom) = match water_type {
"river" | "riverbank" => ("river", 9u8),
"lake" => ("lake", 6),
"reservoir" => ("lake", 6),
"pond" => ("pond", 10),
"basin" => ("basin", 12),
"canal" => ("canal", 10),
"stream" => ("stream", 13),
_ => ("water", 6),
};
return emit_water_feature(feature, kind, min_zoom, true);
}
if feature.has_tag("natural", "spring") || feature.has_tag("natural", "hot_spring") {
return emit_water_feature(feature, "spring", 14, false);
}
if feature.has_tag("natural", "waterfall") || feature.has_tag("waterway", "waterfall") {
return emit_water_feature(feature, "waterfall", 13, false);
}
if feature.has_tag("natural", "reef") {
return emit_water_feature(feature, "reef", 11, false);
}
if feature.has_tag("leisure", "swimming_pool") {
return emit_water_feature(feature, "swimming_pool", 14, false);
}
if feature.has_tag("landuse", "reservoir") {
return emit_water_feature(feature, "lake", 6, true);
}
if feature.has_tag("landuse", "basin") {
return emit_water_feature(feature, "basin", 12, false);
}
if feature.has_tag("natural", "bay") {
return emit_water_feature(feature, "bay", 6, true);
}
if feature.has_tag("natural", "fjord") {
return emit_water_feature(feature, "fjord", 6, true);
}
if feature.has_tag("natural", "strait") {
return emit_water_feature(feature, "strait", 6, true);
}
if feature.has_tag("amenity", "fountain") {
return emit_water_feature(feature, "fountain", 14, false);
}
if feature.has_tag("waterway", "dock") || feature.has_tag("waterway", "boatyard") {
return emit_water_feature(feature, "dock", 12, false);
}
if feature.has_tag("natural", "sea") {
return emit_water_feature(feature, "sea", 3, true);
}
if feature.has_tag("place", "ocean") {
return emit_water_feature(feature, "ocean", 0, true);
}
None
}
fn process_osm_waterway(feature: &InputFeature, waterway: &str) -> Option<ProcessedFeature> {
let (kind, base_zoom) = match waterway {
"river" => ("river", 9u8),
"stream" => ("stream", 13),
"canal" => ("canal", 10),
"drain" => ("drain", 15),
"ditch" => ("ditch", 15),
"riverbank" => ("river", 6),
_ => return None,
};
let min_zoom = if feature.can_be_line() && !feature.can_be_polygon() {
base_zoom.saturating_add(1)
} else {
base_zoom
};
emit_water_feature(feature, kind, min_zoom, false)
}
fn emit_water_feature(
feature: &InputFeature,
kind: &str,
min_zoom: u8,
generate_label: bool,
) -> Option<ProcessedFeature> {
let mut properties = Vec::with_capacity(3);
let min_zoom = match kind {
"lake" | "pond" | "basin" | "water" => {
area_based_min_zoom(feature.geometry.as_ref(), min_zoom)
}
_ => min_zoom,
};
properties.push(("kind".into(), PropValue::Str(kind.into())));
push_names(feature, &mut properties);
if generate_label && feature.has_key("name") && feature.can_be_polygon() {
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 {
return Some(ProcessedFeature {
id: feature.id,
geometry: geom,
layer: Layer::Water,
min_zoom,
max_zoom: 15,
sort_rank: 200,
properties,
});
}
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Water,
min_zoom,
max_zoom: 15,
sort_rank: 200,
properties,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::input::{DataSource, OsmElementType};
use std::collections::HashMap;
fn small_poly() -> 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 big_poly() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
geo::LineString::from(vec![
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(0.0, 10.0),
(0.0, 0.0),
]),
vec![],
)))
}
fn tiny_poly() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
geo::LineString::from(vec![(0.0, 0.0), (0.001, 0.0), (0.001, 0.001), (0.0, 0.0)]),
vec![],
)))
}
fn multipoly() -> 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![],
),
])))
}
fn line() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
])))
}
fn point() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0)))
}
fn make_with(
props: &[(&str, PropValue)],
geom: 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: geom,
properties,
bbox: None,
source,
}
}
fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
make_with(props, small_poly(), overture_src())
}
fn overture_src() -> DataSource {
DataSource::Overture {
theme: String::new(),
otype: String::new(),
}
}
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(),
}
}
fn has_kind(feat: &ProcessedFeature, k: &str) -> bool {
feat.properties
.iter()
.any(|(key, v)| key == "kind" && *v == PropValue::Str(k.into()))
}
#[test]
fn ocean_z0() {
let feat = make_feature(&[("subtype", PropValue::Str("ocean".into()))]);
let r = process(&feat).unwrap();
assert_eq!(r.layer, Layer::Water);
assert_eq!(r.min_zoom, 0);
assert!(has_kind(&r, "ocean"));
}
#[test]
fn lake_z6() {
let feat = make_feature(&[("subtype", PropValue::Str("lake".into()))]);
assert_eq!(process(&feat).unwrap().min_zoom, 6);
}
#[test]
fn river_z9() {
let feat = make_feature(&[("subtype", PropValue::Str("river".into()))]);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 9);
assert!(has_kind(&r, "river"));
}
#[test]
fn stream_z13() {
let feat = make_feature(&[("subtype", PropValue::Str("stream".into()))]);
assert_eq!(process(&feat).unwrap().min_zoom, 13);
}
#[test]
fn default_water_z6() {
let feat = make_feature(&[]);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 6);
assert_eq!(r.sort_rank, 200);
}
#[test]
fn overture_all_subtypes() {
let cases = [
("sea", 3u8),
("pond", 10),
("reservoir", 6),
("canal", 10),
("drain", 15),
("ditch", 15),
("reef", 11),
("swimming_pool", 14),
("fountain", 14),
("dock", 12),
("fjord", 6),
("strait", 6),
("bay", 6),
("playa", 6),
("spring", 14),
("waterfall", 13),
("hot_spring", 14),
("water_well", 15),
];
for (sub, _expected_min) in cases {
let feat = make_feature(&[("subtype", PropValue::Str(sub.into()))]);
let r = process(&feat).unwrap();
assert!(r.min_zoom >= 3 || sub == "ocean");
}
}
#[test]
fn overture_basin_uses_area() {
let feat = make_with(
&[("subtype", PropValue::Str("basin".into()))],
tiny_poly(),
overture_src(),
);
let r = process(&feat).unwrap();
assert!(r.min_zoom >= 12);
}
#[test]
fn overture_with_name_and_class() {
let feat = make_feature(&[
("subtype", PropValue::Str("lake".into())),
("names.primary", PropValue::Str("Tahoe".into())),
("class", PropValue::Str("alpine".into())),
]);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Tahoe".into()))
);
assert!(
r.properties
.iter()
.any(|(k, v)| k == "kind_detail" && *v == PropValue::Str("alpine".into()))
);
}
#[test]
fn overture_falls_back_to_name_property() {
let feat = make_feature(&[
("subtype", PropValue::Str("river".into())),
("name", PropValue::Str("Nile".into())),
]);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Nile".into()))
);
}
#[test]
fn overture_class_decides_over_subtype() {
let cases = [
("canal", "ditch", "ditch", 15),
("canal", "drain", "drain", 15),
("canal", "moat", "canal", 10),
("human_made", "swimming_pool", "swimming_pool", 14),
("physical", "waterfall", "waterfall", 13),
("spring", "hot_spring", "spring", 14),
];
for (subtype, class, kind, zoom) in cases {
let feat = make_with(
&[
("subtype", PropValue::Str(subtype.into())),
("class", PropValue::Str(class.into())),
],
Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
]))),
overture_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, kind), "{subtype}/{class}");
assert_eq!(r.min_zoom, zoom, "{subtype}/{class}");
}
}
#[test]
fn overture_bay_and_lagoon_classes() {
for (subtype, class, kind) in [
("physical", "bay", "bay"),
("lake", "lagoon", "lake"),
("pond", "fishpond", "pond"),
] {
let feat = make_feature(&[
("subtype", PropValue::Str(subtype.into())),
("class", PropValue::Str(class.into())),
]);
assert!(
has_kind(&process(&feat).unwrap(), kind),
"{subtype}/{class}"
);
}
}
#[test]
fn overture_unknown_subtype() {
let feat = make_feature(&[("subtype", PropValue::Str("mystery".into()))]);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "water"));
}
#[test]
fn water_polygons_z6() {
let feat = make_with(&[], small_poly(), DataSource::WaterPolygons);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 6);
assert!(has_kind(&r, "ocean"));
}
#[test]
fn ne_50m_ocean() {
let feat = make_with(&[], small_poly(), ne_src("ne_50m_ocean"));
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 0);
assert_eq!(r.max_zoom, 4);
assert!(has_kind(&r, "ocean"));
}
#[test]
fn ne_50m_lakes() {
let feat = make_with(&[], small_poly(), ne_src("ne_50m_lakes"));
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 0);
assert!(has_kind(&r, "lake"));
}
#[test]
fn ne_10m_ocean_z5() {
let feat = make_with(&[], small_poly(), ne_src("ne_10m_ocean"));
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 5);
assert_eq!(r.max_zoom, 5);
}
#[test]
fn ne_10m_lakes_z5() {
let feat = make_with(&[], small_poly(), ne_src("ne_10m_lakes"));
assert_eq!(process(&feat).unwrap().min_zoom, 5);
}
#[test]
fn ne_lake_featurecla_playa() {
let feat = make_with(
&[("featurecla", PropValue::Str("Playa".into()))],
small_poly(),
ne_src("ne_50m_lakes"),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "playa"));
}
#[test]
fn ne_lake_featurecla_reservoir() {
let feat = make_with(
&[("featurecla", PropValue::Str("Reservoir".into()))],
small_poly(),
ne_src("ne_50m_lakes"),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "lake"));
}
#[test]
fn ne_lake_featurecla_alkaline() {
let feat = make_with(
&[("featurecla", PropValue::Str("Alkaline Lake".into()))],
small_poly(),
ne_src("ne_50m_lakes"),
);
assert!(has_kind(&process(&feat).unwrap(), "lake"));
}
fn has(r: &ProcessedFeature, key: &str, value: &str) -> bool {
r.properties
.iter()
.any(|(k, v)| k == key && *v == PropValue::Str(value.into()))
}
#[test]
fn ne_lake_maps_name_columns_to_language_attributes() {
let feat = make_with(
&[
("name", PropValue::Str("Ladoga".into())),
("name_en", PropValue::Str("Lake Ladoga".into())),
("name_ru", PropValue::Str("Ладожское озеро".into())),
("name_zh", PropValue::Str("拉多加湖".into())),
],
small_poly(),
ne_src("ne_50m_lakes"),
);
let r = process(&feat).unwrap();
assert!(has(&r, "name:en", "Lake Ladoga"));
assert!(has(&r, "name:ru", "Ладожское озеро"));
assert!(has(&r, "name:zh-Hans", "拉多加湖"));
}
#[test]
fn osm_lake_label_carries_script_and_name_en() {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str("lake".into())),
("name", PropValue::Str("شالمار متل".into())),
("name:en", PropValue::Str("Dal Lake".into())),
],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has(&r, "script", "Arabic"));
assert!(has(&r, "name:en", "Dal Lake"));
}
#[test]
fn ne_with_name() {
let feat = make_with(
&[("name", PropValue::Str("Lake X".into()))],
small_poly(),
ne_src("ne_50m_lakes"),
);
let r = process(&feat).unwrap();
assert!(
r.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("Lake X".into()))
);
}
#[test]
fn ne_unknown_layer() {
let feat = make_with(&[], small_poly(), ne_src("ne_50m_admin"));
assert!(process(&feat).is_none());
}
#[test]
fn osm_waterway_river_line_bumps_zoom() {
let feat = make_with(
&[("waterway", PropValue::Str("river".into()))],
line(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 10);
assert!(has_kind(&r, "river"));
}
#[test]
fn osm_waterway_stream() {
let feat = make_with(
&[("waterway", PropValue::Str("stream".into()))],
line(),
osm_src(),
);
assert_eq!(process(&feat).unwrap().min_zoom, 14);
}
#[test]
fn osm_waterway_canal() {
let feat = make_with(
&[("waterway", PropValue::Str("canal".into()))],
line(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 11);
assert!(has_kind(&r, "canal"));
}
#[test]
fn osm_waterway_drain() {
let feat = make_with(
&[("waterway", PropValue::Str("drain".into()))],
line(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(r.min_zoom >= 15);
assert!(has_kind(&r, "drain"));
}
#[test]
fn osm_waterway_ditch() {
let feat = make_with(
&[("waterway", PropValue::Str("ditch".into()))],
line(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), "ditch"));
}
#[test]
fn osm_waterway_riverbank_polygon() {
let feat = make_with(
&[("waterway", PropValue::Str("riverbank".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 6);
assert!(has_kind(&r, "river"));
}
#[test]
fn osm_waterway_unknown() {
let feat = make_with(
&[("waterway", PropValue::Str("unknown".into()))],
line(),
osm_src(),
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_natural_water_default() {
let feat = make_with(
&[("natural", PropValue::Str("water".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "water"));
}
#[test]
fn osm_natural_water_river() {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str("river".into())),
],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "river"));
assert_eq!(r.min_zoom, 9);
}
#[test]
fn osm_natural_water_lake_with_name_emits_label() {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str("lake".into())),
("name", PropValue::Str("L".into())),
],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
}
#[test]
fn osm_natural_water_lake_multipolygon_label() {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str("lake".into())),
("name", PropValue::Str("L".into())),
],
multipoly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
}
#[test]
fn osm_natural_water_lake_no_name_keeps_polygon() {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str("lake".into())),
],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Polygon(_)));
}
#[test]
fn osm_natural_water_kinds() {
for (water, kind) in [
("riverbank", "river"),
("reservoir", "lake"),
("pond", "pond"),
("basin", "basin"),
("canal", "canal"),
("stream", "stream"),
] {
let feat = make_with(
&[
("natural", PropValue::Str("water".into())),
("water", PropValue::Str(water.into())),
],
small_poly(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), kind), "{water}");
}
}
#[test]
fn osm_spring() {
let feat = make_with(
&[("natural", PropValue::Str("spring".into()))],
point(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "spring"));
assert_eq!(r.min_zoom, 14);
}
#[test]
fn osm_hot_spring() {
let feat = make_with(
&[("natural", PropValue::Str("hot_spring".into()))],
point(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), "spring"));
}
#[test]
fn osm_natural_waterfall() {
let feat = make_with(
&[("natural", PropValue::Str("waterfall".into()))],
point(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 13);
assert!(has_kind(&r, "waterfall"));
}
#[test]
fn osm_reef() {
let feat = make_with(
&[("natural", PropValue::Str("reef".into()))],
small_poly(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), "reef"));
}
#[test]
fn osm_swimming_pool() {
let feat = make_with(
&[("leisure", PropValue::Str("swimming_pool".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 14);
assert!(has_kind(&r, "swimming_pool"));
}
#[test]
fn osm_landuse_reservoir_emits_lake_label() {
let feat = make_with(
&[
("landuse", PropValue::Str("reservoir".into())),
("name", PropValue::Str("R".into())),
],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "lake"));
assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
}
#[test]
fn osm_landuse_basin() {
let feat = make_with(
&[("landuse", PropValue::Str("basin".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert!(has_kind(&r, "basin"));
}
#[test]
fn osm_natural_bay_fjord_strait() {
for nat in ["bay", "fjord", "strait"] {
let feat = make_with(
&[("natural", PropValue::Str(nat.into()))],
small_poly(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), nat));
}
}
#[test]
fn osm_amenity_fountain() {
let feat = make_with(
&[("amenity", PropValue::Str("fountain".into()))],
point(),
osm_src(),
);
assert!(has_kind(&process(&feat).unwrap(), "fountain"));
}
#[test]
fn osm_natural_sea() {
let feat = make_with(
&[("natural", PropValue::Str("sea".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 3);
assert!(has_kind(&r, "sea"));
}
#[test]
fn osm_place_ocean() {
let feat = make_with(
&[("place", PropValue::Str("ocean".into()))],
small_poly(),
osm_src(),
);
let r = process(&feat).unwrap();
assert_eq!(r.min_zoom, 0);
assert!(has_kind(&r, "ocean"));
}
#[test]
fn osm_unrelated_returns_none() {
let feat = make_with(
&[("highway", PropValue::Str("primary".into()))],
line(),
osm_src(),
);
assert!(process(&feat).is_none());
}
#[test]
fn other_source_returns_none() {
let feat = make_with(&[], small_poly(), DataSource::IndiaBoundary);
assert!(process(&feat).is_none());
let feat = make_with(&[], small_poly(), DataSource::LandPolygons);
assert!(process(&feat).is_none());
}
#[test]
fn area_zoom_huge_polygon() {
let g = big_poly();
assert_eq!(area_based_min_zoom(g.as_ref(), 0), 3);
}
#[test]
fn area_zoom_multipolygon() {
let g = multipoly();
let z = area_based_min_zoom(g.as_ref(), 0);
assert!(z >= 3);
}
#[test]
fn area_zoom_point_clamps_to_13() {
let g = point();
assert_eq!(area_based_min_zoom(g.as_ref(), 0), 13);
assert_eq!(area_based_min_zoom(g.as_ref(), 14), 14);
}
#[test]
fn area_zoom_respects_floor() {
let g = big_poly();
assert_eq!(area_based_min_zoom(g.as_ref(), 8), 8);
}
#[test]
fn area_zoom_tiers() {
let mk = |side: f64| {
geo::Geometry::Polygon(geo::Polygon::new(
geo::LineString::from(vec![
(0.0, 0.0),
(side, 0.0),
(side, side),
(0.0, side),
(0.0, 0.0),
]),
vec![],
))
};
assert_eq!(area_based_min_zoom(&mk(0.2), 0), 5);
assert_eq!(area_based_min_zoom(&mk(0.05), 0), 7);
assert_eq!(area_based_min_zoom(&mk(0.015), 0), 9);
assert_eq!(area_based_min_zoom(&mk(0.005), 0), 11);
assert_eq!(area_based_min_zoom(&mk(0.001), 0), 13);
}
}