s2json-core 1.18.0

This library supports the S2JSON 1.0 Specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#![no_std]
#![feature(coverage_attribute)]
#![deny(missing_docs)]

//! The `s2json` Rust crate provides functionalities to read and write S2JSON Spec data structures.
//! This crate is a 0 dependency package that uses `no_std` and is intended to be used in
//! embedded systems and WASM applications.
//! NOTE: WM stands for WGS84 and S2 stands for S2Geometry

extern crate alloc;

/// All geometry types and structs
pub mod geometry;
/// BTreeMap wrapper
pub mod map;
/// All values types and structs
pub mod value;
/// All values impl
pub mod value_impl;
/// The VectorPoint struct is a powerful tool for 2D and 3D points
pub mod vector_point;

use alloc::{string::String, vec::Vec};
pub use geometry::*;
pub use map::*;
use serde::{Deserialize, Serialize};
pub use value::*;
pub use vector_point::*;

/// All projections that can be used
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Projection {
    /// S2
    #[default]
    S2,
    /// WG
    WG,
}

//? S2 specific type

/// Cube-face on the S2 sphere
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Face {
    /// Face 0
    #[default]
    Face0 = 0,
    /// Face 1
    Face1 = 1,
    /// Face 2
    Face2 = 2,
    /// Face 3
    Face3 = 3,
    /// Face 4
    Face4 = 4,
    /// Face 5
    Face5 = 5,
}
impl From<Face> for u8 {
    fn from(face: Face) -> Self {
        face as u8
    }
}
impl From<u8> for Face {
    fn from(face: u8) -> Self {
        match face {
            1 => Face::Face1,
            2 => Face::Face2,
            3 => Face::Face3,
            4 => Face::Face4,
            5 => Face::Face5,
            _ => Face::Face0,
        }
    }
}
impl serde::Serialize for Face {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u8(*self as u8)
    }
}

impl<'de> serde::Deserialize<'de> for Face {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = u8::deserialize(deserializer)?;
        match value {
            0 => Ok(Face::Face0),
            1 => Ok(Face::Face1),
            2 => Ok(Face::Face2),
            3 => Ok(Face::Face3),
            4 => Ok(Face::Face4),
            5 => Ok(Face::Face5),
            _ => Err(serde::de::Error::custom("Invalid face value")),
        }
    }
}

/// FeatureCollection type string
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub enum FeatureCollectionType {
    /// WM FeatureCollection
    #[default]
    FeatureCollection,
}
impl From<&str> for FeatureCollectionType {
    fn from(_: &str) -> Self {
        FeatureCollectionType::FeatureCollection
    }
}

/// FeatureCollection type string
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub enum S2FeatureCollectionType {
    /// WM FeatureCollection
    #[default]
    S2FeatureCollection,
}
impl From<&str> for S2FeatureCollectionType {
    fn from(_: &str) -> Self {
        S2FeatureCollectionType::S2FeatureCollection
    }
}

//? FeatureCollections

/// WM FeatureCollection
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct FeatureCollection<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue>
{
    /// Type will always be "FeatureCollection"
    #[serde(rename = "type")]
    pub _type: FeatureCollectionType,
    /// Collection of WM features
    pub features: Vec<WMFeature<M, P, D>>,
    /// Attribution data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributions: Option<Attributions>,
    /// Bounding box
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bbox: Option<BBox>,
}
impl<M, P: MValueCompatible, D: MValueCompatible> FeatureCollection<M, P, D> {
    /// Create a new FeatureCollection
    pub fn new(attributions: Option<Attributions>) -> Self {
        Self { _type: "FeatureCollection".into(), features: Vec::new(), attributions, bbox: None }
    }

    /// update the bounding box
    pub fn update_bbox(&mut self, bbox: BBox) {
        let mut self_bbox = self.bbox.unwrap_or_default();
        self_bbox = self_bbox.merge(&bbox);
        self.bbox = Some(self_bbox);
    }
}

/// S2 FeatureCollection
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct S2FeatureCollection<
    M = (),
    P: MValueCompatible = Properties,
    D: MValueCompatible = MValue,
> {
    /// Type will always be "S2FeatureCollection"
    #[serde(rename = "type")]
    pub _type: S2FeatureCollectionType,
    /// Collection of S2 features
    pub features: Vec<VectorFeature<M, P, D>>,
    /// Track the faces that were used to generate the features
    pub faces: Vec<Face>,
    /// Attribution data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributions: Option<Attributions>,
    /// Bounding box
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bbox: Option<BBox>,
}
impl<M, P: MValueCompatible, D: MValueCompatible> S2FeatureCollection<M, P, D> {
    /// Create a new S2FeatureCollection
    pub fn new(attributions: Option<Attributions>) -> Self {
        Self {
            _type: "S2FeatureCollection".into(),
            features: Vec::new(),
            faces: Vec::new(),
            attributions,
            bbox: None,
        }
    }

    /// update the bounding box
    pub fn update_bbox(&mut self, bbox: BBox) {
        let mut self_bbox = self.bbox.unwrap_or_default();
        self_bbox = self_bbox.merge(&bbox);
        self.bbox = Some(self_bbox);
    }

    /// Add a face, ensuring it is unique
    pub fn add_face(&mut self, face: Face) {
        if !self.faces.contains(&face) {
            self.faces.push(face);
        }
    }
}

//? Features

/// Feature type string
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub enum FeatureType {
    /// WM Feature
    #[default]
    Feature,
}
impl From<&str> for FeatureType {
    fn from(_: &str) -> Self {
        FeatureType::Feature
    }
}

/// Component to build an WM Feature
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Feature<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue> {
    /// Type will always be "Feature"
    #[serde(rename = "type")]
    pub _type: FeatureType,
    /// Unique identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<u64>,
    /// Properties of the feature
    pub properties: P,
    /// Geometry of the feature
    pub geometry: Geometry<D>,
    /// Metadata of the feature
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<M>,
}
impl<M, P: MValueCompatible, D: MValueCompatible> Feature<M, P, D> {
    /// Create a new Feature
    pub fn new(id: Option<u64>, properties: P, geometry: Geometry<D>, metadata: Option<M>) -> Self {
        Self { _type: "Feature".into(), id, properties, geometry, metadata }
    }
}
impl<M, P: MValueCompatible, D: MValueCompatible> Default for Feature<M, P, D> {
    fn default() -> Self {
        Self {
            _type: "Feature".into(),
            id: None,
            properties: Default::default(),
            geometry: Default::default(),
            metadata: None,
        }
    }
}

/// Feature type string
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub enum VectorFeatureType {
    /// WM Feature
    #[default]
    VectorFeature,
    /// S2 Feature
    S2Feature,
}
impl From<&str> for VectorFeatureType {
    fn from(s: &str) -> Self {
        match s {
            "S2Feature" => VectorFeatureType::S2Feature,
            _ => VectorFeatureType::VectorFeature,
        }
    }
}

/// Component to build an WM or S2 Vector Feature
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct VectorFeature<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue> {
    /// Type will always be "VectorFeature"
    #[serde(rename = "type")]
    pub _type: VectorFeatureType,
    /// Unique identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<u64>,
    /// Face of the feature
    pub face: Face,
    /// Properties of the feature
    pub properties: P,
    /// Geometry of the feature
    pub geometry: VectorGeometry<D>,
    /// Metadata of the feature
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<M>,
}
impl<M, P: MValueCompatible, D: MValueCompatible> Default for VectorFeature<M, P, D> {
    fn default() -> Self {
        Self {
            _type: "VectorFeature".into(),
            face: 0.into(),
            id: None,
            properties: Default::default(),
            geometry: Default::default(),
            metadata: None,
        }
    }
}
impl<M, P: MValueCompatible, D: MValueCompatible> VectorFeature<M, P, D> {
    /// Create a new VectorFeature in the WM format
    pub fn new_wm(
        id: Option<u64>,
        properties: P,
        geometry: VectorGeometry<D>,
        metadata: Option<M>,
    ) -> Self {
        Self { _type: "VectorFeature".into(), face: 0.into(), id, properties, geometry, metadata }
    }

    /// Create a new VectorFeature in the WM format
    pub fn new_s2(
        id: Option<u64>,
        face: Face,
        properties: P,
        geometry: VectorGeometry<D>,
        metadata: Option<M>,
    ) -> Self {
        Self { _type: "S2Feature".into(), face, id, properties, geometry, metadata }
    }

    /// Create a new VectorFeature using an input VectorFeature. Assign new geometry if provided
    pub fn from_vector_feature(
        feature: &VectorFeature<M, P, D>,
        geometry: Option<VectorGeometry<D>>,
    ) -> Self
    where
        M: Clone,
    {
        Self {
            _type: feature._type.clone(),
            id: feature.id,
            face: feature.face,
            properties: feature.properties.clone(),
            geometry: geometry.unwrap_or(feature.geometry.clone()),
            metadata: feature.metadata.clone(),
        }
    }
}

//? Utility types

/// Attribution data is stored in an object.
/// The key is the name of the attribution, and the value is the href link
/// e.g. { "Open S2": "https://opens2.com/legal/data" }
pub type Attributions = Map<String, String>;

/// Either an S2 or WM FeatureCollection
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum FeatureCollections<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue>
{
    /// An WM FeatureCollection
    FeatureCollection(FeatureCollection<M, P, D>),
    /// An S2 FeatureCollection
    S2FeatureCollection(S2FeatureCollection<M, P, D>),
}

/// Either an S2 or WM Feature
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Features<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue> {
    /// An WM Feature
    Feature(Feature<M, P, D>),
    /// An WM or S2 Vector Feature
    VectorFeature(VectorFeature<M, P, D>),
}

/// Either an WM Feature or an WM Vector Feature
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum WMFeature<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue> {
    /// An WM Feature
    Feature(Feature<M, P, D>),
    /// An WM Vector Feature
    VectorFeature(VectorFeature<M, P, D>),
}

/// All major S2JSON types
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum JSONCollection<M = (), P: MValueCompatible = Properties, D: MValueCompatible = MValue> {
    /// An WM FeatureCollection
    FeatureCollection(FeatureCollection<M, P, D>),
    /// An S2 FeatureCollection
    S2FeatureCollection(S2FeatureCollection<M, P, D>),
    /// An WM Feature
    Feature(Feature<M, P, D>),
    /// An WM Vector Feature
    VectorFeature(VectorFeature<M, P, D>),
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{string::ToString, vec};

    #[test]
    fn face() {
        let face = Face::Face0;
        assert_eq!(u8::from(face), 0);
        let face = Face::Face1;
        assert_eq!(u8::from(face), 1);
        let face = Face::Face2;
        assert_eq!(u8::from(face), 2);
        let face = Face::Face3;
        assert_eq!(u8::from(face), 3);
        let face = Face::Face4;
        assert_eq!(u8::from(face), 4);
        let face = Face::Face5;
        assert_eq!(u8::from(face), 5);

        assert_eq!(Face::Face0, Face::from(0));
        assert_eq!(Face::Face1, Face::from(1));
        assert_eq!(Face::Face2, Face::from(2));
        assert_eq!(Face::Face3, Face::from(3));
        assert_eq!(Face::Face4, Face::from(4));
        assert_eq!(Face::Face5, Face::from(5));
    }

    #[test]
    fn defaults() {
        let f: Feature = Default::default();
        assert_eq!(f._type, "Feature".into());
        assert_eq!(f.id, None);
        assert_eq!(f.properties, Properties::default());
        assert_eq!(f.geometry, Geometry::default());
        assert_eq!(f.metadata, None);

        let f: VectorFeature = Default::default();
        assert_eq!(f._type, "VectorFeature".into());
        assert_eq!(f.id, None);
        assert_eq!(f.face, 0.into());
        assert_eq!(f.properties, Properties::default());
        assert_eq!(f.geometry, VectorGeometry::default());
        assert_eq!(f.metadata, None);
    }

    #[test]
    fn feature_collection_new() {
        let mut attributions = Attributions::new();
        attributions.insert("Open S2".to_string(), "https://opens2.com/legal/data".to_string());
        let mut fc = FeatureCollection::<()>::new(Some(attributions.clone()));
        assert_eq!(fc._type, FeatureCollectionType::FeatureCollection);
        assert_eq!(fc.features.len(), 0);
        assert_eq!(fc.attributions, Some(attributions.clone()));
        // update_bbox
        fc.update_bbox(BBox::new(5., -2., 35., 2.2));
        assert_eq!(fc.bbox, Some(BBox::new(5., -2., 35., 2.2)));

        let string = serde_json::to_string(&fc).unwrap();
        assert_eq!(string, "{\"type\":\"FeatureCollection\",\"features\":[],\"attributions\":{\"Open S2\":\"https://opens2.com/legal/data\"},\"bbox\":[5.0,-2.0,35.0,2.2]}");
        let back_to_fc: FeatureCollection = serde_json::from_str(&string).unwrap();
        assert_eq!(back_to_fc, fc);
    }

    #[test]
    fn s2_feature_collection_new() {
        let mut attributions = Attributions::new();
        attributions.insert("Open S2".to_string(), "https://opens2.com/legal/data".to_string());
        let mut fc = S2FeatureCollection::new(Some(attributions.clone()));
        assert_eq!(fc._type, S2FeatureCollectionType::S2FeatureCollection);
        assert_eq!(fc.features.len(), 0);
        assert_eq!(fc.attributions, Some(attributions.clone()));
        // update_bbox
        fc.update_bbox(BBox::new(5., -2., 35., 2.2));
        assert_eq!(fc.bbox, Some(BBox::new(5., -2., 35., 2.2)));
        // add face
        fc.add_face(0.into());
        fc.add_face(3.into());
        assert_eq!(fc.faces, vec![0.into(), 3.into()]);

        let string = serde_json::to_string(&fc).unwrap();
        assert_eq!(string, "{\"type\":\"S2FeatureCollection\",\"features\":[],\"faces\":[0,3],\"attributions\":{\"Open S2\":\"https://opens2.com/legal/data\"},\"bbox\":[5.0,-2.0,35.0,2.2]}");
        let back_to_fc: S2FeatureCollection = serde_json::from_str(&string).unwrap();
        assert_eq!(back_to_fc, fc);
    }

    #[test]
    fn feature_new() {
        let fc: Feature = Feature::new(
            Some(22),
            Properties::new(),
            Geometry::Point(PointGeometry {
                _type: "Point".into(),
                coordinates: Point(0.0, 0.0),
                m_values: None,
                bbox: None,
            }),
            None,
        );
        assert_eq!(fc.id, Some(22));
        assert_eq!(fc._type, "Feature".into());
        assert_eq!(
            fc.geometry,
            Geometry::Point(PointGeometry {
                _type: "Point".into(),
                coordinates: Point(0.0, 0.0),
                m_values: None,
                bbox: None,
            })
        );
        assert_eq!(fc.properties, Properties::new());
        assert_eq!(fc.metadata, None);
    }

    #[test]
    fn s2_feature_new() {
        let fc: VectorFeature = VectorFeature::new_wm(
            Some(55),
            Properties::new(),
            VectorGeometry::Point(VectorPointGeometry {
                _type: "Point".into(),
                coordinates: VectorPoint { x: 0.0, y: 1.0, z: Some(3.), m: None, t: None },
                bbox: None,
                is_3d: true,
                offset: None,
                vec_bbox: None,
                indices: None,
                tesselation: None,
            }),
            None,
        );
        assert_eq!(fc.id, Some(55));
        assert_eq!(fc._type, "VectorFeature".into());
        assert_eq!(
            fc.geometry,
            VectorGeometry::Point(VectorPointGeometry {
                _type: "Point".into(),
                coordinates: VectorPoint { x: 0.0, y: 1.0, z: Some(3.), m: None, t: None },
                bbox: None,
                is_3d: true,
                offset: None,
                vec_bbox: None,
                indices: None,
                tesselation: None,
            })
        );
        assert_eq!(fc.properties, Properties::new());
        assert_eq!(fc.metadata, None);
        assert_eq!(fc.face, 0.into());

        // S2

        #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
        struct MetaTest {
            name: String,
            value: String,
        }

        let fc = VectorFeature::<MetaTest>::new_s2(
            Some(55),
            3.into(),
            Properties::new(),
            VectorGeometry::Point(VectorPointGeometry {
                _type: "Point".into(),
                coordinates: VectorPoint { x: 0.0, y: 1.0, z: Some(3.), m: None, t: None },
                bbox: None,
                is_3d: true,
                offset: None,
                vec_bbox: None,
                indices: None,
                tesselation: None,
            }),
            Some(MetaTest { name: "test".to_string(), value: "value".to_string() }),
        );
        assert_eq!(fc.id, Some(55));
        assert_eq!(fc._type, "S2Feature".into());
        assert_eq!(
            fc.geometry,
            VectorGeometry::Point(VectorPointGeometry {
                _type: "Point".into(),
                coordinates: VectorPoint { x: 0.0, y: 1.0, z: Some(3.), m: None, t: None },
                bbox: None,
                is_3d: true,
                offset: None,
                vec_bbox: None,
                indices: None,
                tesselation: None,
            })
        );
        assert_eq!(fc.properties, Properties::new());
        assert_eq!(
            fc.metadata,
            Some(MetaTest { name: "test".to_string(), value: "value".to_string() })
        );
        assert_eq!(fc.face, 3.into());

        let fc_to_str = serde_json::to_string(&fc).unwrap();
        assert_eq!(
            fc_to_str,
            "{\"type\":\"S2Feature\",\"id\":55,\"face\":3,\"properties\":{},\"geometry\":{\"type\"\
             :\"Point\",\"is3D\":true,\"coordinates\":{\"x\":0.0,\"y\":1.0,\"z\":3.0},\"indices\":\
             null,\"tesselation\":null},\"metadata\":{\"name\":\"test\",\"value\":\"value\"}}"
        );

        // from_vector_feature

        let new_geo = VectorGeometry::Point(VectorPointGeometry {
            _type: "Point".into(),
            coordinates: VectorPoint { x: 5.0, y: 4.0, z: Some(-3.), m: None, t: None },
            bbox: None,
            is_3d: true,
            offset: None,
            vec_bbox: None,
            indices: None,
            tesselation: None,
        });
        let fc_clone_new_geometry =
            VectorFeature::<MetaTest>::from_vector_feature(&fc, Some(new_geo.clone()));

        assert_eq!(fc_clone_new_geometry.geometry, new_geo);
    }

    #[test]
    fn parse_feature_multipoint() {
        let json_string = r#"{
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "MultiPoint",
                "coordinates": [
                    [-13.292352825505162, 54.34883408204476],
                    [36.83102287804303, 59.56941785818924],
                    [50.34083898563978, 16.040052775278994],
                    [76.38149901912357, 35.155968522292056]
                ]
            }
        }"#;

        let feature: Feature = serde_json::from_str(json_string).unwrap();
        assert_eq!(feature._type, "Feature".into());
        assert_eq!(
            feature.geometry,
            Geometry::MultiPoint(MultiPointGeometry {
                _type: "MultiPoint".into(),
                coordinates: vec![
                    Point(-13.292352825505162, 54.34883408204476),
                    Point(36.83102287804303, 59.56941785818924),
                    Point(50.34083898563978, 16.040052775278994),
                    Point(76.38149901912357, 35.155968522292056),
                ],
                ..Default::default()
            })
        );

        let back_to_str = serde_json::to_string(&feature).unwrap();
        assert_eq!(
            back_to_str,
            "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"MultiPoint\",\"\
             coordinates\":[[-13.292352825505162,54.34883408204476],[36.83102287804303,59.\
             56941785818924],[50.34083898563978,16.040052775278994],[76.38149901912357,35.\
             155968522292056]]}}"
        );
    }

    #[test]
    fn parse_feature_linestring() {
        let json_string = r#"{
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [-13.292352825505162, 54.34883408204476],
                    [36.83102287804303, 59.56941785818924],
                    [50.34083898563978, 16.040052775278994],
                    [76.38149901912357, 35.155968522292056]
                ]
            }
        }"#;

        let feature: Feature = serde_json::from_str(json_string).unwrap();
        assert_eq!(feature._type, "Feature".into());
        assert_eq!(
            feature.geometry,
            Geometry::LineString(LineStringGeometry {
                _type: "LineString".into(),
                coordinates: vec![
                    Point(-13.292352825505162, 54.34883408204476),
                    Point(36.83102287804303, 59.56941785818924),
                    Point(50.34083898563978, 16.040052775278994),
                    Point(76.38149901912357, 35.155968522292056),
                ],
                ..Default::default()
            })
        );

        let back_to_str = serde_json::to_string(&feature).unwrap();
        assert_eq!(
            back_to_str,
            "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"LineString\",\"\
             coordinates\":[[-13.292352825505162,54.34883408204476],[36.83102287804303,59.\
             56941785818924],[50.34083898563978,16.040052775278994],[76.38149901912357,35.\
             155968522292056]]}}"
        );
    }

    #[test]
    fn parse_vector_feature_linestring() {
        let json_string = r#"{
            "type": "VectorFeature",
            "face": 0,
            "properties": {},
            "geometry": {
                "type": "LineString",
                "is3D": false,
                "coordinates": [
                    { "x": -13.292352825505162, "y": 54.34883408204476 },
                    { "x": 36.83102287804303, "y": 59.56941785818924 },
                    { "x": 50.34083898563978, "y": 16.040052775278994 },
                    { "x": 76.38149901912357, "y": 35.155968522292056 }
                ]
            }
        }"#;

        let feature: VectorFeature = serde_json::from_str(json_string).unwrap();
        assert_eq!(feature._type, "VectorFeature".into());
        let geometry = feature.geometry;
        assert_eq!(
            geometry,
            VectorGeometry::LineString(VectorLineStringGeometry {
                _type: VectorGeometryType::LineString,
                is_3d: false,
                coordinates: vec![
                    VectorPoint::from_xy(-13.292352825505162, 54.34883408204476),
                    VectorPoint::from_xy(36.83102287804303, 59.56941785818924),
                    VectorPoint::from_xy(50.34083898563978, 16.040052775278994),
                    VectorPoint::from_xy(76.38149901912357, 35.155968522292056),
                ],
                ..Default::default()
            })
        )
    }

    #[test]
    fn parse_vector_feature_multipoint() {
        let json_string = r#"{
            "type": "VectorFeature",
            "face": 0,
            "properties": {},
            "geometry": {
                "type": "MultiPoint",
                "is3D": false,
                "coordinates": [
                    { "x": -13.292352825505162, "y": 54.34883408204476 },
                    { "x": 36.83102287804303, "y": 59.56941785818924 },
                    { "x": 50.34083898563978, "y": 16.040052775278994 },
                    { "x": 76.38149901912357, "y": 35.155968522292056 }
                ]
            }
        }"#;

        let feature: VectorFeature = serde_json::from_str(json_string).unwrap();
        assert_eq!(feature._type, "VectorFeature".into());
        let geometry = feature.geometry;
        assert_eq!(
            geometry,
            VectorGeometry::MultiPoint(VectorMultiPointGeometry {
                _type: VectorGeometryType::MultiPoint,
                is_3d: false,
                coordinates: vec![
                    VectorPoint::from_xy(-13.292352825505162, 54.34883408204476),
                    VectorPoint::from_xy(36.83102287804303, 59.56941785818924),
                    VectorPoint::from_xy(50.34083898563978, 16.040052775278994),
                    VectorPoint::from_xy(76.38149901912357, 35.155968522292056),
                ],
                ..Default::default()
            })
        )
    }

    #[test]
    fn serde_face() {
        let face_0 = Face::Face0;
        let serialized = serde_json::to_string(&face_0).unwrap();
        assert_eq!(serialized, "0");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face0);

        let face_1 = Face::Face1;
        let serialized = serde_json::to_string(&face_1).unwrap();
        assert_eq!(serialized, "1");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face1);

        let face_2 = Face::Face2;
        let serialized = serde_json::to_string(&face_2).unwrap();
        assert_eq!(serialized, "2");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face2);

        let face_3 = Face::Face3;
        let serialized = serde_json::to_string(&face_3).unwrap();
        assert_eq!(serialized, "3");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face3);

        let face_4 = Face::Face4;
        let serialized = serde_json::to_string(&face_4).unwrap();
        assert_eq!(serialized, "4");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face4);

        let face_5 = Face::Face5;
        let serialized = serde_json::to_string(&face_5).unwrap();
        assert_eq!(serialized, "5");
        let deserialize = serde_json::from_str::<Face>(&serialized).unwrap();
        assert_eq!(deserialize, Face::Face5);
    }

    #[test]
    #[should_panic(expected = "Invalid face value")]
    fn serde_face_err() {
        let _ = serde_json::from_str::<Face>("6").unwrap();
    }
}