s2json_core/geometry/
primitive.rs

1use crate::*;
2use alloc::vec::Vec;
3use serde::{Deserialize, Serialize};
4
5/// Definition of a Point. May represent WebMercator Lon-Lat or S2Geometry S-T
6#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
7pub struct Point(pub f64, pub f64);
8/// Definition of a MultiPoint
9pub type MultiPoint = Vec<Point>;
10/// Definition of a LineString
11pub type LineString = Vec<Point>;
12/// Definition of a MultiLineString
13pub type MultiLineString = Vec<LineString>;
14/// Definition of a Polygon
15pub type Polygon = Vec<Vec<Point>>;
16/// Definition of a MultiPolygon
17pub type MultiPolygon = Vec<Polygon>;
18/// Definition of a 3D Point. May represent WebMercator Lon-Lat or S2Geometry S-T with a z-value
19#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
20pub struct Point3D(pub f64, pub f64, pub f64);
21/// Definition of a 3D MultiPoint
22pub type MultiPoint3D = Vec<Point3D>;
23/// Definition of a 3D LineString
24pub type LineString3D = Vec<Point3D>;
25/// Definition of a 3D MultiLineString
26pub type MultiLineString3D = Vec<LineString3D>;
27/// Definition of a 3D Polygon
28pub type Polygon3D = Vec<Vec<Point3D>>;
29/// Definition of a 3D MultiPolygon
30pub type MultiPolygon3D = Vec<Polygon3D>;
31/// Define a Point or Point3D
32#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
33pub struct PointOrPoint3D(pub f64, pub f64, pub Option<f64>);
34impl From<Point> for PointOrPoint3D {
35    fn from(p: Point) -> Self {
36        PointOrPoint3D(p.0, p.1, None)
37    }
38}
39impl From<Point3D> for PointOrPoint3D {
40    fn from(p: Point3D) -> Self {
41        PointOrPoint3D(p.0, p.1, Some(p.2))
42    }
43}
44
45impl GetXY for Point {
46    fn x(&self) -> f64 {
47        self.0
48    }
49    fn y(&self) -> f64 {
50        self.1
51    }
52}
53impl GetZ for Point {
54    fn z(&self) -> Option<f64> {
55        None
56    }
57}
58impl GetXYZ for Point {}
59impl GetXY for Point3D {
60    fn x(&self) -> f64 {
61        self.0
62    }
63    fn y(&self) -> f64 {
64        self.1
65    }
66}
67impl GetXY for PointOrPoint3D {
68    fn x(&self) -> f64 {
69        self.0
70    }
71    fn y(&self) -> f64 {
72        self.1
73    }
74}
75impl GetZ for Point3D {
76    fn z(&self) -> Option<f64> {
77        Some(self.2)
78    }
79}
80impl GetXYZ for Point3D {}
81impl GetZ for PointOrPoint3D {
82    fn z(&self) -> Option<f64> {
83        self.2
84    }
85}
86impl GetXYZ for PointOrPoint3D {}
87
88/// Enum to represent specific geometry types as strings
89#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
90pub enum GeometryType {
91    /// Point
92    #[default]
93    Point,
94    /// MultiPoint
95    MultiPoint,
96    /// LineString
97    LineString,
98    /// MultiLineString
99    MultiLineString,
100    /// Polygon
101    Polygon,
102    /// MultiPolygon
103    MultiPolygon,
104    /// 3D Point
105    Point3D,
106    /// 3D MultiPoint
107    MultiPoint3D,
108    /// 3D LineString
109    LineString3D,
110    /// 3D MultiLineString
111    MultiLineString3D,
112    /// 3D Polygon
113    Polygon3D,
114    /// 3D MultiPolygon
115    MultiPolygon3D,
116}
117impl From<&str> for GeometryType {
118    fn from(s: &str) -> Self {
119        match s {
120            "Point" => GeometryType::Point,
121            "MultiPoint" => GeometryType::MultiPoint,
122            "LineString" => GeometryType::LineString,
123            "MultiLineString" => GeometryType::MultiLineString,
124            "Polygon" => GeometryType::Polygon,
125            "MultiPolygon" => GeometryType::MultiPolygon,
126            "Point3D" => GeometryType::Point3D,
127            "MultiPoint3D" => GeometryType::MultiPoint3D,
128            "LineString3D" => GeometryType::LineString3D,
129            "MultiLineString3D" => GeometryType::MultiLineString3D,
130            "Polygon3D" => GeometryType::Polygon3D,
131            "MultiPolygon3D" => GeometryType::MultiPolygon3D,
132            _ => panic!("Invalid geometry type: {}", s),
133        }
134    }
135}
136
137/// All possible geometry shapes
138#[derive(Clone, Serialize, Debug, PartialEq)]
139#[serde(untagged)]
140pub enum Geometry<M: Clone + Default = MValue> {
141    /// Point Shape
142    Point(PointGeometry<M>),
143    /// MultiPoint Shape
144    MultiPoint(MultiPointGeometry<M>),
145    /// LineString Shape
146    LineString(LineStringGeometry<M>),
147    /// MultiLineString Shape
148    MultiLineString(MultiLineStringGeometry<M>),
149    /// Polygon Shape
150    Polygon(PolygonGeometry<M>),
151    /// MultiPolygon Shape
152    MultiPolygon(MultiPolygonGeometry<M>),
153    /// Point3D Shape
154    Point3D(Point3DGeometry<M>),
155    /// MultiPoint3D Shape
156    MultiPoint3D(MultiPoint3DGeometry<M>),
157    /// LineString3D Shape
158    LineString3D(LineString3DGeometry<M>),
159    /// MultiLineString3D Shape
160    MultiLineString3D(MultiLineString3DGeometry<M>),
161    /// Polygon3D Shape
162    Polygon3D(Polygon3DGeometry<M>),
163    /// MultiPolygon3D Shape
164    MultiPolygon3D(MultiPolygon3DGeometry<M>),
165}
166impl<M: Clone + Default> Default for Geometry<M> {
167    fn default() -> Self {
168        Geometry::Point(PointGeometry::<M>::default())
169    }
170}
171
172/// BaseGeometry is the a generic geometry type
173#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
174pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
175    /// The geometry type
176    #[serde(rename = "type")]
177    pub _type: GeometryType,
178    /// The geometry shape
179    pub coordinates: G,
180    /// The M-Values shape
181    #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
182    pub m_values: Option<M>,
183    /// The BBox shape
184    #[serde(skip_serializing_if = "Option::is_none")]
185    pub bbox: Option<B>,
186}
187
188/// PointGeometry is a point
189pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
190/// MultiPointGeometry contains multiple points
191pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
192/// LineStringGeometry is a line
193pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
194/// MultiLineStringGeometry contains multiple lines
195pub type MultiLineStringGeometry<M = MValue> =
196    BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
197/// PolygonGeometry is a polygon with potential holes
198pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
199/// MultiPolygonGeometry is a polygon with multiple polygons with their own potential holes
200pub type MultiPolygonGeometry<M = MValue> =
201    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
202/// Point3DGeometry is a 3D point
203pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
204/// MultiPoint3DGeometry contains multiple 3D points
205pub type MultiPoint3DGeometry<M = MValue> =
206    BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
207/// LineString3DGeometry is a 3D line
208pub type LineString3DGeometry<M = MValue> =
209    BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
210/// MultiLineString3DGeometry contains multiple 3D lines
211pub type MultiLineString3DGeometry<M = MValue> =
212    BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
213/// Polygon3DGeometry is a 3D polygon with potential holes
214pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
215/// MultiPolygon3DGeometry is a 3D polygon with multiple polygons with their own potential holes
216pub type MultiPolygon3DGeometry<M = MValue> =
217    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;