Skip to main content

atmos/os_lib/site_compat/
osm.rs

1//! OpenStreetMap の埋め込みの URL の読み方とタイルの URL(2026-11-19、`web_engine/osm_embed.rs` から分けた)。
2//!
3//! 地図を描く計算(`Embed`・`Plan`・`MapState`・投影・ズーム)は、提供元に依らないのでエンジン側の
4//! `web_engine/embed_map.rs` に残してある。ここは OSM に固有の 2 点(URL の判定と読み取り・タイル URL)だけ。
5
6extern crate alloc;
7use alloc::string::String;
8use alloc::vec::Vec;
9use crate::os_lib::web_engine::embed_map::Embed;
10
11/// 地図のタイルの URL。
12// [S4-AUDIT: 要継続] 根拠: OSM タイル URL。iframe 地図のネイティブ描画に必要。
13pub fn osm_tile_url(z: u32, x: i64, y: i64) -> String {
14    alloc::format!("https://tile.openstreetmap.org/{}/{}/{}.png", z, x, y)
15}
16
17/// URL が OSM の埋め込みページなら、その範囲とマーカーを返す。
18// [S4-AUDIT: 要継続] 根拠: OSM 埋め込み URL の bbox/marker 読み取り。iframe 内の JS 地図(Leaflet 等)を実行せず代替するため必要。
19pub 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        // HTML 属性由来の `&amp;` は属性解析で `&` に戻っている前提だが、念のため許容する。
34        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}