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