flatgeobuf 6.0.1

FlatGeobuf for Rust
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use geo_traits::{
    CoordTrait, Dimensions, GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait,
    MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait,
    UnimplementedLine, UnimplementedRect, UnimplementedTriangle,
};

#[derive(Debug, Clone)]
pub struct Coord<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
    /// The coordinate offset
    ///
    /// Note each coord_offset points to an xy coordinate pair, and must be multiplied by 2 to get
    /// the buffer coord_offset
    coord_offset: usize,
}

impl CoordTrait for Coord<'_> {
    type T = f64;

    fn dim(&self) -> Dimensions {
        self.dim
    }

    fn nth_or_panic(&self, n: usize) -> Self::T {
        match n {
            0 => self.x(),
            1 => self.y(),
            2 => match self.dim {
                Dimensions::Xyz | Dimensions::Xyzm => self.geom.z().unwrap().get(self.coord_offset),
                Dimensions::Xym => self.geom.m().unwrap().get(self.coord_offset),
                // Data from FlatGeobuf always has known dimension
                Dimensions::Xy | Dimensions::Unknown(_) => unreachable!("Unreachable for 3D data"),
            },
            3 => match self.dim {
                Dimensions::Xyzm => self.geom.m().unwrap().get(self.coord_offset),
                _ => unreachable!("Unreachable for 4D data"),
            },
            _ => panic!("Unexpected dim {n}"),
        }
    }

    fn x(&self) -> Self::T {
        self.geom.xy().unwrap().get(self.coord_offset * 2)
    }

    fn y(&self) -> Self::T {
        self.geom.xy().unwrap().get((self.coord_offset * 2) + 1)
    }
}

#[derive(Debug, Clone)]
pub struct Point<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
    /// The coordinate offset
    ///
    /// Note each coord_offset points to an xy coordinate pair, and must be multiplied by 2 to get
    /// the buffer coord_offset
    coord_offset: usize,
}

impl<'a> Point<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        Self {
            geom,
            dim,
            coord_offset: 0,
        }
    }
}

impl<'a> PointTrait for Point<'a> {
    type CoordType<'b>
        = Coord<'a>
    where
        Self: 'b;

    fn coord(&self) -> Option<Self::CoordType<'_>> {
        // FlatGeobuf doesn't support empty geometries
        Some(Coord {
            geom: self.geom,
            dim: self.dim,
            coord_offset: self.coord_offset,
        })
    }
}

#[derive(Debug, Clone)]
pub struct LineString<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,

    /// This coord_offset will be non-zero when the LineString is a reference onto an external
    /// geometry, e.g. a Polygon
    coord_offset: usize,

    /// This length cannot be inferred from the underlying buffer when this LineString is a
    /// reference on e.g. a Polygon
    length: usize,
}

impl<'a> LineString<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        let length = geom.xy().unwrap().len() / 2;
        Self {
            geom,
            dim,
            coord_offset: 0,
            length,
        }
    }
}

impl<'a> LineStringTrait for LineString<'a> {
    type CoordType<'b>
        = Coord<'a>
    where
        Self: 'b;

    fn num_coords(&self) -> usize {
        self.length
    }

    unsafe fn coord_unchecked(&self, i: usize) -> Self::CoordType<'_> {
        Coord {
            geom: self.geom,
            dim: self.dim,
            coord_offset: self.coord_offset + i,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Polygon<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
}

impl<'a> Polygon<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        Self { geom, dim }
    }
}

impl<'a> PolygonTrait for Polygon<'a> {
    type RingType<'b>
        = LineString<'a>
    where
        Self: 'b;

    fn num_interiors(&self) -> usize {
        if let Some(ends) = self.geom.ends() {
            ends.len() - 1
        } else {
            0
        }
    }

    fn exterior(&self) -> Option<Self::RingType<'_>> {
        if let Some(ends) = self.geom.ends() {
            let exterior_end = ends.get(0);
            Some(LineString {
                geom: self.geom,
                dim: self.dim,
                coord_offset: 0,
                length: exterior_end.try_into().unwrap(),
            })
        } else {
            Some(LineString::new(self.geom, self.dim))
        }
    }

    unsafe fn interior_unchecked(&self, i: usize) -> Self::RingType<'_> {
        let ends = self.geom.ends().unwrap();
        let start = ends.get(i);
        let end = ends.get(i + 1);
        LineString {
            geom: self.geom,
            dim: self.dim,
            coord_offset: start.try_into().unwrap(),
            length: (end - start).try_into().unwrap(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct MultiPoint<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,

    /// This coord_offset will be non-zero when the MultiPoint is a reference onto an external
    /// geometry, e.g. a GeometryCollection
    coord_offset: usize,

    /// This length is not inferred from the underlying buffer because this MultiPoint could be a
    /// reference on e.g. a GeometryCollection
    length: usize,
}

impl<'a> MultiPoint<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        let length = geom.xy().unwrap().len() / 2;
        Self {
            geom,
            dim,
            coord_offset: 0,
            length,
        }
    }
}

impl<'a> MultiPointTrait for MultiPoint<'a> {
    type InnerPointType<'b>
        = Point<'a>
    where
        Self: 'b;

    fn num_points(&self) -> usize {
        self.length
    }

    unsafe fn point_unchecked(&self, i: usize) -> Self::InnerPointType<'_> {
        Point {
            geom: self.geom,
            dim: self.dim,
            coord_offset: self.coord_offset + i,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MultiLineString<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
}

impl<'a> MultiLineString<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        Self { geom, dim }
    }
}

impl<'a> MultiLineStringTrait for MultiLineString<'a> {
    type InnerLineStringType<'b>
        = LineString<'a>
    where
        Self: 'b;

    fn num_line_strings(&self) -> usize {
        if let Some(ends) = self.geom.ends() {
            ends.len()
        } else {
            1
        }
    }

    unsafe fn line_string_unchecked(&self, i: usize) -> Self::InnerLineStringType<'_> {
        if let Some(ends) = self.geom.ends() {
            let start = if i == 0 { 0 } else { ends.get(i - 1) };
            let end = ends.get(i);
            LineString {
                geom: self.geom,
                dim: self.dim,
                coord_offset: start.try_into().unwrap(),
                length: (end - start).try_into().unwrap(),
            }
        } else {
            assert_eq!(i, 0);
            LineString::new(self.geom, self.dim)
        }
    }
}

#[derive(Debug, Clone)]
pub struct MultiPolygon<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
}

impl<'a> MultiPolygon<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        Self { geom, dim }
    }
}

impl<'a> MultiPolygonTrait for MultiPolygon<'a> {
    type InnerPolygonType<'b>
        = Polygon<'a>
    where
        Self: 'b;

    fn num_polygons(&self) -> usize {
        self.geom.parts().unwrap().len()
    }

    unsafe fn polygon_unchecked(&self, i: usize) -> Self::InnerPolygonType<'_> {
        Polygon {
            geom: self.geom.parts().unwrap().get(i),
            dim: self.dim,
        }
    }
}

#[derive(Debug, Clone)]
pub enum Geometry<'a> {
    Point(Point<'a>),
    LineString(LineString<'a>),
    Polygon(Polygon<'a>),
    MultiPoint(MultiPoint<'a>),
    MultiLineString(MultiLineString<'a>),
    MultiPolygon(MultiPolygon<'a>),
    #[allow(clippy::enum_variant_names)]
    GeometryCollection(GeometryCollection<'a>),
}

impl<'a> Geometry<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        match geom.type_() {
            crate::GeometryType::Point => Self::Point(Point::new(geom, dim)),
            crate::GeometryType::LineString => Self::LineString(LineString::new(geom, dim)),
            crate::GeometryType::Polygon => Self::Polygon(Polygon::new(geom, dim)),
            crate::GeometryType::MultiPoint => Self::MultiPoint(MultiPoint::new(geom, dim)),
            crate::GeometryType::MultiLineString => {
                Self::MultiLineString(MultiLineString::new(geom, dim))
            }
            crate::GeometryType::MultiPolygon => Self::MultiPolygon(MultiPolygon::new(geom, dim)),
            crate::GeometryType::GeometryCollection => {
                Self::GeometryCollection(GeometryCollection::new(geom, dim))
            }
            t => panic!("Unexpected type {t:?}"),
        }
    }
}

impl<'a> GeometryTrait for Geometry<'a> {
    type T = f64;
    type PointType<'b>
        = Point<'a>
    where
        Self: 'b;
    type LineStringType<'b>
        = LineString<'a>
    where
        Self: 'b;
    type PolygonType<'b>
        = Polygon<'a>
    where
        Self: 'b;
    type MultiPointType<'b>
        = MultiPoint<'a>
    where
        Self: 'b;
    type MultiLineStringType<'b>
        = MultiLineString<'a>
    where
        Self: 'b;
    type MultiPolygonType<'b>
        = MultiPolygon<'a>
    where
        Self: 'b;
    type GeometryCollectionType<'b>
        = GeometryCollection<'a>
    where
        Self: 'b;
    type RectType<'b>
        = UnimplementedRect<f64>
    where
        Self: 'b;
    type TriangleType<'b>
        = UnimplementedTriangle<f64>
    where
        Self: 'b;
    type LineType<'b>
        = UnimplementedLine<f64>
    where
        Self: 'b;

    fn dim(&self) -> Dimensions {
        match self {
            Self::Point(g) => g.dim,
            Self::LineString(g) => g.dim,
            Self::Polygon(g) => g.dim,
            Self::MultiPoint(g) => g.dim,
            Self::MultiLineString(g) => g.dim,
            Self::MultiPolygon(g) => g.dim,
            Self::GeometryCollection(g) => g.dim,
        }
    }

    fn as_type(
        &self,
    ) -> geo_traits::GeometryType<
        '_,
        Point<'a>,
        LineString<'a>,
        Polygon<'a>,
        MultiPoint<'a>,
        MultiLineString<'a>,
        MultiPolygon<'a>,
        GeometryCollection<'a>,
        UnimplementedRect<f64>,
        UnimplementedTriangle<f64>,
        UnimplementedLine<f64>,
    > {
        match self {
            Self::Point(pt) => geo_traits::GeometryType::Point(pt),
            Self::LineString(pt) => geo_traits::GeometryType::LineString(pt),
            Self::Polygon(pt) => geo_traits::GeometryType::Polygon(pt),
            Self::MultiPoint(pt) => geo_traits::GeometryType::MultiPoint(pt),
            Self::MultiLineString(pt) => geo_traits::GeometryType::MultiLineString(pt),
            Self::MultiPolygon(pt) => geo_traits::GeometryType::MultiPolygon(pt),
            Self::GeometryCollection(pt) => geo_traits::GeometryType::GeometryCollection(pt),
        }
    }
}

#[derive(Debug, Clone)]
pub struct GeometryCollection<'a> {
    geom: crate::Geometry<'a>,
    dim: Dimensions,
}

impl<'a> GeometryCollection<'a> {
    pub(super) fn new(geom: crate::Geometry<'a>, dim: Dimensions) -> Self {
        Self { geom, dim }
    }
}

impl<'a> GeometryCollectionTrait for GeometryCollection<'a> {
    type GeometryType<'b>
        = Geometry<'a>
    where
        Self: 'b;

    fn num_geometries(&self) -> usize {
        let parts = self.geom.parts().unwrap();
        parts.len()
    }

    unsafe fn geometry_unchecked(&self, i: usize) -> Self::GeometryType<'_> {
        Geometry::new(self.geom.parts().unwrap().get(i), self.dim)
    }
}

macro_rules! impl_specialization {
    ($geometry_type:ident) => {
        impl<'a> GeometryTrait for $geometry_type<'a> {
            type T = f64;
            type PointType<'b>
                = Point<'a>
            where
                Self: 'b;
            type LineStringType<'b>
                = LineString<'a>
            where
                Self: 'b;
            type PolygonType<'b>
                = Polygon<'a>
            where
                Self: 'b;
            type MultiPointType<'b>
                = MultiPoint<'a>
            where
                Self: 'b;
            type MultiLineStringType<'b>
                = MultiLineString<'a>
            where
                Self: 'b;
            type MultiPolygonType<'b>
                = MultiPolygon<'a>
            where
                Self: 'b;
            type GeometryCollectionType<'b>
                = GeometryCollection<'a>
            where
                Self: 'b;
            type RectType<'b>
                = UnimplementedRect<Self::T>
            where
                Self: 'b;
            type TriangleType<'b>
                = UnimplementedTriangle<Self::T>
            where
                Self: 'b;
            type LineType<'b>
                = UnimplementedLine<Self::T>
            where
                Self: 'b;

            fn dim(&self) -> Dimensions {
                self.dim
            }

            fn as_type(
                &self,
            ) -> GeometryType<
                '_,
                Point<'a>,
                LineString<'a>,
                Polygon<'a>,
                MultiPoint<'a>,
                MultiLineString<'a>,
                MultiPolygon<'a>,
                GeometryCollection<'a>,
                Self::RectType<'_>,
                Self::TriangleType<'_>,
                Self::LineType<'_>,
            > {
                GeometryType::$geometry_type(self)
            }
        }

        impl<'a> GeometryTrait for &$geometry_type<'a> {
            type T = f64;
            type PointType<'b>
                = Point<'a>
            where
                Self: 'b;
            type LineStringType<'b>
                = LineString<'a>
            where
                Self: 'b;
            type PolygonType<'b>
                = Polygon<'a>
            where
                Self: 'b;
            type MultiPointType<'b>
                = MultiPoint<'a>
            where
                Self: 'b;
            type MultiLineStringType<'b>
                = MultiLineString<'a>
            where
                Self: 'b;
            type MultiPolygonType<'b>
                = MultiPolygon<'a>
            where
                Self: 'b;
            type GeometryCollectionType<'b>
                = GeometryCollection<'a>
            where
                Self: 'b;
            type RectType<'b>
                = UnimplementedRect<Self::T>
            where
                Self: 'b;
            type TriangleType<'b>
                = UnimplementedTriangle<Self::T>
            where
                Self: 'b;
            type LineType<'b>
                = UnimplementedLine<Self::T>
            where
                Self: 'b;

            fn dim(&self) -> Dimensions {
                self.dim
            }

            fn as_type(
                &self,
            ) -> GeometryType<
                '_,
                Point<'a>,
                LineString<'a>,
                Polygon<'a>,
                MultiPoint<'a>,
                MultiLineString<'a>,
                MultiPolygon<'a>,
                GeometryCollection<'a>,
                Self::RectType<'_>,
                Self::TriangleType<'_>,
                Self::LineType<'_>,
            > {
                GeometryType::$geometry_type(self)
            }
        }
    };
}

impl_specialization!(Point);
impl_specialization!(LineString);
impl_specialization!(Polygon);
impl_specialization!(MultiPoint);
impl_specialization!(MultiLineString);
impl_specialization!(MultiPolygon);
impl_specialization!(GeometryCollection);