Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / extract_fallback_part1.rs
extract_fallback_part1.rs218 lines · 10.4 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | |
| 5 | /// A closed square ring, `min..max` in both axes. |
| 6 | fn square(min: f64, max: f64) -> Polygon { |
| 7 | vec![vec![(min, min), (max, min), (max, max), (min, max), (min, min)]] |
| 8 | } |
| 9 | |
| 10 | fn boundary(id: u64, level: u16, polygons: Vec<Polygon>) -> Boundary { |
| 11 | Boundary::new(id, level, polygons) |
| 12 | } |
| 13 | /// The score is area × headcount² in log space, population weighted double so a |
| 14 | /// microstate's headcount can actually lift it. Measured scores: Russia 28.63, |
| 15 | /// USA 29.26, France 26.57, Mongolia 24.32, Singapore 21.73, Liechtenstein |
| 16 | /// 16.56, Nauru 14.82, Vatican City 10.62 — every expectation asserted. |
| 17 | #[test] |
| 18 | fn country_zoom_scores_area_times_headcount() { |
| 19 | // z0: score ≥ 26 — vast and populous, plus populous western Europe (a z0 |
| 20 | // world tile holds four labels that never share a tile, so no collision). |
| 21 | assert_eq!(country_zoom(289.0, 55.0, 144_000_000), 0, "Russia"); |
| 22 | assert_eq!(country_zoom(170.0, 40.0, 335_000_000), 0, "USA"); |
| 23 | assert_eq!(country_zoom(9.5, 47.0, 68_000_000), 0, "France"); |
| 24 | assert_eq!(country_zoom(6.2, 51.0, 84_000_000), 0, "Germany"); |
| 25 | // z1: 23.5–26. Switzerland (23.67) leads Mongolia (24.32)? No: 24.32 ≥ |
| 26 | // 23.5 → z1 as well. Both vast-and-empty vs small-and-rich meet here. |
| 27 | assert_eq!(country_zoom(27.0, 46.0, 3_000_000), 1, "Mongolia"); |
| 28 | assert_eq!(country_zoom(0.72, 46.8, 8_800_000), 1, "Switzerland"); |
| 29 | // z2: 20–23.5 — tiny but millions of people. |
| 30 | assert_eq!(country_zoom(0.012, 1.35, 6_000_000), 2, "Singapore"); |
| 31 | // z3: 14.5–20 — everything else with a pulse, on by z3. |
| 32 | assert_eq!(country_zoom(0.0028, 47.1, 39_000), 3, "Liechtenstein"); |
| 33 | assert_eq!(country_zoom(0.00037, -0.5, 12_000), 3, "Nauru"); |
| 34 | // z4 backstop, never later: tiny AND near-empty. |
| 35 | assert_eq!(country_zoom(0.000007, 41.9, 800), 4, "Vatican City"); |
| 36 | // Antarctica (11.71): vast but uncounted. The backstop is honest — no |
| 37 | // headcount exists to score, and area alone must not outrank every |
| 38 | // populated microstate. |
| 39 | assert_eq!(country_zoom(240.0, -80.0, 0), 4, "Antarctica"); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn nested_shapes_link_each_place_kind_to_its_own_level() { |
| 44 | // Country 0..100 (level 2), region 10..50 (level 4), locality 20..30 (level 8). |
| 45 | let country = boundary(1, 2, vec![square(0.0, 100.0)]); |
| 46 | let region = boundary(2, 4, vec![square(10.0, 50.0)]); |
| 47 | let locality = boundary(3, 8, vec![square(20.0, 30.0)]); |
| 48 | let all = [&country, ®ion, &locality]; |
| 49 | // A country label inside all three still links the country: bands isolate levels. |
| 50 | assert_eq!(select_link((25.0, 25.0), Band::Country, &all), Some(1)); |
| 51 | assert_eq!(select_link((25.0, 25.0), Band::Region, &all), Some(2)); |
| 52 | assert_eq!(select_link((25.0, 25.0), Band::Locality, &all), Some(3)); |
| 53 | // A region label outside the region links nothing, not the country it sits in — |
| 54 | // bands never fall back across levels. |
| 55 | assert_eq!(select_link((5.0, 5.0), Band::Region, &all), None); |
| 56 | assert_eq!(select_link((5.0, 5.0), Band::Country, &all), Some(1)); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn smallest_in_band_container_wins() { |
| 61 | // Two localities nested: the tighter one wins. |
| 62 | let big = boundary(1, 8, vec![square(20.0, 40.0)]); |
| 63 | let small = boundary(2, 8, vec![square(25.0, 35.0)]); |
| 64 | let both = [&big, &small]; |
| 65 | assert_eq!(select_link((30.0, 30.0), Band::Locality, &both), Some(2)); |
| 66 | // Order-independent. |
| 67 | let swapped = [&small, &big]; |
| 68 | assert_eq!(select_link((30.0, 30.0), Band::Locality, &swapped), Some(2)); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn county_levels_match_nothing() { |
| 73 | // Levels 5–6 are the documented gap: a place inside only a county links nowhere. |
| 74 | let county = boundary(1, 6, vec![square(0.0, 100.0)]); |
| 75 | let only = [&county]; |
| 76 | assert_eq!(select_link((50.0, 50.0), Band::Country, &only), None); |
| 77 | assert_eq!(select_link((50.0, 50.0), Band::Region, &only), None); |
| 78 | assert_eq!(select_link((50.0, 50.0), Band::Locality, &only), None); |
| 79 | // A county boundary never steals a pick even when it is the tightest container. |
| 80 | let country = boundary(2, 2, vec![square(0.0, 100.0)]); |
| 81 | let mixed = [&county, &country]; |
| 82 | assert_eq!(select_link((50.0, 50.0), Band::Country, &mixed), Some(2)); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn near_tie_links_nothing() { |
| 87 | // Two identical claims: areas equal, diff 0% — ambiguous. |
| 88 | let first = boundary(1, 8, vec![square(0.0, 10.0)]); |
| 89 | let second = boundary(2, 8, vec![square(0.0, 10.0)]); |
| 90 | assert_eq!(select_link((5.0, 5.0), Band::Locality, &[&first, &second]), None); |
| 91 | // Areas within 1% (10.0 vs 10.04 wide — 0.8% area diff) still ambiguous. |
| 92 | let narrow = boundary(3, 8, vec![square(0.0, 10.0)]); |
| 93 | let wide = boundary(4, 8, vec![square(0.0, 10.04)]); |
| 94 | assert_eq!(select_link((5.0, 5.0), Band::Locality, &[&narrow, &wide]), None); |
| 95 | // A clear 2x nesting is not a tie. |
| 96 | let big = boundary(5, 8, vec![square(0.0, 20.0)]); |
| 97 | assert_eq!(select_link((5.0, 5.0), Band::Locality, &[&narrow, &big]), Some(3)); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn a_point_on_a_ring_edge_links_nothing() { |
| 102 | let square8 = boundary(1, 8, vec![square(0.0, 10.0)]); |
| 103 | let only = [&square8]; |
| 104 | // On the west edge: ray-casting is a coin flip, so this is a deliberate miss. |
| 105 | assert_eq!(select_link((0.0, 5.0), Band::Locality, &only), None); |
| 106 | // Just inside still links. |
| 107 | assert_eq!(select_link((0.5, 5.0), Band::Locality, &only), Some(1)); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn holes_punch_out() { |
| 112 | // A locality donut: exterior 0..10 with a 4..6 hole. A place in the hole is not inside. |
| 113 | let donut = boundary(1, 8, vec![vec![ |
| 114 | vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (0.0, 0.0)], |
| 115 | vec![(4.0, 4.0), (6.0, 4.0), (6.0, 6.0), (4.0, 6.0), (4.0, 4.0)], |
| 116 | ]]); |
| 117 | let only = [&donut]; |
| 118 | assert_eq!(select_link((2.0, 2.0), Band::Locality, &only), Some(1)); |
| 119 | assert_eq!(select_link((5.0, 5.0), Band::Locality, &only), None); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn sub_locality_places_never_link() { |
| 124 | assert_eq!(band_for_place_kind("neighbourhood"), None); |
| 125 | assert_eq!(band_for_place_kind("macrohood"), None); |
| 126 | assert_eq!(band_for_place_kind("country"), Some(Band::Country)); |
| 127 | assert_eq!(band_for_place_kind("region"), Some(Band::Region)); |
| 128 | assert_eq!(band_for_place_kind("locality"), Some(Band::Locality)); |
| 129 | } |
| 130 | |
| 131 | /// End to end over a spilled store: member links win, member-less places gain links |
| 132 | /// (including a way-id place the node-keyed member map could never reach), and the county |
| 133 | /// gap still misses. |
| 134 | #[test] |
| 135 | fn member_links_win_and_member_less_places_gain_links() { |
| 136 | use crate::extract::{tagged_id, Feature, ELEMENT_NODE, ELEMENT_RELATION, ELEMENT_WAY}; |
| 137 | |
| 138 | fn place(id: u64, place_tag: &str, point: (f64, f64)) -> Feature { |
| 139 | let class = crate::schema::places::classify( |
| 140 | &[("place", place_tag), ("name", "Test")] as &[(&str, &str)], |
| 141 | ) |
| 142 | .expect("classified"); |
| 143 | Feature { |
| 144 | class, |
| 145 | geometry: Geometry::Points(vec![point]), |
| 146 | name: Some("Test".to_string()), |
| 147 | id, |
| 148 | transit_color: 0, |
| 149 | transit_ordinal: 0, |
| 150 | transit_lanes: 0, |
| 151 | transit_taper: 0, |
| 152 | lane_count: 0, |
| 153 | turn_fwd: Vec::new(), |
| 154 | turn_bwd: Vec::new(), |
| 155 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 156 | building: None, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | fn shape(id: u64, level: &str, ring: Polygon) -> Feature { |
| 161 | let class = crate::schema::boundaries::region_area( |
| 162 | &[("boundary", "administrative"), ("admin_level", level)] as &[(&str, &str)], |
| 163 | false, |
| 164 | ) |
| 165 | .expect("region shape"); |
| 166 | Feature { |
| 167 | class, |
| 168 | geometry: Geometry::Polygons(vec![ring]), |
| 169 | name: None, |
| 170 | id, |
| 171 | transit_color: 0, |
| 172 | transit_ordinal: 0, |
| 173 | transit_lanes: 0, |
| 174 | transit_taper: 0, |
| 175 | lane_count: 0, |
| 176 | turn_fwd: Vec::new(), |
| 177 | turn_bwd: Vec::new(), |
| 178 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 179 | building: None, |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // A city boundary (level 8) 20..30 and a county boundary (level 6) 0..100. |
| 184 | let city_id = tagged_id(201, ELEMENT_RELATION); |
| 185 | let county_id = tagged_id(202, ELEMENT_RELATION); |
| 186 | let member_target = tagged_id(203, ELEMENT_RELATION); |
| 187 | // A member-linked town: fallback would pick the city, the member must win. |
| 188 | let linked_town = tagged_id(101, ELEMENT_NODE); |
| 189 | // A member-less town (node) and a member-less village mapped as a way. |
| 190 | let bare_town = tagged_id(102, ELEMENT_NODE); |
| 191 | let way_village = tagged_id(103, ELEMENT_WAY); |
| 192 | // A country label sitting where only the county covers it: must stay unlinked. |
| 193 | let bare_country = tagged_id(104, ELEMENT_NODE); |
| 194 | let features = vec![ |
| 195 | shape(city_id, "8", square(20.0, 30.0)), |
| 196 | shape(county_id, "6", square(0.0, 100.0)), |
| 197 | place(linked_town, "town", (25.0, 25.0)), |
| 198 | place(bare_town, "town", (26.0, 26.0)), |
| 199 | place(way_village, "village", (27.0, 27.0)), |
| 200 | place(bare_country, "country", (50.0, 50.0)), |
| 201 | ]; |
| 202 | let store = crate::store::Store::of(&features).expect("spilled"); |
| 203 | |
| 204 | let mut links: HashMap<u64, u64> = HashMap::new(); |
| 205 | links.insert(linked_town, member_target); |
| 206 | let stats = extend_region_links(&store, &mut links).expect("fallback"); |
| 207 | |
| 208 | assert_eq!(links.get(&linked_town), Some(&member_target), "member wins over fallback"); |
| 209 | assert_eq!(links.get(&bare_town), Some(&city_id), "member-less town gains the city"); |
| 210 | assert_eq!(links.get(&way_village), Some(&city_id), "way-id place links by position"); |
| 211 | assert!(!links.contains_key(&bare_country), "county gap: no link for the country label"); |
| 212 | |
| 213 | assert_eq!(stats.places_total, 4); |
| 214 | assert_eq!(stats.member_linked, 1); |
| 215 | assert_eq!(stats.fallback_linked, 2); |
| 216 | assert_eq!(stats.fallback_missed, 1); |
| 217 | } |
| 218 | } |