1use geo::Centroid;
11use std::sync::Arc;
12
13use crate::layers::Layer;
14use crate::source::input::{DataSource, InputFeature};
15use crate::tile::feature::PropValue;
16
17use super::buildings::ProcessedFeature;
18
19pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
23 match &feature.source {
24 DataSource::Overture { .. } => process_overture(feature),
25 DataSource::LandPolygons => process_land_polygons(feature),
26 DataSource::NaturalEarth { .. } => process_ne(feature),
27 DataSource::Osm { .. } => process_osm(feature),
28 _ => None,
29 }
30}
31
32fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
40 if feature.get_str("subtype").is_some_and(|s| s != "land") {
41 return None;
42 }
43
44 let (kind, min_zoom) = if feature.can_be_polygon() {
45 ("earth", 6u8)
46 } else if feature.is_point() {
47 match feature.get_str("class") {
48 Some("island") => ("island", 6),
49 Some("islet") => ("island", 12),
50 _ => return None,
51 }
52 } else {
53 return None;
54 };
55
56 let mut properties = Vec::with_capacity(2);
57 properties.push(("kind".into(), PropValue::Str(kind.into())));
58
59 if let Some(name) = feature.get_str("names.primary").or(feature.get_str("name")) {
60 properties.push(("name".into(), PropValue::Str(name.to_string())));
61 }
62
63 Some(ProcessedFeature {
64 id: feature.id,
65 geometry: feature.geometry.clone(),
66 layer: Layer::Earth,
67 min_zoom,
68 max_zoom: 15,
69 sort_rank: 200,
70 properties,
71 })
72}
73
74fn process_land_polygons(feature: &InputFeature) -> Option<ProcessedFeature> {
78 let properties = vec![("kind".into(), PropValue::Str("earth".into()))];
79
80 Some(ProcessedFeature {
81 id: feature.id,
82 geometry: feature.geometry.clone(),
83 layer: Layer::Earth,
84 min_zoom: 6,
85 max_zoom: 15,
86 sort_rank: 200,
87 properties,
88 })
89}
90
91fn process_ne(feature: &InputFeature) -> Option<ProcessedFeature> {
96 let source_layer = feature.ne_source_layer()?;
97
98 let (min_zoom, max_zoom) = match source_layer {
99 "ne_50m_land" => (0u8, 4u8),
100 "ne_10m_land" => (5, 5),
101 _ => return None,
102 };
103
104 let properties = vec![("kind".into(), PropValue::Str("earth".into()))];
105
106 Some(ProcessedFeature {
107 id: feature.id,
108 geometry: feature.geometry.clone(),
109 layer: Layer::Earth,
110 min_zoom,
111 max_zoom,
112 sort_rank: 200,
113 properties,
114 })
115}
116
117fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
122 if feature.can_be_line() && !feature.can_be_polygon() && feature.has_tag("natural", "cliff") {
124 let mut properties = Vec::with_capacity(3);
125 properties.push(("kind".into(), PropValue::Str("cliff".into())));
126 properties.push(("min_zoom".into(), PropValue::I64(13)));
127
128 if let Some(name) = feature.get_str("name") {
129 properties.push(("name".into(), PropValue::Str(name.to_string())));
130 }
131
132 return Some(ProcessedFeature {
133 id: feature.id,
134 geometry: feature.geometry.clone(),
135 layer: Layer::Earth,
136 min_zoom: 12,
137 max_zoom: 15,
138 sort_rank: 200,
139 properties,
140 });
141 }
142
143 if feature.can_be_polygon() && feature.has_tag("place", "island") {
145 let label_geom = match feature.geometry.as_ref() {
146 geo::Geometry::Polygon(p) => p.centroid().map(|c| Arc::new(geo::Geometry::Point(c))),
147 geo::Geometry::MultiPolygon(mp) => {
148 mp.centroid().map(|c| Arc::new(geo::Geometry::Point(c)))
149 }
150 _ => None,
151 };
152
153 if let Some(geom) = label_geom {
154 let mut properties = Vec::with_capacity(2);
155 properties.push(("kind".into(), PropValue::Str("island".into())));
156
157 if let Some(name) = feature.get_str("name") {
158 properties.push(("name".into(), PropValue::Str(name.to_string())));
159 }
160
161 return Some(ProcessedFeature {
162 id: feature.id,
163 geometry: geom,
164 layer: Layer::Earth,
165 min_zoom: 6,
166 max_zoom: 15,
167 sort_rank: 200,
168 properties,
169 });
170 }
171 }
172
173 None
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179 use crate::source::input::{DataSource, OsmElementType};
180 use std::collections::HashMap;
181
182 fn polygon_geom() -> Arc<geo::Geometry<f64>> {
183 Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
184 geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
185 vec![],
186 )))
187 }
188
189 fn multipoly_geom() -> Arc<geo::Geometry<f64>> {
190 Arc::new(geo::Geometry::MultiPolygon(geo::MultiPolygon(vec![
191 geo::Polygon::new(
192 geo::LineString::from(vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]),
193 vec![],
194 ),
195 geo::Polygon::new(
196 geo::LineString::from(vec![(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 2.0)]),
197 vec![],
198 ),
199 ])))
200 }
201
202 fn line_geom() -> Arc<geo::Geometry<f64>> {
203 Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
204 (0.0, 0.0),
205 (1.0, 1.0),
206 ])))
207 }
208
209 fn point_geom() -> Arc<geo::Geometry<f64>> {
210 Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0)))
211 }
212
213 fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
214 make_with(
215 props,
216 polygon_geom(),
217 DataSource::Overture {
218 theme: String::new(),
219 otype: String::new(),
220 },
221 )
222 }
223
224 fn make_with(
225 props: &[(&str, PropValue)],
226 geometry: Arc<geo::Geometry<f64>>,
227 source: DataSource,
228 ) -> InputFeature {
229 let mut properties = HashMap::new();
230 for (k, v) in props {
231 properties.insert(k.to_string(), v.clone());
232 }
233 InputFeature {
234 id: 1,
235 geometry,
236 properties,
237 bbox: None,
238 source,
239 }
240 }
241
242 fn osm_src() -> DataSource {
243 DataSource::Osm {
244 element_type: OsmElementType::Way,
245 relations: vec![],
246 }
247 }
248
249 fn ne_src(layer: &str) -> DataSource {
250 DataSource::NaturalEarth {
251 source_layer: layer.to_string(),
252 }
253 }
254
255 #[test]
256 fn default_land_is_earth() {
257 let feat = make_feature(&[]);
258 let result = process(&feat).unwrap();
259 assert_eq!(result.layer, Layer::Earth);
260 assert_eq!(result.min_zoom, 6);
261 assert_eq!(result.sort_rank, 200);
262 }
263
264 fn kind(r: &ProcessedFeature) -> Option<&PropValue> {
265 r.properties
266 .iter()
267 .find(|(k, _)| k == "kind")
268 .map(|(_, v)| v)
269 }
270
271 fn overture_src() -> DataSource {
272 DataSource::Overture {
273 theme: "base".into(),
274 otype: "land".into(),
275 }
276 }
277
278 #[test]
279 fn overture_land_polygon_is_earth_at_z6() {
280 let feat = make_feature(&[
281 ("subtype", PropValue::Str("land".into())),
282 ("class", PropValue::Str("island".into())),
283 ]);
284 let r = process(&feat).unwrap();
285 assert_eq!(kind(&r), Some(&PropValue::Str("earth".into())));
286 assert_eq!(r.min_zoom, 6);
287 }
288
289 #[test]
292 fn overture_non_land_subtypes_are_not_earth() {
293 for subtype in [
294 "tree", "shrub", "forest", "grass", "physical", "rock", "wetland", "sand", "reef",
295 "glacier", "desert", "crater",
296 ] {
297 let feat = make_feature(&[("subtype", PropValue::Str(subtype.into()))]);
298 assert!(process(&feat).is_none(), "{subtype}");
299 }
300 }
301
302 #[test]
303 fn overture_island_point_is_island_label_at_z6() {
304 let feat = make_with(
305 &[
306 ("subtype", PropValue::Str("land".into())),
307 ("class", PropValue::Str("island".into())),
308 ],
309 point_geom(),
310 overture_src(),
311 );
312 let r = process(&feat).unwrap();
313 assert_eq!(kind(&r), Some(&PropValue::Str("island".into())));
314 assert_eq!(r.min_zoom, 6);
315 }
316
317 #[test]
318 fn overture_islet_point_is_island_label_at_z12() {
319 let feat = make_with(
320 &[
321 ("subtype", PropValue::Str("land".into())),
322 ("class", PropValue::Str("islet".into())),
323 ],
324 point_geom(),
325 overture_src(),
326 );
327 let r = process(&feat).unwrap();
328 assert_eq!(kind(&r), Some(&PropValue::Str("island".into())));
329 assert_eq!(r.min_zoom, 12);
330 }
331
332 #[test]
333 fn overture_other_land_points_and_lines_are_dropped() {
334 let point = make_with(
335 &[
336 ("subtype", PropValue::Str("land".into())),
337 ("class", PropValue::Str("land".into())),
338 ],
339 point_geom(),
340 overture_src(),
341 );
342 assert!(process(&point).is_none());
343 let line = make_with(
344 &[("subtype", PropValue::Str("land".into()))],
345 line_geom(),
346 overture_src(),
347 );
348 assert!(process(&line).is_none());
349 }
350
351 #[test]
352 fn overture_uses_names_primary() {
353 let feat = make_feature(&[
354 ("subtype", PropValue::Str("land".into())),
355 ("names.primary", PropValue::Str("Madagascar".into())),
356 ]);
357 let r = process(&feat).unwrap();
358 assert!(
359 r.properties
360 .iter()
361 .any(|(k, v)| k == "name" && *v == PropValue::Str("Madagascar".into()))
362 );
363 }
364
365 #[test]
366 fn overture_falls_back_to_name() {
367 let feat = make_feature(&[
368 ("subtype", PropValue::Str("land".into())),
369 ("name", PropValue::Str("Bali".into())),
370 ]);
371 let r = process(&feat).unwrap();
372 assert!(
373 r.properties
374 .iter()
375 .any(|(k, v)| k == "name" && *v == PropValue::Str("Bali".into()))
376 );
377 }
378
379 #[test]
380 fn land_polygons_z6() {
381 let feat = make_with(&[], polygon_geom(), DataSource::LandPolygons);
382 let r = process(&feat).unwrap();
383 assert_eq!(r.min_zoom, 6);
384 assert_eq!(r.max_zoom, 15);
385 assert_eq!(r.layer, Layer::Earth);
386 assert!(
387 r.properties
388 .iter()
389 .any(|(k, v)| k == "kind" && *v == PropValue::Str("earth".into()))
390 );
391 }
392
393 #[test]
394 fn ne_50m_land_z0_4() {
395 let feat = make_with(&[], polygon_geom(), ne_src("ne_50m_land"));
396 let r = process(&feat).unwrap();
397 assert_eq!(r.min_zoom, 0);
398 assert_eq!(r.max_zoom, 4);
399 }
400
401 #[test]
402 fn ne_10m_land_z5() {
403 let feat = make_with(&[], polygon_geom(), ne_src("ne_10m_land"));
404 let r = process(&feat).unwrap();
405 assert_eq!(r.min_zoom, 5);
406 assert_eq!(r.max_zoom, 5);
407 }
408
409 #[test]
410 fn ne_unknown_layer_returns_none() {
411 let feat = make_with(&[], polygon_geom(), ne_src("ne_50m_admin"));
412 assert!(process(&feat).is_none());
413 }
414
415 #[test]
416 fn osm_cliff_line_z12() {
417 let feat = make_with(
418 &[("natural", PropValue::Str("cliff".into()))],
419 line_geom(),
420 osm_src(),
421 );
422 let r = process(&feat).unwrap();
423 assert_eq!(r.min_zoom, 12);
424 assert!(
425 r.properties
426 .iter()
427 .any(|(k, v)| k == "kind" && *v == PropValue::Str("cliff".into()))
428 );
429 assert!(r.properties.iter().any(|(k, _)| k == "min_zoom"));
430 }
431
432 #[test]
433 fn osm_cliff_line_with_name() {
434 let feat = make_with(
435 &[
436 ("natural", PropValue::Str("cliff".into())),
437 ("name", PropValue::Str("Big Cliff".into())),
438 ],
439 line_geom(),
440 osm_src(),
441 );
442 let r = process(&feat).unwrap();
443 assert!(
444 r.properties
445 .iter()
446 .any(|(k, v)| k == "name" && *v == PropValue::Str("Big Cliff".into()))
447 );
448 }
449
450 #[test]
451 fn osm_island_polygon_emits_centroid_label() {
452 let feat = make_with(
453 &[
454 ("place", PropValue::Str("island".into())),
455 ("name", PropValue::Str("Tiny".into())),
456 ],
457 polygon_geom(),
458 osm_src(),
459 );
460 let r = process(&feat).unwrap();
461 assert_eq!(r.min_zoom, 6);
462 assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
463 assert!(
464 r.properties
465 .iter()
466 .any(|(k, v)| k == "kind" && *v == PropValue::Str("island".into()))
467 );
468 }
469
470 #[test]
471 fn osm_island_multipolygon_emits_centroid() {
472 let feat = make_with(
473 &[("place", PropValue::Str("island".into()))],
474 multipoly_geom(),
475 osm_src(),
476 );
477 let r = process(&feat).unwrap();
478 assert!(matches!(r.geometry.as_ref(), geo::Geometry::Point(_)));
479 }
480
481 #[test]
482 fn osm_island_point_returns_none() {
483 let feat = make_with(
485 &[("place", PropValue::Str("island".into()))],
486 point_geom(),
487 osm_src(),
488 );
489 assert!(process(&feat).is_none());
490 }
491
492 #[test]
493 fn osm_unrelated_returns_none() {
494 let feat = make_with(
495 &[("highway", PropValue::Str("primary".into()))],
496 line_geom(),
497 osm_src(),
498 );
499 assert!(process(&feat).is_none());
500 }
501
502 #[test]
503 fn osm_cliff_polygon_not_matched_as_line() {
504 let feat = make_with(
507 &[("natural", PropValue::Str("cliff".into()))],
508 polygon_geom(),
509 osm_src(),
510 );
511 assert!(process(&feat).is_none());
512 }
513
514 #[test]
515 fn other_source_returns_none() {
516 let feat = make_with(&[], polygon_geom(), DataSource::IndiaBoundary);
517 assert!(process(&feat).is_none());
518 let feat = make_with(&[], polygon_geom(), DataSource::WaterPolygons);
519 assert!(process(&feat).is_none());
520 }
521}