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 GetXY for Point3D {
54    fn x(&self) -> f64 {
55        self.0
56    }
57    fn y(&self) -> f64 {
58        self.1
59    }
60}
61impl GetXY for PointOrPoint3D {
62    fn x(&self) -> f64 {
63        self.0
64    }
65    fn y(&self) -> f64 {
66        self.1
67    }
68}
69impl GetZ for Point3D {
70    fn z(&self) -> f64 {
71        self.2
72    }
73}
74impl GetZ for PointOrPoint3D {
75    fn z(&self) -> f64 {
76        self.2.unwrap_or_default()
77    }
78}
79
80/// Enum to represent specific geometry types as strings
81#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
82pub enum GeometryType {
83    /// Point
84    #[default]
85    Point,
86    /// MultiPoint
87    MultiPoint,
88    /// LineString
89    LineString,
90    /// MultiLineString
91    MultiLineString,
92    /// Polygon
93    Polygon,
94    /// MultiPolygon
95    MultiPolygon,
96    /// 3D Point
97    Point3D,
98    /// 3D MultiPoint
99    MultiPoint3D,
100    /// 3D LineString
101    LineString3D,
102    /// 3D MultiLineString
103    MultiLineString3D,
104    /// 3D Polygon
105    Polygon3D,
106    /// 3D MultiPolygon
107    MultiPolygon3D,
108}
109impl From<&str> for GeometryType {
110    fn from(s: &str) -> Self {
111        match s {
112            "Point" => GeometryType::Point,
113            "MultiPoint" => GeometryType::MultiPoint,
114            "LineString" => GeometryType::LineString,
115            "MultiLineString" => GeometryType::MultiLineString,
116            "Polygon" => GeometryType::Polygon,
117            "MultiPolygon" => GeometryType::MultiPolygon,
118            "Point3D" => GeometryType::Point3D,
119            "MultiPoint3D" => GeometryType::MultiPoint3D,
120            "LineString3D" => GeometryType::LineString3D,
121            "MultiLineString3D" => GeometryType::MultiLineString3D,
122            "Polygon3D" => GeometryType::Polygon3D,
123            "MultiPolygon3D" => GeometryType::MultiPolygon3D,
124            _ => panic!("Invalid geometry type: {}", s),
125        }
126    }
127}
128
129/// All possible geometry shapes
130#[derive(Clone, Serialize, Debug, PartialEq)]
131#[serde(untagged)]
132pub enum Geometry<M: Clone + Default = MValue> {
133    /// Point Shape
134    Point(PointGeometry<M>),
135    /// MultiPoint Shape
136    MultiPoint(MultiPointGeometry<M>),
137    /// LineString Shape
138    LineString(LineStringGeometry<M>),
139    /// MultiLineString Shape
140    MultiLineString(MultiLineStringGeometry<M>),
141    /// Polygon Shape
142    Polygon(PolygonGeometry<M>),
143    /// MultiPolygon Shape
144    MultiPolygon(MultiPolygonGeometry<M>),
145    /// Point3D Shape
146    Point3D(Point3DGeometry<M>),
147    /// MultiPoint3D Shape
148    MultiPoint3D(MultiPoint3DGeometry<M>),
149    /// LineString3D Shape
150    LineString3D(LineString3DGeometry<M>),
151    /// MultiLineString3D Shape
152    MultiLineString3D(MultiLineString3DGeometry<M>),
153    /// Polygon3D Shape
154    Polygon3D(Polygon3DGeometry<M>),
155    /// MultiPolygon3D Shape
156    MultiPolygon3D(MultiPolygon3DGeometry<M>),
157}
158impl<M: Clone + Default> Default for Geometry<M> {
159    fn default() -> Self {
160        Geometry::Point(PointGeometry::<M>::default())
161    }
162}
163
164/// BaseGeometry is the a generic geometry type
165#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
166pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
167    /// The geometry type
168    #[serde(rename = "type")]
169    pub _type: GeometryType,
170    /// The geometry shape
171    pub coordinates: G,
172    /// The M-Values shape
173    #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
174    pub m_values: Option<M>,
175    /// The BBox shape
176    #[serde(skip_serializing_if = "Option::is_none")]
177    pub bbox: Option<B>,
178}
179
180/// PointGeometry is a point
181pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
182/// MultiPointGeometry contains multiple points
183pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
184/// LineStringGeometry is a line
185pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
186/// MultiLineStringGeometry contains multiple lines
187pub type MultiLineStringGeometry<M = MValue> =
188    BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
189/// PolygonGeometry is a polygon with potential holes
190pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
191/// MultiPolygonGeometry is a polygon with multiple polygons with their own potential holes
192pub type MultiPolygonGeometry<M = MValue> =
193    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
194/// Point3DGeometry is a 3D point
195pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
196/// MultiPoint3DGeometry contains multiple 3D points
197pub type MultiPoint3DGeometry<M = MValue> =
198    BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
199/// LineString3DGeometry is a 3D line
200pub type LineString3DGeometry<M = MValue> =
201    BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
202/// MultiLineString3DGeometry contains multiple 3D lines
203pub type MultiLineString3DGeometry<M = MValue> =
204    BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
205/// Polygon3DGeometry is a 3D polygon with potential holes
206pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
207/// MultiPolygon3DGeometry is a 3D polygon with multiple polygons with their own potential holes
208pub type MultiPolygon3DGeometry<M = MValue> =
209    BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;