geonative-core 0.4.0

Core data model and driver traits for the geonative geospatial library
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
//! Simple-Features geometry tree with optional Z/M ordinates.
//!
//! The shape is deliberately close to OGC Simple Features + WKB so writers
//! (WKB, GeoJSON, Shapefile, GeoParquet) reduce to direct tree walks.

/// A single coordinate. `z` and `m` are `Option`s so we can distinguish
/// "this geometry has no Z dimension" from "Z is invalid". We do **not** use
/// NaN as a sentinel — that's a GDB-specific quirk handled inside its reader.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Coord {
    pub x: f64,
    pub y: f64,
    pub z: Option<f64>,
    pub m: Option<f64>,
}

impl Coord {
    pub const fn xy(x: f64, y: f64) -> Self {
        Self {
            x,
            y,
            z: None,
            m: None,
        }
    }

    pub const fn xyz(x: f64, y: f64, z: f64) -> Self {
        Self {
            x,
            y,
            z: Some(z),
            m: None,
        }
    }

    pub const fn xym(x: f64, y: f64, m: f64) -> Self {
        Self {
            x,
            y,
            z: None,
            m: Some(m),
        }
    }

    pub const fn xyzm(x: f64, y: f64, z: f64, m: f64) -> Self {
        Self {
            x,
            y,
            z: Some(z),
            m: Some(m),
        }
    }

    pub const fn has_z(&self) -> bool {
        self.z.is_some()
    }

    pub const fn has_m(&self) -> bool {
        self.m.is_some()
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct LineString {
    pub coords: Vec<Coord>,
}

impl LineString {
    pub fn new(coords: Vec<Coord>) -> Self {
        Self { coords }
    }

    pub fn is_empty(&self) -> bool {
        self.coords.is_empty()
    }
}

/// A polygon: one exterior ring plus zero or more interior rings (holes).
///
/// Ring orientation in this IR is **OGC-standard**: exterior counter-clockwise,
/// interior clockwise. Drivers that read formats with a different convention
/// (Esri Shapefile/GDB: exterior CW, hole CCW) re-orient at read time.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Polygon {
    pub exterior: LineString,
    pub holes: Vec<LineString>,
}

impl Polygon {
    pub fn new(exterior: LineString, holes: Vec<LineString>) -> Self {
        Self { exterior, holes }
    }

    pub fn is_empty(&self) -> bool {
        self.exterior.is_empty()
    }
}

/// Discriminant of [`Geometry`]. Used in `Geometry::Empty(_)` to preserve
/// the type of a typed-empty geometry (WKB requires this).
///
/// Marked `#[non_exhaustive]` because future versions may add `Triangle`,
/// `TIN`, `PolyhedralSurface`, or richer multi-* variants without that
/// counting as a SemVer-breaking change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum GeometryType {
    Point,
    LineString,
    Polygon,
    MultiPoint,
    MultiLineString,
    MultiPolygon,
    GeometryCollection,
}

/// The geometry tree. Marked `#[non_exhaustive]` so future SemVer-minor
/// releases can add variants (curves, surfaces, Z/M-bearing variants once
/// the IR grows past 2D).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Geometry {
    Point(Coord),
    LineString(LineString),
    Polygon(Polygon),
    MultiPoint(Vec<Coord>),
    MultiLineString(Vec<LineString>),
    MultiPolygon(Vec<Polygon>),
    GeometryCollection(Vec<Geometry>),
    /// A typed-empty geometry (e.g. `POINT EMPTY`, `POLYGON EMPTY`). The tag
    /// is preserved so WKB serialization round-trips correctly.
    Empty(GeometryType),
}

impl Geometry {
    pub fn type_of(&self) -> GeometryType {
        match self {
            Geometry::Point(_) => GeometryType::Point,
            Geometry::LineString(_) => GeometryType::LineString,
            Geometry::Polygon(_) => GeometryType::Polygon,
            Geometry::MultiPoint(_) => GeometryType::MultiPoint,
            Geometry::MultiLineString(_) => GeometryType::MultiLineString,
            Geometry::MultiPolygon(_) => GeometryType::MultiPolygon,
            Geometry::GeometryCollection(_) => GeometryType::GeometryCollection,
            Geometry::Empty(t) => *t,
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            Geometry::Empty(_) => true,
            Geometry::Point(_) => false,
            Geometry::LineString(ls) => ls.is_empty(),
            Geometry::Polygon(p) => p.is_empty(),
            Geometry::MultiPoint(v) => v.is_empty(),
            Geometry::MultiLineString(v) => v.iter().all(LineString::is_empty),
            Geometry::MultiPolygon(v) => v.iter().all(Polygon::is_empty),
            Geometry::GeometryCollection(v) => v.iter().all(Geometry::is_empty),
        }
    }

    /// Total coordinate count across the geometry tree. O(N) walk.
    ///
    /// This is the right number to use when budgeting memory for an
    /// operation that scales with vertex density (Hilbert sort, geometry
    /// simplification, WKB encode). A single polygon with 100 000
    /// vertices charges 100 000; a `MultiPolygon` of 50 such parts
    /// charges 5 000 000 — feature *count* alone hides this asymmetry.
    pub fn coord_count(&self) -> usize {
        match self {
            Geometry::Empty(_) => 0,
            Geometry::Point(_) => 1,
            Geometry::LineString(ls) => ls.coords.len(),
            Geometry::Polygon(p) => {
                p.exterior.coords.len()
                    + p.holes.iter().map(|h| h.coords.len()).sum::<usize>()
            }
            Geometry::MultiPoint(v) => v.len(),
            Geometry::MultiLineString(v) => v.iter().map(|ls| ls.coords.len()).sum(),
            Geometry::MultiPolygon(v) => v
                .iter()
                .map(|p| {
                    p.exterior.coords.len()
                        + p.holes.iter().map(|h| h.coords.len()).sum::<usize>()
                })
                .sum(),
            Geometry::GeometryCollection(v) => v.iter().map(Geometry::coord_count).sum(),
        }
    }

    /// True if any coordinate in the tree carries a Z ordinate.
    pub fn has_z(&self) -> bool {
        match self {
            Geometry::Empty(_) => false,
            Geometry::Point(c) => c.has_z(),
            Geometry::LineString(ls) => ls.coords.iter().any(Coord::has_z),
            Geometry::Polygon(p) => {
                p.exterior.coords.iter().any(Coord::has_z)
                    || p.holes.iter().any(|h| h.coords.iter().any(Coord::has_z))
            }
            Geometry::MultiPoint(v) => v.iter().any(Coord::has_z),
            Geometry::MultiLineString(v) => v.iter().any(|ls| ls.coords.iter().any(Coord::has_z)),
            Geometry::MultiPolygon(v) => v.iter().any(|p| {
                p.exterior.coords.iter().any(Coord::has_z)
                    || p.holes.iter().any(|h| h.coords.iter().any(Coord::has_z))
            }),
            Geometry::GeometryCollection(v) => v.iter().any(Geometry::has_z),
        }
    }

    /// Compute the 2D bounding box `[xmin, ymin, xmax, ymax]`.
    ///
    /// Returns `None` for empty geometries (no coordinates to bound). NaN
    /// coordinates are skipped, so `POINT EMPTY` (stored as NaN/NaN) also
    /// yields `None`.
    pub fn bbox(&self) -> Option<[f64; 4]> {
        let mut acc: Option<[f64; 4]> = None;
        for_each_xy(self, &mut |x, y| {
            if !x.is_finite() || !y.is_finite() {
                return;
            }
            match &mut acc {
                None => acc = Some([x, y, x, y]),
                Some(b) => {
                    if x < b[0] {
                        b[0] = x;
                    }
                    if y < b[1] {
                        b[1] = y;
                    }
                    if x > b[2] {
                        b[2] = x;
                    }
                    if y > b[3] {
                        b[3] = y;
                    }
                }
            }
        });
        acc
    }

    /// True if any coordinate in the tree carries an M ordinate.
    pub fn has_m(&self) -> bool {
        match self {
            Geometry::Empty(_) => false,
            Geometry::Point(c) => c.has_m(),
            Geometry::LineString(ls) => ls.coords.iter().any(Coord::has_m),
            Geometry::Polygon(p) => {
                p.exterior.coords.iter().any(Coord::has_m)
                    || p.holes.iter().any(|h| h.coords.iter().any(Coord::has_m))
            }
            Geometry::MultiPoint(v) => v.iter().any(Coord::has_m),
            Geometry::MultiLineString(v) => v.iter().any(|ls| ls.coords.iter().any(Coord::has_m)),
            Geometry::MultiPolygon(v) => v.iter().any(|p| {
                p.exterior.coords.iter().any(Coord::has_m)
                    || p.holes.iter().any(|h| h.coords.iter().any(Coord::has_m))
            }),
            Geometry::GeometryCollection(v) => v.iter().any(Geometry::has_m),
        }
    }
}

/// Walk every (x, y) coordinate in the tree and invoke `f`. Used by `bbox`
/// and (later) by visitor-style writers.
fn for_each_xy(g: &Geometry, f: &mut dyn FnMut(f64, f64)) {
    match g {
        Geometry::Empty(_) => {}
        Geometry::Point(c) => f(c.x, c.y),
        Geometry::LineString(ls) => {
            for c in &ls.coords {
                f(c.x, c.y);
            }
        }
        Geometry::Polygon(p) => {
            for c in &p.exterior.coords {
                f(c.x, c.y);
            }
            for h in &p.holes {
                for c in &h.coords {
                    f(c.x, c.y);
                }
            }
        }
        Geometry::MultiPoint(v) => {
            for c in v {
                f(c.x, c.y);
            }
        }
        Geometry::MultiLineString(v) => {
            for ls in v {
                for c in &ls.coords {
                    f(c.x, c.y);
                }
            }
        }
        Geometry::MultiPolygon(v) => {
            for p in v {
                for c in &p.exterior.coords {
                    f(c.x, c.y);
                }
                for h in &p.holes {
                    for c in &h.coords {
                        f(c.x, c.y);
                    }
                }
            }
        }
        Geometry::GeometryCollection(v) => {
            for inner in v {
                for_each_xy(inner, f);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn coord_constructors_track_dimensions() {
        assert!(!Coord::xy(1.0, 2.0).has_z());
        assert!(!Coord::xy(1.0, 2.0).has_m());

        let c = Coord::xyz(1.0, 2.0, 3.0);
        assert!(c.has_z() && !c.has_m());
        assert_eq!(c.z, Some(3.0));

        let c = Coord::xym(1.0, 2.0, 99.0);
        assert!(!c.has_z() && c.has_m());
        assert_eq!(c.m, Some(99.0));

        let c = Coord::xyzm(1.0, 2.0, 3.0, 99.0);
        assert!(c.has_z() && c.has_m());
    }

    #[test]
    fn geometry_type_of_matches_variant() {
        assert_eq!(
            Geometry::Point(Coord::xy(0.0, 0.0)).type_of(),
            GeometryType::Point
        );
        assert_eq!(
            Geometry::Polygon(Polygon::default()).type_of(),
            GeometryType::Polygon
        );
        assert_eq!(
            Geometry::Empty(GeometryType::MultiPolygon).type_of(),
            GeometryType::MultiPolygon
        );
    }

    #[test]
    fn empty_detection() {
        assert!(Geometry::Empty(GeometryType::Point).is_empty());
        assert!(Geometry::LineString(LineString::default()).is_empty());
        assert!(!Geometry::Point(Coord::xy(0.0, 0.0)).is_empty());

        let ls = LineString::new(vec![Coord::xy(0.0, 0.0), Coord::xy(1.0, 1.0)]);
        assert!(!Geometry::LineString(ls).is_empty());
    }

    #[test]
    fn bbox_of_polygon_with_hole() {
        let p = Polygon::new(
            LineString::new(vec![
                Coord::xy(0.0, 0.0),
                Coord::xy(10.0, 0.0),
                Coord::xy(10.0, 10.0),
                Coord::xy(0.0, 10.0),
                Coord::xy(0.0, 0.0),
            ]),
            vec![LineString::new(vec![
                Coord::xy(2.0, 2.0),
                Coord::xy(4.0, 2.0),
                Coord::xy(4.0, 4.0),
                Coord::xy(2.0, 4.0),
                Coord::xy(2.0, 2.0),
            ])],
        );
        let bbox = Geometry::Polygon(p).bbox().unwrap();
        assert_eq!(bbox, [0.0, 0.0, 10.0, 10.0]);
    }

    #[test]
    fn bbox_of_empty_geometry_is_none() {
        assert!(Geometry::Empty(GeometryType::Polygon).bbox().is_none());
        assert!(Geometry::LineString(LineString::default()).bbox().is_none());
    }

    #[test]
    fn bbox_skips_nan() {
        let g = Geometry::MultiPoint(vec![
            Coord::xy(1.0, 2.0),
            Coord::xy(f64::NAN, f64::NAN),
            Coord::xy(5.0, 6.0),
        ]);
        assert_eq!(g.bbox(), Some([1.0, 2.0, 5.0, 6.0]));
    }

    #[test]
    fn bbox_of_geometry_collection() {
        let g = Geometry::GeometryCollection(vec![
            Geometry::Point(Coord::xy(-1.0, -1.0)),
            Geometry::Point(Coord::xy(5.0, 5.0)),
        ]);
        assert_eq!(g.bbox(), Some([-1.0, -1.0, 5.0, 5.0]));
    }

    #[test]
    fn has_z_m_recursion() {
        let ls = LineString::new(vec![Coord::xy(0.0, 0.0), Coord::xyz(1.0, 1.0, 5.0)]);
        let g = Geometry::LineString(ls);
        assert!(g.has_z());
        assert!(!g.has_m());

        let mls = Geometry::MultiLineString(vec![LineString::new(vec![Coord::xym(0.0, 0.0, 7.0)])]);
        assert!(!mls.has_z());
        assert!(mls.has_m());

        let collection =
            Geometry::GeometryCollection(vec![Geometry::Point(Coord::xyzm(0.0, 0.0, 1.0, 2.0))]);
        assert!(collection.has_z());
        assert!(collection.has_m());
    }
}