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