Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / schema / poi.rs

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