Repository navigation
Expand file tree
/
Copy pathtypes.rs
More file actions
139 lines (121 loc) · 3.46 KB
/
Copy pathtypes.rs
File metadata and controls
139 lines (121 loc) · 3.46 KB
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
use std::collections::HashMap;
use std::fmt;
pub use datapod::{
Euler as Heading, Geo, Linestring as Path3, Point as Point3, Polygon as Polygon3,
Segment as Segment3,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Crs {
Wgs,
Enu,
}
impl Crs {
pub fn parse(value: &str) -> crate::Result<Self> {
match value {
"EPSG:4326" | "WGS84" | "WGS" => Ok(Self::Wgs),
"ENU" | "ECEF" => Ok(Self::Enu),
other => Err(crate::Error::InvalidGeoJson(format!(
"Unknown CRS string: {other}"
))),
}
}
pub const fn as_geojson_name(self) -> &'static str {
match self {
Self::Wgs => "EPSG:4326",
Self::Enu => "ENU",
}
}
}
/// Geometry variants. Each owns its data inline — `Path` wraps a
/// `Linestring` (which owns its `Vec<Point>`), `Polygon` wraps a
/// `datapod::Polygon` (likewise). `Point` and `Segment` are fixed-Pod.
#[derive(Debug, Clone, PartialEq)]
pub enum Geometry {
Point(Point3),
Segment(Segment3),
Path(Path3),
Polygon(Polygon3),
}
impl Geometry {
/// Construct a `Path` variant from a vertex buffer.
pub fn path(vertices: Vec<Point3>) -> Self {
Self::Path(Path3::new(vertices))
}
/// Construct a `Polygon` variant from a vertex buffer.
pub fn polygon(vertices: Vec<Point3>) -> Self {
Self::Polygon(Polygon3::new(vertices))
}
}
impl From<Point3> for Geometry {
fn from(value: Point3) -> Self {
Self::Point(value)
}
}
impl From<Segment3> for Geometry {
fn from(value: Segment3) -> Self {
Self::Segment(value)
}
}
impl From<Path3> for Geometry {
fn from(value: Path3) -> Self {
Self::Path(value)
}
}
impl From<Polygon3> for Geometry {
fn from(value: Polygon3) -> Self {
Self::Polygon(value)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Feature {
pub geometry: Geometry,
pub properties: HashMap<String, String>,
}
impl Feature {
pub fn new(geometry: impl Into<Geometry>, properties: HashMap<String, String>) -> Self {
Self {
geometry: geometry.into(),
properties,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FeatureCollection {
pub datum: Geo,
pub heading: Heading,
pub features: Vec<Feature>,
pub global_properties: HashMap<String, String>,
}
impl FeatureCollection {
pub fn new(datum: Geo, heading: Heading) -> Self {
Self {
datum,
heading,
features: Vec::new(),
global_properties: HashMap::new(),
}
}
}
impl fmt::Display for FeatureCollection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"DATUM: {}, {}, {}",
self.datum.latitude, self.datum.longitude, self.datum.altitude
)?;
writeln!(f, "HEADING: {}", self.heading.yaw)?;
writeln!(f, "FEATURES: {}", self.features.len())?;
for feature in &self.features {
match &feature.geometry {
Geometry::Polygon(_) => writeln!(f, " POLYGON")?,
Geometry::Segment(_) => writeln!(f, " LINE")?,
Geometry::Path(_) => writeln!(f, " PATH")?,
Geometry::Point(_) => writeln!(f, " POINT")?,
}
if !feature.properties.is_empty() {
writeln!(f, " PROPS:{}", feature.properties.len())?;
}
}
Ok(())
}
}