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
#![no_std]
#![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: WG stands for WGS84 and S2 stands for S2Geometry

extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

/// All geometry types and structs
pub mod geometry;
/// All values types and structs
pub mod values;

pub use geometry::*;
pub use values::*;

//? S2 specific type

/// Cube-face on the S2 sphere
pub type Face = u8;
/// Cube face 0
pub const FACE_0: Face = 0;
/// Cube face 1
pub const FACE_1: Face = 1;
/// Cube face 2
pub const FACE_2: Face = 2;
/// Cube face 3
pub const FACE_3: Face = 3;
/// Cube face 4
pub const FACE_4: Face = 4;
/// Cube face 5
pub const FACE_5: Face = 5;

//? FeatureCollections

/// WG FeatureCollection
#[derive(Debug, PartialEq)]
pub struct FeatureCollection {
    /// Collection of WG features
    pub features: Vec<Feature>,
    /// Attribution data
    pub attributions: Option<Attributions>,
    /// Bounding box
    pub bbox: Option<BBox>,
}

/// S2 FeatureCollection
#[derive(Debug, PartialEq)]
pub struct S2FeatureCollection {
    /// Collection of S2 features
    pub features: Vec<S2Feature>,
    /// Attribution data
    pub attributions: Option<Attributions>,
    /// Bounding box
    pub bbox: Option<BBox>,
}

//? Features

/// Component to build either an S2 or WG Feature
#[derive(Debug, PartialEq)]
pub struct Feature {
    /// Unique identifier
    pub id: Option<u64>,
    /// Properties of the feature
    pub properties: Properties,
    /// Geometry of the feature
    pub geometry: Geometry,
}

/// Component to build either an S2 or WG Feature
#[derive(Debug, PartialEq)]
pub struct S2Feature {
    /// Unique identifier
    pub id: Option<u64>,
    /// Cube-Face of the feature
    pub face: Face,
    /// Properties of the feature
    pub properties: Properties,
    /// Geometry of the feature
    pub geometry: Geometry,
}

//? 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 = BTreeMap<String, String>;

/// Either an S2 or WG FeatureCollection
pub enum FeatureCollections {
    /// An WG FeatureCollection
    FeatureCollection(FeatureCollection),
    /// An S2 FeatureCollection
    S2FeatureCollection(S2FeatureCollection),
}

/// Either an S2 or WG Feature
pub enum Features {
    /// An WG Feature
    Feature(Feature),
    /// An S2 Feature
    S2Feature(S2Feature),
}

/// All major S2JSON types
pub enum JSONCollection {
    /// An WG FeatureCollection
    FeatureCollection(FeatureCollection),
    /// An S2 FeatureCollection
    S2FeatureCollection(S2FeatureCollection),
    /// An WG Feature
    Feature(Feature),
    /// An S2 Feature
    S2Feature(S2Feature),
}