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
use serde::{Deserialize, Serialize};
use aabb_quadtree::geom::{Point, Rect};
use crate::{LonLat, Polygon, Pt2D, Ring};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Bounds {
pub min_x: f64,
pub min_y: f64,
pub max_x: f64,
pub max_y: f64,
}
impl Bounds {
pub fn new() -> Bounds {
Bounds {
min_x: f64::MAX,
min_y: f64::MAX,
max_x: f64::MIN,
max_y: f64::MIN,
}
}
pub fn from(pts: &Vec<Pt2D>) -> Bounds {
let mut b = Bounds::new();
for pt in pts {
b.update(*pt);
}
b
}
pub fn update(&mut self, pt: Pt2D) {
self.min_x = self.min_x.min(pt.x());
self.max_x = self.max_x.max(pt.x());
self.min_y = self.min_y.min(pt.y());
self.max_y = self.max_y.max(pt.y());
}
pub fn union(&mut self, other: Bounds) {
self.update(Pt2D::new(other.min_x, other.min_y));
self.update(Pt2D::new(other.max_x, other.max_y));
}
pub fn contains(&self, pt: Pt2D) -> bool {
pt.x() >= self.min_x && pt.x() <= self.max_x && pt.y() >= self.min_y && pt.y() <= self.max_y
}
pub fn as_bbox(&self) -> Rect {
Rect {
top_left: Point {
x: self.min_x as f32,
y: self.min_y as f32,
},
bottom_right: Point {
x: self.max_x as f32,
y: self.max_y as f32,
},
}
}
pub fn get_rectangle(&self) -> Polygon {
Ring::must_new(vec![
Pt2D::new(self.min_x, self.min_y),
Pt2D::new(self.max_x, self.min_y),
Pt2D::new(self.max_x, self.max_y),
Pt2D::new(self.min_x, self.max_y),
Pt2D::new(self.min_x, self.min_y),
])
.to_polygon()
}
pub fn width(&self) -> f64 {
self.max_x - self.min_x
}
pub fn height(&self) -> f64 {
self.max_y - self.min_y
}
pub fn center(&self) -> Pt2D {
Pt2D::new(
self.min_x + self.width() / 2.0,
self.min_y + self.height() / 2.0,
)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GPSBounds {
pub(crate) min_lon: f64,
pub(crate) min_lat: f64,
pub(crate) max_lon: f64,
pub(crate) max_lat: f64,
}
impl GPSBounds {
pub fn new() -> GPSBounds {
GPSBounds {
min_lon: f64::MAX,
min_lat: f64::MAX,
max_lon: f64::MIN,
max_lat: f64::MIN,
}
}
pub fn from(pts: Vec<LonLat>) -> GPSBounds {
let mut b = GPSBounds::new();
for pt in pts {
b.update(pt);
}
b
}
pub fn update(&mut self, pt: LonLat) {
self.min_lon = self.min_lon.min(pt.x());
self.max_lon = self.max_lon.max(pt.x());
self.min_lat = self.min_lat.min(pt.y());
self.max_lat = self.max_lat.max(pt.y());
}
pub fn contains(&self, pt: LonLat) -> bool {
pt.x() >= self.min_lon
&& pt.x() <= self.max_lon
&& pt.y() >= self.min_lat
&& pt.y() <= self.max_lat
}
pub fn get_max_world_pt(&self) -> Pt2D {
let width = LonLat::new(self.min_lon, self.min_lat)
.gps_dist(LonLat::new(self.max_lon, self.min_lat));
let height = LonLat::new(self.min_lon, self.min_lat)
.gps_dist(LonLat::new(self.min_lon, self.max_lat));
Pt2D::new(width.inner_meters(), height.inner_meters())
}
pub fn to_bounds(&self) -> Bounds {
let mut b = Bounds::new();
b.update(Pt2D::new(0.0, 0.0));
b.update(self.get_max_world_pt());
b
}
pub fn try_convert(&self, pts: &Vec<LonLat>) -> Option<Vec<Pt2D>> {
let mut result = Vec::new();
for gps in pts {
if !self.contains(*gps) {
return None;
}
result.push(gps.to_pt(self));
}
Some(result)
}
pub fn convert(&self, pts: &Vec<LonLat>) -> Vec<Pt2D> {
pts.iter().map(|gps| gps.to_pt(self)).collect()
}
pub fn convert_back(&self, pts: &Vec<Pt2D>) -> Vec<LonLat> {
pts.iter().map(|pt| pt.to_gps(self)).collect()
}
}