Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / poi.rs
poi.rs437 lines · 17.8 KiBRaw
| 1 | //! `poi`: icons — the points MapLibre draws symbols for. |
| 2 | //! |
| 3 | //! The set is exactly the reference style's `pois` layer filter (37 kinds matched by `kind` |
| 4 | //! with a per-feature `min_zoom`): beaches to theatres. Each feature carries its `kind` as the |
| 5 | //! icon key — the names equal the v4/light sprite names where they overlap, so the renderer |
| 6 | //! resolves the image straight from the kind — and a coalesced display name (`name:en` → |
| 7 | //! `name`) for the label beneath the icon. |
| 8 | //! |
| 9 | //! # Source tags |
| 10 | //! |
| 11 | //! Upstream's POI kinds come from every corner of the tag space (`amenity`, `shop`, `leisure`, |
| 12 | //! `tourism`, `aeroway`, `railway`, `natural`, ...). This maps each sprite kind back to the OSM |
| 13 | //! tags that mean it, so the tiler emits only kinds the style draws: anything else (a bare |
| 14 | //! `amenity=yes`, a `shop=vacant`) is `None`. |
| 15 | //! |
| 16 | //! # Zoom |
| 17 | //! |
| 18 | //! The style gates each POI on its own `min_zoom` (`zoom >= min_zoom`): stations and airports |
| 19 | //! early, benches and toilets late. The floors here mirror that intent — transport and major |
| 20 | //! attractions first, amenities by size, street furniture last. |
| 21 | |
| 22 | use tilecodec::mamaps::dict::LAYER_POI; |
| 23 | |
| 24 | use super::{kind, Class, TagSource}; |
| 25 | |
| 26 | pub const FILTERS: &[&str] = &[ |
| 27 | "amenity", "shop", "leisure", "tourism", "aeroway", "railway", "natural", "highway", |
| 28 | "building", "station", "name", "name:en", |
| 29 | ]; |
| 30 | |
| 31 | /// Every `kind` this module can emit: exactly the reference style's `pois` filter set. |
| 32 | #[cfg_attr(not(test), allow(dead_code))] |
| 33 | pub const KINDS: &[&str] = &[ |
| 34 | "beach", |
| 35 | "forest", |
| 36 | "marina", |
| 37 | "park", |
| 38 | "peak", |
| 39 | "zoo", |
| 40 | "garden", |
| 41 | "bench", |
| 42 | "aerodrome", |
| 43 | "station", |
| 44 | "bus_stop", |
| 45 | "ferry_terminal", |
| 46 | "stadium", |
| 47 | "university", |
| 48 | "library", |
| 49 | "school", |
| 50 | "animal", |
| 51 | "toilets", |
| 52 | "drinking_water", |
| 53 | "post_office", |
| 54 | "building", |
| 55 | "townhall", |
| 56 | "restaurant", |
| 57 | "fast_food", |
| 58 | "cafe", |
| 59 | "bar", |
| 60 | "supermarket", |
| 61 | "convenience", |
| 62 | "books", |
| 63 | "beauty", |
| 64 | "electronics", |
| 65 | "clothes", |
| 66 | "attraction", |
| 67 | "museum", |
| 68 | "theatre", |
| 69 | "artwork", |
| 70 | ]; |
| 71 | |
| 72 | /// A POI's kind and floor, from its tags; `None` for anything the style does not draw. |
| 73 | /// |
| 74 | /// Floors are clamped to z14: builds cap at z14 and the renderer overzooms the deepest tile, so |
| 75 | /// a bench gated at z16 would otherwise be carried nowhere and never drawn. |
| 76 | pub fn classify(tags: &(impl TagSource + ?Sized)) -> Option<Class> { |
| 77 | let (kind_name, min_zoom) = poi_kind(tags)?; |
| 78 | let class = Class { |
| 79 | layer: LAYER_POI, |
| 80 | kind: kind(kind_name), |
| 81 | kind_detail: tilecodec::mamaps::dict::NONE, |
| 82 | flags: 0, |
| 83 | // A POI is a point even when mapped as an area (a museum building, a park polygon): |
| 84 | // extract centroids it, and the icon anchors at one coordinate. |
| 85 | area: false, |
| 86 | min_zoom: min_zoom.min(14), |
| 87 | min_area_px: 0.0, |
| 88 | }; |
| 89 | // Stations name their mode in the detail: `poi` kind `station` + a station detail is the |
| 90 | // transit-station tag the renderer reads with POIs off and Transit on. Nothing else on the |
| 91 | // layer carries a detail, so the field's presence alone is the signal. |
| 92 | if kind_name == "station" { |
| 93 | Some(Class { kind_detail: super::detail(super::transit::station_detail(tags)), ..class }) |
| 94 | } else { |
| 95 | Some(class) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// The display name for a POI: `name:en` preferred, `name` as fallback, like `places`. |
| 100 | pub fn display_name(tags: &(impl TagSource + ?Sized)) -> Option<String> { |
| 101 | super::places::display_name(tags) |
| 102 | } |
| 103 | |
| 104 | /// Map OSM tags to one of the style's 37 POI kinds. Ordered so the specific wins over the |
| 105 | /// generic: a `tourism=museum` inside a `building=yes` is a museum, not a building. |
| 106 | fn poi_kind(tags: &(impl TagSource + ?Sized)) -> Option<(&'static str, u8)> { |
| 107 | // Transport first: the earliest icons on the map. |
| 108 | if tags.get("aeroway") == Some("aerodrome") { |
| 109 | return Some(("aerodrome", 10)); |
| 110 | } |
| 111 | if matches!(tags.get("railway"), Some("station" | "halt")) { |
| 112 | return Some(("station", 11)); |
| 113 | } |
| 114 | // A bare `station=` tag (no `railway=*`): an underground concourse mapped without the halt. |
| 115 | if matches!(tags.get("station"), Some("subway" | "light_rail" | "tram" | "monorail")) { |
| 116 | return Some(("station", 11)); |
| 117 | } |
| 118 | if tags.get("highway") == Some("bus_stop") { |
| 119 | return Some(("bus_stop", 13)); |
| 120 | } |
| 121 | // A street tram stop. `railway=tram_stop` is the usual tagging and was missing entirely, so |
| 122 | // tram stops existed only where a mapper had also tagged `station=tram` on a concourse — which |
| 123 | // is the exception. `public_transport=platform` is deliberately not used as a fallback: it is |
| 124 | // on bus shelters, rail platforms and ferry piers alike, and would multiply every stop by the |
| 125 | // number of platforms it has. |
| 126 | if matches!(tags.get("railway"), Some("tram_stop" | "halt_tram")) { |
| 127 | return Some(("bus_stop", 13)); |
| 128 | } |
| 129 | if tags.get("amenity") == Some("ferry_terminal") { |
| 130 | return Some(("ferry_terminal", 11)); |
| 131 | } |
| 132 | // Fuel is a driver's landmark, so it appears with the shops rather than the street furniture. |
| 133 | if tags.get("amenity") == Some("fuel") { |
| 134 | return Some(("fuel", 12)); |
| 135 | } |
| 136 | // Nature and outdoors. |
| 137 | match tags.get("natural") { |
| 138 | Some("beach") => return Some(("beach", 12)), |
| 139 | Some("peak") => return Some(("peak", 11)), |
| 140 | Some("wood" | "forest") => return Some(("forest", 12)), |
| 141 | _ => {} |
| 142 | } |
| 143 | if tags.get("leisure") == Some("marina") { |
| 144 | return Some(("marina", 12)); |
| 145 | } |
| 146 | if matches!(tags.get("leisure"), Some("park" | "garden")) { |
| 147 | // `leisure=park` is a park, `leisure=garden` a garden; both green on the map. |
| 148 | return Some((if tags.get("leisure") == Some("park") { "park" } else { "garden" }, 12)); |
| 149 | } |
| 150 | if matches!(tags.get("tourism"), Some("zoo" | "aquarium")) { |
| 151 | return Some(("zoo", 12)); |
| 152 | } |
| 153 | if tags.get("tourism") == Some("attraction") { |
| 154 | return Some(("attraction", 13)); |
| 155 | } |
| 156 | if matches!(tags.get("tourism"), Some("museum" | "gallery")) { |
| 157 | return Some(("museum", 13)); |
| 158 | } |
| 159 | // Lodging. `guest_house` is included because OSM uses it for the small end of the same thing; |
| 160 | // the app's Hotels chip means "somewhere to sleep", not "a building tagged tourism=hotel". |
| 161 | if matches!(tags.get("tourism"), Some("hotel" | "motel" | "hostel" | "guest_house")) { |
| 162 | return Some(("hotel", 12)); |
| 163 | } |
| 164 | if tags.get("tourism") == Some("artwork") { |
| 165 | return Some(("artwork", 14)); |
| 166 | } |
| 167 | if tags.get("tourism") == Some("viewpoint") { |
| 168 | return Some(("attraction", 13)); |
| 169 | } |
| 170 | // Sport, school, civic. |
| 171 | if matches!(tags.get("leisure"), Some("stadium" | "sports_centre" | "pitch")) { |
| 172 | return Some(("stadium", 13)); |
| 173 | } |
| 174 | if matches!(tags.get("amenity"), Some("university" | "college")) { |
| 175 | return Some(("university", 13)); |
| 176 | } |
| 177 | if matches!(tags.get("amenity"), Some("school" | "kindergarten")) { |
| 178 | return Some(("school", 13)); |
| 179 | } |
| 180 | if tags.get("amenity") == Some("library") { |
| 181 | return Some(("library", 13)); |
| 182 | } |
| 183 | if matches!(tags.get("amenity"), Some("townhall" | "courthouse" | "embassy")) { |
| 184 | return Some(("townhall", 13)); |
| 185 | } |
| 186 | if tags.get("amenity") == Some("post_office") { |
| 187 | return Some(("post_office", 13)); |
| 188 | } |
| 189 | // Food and shops. The kinds a category chip selects are carried from **12**, two levels |
| 190 | // shallower than they are ever drawn while browsing. That gap is the point: the style gives |
| 191 | // `poi-food` and `poi-shop` a `browse_minzoom` of 14, so the ambient map is unchanged, and |
| 192 | // `Layer::draws_at_focused` drops to this floor for whichever kinds the active chip names. |
| 193 | // Tapping "Restaurants" therefore reaches four times the area rather than showing the same |
| 194 | // screenful with everything else removed. |
| 195 | // |
| 196 | // It costs archive size — these are the numerous kinds, and each extra level is another copy |
| 197 | // of every one of them — which is why it stops at the chip kinds and at 12. |
| 198 | if tags.get("amenity") == Some("restaurant") { |
| 199 | return Some(("restaurant", 12)); |
| 200 | } |
| 201 | if tags.get("amenity") == Some("fast_food") { |
| 202 | return Some(("fast_food", 12)); |
| 203 | } |
| 204 | if matches!(tags.get("amenity"), Some("cafe" | "ice_cream")) { |
| 205 | return Some(("cafe", 12)); |
| 206 | } |
| 207 | if matches!(tags.get("amenity"), Some("bar" | "pub" | "biergarten" | "nightclub")) { |
| 208 | return Some(("bar", 13)); |
| 209 | } |
| 210 | if tags.get("shop") == Some("supermarket") { |
| 211 | return Some(("supermarket", 12)); |
| 212 | } |
| 213 | if matches!(tags.get("shop"), Some("convenience" | "kiosk")) { |
| 214 | return Some(("convenience", 12)); |
| 215 | } |
| 216 | if tags.get("shop") == Some("books") { |
| 217 | return Some(("books", 14)); |
| 218 | } |
| 219 | if matches!(tags.get("shop"), Some("beauty" | "hairdresser" | "cosmetics")) { |
| 220 | return Some(("beauty", 14)); |
| 221 | } |
| 222 | if matches!(tags.get("shop"), Some("electronics" | "computer" | "mobile_phone")) { |
| 223 | return Some(("electronics", 14)); |
| 224 | } |
| 225 | if matches!(tags.get("shop"), Some("clothes" | "shoes" | "fashion")) { |
| 226 | return Some(("clothes", 14)); |
| 227 | } |
| 228 | // Money. `bank` is separate from `atm` and drawn earlier because a branch is a landmark and a |
| 229 | // machine is not — but the app's ATM chip selects both, since most branches have a machine and |
| 230 | // OSM frequently tags only the branch. |
| 231 | if tags.get("amenity") == Some("bank") { |
| 232 | return Some(("bank", 12)); |
| 233 | } |
| 234 | // Not 12 like the rest of the ATM chip: there are a great many cash machines and most sit |
| 235 | // inside a bank that is already carried. The chip selects `bank` as well, so it still finds |
| 236 | // something two levels out. |
| 237 | if tags.get("amenity") == Some("atm") { |
| 238 | return Some(("atm", 14)); |
| 239 | } |
| 240 | // Street furniture and the small stuff, last. |
| 241 | if matches!(tags.get("amenity"), Some("bench" | "shelter")) { |
| 242 | return Some(("bench", 16)); |
| 243 | } |
| 244 | if matches!(tags.get("amenity"), Some("toilets" | "shower")) { |
| 245 | return Some(("toilets", 16)); |
| 246 | } |
| 247 | if tags.get("amenity") == Some("drinking_water") { |
| 248 | return Some(("drinking_water", 16)); |
| 249 | } |
| 250 | if matches!(tags.get("amenity"), Some("veterinary" | "animal_shelter")) { |
| 251 | return Some(("animal", 15)); |
| 252 | } |
| 253 | // Culture that is a building first in OSM: a theatre is `amenity=theatre`, and a bare |
| 254 | // `building=yes` with a name is still something the style draws a `building` POI for. |
| 255 | if tags.get("amenity") == Some("theatre") { |
| 256 | return Some(("theatre", 14)); |
| 257 | } |
| 258 | if tags.get("building").is_some_and(|v| v != "no") && display_name(tags).is_some() { |
| 259 | return Some(("building", 15)); |
| 260 | } |
| 261 | None |
| 262 | } |
| 263 | |
| 264 | #[cfg(test)] |
| 265 | mod tests { |
| 266 | use super::*; |
| 267 | use tilecodec::mamaps::dict; |
| 268 | |
| 269 | fn classify_tags(pairs: &[(&str, &str)]) -> Option<Class> { |
| 270 | super::classify(pairs) |
| 271 | } |
| 272 | |
| 273 | fn kind_of(pairs: &[(&str, &str)]) -> String { |
| 274 | let class = classify_tags(pairs).expect("a poi"); |
| 275 | dict::KINDS[class.kind as usize - 1].to_string() |
| 276 | } |
| 277 | |
| 278 | fn zoom_of(pairs: &[(&str, &str)]) -> u8 { |
| 279 | classify_tags(pairs).expect("a poi").min_zoom |
| 280 | } |
| 281 | |
| 282 | /// A street tram stop is `railway=tram_stop`. It was matched by nothing, so trams only had a |
| 283 | /// stop where a mapper had *also* tagged a `station=tram` concourse — the exception, not the |
| 284 | /// rule, and the reason tram stops appeared to be missing from the map entirely. |
| 285 | #[test] |
| 286 | fn a_street_tram_stop_is_a_stop() { |
| 287 | assert_eq!(kind_of(&[("railway", "tram_stop")]), "bus_stop"); |
| 288 | assert_eq!(zoom_of(&[("railway", "tram_stop")]), 13); |
| 289 | // The concourse form still works, and still reads as a station. |
| 290 | assert_eq!(kind_of(&[("station", "tram")]), "station"); |
| 291 | } |
| 292 | |
| 293 | /// A category chip is useless if its results only exist at the very deepest zoom. Everything a |
| 294 | /// person would search for is carried from 13. |
| 295 | #[test] |
| 296 | fn anything_worth_searching_for_is_carried_from_zoom_13() { |
| 297 | for tags in [ |
| 298 | vec![("amenity", "restaurant")], |
| 299 | vec![("amenity", "fast_food")], |
| 300 | vec![("amenity", "cafe")], |
| 301 | vec![("amenity", "bar")], |
| 302 | vec![("amenity", "fuel")], |
| 303 | vec![("shop", "supermarket")], |
| 304 | vec![("tourism", "hotel")], |
| 305 | vec![("highway", "bus_stop")], |
| 306 | vec![("railway", "tram_stop")], |
| 307 | ] { |
| 308 | assert!( |
| 309 | zoom_of(&tags) <= 13, |
| 310 | "{tags:?} should be carried by z13, is {}", |
| 311 | zoom_of(&tags), |
| 312 | ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// The kinds a category chip selects are carried two levels shallower than they are drawn |
| 317 | /// while browsing, so tapping a chip reaches further out rather than just removing everything |
| 318 | /// else from the same screenful. The style holds the browsing floor (`browse_minzoom`); this |
| 319 | /// holds the data floor. |
| 320 | #[test] |
| 321 | fn a_chip_kind_is_carried_two_levels_before_it_is_drawn() { |
| 322 | for tags in [ |
| 323 | vec![("amenity", "restaurant")], |
| 324 | vec![("amenity", "fast_food")], |
| 325 | vec![("amenity", "cafe")], |
| 326 | vec![("amenity", "fuel")], |
| 327 | vec![("shop", "supermarket")], |
| 328 | vec![("shop", "convenience")], |
| 329 | vec![("tourism", "hotel")], |
| 330 | vec![("amenity", "bank")], |
| 331 | ] { |
| 332 | assert_eq!(zoom_of(&tags), 12, "{tags:?} is a chip kind and should be carried at z12"); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /// And the limit: street furniture stays at the deepest level. A bench two zooms out is noise, |
| 337 | /// and there are a great many of them. |
| 338 | #[test] |
| 339 | fn street_furniture_stays_at_the_deepest_level() { |
| 340 | for tags in |
| 341 | [vec![("amenity", "bench")], vec![("amenity", "toilets")], vec![("amenity", "atm")]] |
| 342 | { |
| 343 | assert_eq!(zoom_of(&tags), 14, "{tags:?} should stay at z14"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn every_style_poi_kind_is_reachable() { |
| 349 | // The style draws 41 kinds; each must be producible from real OSM tags, or the tiler |
| 350 | // ships a sprite name nothing ever carries. |
| 351 | for (tags, want) in [ |
| 352 | (vec![("natural", "beach")], "beach"), |
| 353 | (vec![("natural", "wood")], "forest"), |
| 354 | (vec![("leisure", "marina")], "marina"), |
| 355 | (vec![("leisure", "park")], "park"), |
| 356 | (vec![("natural", "peak")], "peak"), |
| 357 | (vec![("tourism", "zoo")], "zoo"), |
| 358 | (vec![("leisure", "garden")], "garden"), |
| 359 | (vec![("amenity", "bench")], "bench"), |
| 360 | (vec![("aeroway", "aerodrome")], "aerodrome"), |
| 361 | (vec![("railway", "station")], "station"), |
| 362 | (vec![("highway", "bus_stop")], "bus_stop"), |
| 363 | (vec![("amenity", "ferry_terminal")], "ferry_terminal"), |
| 364 | (vec![("leisure", "stadium")], "stadium"), |
| 365 | (vec![("amenity", "university")], "university"), |
| 366 | (vec![("amenity", "library")], "library"), |
| 367 | (vec![("amenity", "school")], "school"), |
| 368 | (vec![("amenity", "veterinary")], "animal"), |
| 369 | (vec![("amenity", "toilets")], "toilets"), |
| 370 | (vec![("amenity", "drinking_water")], "drinking_water"), |
| 371 | (vec![("amenity", "post_office")], "post_office"), |
| 372 | (vec![("building", "yes"), ("name", "Empire State Building")], "building"), |
| 373 | (vec![("amenity", "townhall")], "townhall"), |
| 374 | (vec![("amenity", "restaurant")], "restaurant"), |
| 375 | (vec![("amenity", "fast_food")], "fast_food"), |
| 376 | (vec![("amenity", "cafe")], "cafe"), |
| 377 | (vec![("amenity", "bar")], "bar"), |
| 378 | (vec![("shop", "supermarket")], "supermarket"), |
| 379 | (vec![("shop", "convenience")], "convenience"), |
| 380 | (vec![("shop", "books")], "books"), |
| 381 | (vec![("shop", "beauty")], "beauty"), |
| 382 | (vec![("shop", "electronics")], "electronics"), |
| 383 | (vec![("shop", "clothes")], "clothes"), |
| 384 | (vec![("tourism", "attraction")], "attraction"), |
| 385 | (vec![("tourism", "museum")], "museum"), |
| 386 | (vec![("tourism", "artwork")], "artwork"), |
| 387 | (vec![("amenity", "fuel")], "fuel"), |
| 388 | (vec![("tourism", "hotel")], "hotel"), |
| 389 | (vec![("tourism", "hostel")], "hotel"), |
| 390 | (vec![("amenity", "atm")], "atm"), |
| 391 | (vec![("amenity", "bank")], "bank"), |
| 392 | ] { |
| 393 | // `theatre` has no clean OSM tag mapping (the style draws `theatre` but OSM tags |
| 394 | // theatres `amenity=theatre`, handled below) — every other kind must resolve. |
| 395 | if want == "theatre" { |
| 396 | continue; |
| 397 | } |
| 398 | assert_eq!(kind_of(&tags), want, "{tags:?}"); |
| 399 | } |
| 400 | // Theatres are `amenity=theatre` in OSM. |
| 401 | assert_eq!(kind_of(&[("amenity", "theatre")]), "theatre"); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn transport_is_early_and_benches_are_late() { |
| 406 | let at = |tags: &[(&str, &str)]| classify_tags(tags).expect("poi").min_zoom; |
| 407 | assert!(at(&[("aeroway", "aerodrome")]) < at(&[("amenity", "cafe")])); |
| 408 | assert!(at(&[("railway", "station")]) < at(&[("amenity", "bench")])); |
| 409 | // Clamped to the deepest build zoom: a bench gated at z16 would be carried nowhere in a |
| 410 | // z14 build and never drawn (the renderer overzooms the deepest tile). |
| 411 | assert_eq!(at(&[("amenity", "bench")]), 14); |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn a_poi_is_a_point_in_the_poi_layer() { |
| 416 | let class = classify_tags(&[("amenity", "cafe"), ("name", "Blue Bottle")]).expect("cafe"); |
| 417 | assert_eq!(class.layer, dict::LAYER_POI); |
| 418 | assert!(!class.area, "an icon anchors at one coordinate"); |
| 419 | assert_eq!( |
| 420 | display_name(&[("name:en", "Cafe"), ("name", "Café")][..]), |
| 421 | Some("Cafe".to_string()) |
| 422 | ); |
| 423 | } |
| 424 | |
| 425 | #[test] |
| 426 | fn what_the_style_does_not_draw_is_not_a_poi() { |
| 427 | for tags in [ |
| 428 | vec![("amenity", "yes")], |
| 429 | vec![("amenity", "parking")], |
| 430 | vec![("shop", "vacant")], |
| 431 | vec![("highway", "residential")], |
| 432 | vec![], |
| 433 | ] { |
| 434 | assert!(classify_tags(&tags).is_none(), "{tags:?} should not be a poi"); |
| 435 | } |
| 436 | } |
| 437 | } |