-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
shared.rs
179 lines (147 loc) · 4.72 KB
/
shared.rs
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
//! Shared functionality for geo operations.
// Statics.
use std::ops::Deref;
use geo::{Geometry, SimplifyVwPreserve};
use geojson::GeoJson;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::base::types::Float;
// Constants.
#[cfg(not(feature = "extrasimplified"))]
const SIMPLIFICATION_EPSILON: Float = 0.0001;
#[cfg(feature = "extrasimplified")]
const SIMPLIFICATION_EPSILON: Float = 0.01;
// Types.
/// A rounded integer.
pub type RoundInt = i16;
/// A rounded longitude and latitude.
pub type RoundLngLat = (RoundInt, RoundInt);
//pub type LngLat = (f64, f64);
/// An `(id, Feature)` pair.
pub type IdFeaturePair = (usize, geojson::Feature);
// Concrete helpers.
/// A concrete collection of concrete values.
#[derive(Debug, Serialize, Deserialize)]
pub struct ConcreteVec<T>(Vec<T>);
impl<T> Deref for ConcreteVec<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> From<geojson::FeatureCollection> for ConcreteVec<T>
where
T: From<IdFeaturePair>,
{
fn from(value: geojson::FeatureCollection) -> ConcreteVec<T> {
let values = value.features.into_iter().enumerate().map(T::from).collect::<Vec<T>>();
ConcreteVec(values)
}
}
impl<T> IntoIterator for ConcreteVec<T> {
type IntoIter = std::vec::IntoIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T> IntoIterator for &'a ConcreteVec<T> {
type IntoIter = std::slice::Iter<'a, T>;
type Item = &'a T;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
// Traits.
/// A trait for types that have a [`Geometry`].
///
/// Helps abstract away this property so the helper methods can be generalized.
pub trait HasGeometry {
/// Get the [`Geometry`] of the [`IsTimezone`].
fn geometry(&self) -> &Geometry<Float>;
}
/// A trait for types that have properties.
pub trait HasProperties {
/// Get the properties of the [`HasProperties`].
fn properties(&self) -> Map<String, Value>;
}
/// A trait that allows types to be converted to GeoJSON.
pub trait ToGeoJsonFeature {
/// Convert the type to GeoJSON.
fn to_feature(&self) -> geojson::Feature;
}
impl<T> ToGeoJsonFeature for T
where
T: HasGeometry + HasProperties,
{
fn to_feature(&self) -> geojson::Feature {
let geometry = self.geometry();
let properties = self.properties();
geojson::Feature {
properties: Some(properties),
geometry: Some(geojson::Geometry::from(geometry)),
..geojson::Feature::default()
}
}
}
/// A trait that allows for iterator types to be converted to GeoJSON.
pub trait ToGeoJsonFeatureCollection {
/// Convert the type to GeoJSON.
fn to_feature_collection(&self) -> geojson::FeatureCollection;
}
/// Implementation specifically for [`ConcreteVec`].
impl<'a, L, D, T> ToGeoJsonFeatureCollection for &'a L
where
L: Deref<Target = D>,
D: Deref<Target = [T]>,
T: ToGeoJsonFeature + 'static,
{
fn to_feature_collection(&self) -> geojson::FeatureCollection {
let features = self.iter().map(|x| x.to_feature()).collect();
geojson::FeatureCollection {
features,
bbox: None,
foreign_members: None,
}
}
}
/// A trait to convert to GeoJSON.
pub trait ToGeoJson {
/// Convert the type to GeoJSON.
fn to_geojson(&self) -> GeoJson;
}
impl<T> ToGeoJson for T
where
T: ToGeoJsonFeatureCollection,
{
fn to_geojson(&self) -> GeoJson {
GeoJson::FeatureCollection(self.to_feature_collection())
}
}
// Helper methods.
/// Simplifies a [`Geometry`] using the [Visvalingam-Whyatt algorithm](https://bost.ocks.org/mike/simplify/).
///
/// For geometries that cannot be simplified, the original geometry is returned.
pub fn simplify_geometry(geometry: Geometry<Float>) -> Geometry<Float> {
#[cfg(not(feature = "unsimplified"))]
let geometry = match geometry {
Geometry::Polygon(polygon) => {
let simplified = polygon.simplify_vw_preserve(&SIMPLIFICATION_EPSILON);
Geometry::Polygon(simplified)
}
Geometry::MultiPolygon(multi_polygon) => {
let simplified = multi_polygon.simplify_vw_preserve(&SIMPLIFICATION_EPSILON);
Geometry::MultiPolygon(simplified)
}
Geometry::LineString(line_string) => {
let simplified = line_string.simplify_vw_preserve(&SIMPLIFICATION_EPSILON);
Geometry::LineString(simplified)
}
Geometry::MultiLineString(multi_line_string) => {
let simplified = multi_line_string.simplify_vw_preserve(&SIMPLIFICATION_EPSILON);
Geometry::MultiLineString(simplified)
}
g => g,
};
geometry
}