1use crate::*;
2use alloc::vec::Vec;
3use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)]
7pub enum VectorGeometryType {
8 #[default]
10 Point,
11 MultiPoint,
13 LineString,
15 MultiLineString,
17 Polygon,
19 MultiPolygon,
21}
22impl From<&str> for VectorGeometryType {
23 fn from(s: &str) -> Self {
24 match s {
25 "Point" => VectorGeometryType::Point,
26 "MultiPoint" => VectorGeometryType::MultiPoint,
27 "LineString" => VectorGeometryType::LineString,
28 "MultiLineString" => VectorGeometryType::MultiLineString,
29 "Polygon" => VectorGeometryType::Polygon,
30 "MultiPolygon" => VectorGeometryType::MultiPolygon,
31 _ => panic!("Invalid vector geometry type: {}", s),
32 }
33 }
34}
35
36pub type VectorMultiPoint<M = MValue> = Vec<VectorPoint<M>>;
38pub type VectorLineString<M = MValue> = Vec<VectorPoint<M>>;
40pub type VectorMultiLineString<M = MValue> = Vec<VectorLineString<M>>;
42pub type VectorPolygon<M = MValue> = Vec<VectorLineString<M>>;
44pub type VectorMultiPolygon<M = MValue> = Vec<VectorPolygon<M>>;
46
47#[derive(Clone, Serialize, Debug, PartialEq)]
49#[serde(untagged)]
50pub enum VectorGeometry<M: MValueCompatible = MValue> {
51 Point(VectorPointGeometry<M>),
53 MultiPoint(VectorMultiPointGeometry<M>),
55 LineString(VectorLineStringGeometry<M>),
57 MultiLineString(VectorMultiLineStringGeometry<M>),
59 Polygon(VectorPolygonGeometry<M>),
61 MultiPolygon(VectorMultiPolygonGeometry<M>),
63}
64#[doc(hidden)]
65#[allow(unused_extern_crates, clippy::useless_attribute)]
66extern crate serde as _serde;
67#[automatically_derived]
68#[coverage(off)]
69impl<'de, M: MValueCompatible> _serde::Deserialize<'de> for VectorGeometry<M>
70where
71 M: _serde::Deserialize<'de>,
72{
73 fn deserialize<__D>(__deserializer: __D) -> _serde::__private::Result<Self, __D::Error>
74 where
75 __D: _serde::Deserializer<'de>,
76 {
77 let __content =
78 <_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer)?;
79 let __deserializer =
80 _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
81 if let _serde::__private::Ok(__ok) = _serde::__private::Result::map(
82 <VectorPointGeometry<M> as _serde::Deserialize>::deserialize(__deserializer),
83 VectorGeometry::Point,
84 ) {
85 return _serde::__private::Ok(__ok);
86 }
87 if let _serde::__private::Ok(__ok) = _serde::__private::Result::map(
89 <VectorMultiPointGeometry<M> as _serde::Deserialize>::deserialize(__deserializer),
90 VectorGeometry::MultiPoint,
91 ) {
92 if let VectorGeometry::MultiPoint(multipoint) = &__ok {
94 if multipoint._type == VectorGeometryType::LineString {
95 if let _serde::__private::Ok(__ok2) = _serde::__private::Result::map(
97 <VectorLineStringGeometry<M> as _serde::Deserialize>::deserialize(
98 __deserializer,
99 ),
100 VectorGeometry::LineString,
101 ) {
102 return _serde::__private::Ok(__ok2);
104 }
105 }
106 }
107 return _serde::__private::Ok(__ok);
108 }
109 if let _serde::__private::Ok(__ok) = _serde::__private::Result::map(
111 <VectorMultiLineStringGeometry<M> as _serde::Deserialize>::deserialize(__deserializer),
112 VectorGeometry::MultiLineString,
113 ) {
114 if let VectorGeometry::MultiLineString(multilinestring) = &__ok {
116 if multilinestring._type == VectorGeometryType::Polygon {
117 if let _serde::__private::Ok(__ok2) = _serde::__private::Result::map(
119 <VectorPolygonGeometry<M> as _serde::Deserialize>::deserialize(
120 __deserializer,
121 ),
122 VectorGeometry::Polygon,
123 ) {
124 return _serde::__private::Ok(__ok2);
126 }
127 }
128 }
129 return _serde::__private::Ok(__ok);
130 }
131 if let _serde::__private::Ok(__ok) = _serde::__private::Result::map(
132 <VectorMultiPolygonGeometry<M> as _serde::Deserialize>::deserialize(__deserializer),
133 VectorGeometry::MultiPolygon,
134 ) {
135 return _serde::__private::Ok(__ok);
136 }
137 _serde::__private::Err(_serde::de::Error::custom(
138 "data did not match any variant of untagged enum VectorGeometry",
139 ))
140 }
141}
142impl<M: MValueCompatible> VectorGeometry<M> {
143 pub fn bbox(&self) -> &Option<BBox3D> {
145 match self {
146 VectorGeometry::Point(g) => &g.bbox,
147 VectorGeometry::MultiPoint(g) => &g.bbox,
148 VectorGeometry::LineString(g) => &g.bbox,
149 VectorGeometry::MultiLineString(g) => &g.bbox,
150 VectorGeometry::Polygon(g) => &g.bbox,
151 VectorGeometry::MultiPolygon(g) => &g.bbox,
152 }
153 }
154
155 pub fn vec_bbox(&self) -> &Option<BBox3D> {
157 match self {
158 VectorGeometry::Point(g) => &g.vec_bbox,
159 VectorGeometry::MultiPoint(g) => &g.vec_bbox,
160 VectorGeometry::LineString(g) => &g.vec_bbox,
161 VectorGeometry::MultiLineString(g) => &g.vec_bbox,
162 VectorGeometry::Polygon(g) => &g.vec_bbox,
163 VectorGeometry::MultiPolygon(g) => &g.vec_bbox,
164 }
165 }
166
167 pub fn new_point(coordinates: VectorPoint<M>, bbox: Option<BBox3D>) -> Self {
169 VectorGeometry::Point(VectorPointGeometry {
170 _type: VectorGeometryType::Point,
171 is_3d: coordinates.z.is_some(),
172 coordinates,
173 bbox,
174 ..Default::default()
175 })
176 }
177
178 pub fn new_multipoint(coordinates: VectorMultiPoint<M>, bbox: Option<BBox3D>) -> Self {
180 VectorGeometry::MultiPoint(VectorMultiPointGeometry {
181 _type: VectorGeometryType::MultiPoint,
182 is_3d: coordinates[0].z.is_some(),
183 coordinates,
184 bbox,
185 ..Default::default()
186 })
187 }
188
189 pub fn new_linestring(coordinates: VectorLineString<M>, bbox: Option<BBox3D>) -> Self {
191 VectorGeometry::LineString(VectorLineStringGeometry {
192 _type: VectorGeometryType::LineString,
193 is_3d: coordinates[0].z.is_some(),
194 coordinates,
195 bbox,
196 ..Default::default()
197 })
198 }
199
200 pub fn new_multilinestring(
202 coordinates: VectorMultiLineString<M>,
203 bbox: Option<BBox3D>,
204 ) -> Self {
205 VectorGeometry::MultiLineString(VectorMultiLineStringGeometry {
206 _type: VectorGeometryType::MultiLineString,
207 is_3d: coordinates[0][0].z.is_some(),
208 coordinates,
209 bbox,
210 ..Default::default()
211 })
212 }
213
214 pub fn new_polygon(coordinates: VectorPolygon<M>, bbox: Option<BBox3D>) -> Self {
216 VectorGeometry::Polygon(VectorPolygonGeometry {
217 _type: VectorGeometryType::Polygon,
218 is_3d: coordinates[0][0].z.is_some(),
219 coordinates,
220 bbox,
221 ..Default::default()
222 })
223 }
224
225 pub fn new_multipolygon(coordinates: VectorMultiPolygon<M>, bbox: Option<BBox3D>) -> Self {
227 VectorGeometry::MultiPolygon(VectorMultiPolygonGeometry {
228 _type: VectorGeometryType::MultiPolygon,
229 is_3d: coordinates[0][0][0].z.is_some(),
230 coordinates,
231 bbox,
232 ..Default::default()
233 })
234 }
235}
236impl<M: MValueCompatible> Default for VectorGeometry<M> {
237 fn default() -> Self {
238 VectorGeometry::Point(VectorPointGeometry::default())
239 }
240}
241
242#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
244pub struct VectorBaseGeometry<G = VectorGeometry, O = VectorOffsets> {
245 #[serde(rename = "type")]
247 pub _type: VectorGeometryType,
248 #[serde(rename = "is3D")]
250 pub is_3d: bool,
251 pub coordinates: G,
253 #[serde(skip_serializing_if = "Option::is_none")]
255 pub offset: Option<O>,
256 #[serde(skip_serializing_if = "Option::is_none")]
258 pub bbox: Option<BBox3D>,
259 #[serde(skip)]
261 pub vec_bbox: Option<BBox3D>,
262 pub indices: Option<Vec<u32>>,
264 pub tesselation: Option<f64>,
266}
267
268#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
270pub enum VectorOffsets {
271 LineOffset(VectorLineOffset),
273 MultiLineOffset(VectorMultiLineOffset),
275 PolygonOffset(VectorPolygonOffset),
277 MultiPolygonOffset(VectorMultiPolygonOffset),
279}
280impl Default for VectorOffsets {
281 fn default() -> Self {
282 VectorOffsets::LineOffset(0.0)
283 }
284}
285pub type VectorLineOffset = f64;
287pub type VectorMultiLineOffset = Vec<VectorLineOffset>;
289pub type VectorPolygonOffset = VectorMultiLineOffset;
291pub type VectorMultiPolygonOffset = Vec<VectorPolygonOffset>;
293
294pub type VectorPointGeometry<M = MValue> = VectorBaseGeometry<VectorPoint<M>>;
296pub type VectorMultiPointGeometry<M = MValue> =
298 VectorBaseGeometry<VectorMultiPoint<M>, VectorLineOffset>;
299pub type VectorLineStringGeometry<M = MValue> =
301 VectorBaseGeometry<VectorLineString<M>, VectorLineOffset>;
302pub type VectorMultiLineStringGeometry<M = MValue> =
304 VectorBaseGeometry<VectorMultiLineString<M>, VectorMultiLineOffset>;
305pub type VectorPolygonGeometry<M = MValue> =
307 VectorBaseGeometry<VectorPolygon<M>, VectorPolygonOffset>;
308pub type VectorMultiPolygonGeometry<M = MValue> =
310 VectorBaseGeometry<VectorMultiPolygon<M>, VectorMultiPolygonOffset>;