1use crate::*;
2use alloc::vec::Vec;
3use serde::{Deserialize, Serialize};
4
5#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
7pub struct Point(pub f64, pub f64);
8pub type MultiPoint = Vec<Point>;
10pub type LineString = Vec<Point>;
12pub type MultiLineString = Vec<LineString>;
14pub type Polygon = Vec<Vec<Point>>;
16pub type MultiPolygon = Vec<Polygon>;
18#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Default)]
20pub struct Point3D(pub f64, pub f64, pub f64);
21pub type MultiPoint3D = Vec<Point3D>;
23pub type LineString3D = Vec<Point3D>;
25pub type MultiLineString3D = Vec<LineString3D>;
27pub type Polygon3D = Vec<Vec<Point3D>>;
29pub type MultiPolygon3D = Vec<Polygon3D>;
31#[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#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
82pub enum GeometryType {
83 #[default]
85 Point,
86 MultiPoint,
88 LineString,
90 MultiLineString,
92 Polygon,
94 MultiPolygon,
96 Point3D,
98 MultiPoint3D,
100 LineString3D,
102 MultiLineString3D,
104 Polygon3D,
106 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#[derive(Clone, Serialize, Debug, PartialEq)]
131#[serde(untagged)]
132pub enum Geometry<M: Clone + Default = MValue> {
133 Point(PointGeometry<M>),
135 MultiPoint(MultiPointGeometry<M>),
137 LineString(LineStringGeometry<M>),
139 MultiLineString(MultiLineStringGeometry<M>),
141 Polygon(PolygonGeometry<M>),
143 MultiPolygon(MultiPolygonGeometry<M>),
145 Point3D(Point3DGeometry<M>),
147 MultiPoint3D(MultiPoint3DGeometry<M>),
149 LineString3D(LineString3DGeometry<M>),
151 MultiLineString3D(MultiLineString3DGeometry<M>),
153 Polygon3D(Polygon3DGeometry<M>),
155 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#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
166pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
167 #[serde(rename = "type")]
169 pub _type: GeometryType,
170 pub coordinates: G,
172 #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
174 pub m_values: Option<M>,
175 #[serde(skip_serializing_if = "Option::is_none")]
177 pub bbox: Option<B>,
178}
179
180pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
182pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
184pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
186pub type MultiLineStringGeometry<M = MValue> =
188 BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
189pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
191pub type MultiPolygonGeometry<M = MValue> =
193 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
194pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
196pub type MultiPoint3DGeometry<M = MValue> =
198 BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
199pub type LineString3DGeometry<M = MValue> =
201 BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
202pub type MultiLineString3DGeometry<M = MValue> =
204 BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
205pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
207pub type MultiPolygon3DGeometry<M = MValue> =
209 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;