atmos/os_lib/site_compat/
osm.rs1extern crate alloc;
7use alloc::string::String;
8use alloc::vec::Vec;
9use crate::os_lib::web_engine::embed_map::Embed;
10
11pub fn osm_tile_url(z: u32, x: i64, y: i64) -> String {
14 alloc::format!("https://tile.openstreetmap.org/{}/{}/{}.png", z, x, y)
15}
16
17pub fn parse_embed_url(url: &str) -> Option<Embed> {
20 let u = url.trim();
21 let rest = u
22 .strip_prefix("https://")
23 .or_else(|| u.strip_prefix("http://"))
24 .or_else(|| u.strip_prefix("//"))?;
25 let (host_path, query) = rest.split_once('?')?;
26 let host_ok = host_path.starts_with("www.openstreetmap.org/") || host_path.starts_with("openstreetmap.org/");
27 if !host_ok || !host_path.ends_with("/export/embed.html") {
28 return None;
29 }
30 let mut bbox: Option<[f64; 4]> = None;
31 let mut marker = None;
32 for kv in query.split('&') {
33 let kv = kv.strip_prefix("amp;").unwrap_or(kv);
35 let (k, v) = kv.split_once('=').unwrap_or((kv, ""));
36 let v = v.replace("%2C", ",").replace("%2c", ",");
37 match k {
38 "bbox" => {
39 let n: Vec<f64> = v.split(',').filter_map(|x| x.trim().parse::<f64>().ok()).collect();
40 if n.len() == 4 && n.iter().all(|x| x.is_finite()) {
41 bbox = Some([n[0], n[1], n[2], n[3]]);
42 }
43 }
44 "marker" => {
45 let n: Vec<f64> = v.split(',').filter_map(|x| x.trim().parse::<f64>().ok()).collect();
46 if n.len() == 2 && n.iter().all(|x| x.is_finite()) {
47 marker = Some((n[0], n[1]));
48 }
49 }
50 _ => {}
51 }
52 }
53 let [a, b, c, d] = bbox?;
54 Some(Embed {
55 min_lon: a.min(c),
56 min_lat: b.min(d),
57 max_lon: a.max(c),
58 max_lat: b.max(d),
59 marker,
60 })
61}