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 {
48 fn x(&self) -> f64 {
49 self.0
50 }
51 fn y(&self) -> f64 {
52 self.1
53 }
54}
55impl GetZ for Point {
56 fn z(&self) -> Option<f64> {
57 None
58 }
59}
60impl GetXYZ for Point {}
61impl GetXY for Point3D {
62 fn x(&self) -> f64 {
63 self.0
64 }
65 fn y(&self) -> f64 {
66 self.1
67 }
68}
69impl GetXY for PointOrPoint3D {
70 fn x(&self) -> f64 {
71 self.0
72 }
73 fn y(&self) -> f64 {
74 self.1
75 }
76}
77impl GetZ for Point3D {
78 fn z(&self) -> Option<f64> {
79 Some(self.2)
80 }
81}
82impl GetXYZ for Point3D {}
83impl GetZ for PointOrPoint3D {
84 fn z(&self) -> Option<f64> {
85 self.2
86 }
87}
88impl GetXYZ for PointOrPoint3D {}
89
90impl SetXY for Point {
93 fn set_x(&mut self, x: f64) {
94 self.0 = x;
95 }
96 fn set_y(&mut self, y: f64) {
97 self.1 = y;
98 }
99 fn set_xy(&mut self, x: f64, y: f64) {
100 self.0 = x;
101 self.1 = y;
102 }
103}
104impl SetXY for Point3D {
105 fn set_x(&mut self, x: f64) {
106 self.0 = x;
107 }
108 fn set_y(&mut self, y: f64) {
109 self.1 = y;
110 }
111}
112impl SetZ for Point3D {
113 fn set_z(&mut self, z: f64) {
114 self.2 = z;
115 }
116}
117impl SetXYZ for Point3D {}
118impl SetXY for PointOrPoint3D {
119 fn set_x(&mut self, x: f64) {
120 self.0 = x;
121 }
122 fn set_y(&mut self, y: f64) {
123 self.1 = y;
124 }
125}
126impl SetZ for PointOrPoint3D {
127 fn set_z(&mut self, z: f64) {
128 self.2 = Some(z);
129 }
130}
131impl SetXYZ for PointOrPoint3D {}
132
133impl NewXY for Point {
136 fn new_xy(x: f64, y: f64) -> Self {
137 Self(x, y)
138 }
139}
140impl NewXY for Point3D {
141 fn new_xy(x: f64, y: f64) -> Self {
142 Self(x, y, 0.0)
143 }
144}
145impl NewXY for PointOrPoint3D {
146 fn new_xy(x: f64, y: f64) -> Self {
147 Self(x, y, None)
148 }
149}
150impl NewXYZ for Point3D {
151 fn new_xyz(x: f64, y: f64, z: f64) -> Self {
152 Self(x, y, z)
153 }
154}
155impl NewXYZ for PointOrPoint3D {
156 fn new_xyz(x: f64, y: f64, z: f64) -> Self {
157 Self(x, y, Some(z))
158 }
159}
160
161#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
163pub enum GeometryType {
164 #[default]
166 Point,
167 MultiPoint,
169 LineString,
171 MultiLineString,
173 Polygon,
175 MultiPolygon,
177 Point3D,
179 MultiPoint3D,
181 LineString3D,
183 MultiLineString3D,
185 Polygon3D,
187 MultiPolygon3D,
189}
190impl From<&str> for GeometryType {
191 fn from(s: &str) -> Self {
192 match s {
193 "Point" => GeometryType::Point,
194 "MultiPoint" => GeometryType::MultiPoint,
195 "LineString" => GeometryType::LineString,
196 "MultiLineString" => GeometryType::MultiLineString,
197 "Polygon" => GeometryType::Polygon,
198 "MultiPolygon" => GeometryType::MultiPolygon,
199 "Point3D" => GeometryType::Point3D,
200 "MultiPoint3D" => GeometryType::MultiPoint3D,
201 "LineString3D" => GeometryType::LineString3D,
202 "MultiLineString3D" => GeometryType::MultiLineString3D,
203 "Polygon3D" => GeometryType::Polygon3D,
204 "MultiPolygon3D" => GeometryType::MultiPolygon3D,
205 _ => panic!("Invalid geometry type: {}", s),
206 }
207 }
208}
209
210#[derive(Clone, Serialize, Debug, PartialEq)]
212#[serde(untagged)]
213pub enum Geometry<M: Clone + Default = MValue> {
214 Point(PointGeometry<M>),
216 MultiPoint(MultiPointGeometry<M>),
218 LineString(LineStringGeometry<M>),
220 MultiLineString(MultiLineStringGeometry<M>),
222 Polygon(PolygonGeometry<M>),
224 MultiPolygon(MultiPolygonGeometry<M>),
226 Point3D(Point3DGeometry<M>),
228 MultiPoint3D(MultiPoint3DGeometry<M>),
230 LineString3D(LineString3DGeometry<M>),
232 MultiLineString3D(MultiLineString3DGeometry<M>),
234 Polygon3D(Polygon3DGeometry<M>),
236 MultiPolygon3D(MultiPolygon3DGeometry<M>),
238}
239impl<M: Clone + Default> Default for Geometry<M> {
240 fn default() -> Self {
241 Geometry::Point(PointGeometry::<M>::default())
242 }
243}
244
245#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
247pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
248 #[serde(rename = "type")]
250 pub _type: GeometryType,
251 pub coordinates: G,
253 #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
255 pub m_values: Option<M>,
256 #[serde(skip_serializing_if = "Option::is_none")]
258 pub bbox: Option<B>,
259}
260
261pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
263pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
265pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
267pub type MultiLineStringGeometry<M = MValue> =
269 BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
270pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
272pub type MultiPolygonGeometry<M = MValue> =
274 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
275pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
277pub type MultiPoint3DGeometry<M = MValue> =
279 BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
280pub type LineString3DGeometry<M = MValue> =
282 BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
283pub type MultiLineString3DGeometry<M = MValue> =
285 BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
286pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
288pub type MultiPolygon3DGeometry<M = MValue> =
290 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;