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
#![allow(clippy::new_without_default)]
#[macro_use]
extern crate anyhow;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub use crate::angle::Angle;
pub use crate::bounds::{Bounds, GPSBounds};
pub use crate::circle::Circle;
pub use crate::distance::Distance;
pub use crate::duration::Duration;
pub use crate::find_closest::FindClosest;
pub use crate::gps::LonLat;
pub use crate::line::{InfiniteLine, Line};
pub use crate::percent::Percent;
pub use crate::polygon::{Polygon, Triangle};
pub use crate::polyline::{ArrowCap, PolyLine};
pub use crate::pt::{HashablePt2D, Pt2D};
pub use crate::ring::Ring;
pub use crate::speed::Speed;
pub use crate::stats::{HgramValue, Histogram, Statistic};
pub use crate::time::Time;
mod angle;
mod bounds;
mod circle;
mod conversions;
mod distance;
mod duration;
mod find_closest;
mod gps;
mod line;
mod percent;
mod polygon;
mod polyline;
mod pt;
mod ring;
mod speed;
mod stats;
mod time;
pub const EPSILON_DIST: Distance = Distance::const_meters(0.01);
pub fn trim_f64(x: f64) -> f64 {
(x * 10_000.0).round() / 10_000.0
}
fn serialize_f64<S: Serializer>(x: &f64, s: S) -> Result<S::Ok, S::Error> {
let int = (x * 10_000.0) as i32;
int.serialize(s)
}
fn deserialize_f64<'de, D: Deserializer<'de>>(d: D) -> Result<f64, D::Error> {
let x = <i32>::deserialize(d)?;
Ok(x as f64 / 10_000.0)
}
#[derive(Clone, Serialize, Deserialize, Copy)]
pub struct UnitFmt {
pub round_durations: bool,
pub metric: bool,
}
impl UnitFmt {
pub fn metric() -> Self {
Self {
round_durations: true,
metric: true,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct CornerRadii {
pub top_left: f64,
pub top_right: f64,
pub bottom_right: f64,
pub bottom_left: f64,
}
impl CornerRadii {
pub fn uniform(radius: f64) -> Self {
Self {
top_left: radius,
top_right: radius,
bottom_right: radius,
bottom_left: radius,
}
}
pub fn zero() -> Self {
Self::uniform(0.0)
}
}
impl std::convert::From<f64> for CornerRadii {
fn from(uniform: f64) -> Self {
Self::uniform(uniform)
}
}
impl Default for CornerRadii {
fn default() -> Self {
Self::zero()
}
}
pub fn geometries_with_properties_to_geojson(
input: Vec<(
geojson::Geometry,
serde_json::Map<String, serde_json::Value>,
)>,
) -> geojson::GeoJson {
let mut features = Vec::new();
for (geom, properties) in input {
features.push(geojson::Feature {
bbox: None,
geometry: Some(geom),
id: None,
properties: Some(properties),
foreign_members: None,
});
}
geojson::GeoJson::from(geojson::FeatureCollection {
bbox: None,
features,
foreign_members: None,
})
}
pub fn geometries_to_geojson(input: Vec<geojson::Geometry>) -> geojson::GeoJson {
let mut features = Vec::new();
for geom in input {
features.push(geojson::Feature {
bbox: None,
geometry: Some(geom),
id: None,
properties: None,
foreign_members: None,
});
}
geojson::GeoJson::from(geojson::FeatureCollection {
bbox: None,
features,
foreign_members: None,
})
}
#[cfg(test)]
mod tests {
use super::*;
use rand::{Rng, SeedableRng};
#[test]
fn f64_trimming() {
let mut rng = rand_xorshift::XorShiftRng::seed_from_u64(42);
for _ in 0..1_000 {
let input = rng.gen_range(-214_000.00..214_000.0);
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
abstutil::from_json(abstutil::to_json(&trimmed).as_bytes()).unwrap();
let bincode_roundtrip: f64 =
abstutil::from_binary(&abstutil::to_binary(&trimmed)).unwrap();
assert_eq!(json_roundtrip, trimmed);
assert_eq!(bincode_roundtrip, trimmed);
}
let input = 1.2345678;
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
abstutil::from_json(abstutil::to_json(&trimmed).as_bytes()).unwrap();
let bincode_roundtrip: f64 = abstutil::from_binary(&abstutil::to_binary(&trimmed)).unwrap();
assert_eq!(json_roundtrip, 1.2346);
assert_eq!(bincode_roundtrip, 1.2346);
}
}