1#![no_std]
2#![forbid(unsafe_code)]
3#![deny(missing_docs)]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5
6extern crate alloc;
13
14pub mod geometry;
16pub mod impls;
18pub mod map;
20pub mod shape;
22pub mod value;
24
25use alloc::{string::String, vec::Vec};
26pub use geometry::*;
27pub use impls::*;
28pub use map::*;
29use serde::{Deserialize, Serialize};
30pub use shape::*;
31pub use value::*;
32
33#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
35pub enum Projection {
36 #[default]
38 S2,
39 WG,
41}
42
43#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
47pub enum Face {
48 #[default]
50 Face0 = 0,
51 Face1 = 1,
53 Face2 = 2,
55 Face3 = 3,
57 Face4 = 4,
59 Face5 = 5,
61}
62impl From<Face> for u8 {
63 fn from(face: Face) -> Self {
64 face as u8
65 }
66}
67impl From<u8> for Face {
68 fn from(face: u8) -> Self {
69 match face {
70 1 => Face::Face1,
71 2 => Face::Face2,
72 3 => Face::Face3,
73 4 => Face::Face4,
74 5 => Face::Face5,
75 _ => Face::Face0,
76 }
77 }
78}
79impl serde::Serialize for Face {
80 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
81 where
82 S: serde::Serializer,
83 {
84 serializer.serialize_u8(*self as u8)
85 }
86}
87
88impl<'de> serde::Deserialize<'de> for Face {
89 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
90 where
91 D: serde::Deserializer<'de>,
92 {
93 let value = u8::deserialize(deserializer)?;
94 match value {
95 0 => Ok(Face::Face0),
96 1 => Ok(Face::Face1),
97 2 => Ok(Face::Face2),
98 3 => Ok(Face::Face3),
99 4 => Ok(Face::Face4),
100 5 => Ok(Face::Face5),
101 _ => Err(serde::de::Error::custom("Invalid face value")),
102 }
103 }
104}
105
106#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
108pub enum FeatureCollectionType {
109 #[default]
111 FeatureCollection,
112}
113impl From<&str> for FeatureCollectionType {
114 fn from(_: &str) -> Self {
115 FeatureCollectionType::FeatureCollection
116 }
117}
118
119#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
121pub enum S2FeatureCollectionType {
122 #[default]
124 S2FeatureCollection,
125}
126impl From<&str> for S2FeatureCollectionType {
127 fn from(_: &str) -> Self {
128 S2FeatureCollectionType::S2FeatureCollection
129 }
130}
131
132#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
136pub struct FeatureCollection<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
137 #[serde(rename = "type")]
139 pub _type: FeatureCollectionType,
140 pub features: Vec<Features<M, P, D>>,
142 #[serde(skip_serializing_if = "Option::is_none")]
144 pub attributions: Option<Attributions>,
145 #[serde(skip_serializing_if = "Option::is_none")]
147 pub bbox: Option<BBox>,
148}
149impl<M, P: Clone + Default, D: Clone + Default> FeatureCollection<M, P, D> {
150 pub fn new(attributions: Option<Attributions>) -> Self {
152 Self { _type: "FeatureCollection".into(), features: Vec::new(), attributions, bbox: None }
153 }
154
155 pub fn update_bbox(&mut self, bbox: BBox) {
157 let mut self_bbox = self.bbox.unwrap_or_default();
158 self_bbox = self_bbox.merge(&bbox);
159 self.bbox = Some(self_bbox);
160 }
161}
162
163#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
165pub struct S2FeatureCollection<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue>
166{
167 #[serde(rename = "type")]
169 pub _type: S2FeatureCollectionType,
170 pub features: Vec<VectorFeature<M, P, D>>,
172 pub faces: Vec<Face>,
174 #[serde(skip_serializing_if = "Option::is_none")]
176 pub attributions: Option<Attributions>,
177 #[serde(skip_serializing_if = "Option::is_none")]
179 pub bbox: Option<BBox>,
180}
181impl<M, P: Clone + Default, D: Clone + Default> S2FeatureCollection<M, P, D> {
182 pub fn new(attributions: Option<Attributions>) -> Self {
184 Self {
185 _type: "S2FeatureCollection".into(),
186 features: Vec::new(),
187 faces: Vec::new(),
188 attributions,
189 bbox: None,
190 }
191 }
192
193 pub fn update_bbox(&mut self, bbox: BBox) {
195 let mut self_bbox = self.bbox.unwrap_or_default();
196 self_bbox = self_bbox.merge(&bbox);
197 self.bbox = Some(self_bbox);
198 }
199
200 pub fn add_face(&mut self, face: Face) {
202 if !self.faces.contains(&face) {
203 self.faces.push(face);
204 }
205 }
206}
207
208#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
212pub enum FeatureType {
213 #[default]
215 Feature,
216}
217impl From<&str> for FeatureType {
218 fn from(_: &str) -> Self {
219 FeatureType::Feature
220 }
221}
222
223#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
225pub struct Feature<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
226 #[serde(rename = "type")]
228 pub _type: FeatureType,
229 #[serde(skip_serializing_if = "Option::is_none")]
231 pub id: Option<u64>,
232 #[serde(default)]
234 pub properties: P,
235 pub geometry: Geometry<D>,
237 #[serde(skip_serializing_if = "Option::is_none")]
239 pub metadata: Option<M>,
240}
241impl<M, P: Clone + Default, D: Clone + Default> Feature<M, P, D> {
242 pub fn new(id: Option<u64>, properties: P, geometry: Geometry<D>, metadata: Option<M>) -> Self {
244 Self { _type: "Feature".into(), id, properties, geometry, metadata }
245 }
246}
247impl<M, P: Clone + Default, D: Clone + Default> Default for Feature<M, P, D> {
248 fn default() -> Self {
249 Self {
250 _type: "Feature".into(),
251 id: None,
252 properties: Default::default(),
253 geometry: Default::default(),
254 metadata: None,
255 }
256 }
257}
258
259#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
261pub enum VectorFeatureType {
262 #[default]
264 VectorFeature,
265 S2Feature,
267}
268impl From<&str> for VectorFeatureType {
269 fn from(s: &str) -> Self {
270 match s {
271 "S2Feature" => VectorFeatureType::S2Feature,
272 _ => VectorFeatureType::VectorFeature,
273 }
274 }
275}
276
277#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
279pub struct VectorFeature<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
280 #[serde(rename = "type")]
282 pub _type: VectorFeatureType,
283 #[serde(skip_serializing_if = "Option::is_none")]
285 pub id: Option<u64>,
286 #[serde(default)]
288 pub face: Face,
289 #[serde(default)]
291 pub properties: P,
292 pub geometry: VectorGeometry<D>,
294 #[serde(skip_serializing_if = "Option::is_none")]
296 pub metadata: Option<M>,
297}
298impl<M, P: Clone + Default, D: Clone + Default> Default for VectorFeature<M, P, D> {
299 fn default() -> Self {
300 Self {
301 _type: "VectorFeature".into(),
302 face: 0.into(),
303 id: None,
304 properties: Default::default(),
305 geometry: Default::default(),
306 metadata: None,
307 }
308 }
309}
310impl<M, P: Clone + Default, D: Clone + Default> VectorFeature<M, P, D> {
311 pub fn new_wm(
313 id: Option<u64>,
314 properties: P,
315 geometry: VectorGeometry<D>,
316 metadata: Option<M>,
317 ) -> Self {
318 Self { _type: "VectorFeature".into(), face: 0.into(), id, properties, geometry, metadata }
319 }
320
321 pub fn new_s2(
323 id: Option<u64>,
324 face: Face,
325 properties: P,
326 geometry: VectorGeometry<D>,
327 metadata: Option<M>,
328 ) -> Self {
329 Self { _type: "S2Feature".into(), face, id, properties, geometry, metadata }
330 }
331
332 pub fn from_vector_feature(
334 feature: &VectorFeature<M, P, D>,
335 geometry: Option<VectorGeometry<D>>,
336 ) -> Self
337 where
338 M: Clone,
339 {
340 Self {
341 _type: feature._type.clone(),
342 id: feature.id,
343 face: feature.face,
344 properties: feature.properties.clone(),
345 geometry: geometry.unwrap_or(feature.geometry.clone()),
346 metadata: feature.metadata.clone(),
347 }
348 }
349
350 pub fn to_m_vector_feature<M2>(
353 &self,
354 to_meta: impl FnOnce(Option<&M>) -> Option<M2>,
355 ) -> VectorFeature<M2, Properties, MValue>
356 where
357 M: Clone,
358 P: MValueCompatible,
359 D: MValueCompatible,
360 {
361 VectorFeature {
362 _type: self._type.clone(),
363 id: self.id,
364 face: self.face,
365 properties: self.properties.clone().into(),
366 geometry: self.geometry.to_m_geometry(),
367 metadata: to_meta(self.metadata.as_ref()),
368 }
369 }
370}
371
372pub type Attributions = Map<String, String>;
378
379#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
381#[serde(untagged)]
382pub enum FeatureCollections<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
383 FeatureCollection(FeatureCollection<M, P, D>),
385 S2FeatureCollection(S2FeatureCollection<M, P, D>),
387}
388
389#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
391#[serde(untagged)]
392pub enum Features<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
393 Feature(Feature<M, P, D>),
395 VectorFeature(VectorFeature<M, P, D>),
397}
398
399#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
401#[serde(untagged)]
402pub enum JSONCollection<M = (), P: Clone + Default = Properties, D: Clone + Default = MValue> {
403 FeatureCollection(FeatureCollection<M, P, D>),
405 S2FeatureCollection(S2FeatureCollection<M, P, D>),
407 Feature(Feature<M, P, D>),
409 VectorFeature(VectorFeature<M, P, D>),
411}