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
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
use crate::*;
use alloc::vec::Vec;
use core::cmp::Ordering;
use serde::{Deserialize, Serialize};

/// Definition of a Point. May represent WebMercator Lon-Lat or S2Geometry S-T
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
pub struct Point(pub f64, pub f64);
impl<P: GetXY> From<&P> for Point {
    fn from(p: &P) -> Self {
        Point(p.x(), p.y())
    }
}
/// Definition of a MultiPoint
pub type MultiPoint = Vec<Point>;
/// Definition of a LineString
pub type LineString = Vec<Point>;
/// Definition of a MultiLineString
pub type MultiLineString = Vec<LineString>;
/// Definition of a Polygon
pub type Polygon = Vec<Vec<Point>>;
/// Definition of a MultiPolygon
pub type MultiPolygon = Vec<Polygon>;
/// Definition of a 3D Point. May represent WebMercator Lon-Lat or S2Geometry S-T with a z-value
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
pub struct Point3D(pub f64, pub f64, pub f64);
impl<P: GetXYZ> From<&P> for Point3D {
    fn from(p: &P) -> Self {
        Point3D(p.x(), p.y(), p.z().unwrap_or_default())
    }
}
/// Definition of a 3D MultiPoint
pub type MultiPoint3D = Vec<Point3D>;
/// Definition of a 3D LineString
pub type LineString3D = Vec<Point3D>;
/// Definition of a 3D MultiLineString
pub type MultiLineString3D = Vec<LineString3D>;
/// Definition of a 3D Polygon
pub type Polygon3D = Vec<Vec<Point3D>>;
/// Definition of a 3D MultiPolygon
pub type MultiPolygon3D = Vec<Polygon3D>;
/// Define a Point or Point3D
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
pub struct PointOrPoint3D(pub f64, pub f64, pub Option<f64>);
impl From<Point> for PointOrPoint3D {
    fn from(p: Point) -> Self {
        PointOrPoint3D(p.0, p.1, None)
    }
}
impl From<Point3D> for PointOrPoint3D {
    fn from(p: Point3D) -> Self {
        PointOrPoint3D(p.0, p.1, Some(p.2))
    }
}
impl<P: GetXYZ> From<&P> for PointOrPoint3D {
    fn from(p: &P) -> Self {
        PointOrPoint3D(p.x(), p.y(), p.z())
    }
}

// GET

impl GetXY for (f64, f64) {
    fn x(&self) -> f64 {
        self.0
    }
    fn y(&self) -> f64 {
        self.1
    }
}
impl GetXY for (f64, f64, f64) {
    fn x(&self) -> f64 {
        self.0
    }
    fn y(&self) -> f64 {
        self.1
    }
}
impl GetXY for Point {
    fn x(&self) -> f64 {
        self.0
    }
    fn y(&self) -> f64 {
        self.1
    }
}
impl GetZ for (f64, f64) {
    fn z(&self) -> Option<f64> {
        None
    }
}
impl GetZ for (f64, f64, f64) {
    fn z(&self) -> Option<f64> {
        Some(self.2)
    }
}
impl GetZ for Point {
    fn z(&self) -> Option<f64> {
        None
    }
}
impl GetXY for Point3D {
    fn x(&self) -> f64 {
        self.0
    }
    fn y(&self) -> f64 {
        self.1
    }
}
impl GetXY for PointOrPoint3D {
    fn x(&self) -> f64 {
        self.0
    }
    fn y(&self) -> f64 {
        self.1
    }
}
impl GetZ for Point3D {
    fn z(&self) -> Option<f64> {
        Some(self.2)
    }
}
impl GetZ for PointOrPoint3D {
    fn z(&self) -> Option<f64> {
        self.2
    }
}

// SET

impl SetXY for (f64, f64) {
    fn set_x(&mut self, x: f64) {
        self.0 = x;
    }
    fn set_y(&mut self, y: f64) {
        self.1 = y;
    }
}
impl SetXY for (f64, f64, f64) {
    fn set_x(&mut self, x: f64) {
        self.0 = x;
    }
    fn set_y(&mut self, y: f64) {
        self.1 = y;
    }
}
impl SetXY for Point {
    fn set_x(&mut self, x: f64) {
        self.0 = x;
    }
    fn set_y(&mut self, y: f64) {
        self.1 = y;
    }
}
impl SetXY for Point3D {
    fn set_x(&mut self, x: f64) {
        self.0 = x;
    }
    fn set_y(&mut self, y: f64) {
        self.1 = y;
    }
}
impl SetZ for (f64, f64, f64) {
    fn set_z(&mut self, z: f64) {
        self.2 = z;
    }
}
impl SetZ for Point3D {
    fn set_z(&mut self, z: f64) {
        self.2 = z;
    }
}
impl SetXY for PointOrPoint3D {
    fn set_x(&mut self, x: f64) {
        self.0 = x;
    }
    fn set_y(&mut self, y: f64) {
        self.1 = y;
    }
}
impl SetZ for PointOrPoint3D {
    fn set_z(&mut self, z: f64) {
        self.2 = Some(z);
    }
}

// NEW

impl NewXY for (f64, f64) {
    fn new_xy(x: f64, y: f64) -> Self {
        (x, y)
    }
}
impl NewXY for Point {
    fn new_xy(x: f64, y: f64) -> Self {
        Self(x, y)
    }
}
impl NewXY for Point3D {
    fn new_xy(x: f64, y: f64) -> Self {
        Self(x, y, 0.0)
    }
}
impl NewXY for PointOrPoint3D {
    fn new_xy(x: f64, y: f64) -> Self {
        Self(x, y, None)
    }
}
impl<M: Clone> NewXYM<M> for Point {
    fn new_xym(x: f64, y: f64, _m: M) -> Self {
        Self(x, y)
    }
}
impl<M: Clone> NewXYM<M> for Point3D {
    fn new_xym(x: f64, y: f64, _m: M) -> Self {
        Self(x, y, 0.0)
    }
}
impl<M: Clone> NewXYM<M> for PointOrPoint3D {
    fn new_xym(x: f64, y: f64, _m: M) -> Self {
        Self(x, y, None)
    }
}
impl NewXYZ for (f64, f64, f64) {
    fn new_xyz(x: f64, y: f64, z: f64) -> Self {
        (x, y, z)
    }
}
impl NewXYZ for Point3D {
    fn new_xyz(x: f64, y: f64, z: f64) -> Self {
        Self(x, y, z)
    }
}
impl NewXYZ for PointOrPoint3D {
    fn new_xyz(x: f64, y: f64, z: f64) -> Self {
        Self(x, y, Some(z))
    }
}
impl<M: Clone> NewXYZM<M> for Point3D {
    fn new_xyzm(x: f64, y: f64, z: f64, _m: M) -> Self {
        Self(x, y, z)
    }
}
impl<M: Clone> NewXYZM<M> for PointOrPoint3D {
    fn new_xyzm(x: f64, y: f64, z: f64, _m: M) -> Self {
        Self(x, y, Some(z))
    }
}

// Equalities

impl Eq for Point {}
impl Ord for Point {
    fn cmp(&self, other: &Point) -> Ordering {
        match self.0.partial_cmp(&other.0) {
            Some(Ordering::Equal) => {}
            other => return other.unwrap_or(Ordering::Greater), /* Handle cases where `x` comparison is not equal */
        }
        match self.1.partial_cmp(&other.1) {
            Some(Ordering::Equal) => Ordering::Equal,
            other => other.unwrap_or(Ordering::Greater), /* Handle cases where `y` comparison is not equal */
        }
    }
}
impl PartialOrd for Point {
    fn partial_cmp(&self, other: &Point) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for Point3D {}
impl Ord for Point3D {
    fn cmp(&self, other: &Point3D) -> Ordering {
        match self.0.partial_cmp(&other.0) {
            Some(Ordering::Equal) => {}
            other => return other.unwrap_or(Ordering::Greater), /* Handle cases where `x` comparison is not equal */
        }
        match self.1.partial_cmp(&other.1) {
            Some(Ordering::Equal) => {}
            other => return other.unwrap_or(Ordering::Greater), /* Handle cases where `y` comparison is not equal */
        }
        match self.2.partial_cmp(&other.2) {
            Some(order) => order,
            None => Ordering::Equal, // This handles the NaN case safely
        }
    }
}
impl PartialOrd for Point3D {
    fn partial_cmp(&self, other: &Point3D) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for PointOrPoint3D {}
impl Ord for PointOrPoint3D {
    fn cmp(&self, other: &PointOrPoint3D) -> Ordering {
        match self.0.partial_cmp(&other.0) {
            Some(Ordering::Equal) => {}
            other => return other.unwrap_or(Ordering::Greater), /* Handle cases where `x` comparison is not equal */
        }
        match self.1.partial_cmp(&other.1) {
            Some(Ordering::Equal) => {}
            other => return other.unwrap_or(Ordering::Greater), /* Handle cases where `y` comparison is not equal */
        }
        match self.2.partial_cmp(&other.2) {
            Some(order) => order,
            None => Ordering::Equal, // This handles the NaN case safely
        }
    }
}
impl PartialOrd for PointOrPoint3D {
    fn partial_cmp(&self, other: &PointOrPoint3D) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

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

/// All possible geometry shapes
#[derive(Clone, Serialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum Geometry<M: Clone + Default = MValue> {
    /// Point Shape
    Point(PointGeometry<M>),
    /// MultiPoint Shape
    MultiPoint(MultiPointGeometry<M>),
    /// LineString Shape
    LineString(LineStringGeometry<M>),
    /// MultiLineString Shape
    MultiLineString(MultiLineStringGeometry<M>),
    /// Polygon Shape
    Polygon(PolygonGeometry<M>),
    /// MultiPolygon Shape
    MultiPolygon(MultiPolygonGeometry<M>),
    /// Point3D Shape
    Point3D(Point3DGeometry<M>),
    /// MultiPoint3D Shape
    MultiPoint3D(MultiPoint3DGeometry<M>),
    /// LineString3D Shape
    LineString3D(LineString3DGeometry<M>),
    /// MultiLineString3D Shape
    MultiLineString3D(MultiLineString3DGeometry<M>),
    /// Polygon3D Shape
    Polygon3D(Polygon3DGeometry<M>),
    /// MultiPolygon3D Shape
    MultiPolygon3D(MultiPolygon3DGeometry<M>),
}
impl<M: Clone + Default> Default for Geometry<M> {
    fn default() -> Self {
        Geometry::Point(PointGeometry::<M>::default())
    }
}

/// BaseGeometry is the a generic geometry type
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
    /// The geometry type
    #[serde(rename = "type")]
    pub _type: GeometryType,
    /// The geometry shape
    pub coordinates: G,
    /// The M-Values shape
    #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
    pub m_values: Option<M>,
    /// The BBox shape
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bbox: Option<B>,
}

/// PointGeometry is a point
pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
/// MultiPointGeometry contains multiple points
pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
/// LineStringGeometry is a line
pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
/// MultiLineStringGeometry contains multiple lines
pub type MultiLineStringGeometry<M = MValue> =
    BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
/// PolygonGeometry is a polygon with potential holes
pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
/// MultiPolygonGeometry is a polygon with multiple polygons with their own potential holes
pub type MultiPolygonGeometry<M = MValue> =
    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
/// Point3DGeometry is a 3D point
pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
/// MultiPoint3DGeometry contains multiple 3D points
pub type MultiPoint3DGeometry<M = MValue> =
    BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
/// LineString3DGeometry is a 3D line
pub type LineString3DGeometry<M = MValue> =
    BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
/// MultiLineString3DGeometry contains multiple 3D lines
pub type MultiLineString3DGeometry<M = MValue> =
    BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
/// Polygon3DGeometry is a 3D polygon with potential holes
pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
/// MultiPolygon3DGeometry is a 3D polygon with multiple polygons with their own potential holes
pub type MultiPolygon3DGeometry<M = MValue> =
    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;