s2json-core 1.51.1

This library supports the S2JSON 1.0 Specification
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
use crate::*;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

/// Enum to represent specific vector geometry types as strings
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
pub enum VectorGeometryType {
    /// Point
    #[default]
    Point,
    /// MultiPoint
    MultiPoint,
    /// LineString
    LineString,
    /// MultiLineString
    MultiLineString,
    /// Polygon
    Polygon,
    /// MultiPolygon
    MultiPolygon,
}
impl From<&str> for VectorGeometryType {
    fn from(s: &str) -> Self {
        match s {
            "Point" => VectorGeometryType::Point,
            "MultiPoint" => VectorGeometryType::MultiPoint,
            "LineString" => VectorGeometryType::LineString,
            "MultiLineString" => VectorGeometryType::MultiLineString,
            "Polygon" => VectorGeometryType::Polygon,
            "MultiPolygon" => VectorGeometryType::MultiPolygon,
            _ => panic!("Invalid vector geometry type: {}", s),
        }
    }
}

/// Definition of a Vector MultiPoint
pub type VectorMultiPoint<M = MValue> = Vec<VectorPoint<M>>;
/// Definition of a Vector LineString
pub type VectorLineString<M = MValue> = Vec<VectorPoint<M>>;
/// Definition of a Vector MultiLineString
pub type VectorMultiLineString<M = MValue> = Vec<VectorLineString<M>>;
/// Definition of a Vector Polygon
pub type VectorPolygon<M = MValue> = Vec<VectorLineString<M>>;
/// Definition of a Vector MultiPolygon
pub type VectorMultiPolygon<M = MValue> = Vec<VectorPolygon<M>>;

/// # Vector Geometry
///
/// ## Description
/// All possible geometry shapes. Builds ontop of [`VectorBaseGeometry`].
///
/// ## Usage
/// - [`VectorGeometry::bbox`]: Get the bbox of the geometry
/// - [`VectorGeometry::vec_bbox`]: Get the internal 0-1 clipping style vector bbox
/// - [`VectorGeometry::point`]: Get the geometry point
/// - [`VectorGeometry::new_point`]: Create a new point
/// - [`VectorGeometry::multipoint`]: Get the geometry multi point
/// - [`VectorGeometry::new_multipoint`]: Create a new multipoint
/// - [`VectorGeometry::linestring`]: Get the geometry line string
/// - [`VectorGeometry::new_linestring`]: Create a new linestring
/// - [`VectorGeometry::multilinestring`]: Get the geometry multi line string
/// - [`VectorGeometry::new_multilinestring`]: Create a new multi line string
/// - [`VectorGeometry::polygon`]: Get the geometry polygon
/// - [`VectorGeometry::new_polygon`]: Create a new polygon
/// - [`VectorGeometry::multipolygon`]: Get the geometry multi polygon
/// - [`VectorGeometry::new_multipolygon`]: Create a new multi polygon
/// - [`VectorGeometry::set_tess`]: Set the tessellation of the geometry (polygon and multipolygon only)
/// - [`VectorGeometry::set_indices`]: Set the indices of the geometry (polygon and multipolygon only)
/// - [`VectorGeometry::to_m_geometry`]: Convert the geometry so that all m-values are MValue rather then user defined
#[derive(Clone, Serialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum VectorGeometry<M: Clone + Default = MValue> {
    /// Point Shape
    Point(VectorPointGeometry<M>),
    /// MultiPoint Shape
    MultiPoint(VectorMultiPointGeometry<M>),
    /// LineString Shape
    LineString(VectorLineStringGeometry<M>),
    /// MultiLineString Shape
    MultiLineString(VectorMultiLineStringGeometry<M>),
    /// Polygon Shape
    Polygon(VectorPolygonGeometry<M>),
    /// MultiPolygon Shape
    MultiPolygon(VectorMultiPolygonGeometry<M>),
}
impl<M: Clone + Default> VectorGeometry<M> {
    /// Get the bbox of the geometry
    pub fn bbox(&self) -> &Option<BBox3D> {
        match self {
            VectorGeometry::Point(g) => &g.bbox,
            VectorGeometry::MultiPoint(g) => &g.bbox,
            VectorGeometry::LineString(g) => &g.bbox,
            VectorGeometry::MultiLineString(g) => &g.bbox,
            VectorGeometry::Polygon(g) => &g.bbox,
            VectorGeometry::MultiPolygon(g) => &g.bbox,
        }
    }

    /// Get the vec_bbox of the geometry
    pub fn vec_bbox(&self) -> &Option<BBox3D> {
        match self {
            VectorGeometry::Point(g) => &g.vec_bbox,
            VectorGeometry::MultiPoint(g) => &g.vec_bbox,
            VectorGeometry::LineString(g) => &g.vec_bbox,
            VectorGeometry::MultiLineString(g) => &g.vec_bbox,
            VectorGeometry::Polygon(g) => &g.vec_bbox,
            VectorGeometry::MultiPolygon(g) => &g.vec_bbox,
        }
    }

    /// Get the geometry point
    pub fn point(&self) -> Option<&VectorPoint<M>> {
        match self {
            VectorGeometry::Point(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new point
    pub fn new_point(coordinates: VectorPoint<M>, bbox: Option<BBox3D>) -> Self {
        VectorGeometry::Point(VectorPointGeometry {
            _type: VectorGeometryType::Point,
            is_3d: coordinates.z.is_some(),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// Get the geometry multi point
    pub fn multipoint(&self) -> Option<&VectorMultiPoint<M>> {
        match self {
            VectorGeometry::MultiPoint(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new multipoint
    pub fn new_multipoint(coordinates: VectorMultiPoint<M>, bbox: Option<BBox3D>) -> Self {
        VectorGeometry::MultiPoint(VectorMultiPointGeometry {
            _type: VectorGeometryType::MultiPoint,
            is_3d: coordinates.iter().any(|point| point.z.is_some()),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// Get the geometry linestring
    pub fn linestring(&self) -> Option<&VectorLineString<M>> {
        match self {
            VectorGeometry::LineString(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new linestring
    pub fn new_linestring(coordinates: VectorLineString<M>, bbox: Option<BBox3D>) -> Self {
        VectorGeometry::LineString(VectorLineStringGeometry {
            _type: VectorGeometryType::LineString,
            is_3d: coordinates.iter().any(|point| point.z.is_some()),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// Get the geometry multilinestring
    pub fn multilinestring(&self) -> Option<&VectorMultiLineString<M>> {
        match self {
            VectorGeometry::MultiLineString(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new multilinestring
    pub fn new_multilinestring(
        coordinates: VectorMultiLineString<M>,
        bbox: Option<BBox3D>,
    ) -> Self {
        VectorGeometry::MultiLineString(VectorMultiLineStringGeometry {
            _type: VectorGeometryType::MultiLineString,
            is_3d: coordinates.iter().any(|line| line.iter().any(|point| point.z.is_some())),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// Get the geometry polygon
    pub fn polygon(&self) -> Option<&VectorPolygon<M>> {
        match self {
            VectorGeometry::Polygon(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new polygon
    pub fn new_polygon(coordinates: VectorPolygon<M>, bbox: Option<BBox3D>) -> Self {
        VectorGeometry::Polygon(VectorPolygonGeometry {
            _type: VectorGeometryType::Polygon,
            is_3d: coordinates.iter().any(|ring| ring.iter().any(|point| point.z.is_some())),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// Get the geometry multipolygon
    pub fn multipolygon(&self) -> Option<&VectorMultiPolygon<M>> {
        match self {
            VectorGeometry::MultiPolygon(g) => Some(&g.coordinates),
            _ => None,
        }
    }

    /// Create a new multipolygon
    pub fn new_multipolygon(coordinates: VectorMultiPolygon<M>, bbox: Option<BBox3D>) -> Self {
        VectorGeometry::MultiPolygon(VectorMultiPolygonGeometry {
            _type: VectorGeometryType::MultiPolygon,
            is_3d: coordinates.iter().any(|polygon| {
                polygon.iter().any(|ring| ring.iter().any(|point| point.z.is_some()))
            }),
            coordinates,
            bbox,
            ..Default::default()
        })
    }

    /// set the tessellation of the geometry (polygon and multipolygon only)
    pub fn set_tess(&mut self, tessellation: Vec<f64>) {
        match self {
            VectorGeometry::Polygon(g) => g.tessellation = Some(tessellation),
            VectorGeometry::MultiPolygon(g) => g.tessellation = Some(tessellation),
            _ => {}
        }
    }

    /// set the indices of the geometry (polygon and multipolygon only)
    pub fn set_indices(&mut self, indices: Vec<u32>) {
        match self {
            VectorGeometry::Polygon(g) => g.indices = Some(indices),
            VectorGeometry::MultiPolygon(g) => g.indices = Some(indices),
            _ => {}
        }
    }

    /// Convert the geometry so that all m-values are MValue rather then user defined
    pub fn to_m_geometry(&self) -> VectorGeometry<MValue>
    where
        M: MValueCompatible,
    {
        match self {
            VectorGeometry::Point(g) => VectorGeometry::Point(VectorPointGeometry {
                _type: g._type,
                is_3d: g.is_3d,
                coordinates: g.coordinates.to_m_value(),
                offset: g.offset.clone(),
                bbox: g.bbox,
                vec_bbox: g.vec_bbox,
                ..Default::default()
            }),
            VectorGeometry::MultiPoint(g) => VectorGeometry::MultiPoint(VectorMultiPointGeometry {
                _type: g._type,
                is_3d: g.is_3d,
                coordinates: g.coordinates.iter().map(|point| point.to_m_value()).collect(),
                offset: g.offset,
                bbox: g.bbox,
                vec_bbox: g.vec_bbox,
                ..Default::default()
            }),
            VectorGeometry::LineString(g) => VectorGeometry::LineString(VectorLineStringGeometry {
                _type: g._type,
                is_3d: g.is_3d,
                coordinates: g.coordinates.iter().map(|point| point.to_m_value()).collect(),
                offset: g.offset,
                bbox: g.bbox,
                vec_bbox: g.vec_bbox,
                ..Default::default()
            }),
            VectorGeometry::MultiLineString(g) => {
                VectorGeometry::MultiLineString(VectorMultiLineStringGeometry {
                    _type: g._type,
                    is_3d: g.is_3d,
                    coordinates: g
                        .coordinates
                        .iter()
                        .map(|line| line.iter().map(|point| point.to_m_value()).collect())
                        .collect(),
                    offset: g.offset.clone(),
                    bbox: g.bbox,
                    vec_bbox: g.vec_bbox,
                    ..Default::default()
                })
            }
            VectorGeometry::Polygon(g) => VectorGeometry::Polygon(VectorPolygonGeometry {
                _type: g._type,
                is_3d: g.is_3d,
                coordinates: g
                    .coordinates
                    .iter()
                    .map(|ring| ring.iter().map(|point| point.to_m_value()).collect())
                    .collect(),
                offset: g.offset.clone(),
                bbox: g.bbox,
                vec_bbox: g.vec_bbox,
                ..Default::default()
            }),
            VectorGeometry::MultiPolygon(g) => {
                VectorGeometry::MultiPolygon(VectorMultiPolygonGeometry {
                    _type: g._type,
                    is_3d: g.is_3d,
                    coordinates: g
                        .coordinates
                        .iter()
                        .map(|polygon| {
                            polygon
                                .iter()
                                .map(|ring| ring.iter().map(|point| point.to_m_value()).collect())
                                .collect()
                        })
                        .collect(),
                    offset: g.offset.clone(),
                    bbox: g.bbox,
                    vec_bbox: g.vec_bbox,
                    ..Default::default()
                })
            }
        }
    }
}
impl<M: Clone + Default> Default for VectorGeometry<M> {
    fn default() -> Self {
        VectorGeometry::Point(VectorPointGeometry::default())
    }
}

/// BaseGeometry is the a generic geometry type
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct VectorBaseGeometry<G = VectorGeometry, O = VectorOffsets> {
    /// The geometry type
    #[serde(rename = "type")]
    pub _type: VectorGeometryType,
    /// Specifies if the geometry is 3D or 2D
    #[serde(rename = "is3D", default)]
    pub is_3d: bool,
    /// The geometry shape
    pub coordinates: G,
    /// The geometry offsets if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<O>,
    /// The BBox shape - always in lon-lat
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bbox: Option<BBox3D>,
    /// temporary bbox to track 0->1 clipping
    #[serde(skip)]
    pub vec_bbox: Option<BBox3D>,
    /// Polygon and MultiPolygon specific property
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indices: Option<Vec<u32>>,
    /// Polygon and MultiPolygon specific property
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tessellation: Option<Vec<f64>>,
}

/// All possible geometry offsets
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum VectorOffsets {
    /// LineString offset
    LineOffset(VectorLineOffset),
    /// MultiLineString offset
    MultiLineOffset(VectorMultiLineOffset),
    /// Polygon offset
    PolygonOffset(VectorPolygonOffset),
    /// MultiPolygon offset
    MultiPolygonOffset(VectorMultiPolygonOffset),
}
impl Default for VectorOffsets {
    fn default() -> Self {
        VectorOffsets::LineOffset(0.0)
    }
}
/// An offset defines how far the starting line is from the original starting point pre-slice
pub type VectorLineOffset = f64;
/// A collection of offsets
pub type VectorMultiLineOffset = Vec<VectorLineOffset>;
/// A collection of offsets
pub type VectorPolygonOffset = VectorMultiLineOffset;
/// A collection of collections of offsets
pub type VectorMultiPolygonOffset = Vec<VectorPolygonOffset>;

/// PointGeometry is a point
pub type VectorPointGeometry<M = MValue> = VectorBaseGeometry<VectorPoint<M>>;
/// MultiPointGeometry contains multiple points
pub type VectorMultiPointGeometry<M = MValue> =
    VectorBaseGeometry<VectorMultiPoint<M>, VectorLineOffset>;
/// LineStringGeometry is a line
pub type VectorLineStringGeometry<M = MValue> =
    VectorBaseGeometry<VectorLineString<M>, VectorLineOffset>;
/// MultiLineStringGeometry contains multiple lines
pub type VectorMultiLineStringGeometry<M = MValue> =
    VectorBaseGeometry<VectorMultiLineString<M>, VectorMultiLineOffset>;
/// PolygonGeometry is a polygon with potential holes
pub type VectorPolygonGeometry<M = MValue> =
    VectorBaseGeometry<VectorPolygon<M>, VectorPolygonOffset>;
/// MultiPolygonGeometry is a polygon with multiple polygons with their own potential holes
pub type VectorMultiPolygonGeometry<M = MValue> =
    VectorBaseGeometry<VectorMultiPolygon<M>, VectorMultiPolygonOffset>;