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>);
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#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
49pub enum GeometryType {
50 #[default]
52 Point,
53 MultiPoint,
55 LineString,
57 MultiLineString,
59 Polygon,
61 MultiPolygon,
63 Point3D,
65 MultiPoint3D,
67 LineString3D,
69 MultiLineString3D,
71 Polygon3D,
73 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#[derive(Clone, Serialize, Debug, PartialEq)]
98#[serde(untagged)]
99pub enum Geometry<M: Clone + Default = MValue> {
100 Point(PointGeometry<M>),
102 MultiPoint(MultiPointGeometry<M>),
104 LineString(LineStringGeometry<M>),
106 MultiLineString(MultiLineStringGeometry<M>),
108 Polygon(PolygonGeometry<M>),
110 MultiPolygon(MultiPolygonGeometry<M>),
112 Point3D(Point3DGeometry<M>),
114 MultiPoint3D(MultiPoint3DGeometry<M>),
116 LineString3D(LineString3DGeometry<M>),
118 MultiLineString3D(MultiLineString3DGeometry<M>),
120 Polygon3D(Polygon3DGeometry<M>),
122 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#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
133pub struct BaseGeometry<M = MValue, G = Geometry<M>, B = BBOX> {
134 #[serde(rename = "type")]
136 pub _type: GeometryType,
137 pub coordinates: G,
139 #[serde(rename = "mValues", skip_serializing_if = "Option::is_none")]
141 pub m_values: Option<M>,
142 #[serde(skip_serializing_if = "Option::is_none")]
144 pub bbox: Option<B>,
145}
146
147pub type PointGeometry<M = MValue> = BaseGeometry<M, Point, BBox>;
149pub type MultiPointGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, MultiPoint, BBox>;
151pub type LineStringGeometry<M = MValue> = BaseGeometry<LineStringMValues<M>, LineString, BBox>;
153pub type MultiLineStringGeometry<M = MValue> =
155 BaseGeometry<MultiLineStringMValues<M>, MultiLineString, BBox>;
156pub type PolygonGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon, BBox>;
158pub type MultiPolygonGeometry<M = MValue> =
160 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon, BBox>;
161pub type Point3DGeometry<M = MValue> = BaseGeometry<M, Point3D, BBox3D>;
163pub type MultiPoint3DGeometry<M = MValue> =
165 BaseGeometry<LineStringMValues<M>, MultiPoint3D, BBox3D>;
166pub type LineString3DGeometry<M = MValue> =
168 BaseGeometry<LineStringMValues<M>, LineString3D, BBox3D>;
169pub type MultiLineString3DGeometry<M = MValue> =
171 BaseGeometry<MultiLineStringMValues<M>, MultiLineString3D, BBox3D>;
172pub type Polygon3DGeometry<M = MValue> = BaseGeometry<PolygonMValues<M>, Polygon3D, BBox3D>;
174pub type MultiPolygon3DGeometry<M = MValue> =
176 BaseGeometry<MultiPolygonMValues<M>, MultiPolygon3D, BBox3D>;