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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use anyhow::Result;
use crate::EventCtx;
use geom::{GPSBounds, LonLat, Pt2D};
pub struct URLManager;
impl URLManager {
pub fn update_url_free_param(free_param: String) {
must_update_url(Box::new(move |url| change_url_free_param(url, &free_param)))
}
pub fn update_url_param(key: String, value: String) {
must_update_url(Box::new(move |url| change_url_param(url, &key, &value)))
}
pub fn get_cam_param(ctx: &EventCtx, gps_bounds: &GPSBounds) -> String {
let center = ctx.canvas.center_to_map_pt().to_gps(gps_bounds);
let earth_circumference_equator = 40_075_016.686;
let log_arg =
earth_circumference_equator * center.y().to_radians().cos() * ctx.canvas.cam_zoom;
let zoom_lvl = log_arg.log2() - 8.0;
format!("{:.2}/{:.5}/{:.5}", zoom_lvl, center.y(), center.x())
}
pub fn update_url_cam(ctx: &EventCtx, gps_bounds: &GPSBounds) {
let cam = URLManager::get_cam_param(ctx, gps_bounds);
must_update_url(Box::new(move |url| change_url_param(url, "--cam", &cam)))
}
pub fn change_camera(ctx: &mut EventCtx, raw: Option<&String>, gps_bounds: &GPSBounds) -> bool {
if let Some((pt, zoom)) =
raw.and_then(|raw| URLManager::parse_center_camera(raw, gps_bounds))
{
ctx.canvas.cam_zoom = zoom;
ctx.canvas.center_on_map_pt(pt);
true
} else {
false
}
}
fn parse_center_camera(raw: &str, gps_bounds: &GPSBounds) -> Option<(Pt2D, f64)> {
let parts: Vec<&str> = raw.split('/').collect();
if parts.len() != 3 {
return None;
}
let zoom_lvl = parts[0].parse::<f64>().ok()?;
let lat = parts[1].parse::<f64>().ok()?;
let lon = parts[2].parse::<f64>().ok()?;
let gps = LonLat::new(lon, lat);
if !gps_bounds.contains(gps) {
return None;
}
let pt = gps.to_pt(gps_bounds);
let earth_circumference_equator = 40_075_016.686;
let horiz_meters_per_pixel =
earth_circumference_equator * gps.y().to_radians().cos() / 2.0_f64.powf(zoom_lvl + 8.0);
let cam_zoom = 1.0 / horiz_meters_per_pixel;
Some((pt, cam_zoom))
}
}
fn must_update_url(transform: Box<dyn Fn(String) -> String>) {
if let Err(err) = update_url(transform) {
warn!("Couldn't update URL: {}", err);
}
}
#[allow(unused_variables)]
fn update_url(transform: Box<dyn Fn(String) -> String>) -> Result<()> {
#[cfg(target_arch = "wasm32")]
{
let window = web_sys::window().ok_or(anyhow!("no window?"))?;
let url = window.location().href().map_err(|err| {
anyhow!(err
.as_string()
.unwrap_or("window.location.href failed".to_string()))
})?;
let new_url = (transform)(url);
let history = window.history().map_err(|err| {
anyhow!(err
.as_string()
.unwrap_or("window.history failed".to_string()))
})?;
history
.replace_state_with_url(&wasm_bindgen::JsValue::NULL, "", Some(&new_url))
.map_err(|err| {
anyhow!(err
.as_string()
.unwrap_or("window.history.replace_state failed".to_string()))
})?;
}
Ok(())
}
fn change_url_free_param(url: String, free_param: &str) -> String {
let url_parts = url.split('?').collect::<Vec<_>>();
if url_parts.len() == 1 {
return format!("{}?{}", url, free_param);
}
let mut query_params = String::new();
let mut found_free = false;
let mut first = true;
for x in url_parts[1].split('&') {
if !first {
query_params.push('&');
}
first = false;
if x.starts_with("--") {
query_params.push_str(x);
} else if !found_free {
query_params.push_str(free_param);
found_free = true;
} else {
query_params.push_str(x);
}
}
if !found_free {
if !first {
query_params.push('&');
}
query_params.push_str(free_param);
}
format!("{}?{}", url_parts[0], query_params)
}
fn change_url_param(url: String, key: &str, value: &str) -> String {
let url_parts = url.split('?').collect::<Vec<_>>();
if url_parts.len() == 1 {
return format!("{}?{}={}", url, key, value);
}
let mut query_params = String::new();
let mut found_key = false;
let mut first = true;
for x in url_parts[1].split('&') {
if !first {
query_params.push('&');
}
first = false;
if x.starts_with(key) {
query_params.push_str(&format!("{}={}", key, value));
found_key = true;
} else {
query_params.push_str(x);
}
}
if !found_key {
if !first {
query_params.push('&');
}
query_params.push_str(&format!("{}={}", key, value));
}
format!("{}?{}", url_parts[0], query_params)
}
#[cfg(test)]
mod tests {
#[test]
fn test_change_url_free_param() {
use super::change_url_free_param;
assert_eq!(
"http://0.0.0.0:8000/?--dev&seattle/maps/montlake.bin",
change_url_free_param(
"http://0.0.0.0:8000/?--dev".to_string(),
"seattle/maps/montlake.bin"
)
);
assert_eq!(
"http://0.0.0.0:8000/?--dev&seattle/maps/qa.bin",
change_url_free_param(
"http://0.0.0.0:8000/?--dev&seattle/maps/montlake.bin".to_string(),
"seattle/maps/qa.bin"
)
);
assert_eq!(
"http://0.0.0.0:8000?seattle/maps/montlake.bin",
change_url_free_param(
"http://0.0.0.0:8000".to_string(),
"seattle/maps/montlake.bin"
)
);
}
#[test]
fn test_change_url_param() {
use super::change_url_param;
assert_eq!(
"http://0.0.0.0:8000/?--dev&seattle/maps/montlake.bin&--cam=16.6/53.78449/-1.70701",
change_url_param(
"http://0.0.0.0:8000/?--dev&seattle/maps/montlake.bin".to_string(),
"--cam",
"16.6/53.78449/-1.70701"
)
);
assert_eq!(
"http://0.0.0.0:8000?--cam=16.6/53.78449/-1.70701",
change_url_param(
"http://0.0.0.0:8000".to_string(),
"--cam",
"16.6/53.78449/-1.70701"
)
);
}
}