martin-core 0.11.1

Basic building blocks of MapLibre's Martin tile server.
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
use std::collections::BTreeMap;
use std::fmt::{Debug, Formatter};
use std::num::NonZeroU32;
use std::path::PathBuf;

use async_trait::async_trait;
use geo::MapCoords as _;
use geo_index::rtree::{RTree, RTreeIndex as _};
use geo_types::{Coord, Geometry};
use geojson::GeoJson;
use martin_tile_utils::{Encoding, Format, TileCoord, TileData, TileInfo, webmercator_to_wgs84};
use mlt_core::fast_mvt::{MvtExtent, MvtGeometry, MvtTileBuilder};
use rayon::prelude::*;
use tilejson::{Bounds, Center, TileJSON, VectorLayer};
use tokio::fs;
use tracing::trace;

use crate::CacheZoomRange;
use crate::tiles::geojson::error::GeoJsonError;
use crate::tiles::geojson::process::{
    PreparedFeature, Preprocessed, add_properties, preprocess_geojson,
};
use crate::tiles::geojson::rect::Rect;
use crate::tiles::{BoxedSource, MartinCoreError, MartinCoreResult, Source, UrlQuery};

/// A source for `GeoJSON` files
///
/// Steps to pre-process `GeoJSON` features that have a geometry:
///
/// 1. Convert from WGS84 EPSG:4326 to Web Mercator EPSG:3857
/// 2. Create spatial index using a packed Hilbert R-Tree
///
/// This data source will be used to query features that overlap with a given tile:
///
/// 1. Search for geometries that overlap with a given tile bounding box using the R-Tree
/// 2. Clip geometries with tile bounding box (and optional buffer)
/// 3. Transform into tile coordinate space, validate the geometry and convert to MVT binary format
#[derive(Clone)]
pub struct GeoJsonSource {
    id: String,
    features: Vec<PreparedFeature>,
    rtree: RTree<f64>,
    tilejson: TileJSON,
    tile_info: TileInfo,
    cache_zoom: CacheZoomRange,
    /// Side length of the MVT tile coordinate grid every tile is encoded into.
    extent: NonZeroU32,
    /// Clip margin kept around each tile edge, in tile units (a fraction of `extent`).
    buffer: u32,
}

impl GeoJsonSource {
    /// Create a new `GeoJSON` source rendering tiles at the given MVT `extent` and clip `buffer`.
    pub async fn new(
        id: String,
        path: PathBuf,
        cache_zoom: CacheZoomRange,
        extent: NonZeroU32,
        buffer: u32,
    ) -> Result<Self, GeoJsonError> {
        let geojson_str = fs::read_to_string(&path)
            .await
            .map_err(|err| GeoJsonError::IoError(err, path))?;
        let geojson = geojson_str
            .parse::<GeoJson>()
            .map_err(|err| GeoJsonError::GeoJsonError(Box::new(err)))?;

        let Preprocessed {
            features,
            rtree,
            bounds,
            fields,
        } = preprocess_geojson(geojson)?;

        // Every tile is encoded as a single layer named after the source, so that is the one
        // vector layer to advertise. Clients such as maplibre-gl-inspect build their layers
        // purely from this list and render nothing when it is absent.
        let layer = VectorLayer {
            id: id.clone(),
            fields,
            description: None,
            maxzoom: None,
            minzoom: None,
            other: BTreeMap::default(),
        };

        // The data bounding box is in Web Mercator; reproject its corners back to WGS84
        // so TileJSON advertises the area covered. An empty source has no bounds.
        let tilejson = if let Some(bounds) = bounds {
            let (min_lng, min_lat) = webmercator_to_wgs84(bounds.min().x, bounds.min().y);
            let (max_lng, max_lat) = webmercator_to_wgs84(bounds.max().x, bounds.max().y);
            tilejson::tilejson! {
                tiles: vec![],
                vector_layers: vec![layer],
                bounds: Bounds::new(min_lng, min_lat, max_lng, max_lat),
                center: Center {
                    longitude: f64::midpoint(min_lng, max_lng),
                    latitude: f64::midpoint(min_lat, max_lat),
                    zoom: 0,
                },
            }
        } else {
            tilejson::tilejson! {
                tiles: vec![],
                vector_layers: vec![layer],
            }
        };

        Ok(Self {
            id,
            features,
            rtree,
            tilejson,
            tile_info: TileInfo::new(Format::Mvt, Encoding::Uncompressed),
            cache_zoom,
            extent,
            buffer,
        })
    }
}

#[expect(clippy::missing_fields_in_debug)]
impl Debug for GeoJsonSource {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GeoJsonSource")
            .field("id", &self.id)
            .finish()
    }
}

#[async_trait]
impl Source for GeoJsonSource {
    fn get_id(&self) -> &str {
        &self.id
    }

    fn get_tilejson(&self) -> &TileJSON {
        &self.tilejson
    }

    fn get_tile_info(&self) -> TileInfo {
        self.tile_info
    }

    fn clone_source(&self) -> BoxedSource {
        Box::new(self.clone())
    }
    fn get_version(&self) -> Option<String> {
        self.tilejson.version.clone()
    }

    fn benefits_from_concurrent_scraping(&self) -> bool {
        true
    }

    fn cache_zoom(&self) -> CacheZoomRange {
        self.cache_zoom
    }

    async fn get_tile(
        &self,
        xyz: TileCoord,
        _url_query: Option<&UrlQuery>,
    ) -> MartinCoreResult<TileData> {
        let mut rect = Rect::from_xyz(xyz.x, xyz.y, xyz.z, self.extent, self.buffer);
        rect.add_buffer();

        let indices = self
            .rtree
            .search(rect.min_x, rect.min_y, rect.max_x, rect.max_y);

        if indices.is_empty() {
            trace!(
                "Couldn't find tile data in {}/{}/{} of {}",
                xyz.z, xyz.x, xyz.y, &self.id
            );
            return Ok(Vec::new());
        }

        // Clip and snap to the integer MVT grid in parallel, so the f64 -> i32 conversion happens
        // once per feature here rather than serially at encode time.
        let clipped_fs = indices
            .into_par_iter()
            .filter_map(|i| {
                let f = &self.features[i as usize];
                let geom = rect.clip_transform_validate_geometry(f.geom.clone())?;
                Some(PreparedFeature::<i32> {
                    geom: to_tile_geometry(&geom),
                    properties: f.properties.clone(),
                })
            })
            .collect::<Vec<_>>();

        // MVT features hold a single geometry type, so a GeoJSON GeometryCollection
        // is emitted as one MVT feature per contained geometry, all sharing the properties.
        let mut flattened_fs = Vec::with_capacity(clipped_fs.len());
        for f in clipped_fs {
            flatten_geometry_collections(f, &mut flattened_fs);
        }

        // Coordinates are already in the tile coordinate system, so the extent is only advertised
        // on the layer; no additional scaling happens during encoding.
        let tile = encode_features(&self.id, self.extent, flattened_fs)
            .map_err(MartinCoreError::GeoJsonError)?;
        Ok(tile)
    }
}

/// Encode prepared, tile-space features into a single MVT layer named `layer_name`: one MVT feature
/// each, carrying its properties and geometry.
fn encode_features(
    layer_name: &str,
    extent: MvtExtent,
    features: Vec<PreparedFeature<i32>>,
) -> Result<TileData, GeoJsonError> {
    let mut layer = MvtTileBuilder::with_capacity(1)
        .layer_with_capacity(layer_name, features.len())
        .map_err(GeoJsonError::MvtError)?;
    layer.extent(extent);
    for f in features {
        let mut feature = layer.feature(&f.geom).map_err(GeoJsonError::MvtError)?;
        if let Some(properties) = f.properties {
            add_properties(&mut feature, properties)?;
        }
        layer = feature.end();
    }
    Ok(layer.end().encode())
}

/// Convert a tile-space geometry whose coordinates are already floored to integer grid positions
/// into the integer-coordinate geometry the MVT encoder consumes.
#[expect(
    clippy::cast_possible_truncation,
    reason = "the debug_assert guards the i32 range; coordinates are floored tile-grid positions"
)]
fn to_tile_geometry(geom: &Geometry<f64>) -> MvtGeometry {
    let range = f64::from(i32::MIN)..=f64::from(i32::MAX);
    geom.map_coords(|c| {
        // `as i32` saturates on overflow, silently corrupting geometry. Tile-space coordinates
        // stay within roughly `0..=extent` (plus buffer), so this only trips on an absurd extent.
        debug_assert!(
            range.contains(&c.x) && range.contains(&c.y),
            "tile coordinate ({}, {}) is outside i32 range; extent/buffer too large",
            c.x,
            c.y
        );
        Coord {
            x: c.x as i32,
            y: c.y as i32,
        }
    })
}

/// Expand a feature whose geometry is a `GeometryCollection` into one feature per
/// contained geometry (recursively), since an MVT feature holds a single geometry.
/// All resulting features share the original properties.
/// Features with any other geometry are pushed unchanged.
fn flatten_geometry_collections<T: geo_types::CoordNum>(
    f: PreparedFeature<T>,
    out: &mut Vec<PreparedFeature<T>>,
) {
    match f.geom {
        Geometry::GeometryCollection(geometries) => {
            for geom in geometries {
                flatten_geometry_collections(
                    PreparedFeature {
                        geom,
                        properties: f.properties.clone(),
                    },
                    out,
                );
            }
        }
        geom @ (Geometry::Point(_)
        | Geometry::Line(_)
        | Geometry::LineString(_)
        | Geometry::Polygon(_)
        | Geometry::MultiPoint(_)
        | Geometry::MultiLineString(_)
        | Geometry::MultiPolygon(_)
        | Geometry::Rect(_)
        | Geometry::Triangle(_)) => out.push(PreparedFeature {
            geom,
            properties: f.properties,
        }),
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    use mlt_core::fast_mvt::MvtReaderRef;

    use super::*;

    fn fixtures_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .join("tests")
            .join("fixtures")
            .join("geojson")
    }

    #[tokio::test]
    async fn get_tile() {
        let path = fixtures_dir().join("feature_collection_1.geojson");
        let extent = NonZeroU32::new(4096).expect("4096 is non-zero");
        let geojson_source = GeoJsonSource::new(
            "test-source-1".to_owned(),
            path,
            CacheZoomRange::default(),
            extent,
            64,
        )
        .await
        .unwrap();

        // z1/1/0 covers the northern-eastern hemisphere: polygon id 0 lies fully inside
        // and id 3 is clipped to the tile, while id 1 (North America) is excluded.
        let tile_coord = TileCoord { z: 1, x: 1, y: 0 };
        let tile = geojson_source.get_tile(tile_coord, None).await.unwrap();
        assert!(!tile.is_empty(), "expected a non-empty MVT tile");

        let decoded = MvtReaderRef::new(tile.as_slice())
            .and_then(|r| r.to_tile())
            .expect("output is a valid MVT tile");
        assert_eq!(decoded.layers.len(), 1);
        let layer = &decoded.layers[0];
        assert_eq!(
            layer.name, "test-source-1",
            "layer is named after the source"
        );
        assert_eq!(layer.extent.get(), extent.get());
        assert_eq!(
            layer.features.len(),
            2,
            "id 0 and the clipped id 3 are visible"
        );
    }

    #[tokio::test]
    async fn tilejson_bounds_match_data_extent() {
        use approx::assert_abs_diff_eq;

        // bare_geometry is a polygon spanning lng/lat [10,10]..[20,20].
        // After WGS84 -> WebMercator -> WGS84 the bounds round-trip back to the input extent.
        let path = fixtures_dir().join("bare_geometry.geojson");
        let extent = NonZeroU32::new(4096).expect("4096 is non-zero");
        let source = GeoJsonSource::new(
            "bare".to_owned(),
            path,
            CacheZoomRange::default(),
            extent,
            64,
        )
        .await
        .unwrap();

        let bounds = source.get_tilejson().bounds.expect("bounds should be set");
        assert_abs_diff_eq!(bounds.left, 10.0, epsilon = 1e-6);
        assert_abs_diff_eq!(bounds.bottom, 10.0, epsilon = 1e-6);
        assert_abs_diff_eq!(bounds.right, 20.0, epsilon = 1e-6);
        assert_abs_diff_eq!(bounds.top, 20.0, epsilon = 1e-6);

        let center = source.get_tilejson().center.expect("center should be set");
        assert_abs_diff_eq!(center.longitude, 15.0, epsilon = 1e-6);
        assert_abs_diff_eq!(center.latitude, 15.0, epsilon = 1e-6);
        assert_eq!(center.zoom, 0);
    }

    #[tokio::test]
    async fn tilejson_advertises_the_layer_named_after_the_source() {
        let path = fixtures_dir().join("feature_collection_1.geojson");
        let extent = NonZeroU32::new(4096).expect("4096 is non-zero");
        let source = GeoJsonSource::new(
            "test-source-1".to_owned(),
            path,
            CacheZoomRange::default(),
            extent,
            64,
        )
        .await
        .unwrap();

        let layers = source
            .get_tilejson()
            .vector_layers
            .as_ref()
            .expect("vector_layers should be set");
        assert_eq!(layers.len(), 1);
        assert_eq!(layers[0].id, "test-source-1");
        assert_eq!(
            layers[0].fields,
            BTreeMap::from([("id".to_owned(), "Number".to_owned())])
        );
    }

    #[tokio::test]
    async fn tilejson_fields_name_every_property_type() {
        let path = fixtures_dir().join("properties.geojson");
        let extent = NonZeroU32::new(4096).expect("4096 is non-zero");
        let source = GeoJsonSource::new(
            "props".to_owned(),
            path,
            CacheZoomRange::default(),
            extent,
            64,
        )
        .await
        .unwrap();

        let layers = source
            .get_tilejson()
            .vector_layers
            .as_ref()
            .expect("vector_layers should be set");
        assert_eq!(
            layers[0].fields,
            BTreeMap::from([
                ("prop_array".to_owned(), "String".to_owned()),
                ("prop_bool_false".to_owned(), "Boolean".to_owned()),
                ("prop_bool_true".to_owned(), "Boolean".to_owned()),
                ("prop_float".to_owned(), "Number".to_owned()),
                ("prop_int_negative".to_owned(), "Number".to_owned()),
                ("prop_object".to_owned(), "String".to_owned()),
                ("prop_string".to_owned(), "String".to_owned()),
                ("prop_uint_large".to_owned(), "Number".to_owned()),
            ])
        );
    }

    #[tokio::test]
    async fn tilejson_advertises_a_layer_even_without_features() {
        let dir = tempfile::tempdir().expect("a temp dir");
        let path = dir.path().join("empty.geojson");
        std::fs::write(&path, r#"{"type":"FeatureCollection","features":[]}"#)
            .expect("the fixture is written");
        let extent = NonZeroU32::new(4096).expect("4096 is non-zero");
        let source = GeoJsonSource::new(
            "empty".to_owned(),
            path,
            CacheZoomRange::default(),
            extent,
            64,
        )
        .await
        .unwrap();

        let layers = source
            .get_tilejson()
            .vector_layers
            .as_ref()
            .expect("vector_layers should be set");
        assert_eq!(layers.len(), 1);
        assert_eq!(layers[0].id, "empty");
        assert!(layers[0].fields.is_empty());
    }

    #[test]
    fn empty_feature_collection_has_no_bounds() {
        // No feature contributes a geometry, so there is no extent to advertise.
        let geojson = r#"{"type":"FeatureCollection","features":[]}"#.parse::<GeoJson>().unwrap();
        assert!(preprocess_geojson(geojson).unwrap().bounds.is_none());
    }
}