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};

/// Utilities for reflecting the current map and viewport in the URL on the web. No effect on
/// native.
pub struct URLManager;

impl URLManager {
    /// Modify the current URL to change the first free parameter in the HTTP GET params to the
    /// specified value, adding it if needed.
    pub fn update_url_free_param(free_param: String) {
        must_update_url(Box::new(move |url| change_url_free_param(url, &free_param)))
    }

    /// Modify the current URL to change the first named parameter in the HTTP GET params to the
    /// specified value, adding it if needed.
    pub fn update_url_param(key: String, value: String) {
        must_update_url(Box::new(move |url| change_url_param(url, &key, &value)))
    }

    /// Get an OSM-style `zoom/lat/lon` string
    /// (https://wiki.openstreetmap.org/wiki/Browsing#Other_URL_tricks) based on the current
    /// viewport.
    pub fn get_cam_param(ctx: &EventCtx, gps_bounds: &GPSBounds) -> String {
        let center = ctx.canvas.center_to_map_pt().to_gps(gps_bounds);

        // To calculate zoom, just solve for the inverse of the code in parse_center_camera.
        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;

        // Trim precision
        format!("{:.2}/{:.5}/{:.5}", zoom_lvl, center.y(), center.x())
    }

    /// Modify the current URL to set --cam to an OSM-style `zoom/lat/lon` string
    /// (https://wiki.openstreetmap.org/wiki/Browsing#Other_URL_tricks) based on the current
    /// viewport.
    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)))
    }

    /// Parse an OSM-style `zoom/lat/lon` string
    /// (https://wiki.openstreetmap.org/wiki/Browsing#Other_URL_tricks), changing the canvas
    /// appropriately. Returns true upon success.
    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
        }
    }

    /// Parse an OSM-style `zoom/lat/lon` string
    /// (https://wiki.openstreetmap.org/wiki/Browsing#Other_URL_tricks), returning the map point to
    /// center on and the camera zoom.
    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);

        // To figure out zoom, first calculate horizontal meters per pixel, using the formula from
        // https://wiki.openstreetmap.org/wiki/Zoom_levels.
        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);

        // So this is the width in meters that should cover our screen
        // let horiz_meters_per_screen = ctx.canvas.window_width * horiz_meters_per_pixel;
        // Now we want to make screen_to_map(the top-right corner of the screen) =
        // horiz_meters_per_screen. Easy algebra:
        // let cam_zoom = ctx.canvas.window_width / horiz_meters_per_screen;

        // But actually, the algebra shows we don't even need window_width. Easy!
        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);

        // Setting window.location.href may seem like the obvious thing to do, but that actually
        // refreshes the page. This method just changes the URL and doesn't mess up history. See
        // https://developer.mozilla.org/en-US/docs/Web/API/History_API/Working_with_the_History_API.
        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 {
    // The URL parsing crates I checked had lots of dependencies and didn't even expose such a nice
    // API for doing this anyway.
    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 {
            // Replace the first free parameter
            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 {
    // The URL parsing crates I checked had lots of dependencies and didn't even expose such a nice
    // API for doing this anyway.
    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"
            )
        );
    }
}