geolith-tiles-builder 0.4.2

Builds tiles from OpenStreetMap and Overture Maps: protomaps-schema vector PMTiles, Terrain-RGB, raster archives and 3D Tiles from Gaussian splats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Earth layer — landmass polygons, cliffs, islands.
//!
//! Processes features from multiple data sources:
//! - **`LandPolygons`** (z6+): Pre-processed land polygons from shapefile
//! - **`NaturalEarth`** (z0-5): `ne_50m_land` (z0-4), `ne_10m_land` (z5)
//! - **OSM**: Cliff lines (z12+), island labels from polygon centroids (z6+)
//! - **Overture**: `base/land` features with `subtype = land` only; vegetation
//!   and physical subtypes go to the landuse layer or are dropped

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;

/// Process a feature into an earth layer feature.
///
/// Routes to the appropriate handler based on data source.
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,
    }
}

/// Process Overture `base/land` features.
///
/// Mirrors protomaps `Earth.processOverture`: only `subtype = land` is earth.
/// The style fills every earth polygon as land, so emitting forests, sand,
/// wetlands, and offshore reefs here painted them as land. Those subtypes are
/// handled by the landuse layer; trees and physical points are dropped.
/// Island and islet points become `kind = island` labels.
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,
    })
}

/// Process pre-processed land polygons (z6+).
///
/// These are the `land-polygons-split-3857` polygons reprojected to WGS84.
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,
    })
}

/// Process Natural Earth land features (z0-5).
///
/// - `ne_50m_land` → z0-4
/// - `ne_10m_land` → z5
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,
    })
}

/// Process OSM features relevant to the earth layer.
///
/// - `natural=cliff` lines → kind: "cliff" (z12+)
/// - `place=island` polygons → kind: "island" point label (z6+)
fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
    // Cliff lines
    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,
        });
    }

    // Island labels (centroid point from polygon)
    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);
    }

    /// Subtypes observed in `base/land` for the 2026-08-19.0 release. None of
    /// them is land: filling them as earth painted reefs as islands.
    #[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() {
        // place=island on a point geometry — can_be_polygon is false → no match.
        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() {
        // cliff with polygon geom (can_be_polygon) → cliff branch requires line
        // and not polygon. Falls through to island/None.
        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());
    }
}