Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / roads.rs
roads.rs331 lines · 14.3 KiBRaw
| 1 | //! `roads`: six kinds, thirty details, and three booleans. |
| 2 | //! |
| 3 | //! **This is where roads finally become correct.** The MVT overlay this replaces carried a road's |
| 4 | //! class and nothing else, so a bridge drew as road-coloured tarmac laid over a river and a tunnel |
| 5 | //! drew as though it were on the surface. The three flags are the whole fix, and they cost three |
| 6 | //! bits. |
| 7 | //! |
| 8 | //! # kind versus kind_detail |
| 9 | //! |
| 10 | //! `kind` is the six-way class the style paints — one colour and one width ramp per class. It is |
| 11 | //! deliberately coarse: a map that gave `tertiary` its own colour would be unreadable. |
| 12 | //! |
| 13 | //! `kind_detail` is the OSM `highway` value itself, unreduced. Nothing in the style filters on more |
| 14 | //! than four of them today, and carrying the rest is what lets a future style draw a track |
| 15 | //! differently from a motorway link without a rebuild — and what lets the differential harness |
| 16 | //! compare this generator against upstream value for value rather than class for class. |
| 17 | //! |
| 18 | //! # Minimum zoom is the whole game |
| 19 | //! |
| 20 | //! A motorway belongs on a continent and a service road does not. Six thousand miles of residential |
| 21 | //! street at z8 is not detail, it is a grey wash — and it is also most of the bytes. The `min_zoom` |
| 22 | //! column below is the single most consequential table in this crate. |
| 23 | |
| 24 | use tilecodec::mamaps::body::{ |
| 25 | Carriageway, FLAG_IS_BRIDGE, FLAG_IS_LINK, FLAG_IS_ONEWAY, FLAG_IS_TUNNEL, |
| 26 | }; |
| 27 | use tilecodec::mamaps::dict::LAYER_ROADS; |
| 28 | |
| 29 | use super::{detail, kind, Class, TagSource}; |
| 30 | |
| 31 | pub const FILTERS: &[&str] = &["highway", "railway", "aeroway", "route", "man_made"]; |
| 32 | |
| 33 | /// Every `kind` this module can emit. |
| 34 | #[cfg_attr(not(test), allow(dead_code))] |
| 35 | pub const KINDS: &[&str] = |
| 36 | &["highway", "major_road", "minor_road", "path", "other", "rail", "ferry", "aerialway"]; |
| 37 | |
| 38 | /// Every `kind_detail` this module can emit. |
| 39 | #[cfg_attr(not(test), allow(dead_code))] |
| 40 | pub const DETAILS: &[&str] = &[ |
| 41 | "motorway", |
| 42 | "motorway_link", |
| 43 | "trunk", |
| 44 | "trunk_link", |
| 45 | "primary", |
| 46 | "primary_link", |
| 47 | "secondary", |
| 48 | "secondary_link", |
| 49 | "tertiary", |
| 50 | "tertiary_link", |
| 51 | "residential", |
| 52 | "unclassified", |
| 53 | "living_street", |
| 54 | "alley", |
| 55 | "service", |
| 56 | "track", |
| 57 | "path", |
| 58 | "footway", |
| 59 | "sidewalk", |
| 60 | "crossing", |
| 61 | "steps", |
| 62 | "corridor", |
| 63 | "cycleway", |
| 64 | "pedestrian", |
| 65 | "rail", |
| 66 | "subway", |
| 67 | "tram", |
| 68 | "light_rail", |
| 69 | "turntable", |
| 70 | "runway", |
| 71 | "taxiway", |
| 72 | "pier", |
| 73 | ]; |
| 74 | |
| 75 | /// `(OSM highway value, kind, kind_detail, min_zoom)`, ordered by importance. |
| 76 | /// |
| 77 | /// The `min_zoom` column is the accumulated judgement: a motorway carries a continent, a trunk road |
| 78 | /// a country, a residential street a neighbourhood. Wrong by two levels either way and a mid-zoom |
| 79 | /// tile is either empty or a grey wash. |
| 80 | const HIGHWAYS: &[(&str, &str, &str, u8)] = &[ |
| 81 | ("motorway", "highway", "motorway", 3), |
| 82 | ("motorway_link", "highway", "motorway_link", 11), |
| 83 | ("trunk", "major_road", "trunk", 5), |
| 84 | ("trunk_link", "major_road", "trunk_link", 11), |
| 85 | ("primary", "major_road", "primary", 7), |
| 86 | ("primary_link", "major_road", "primary_link", 12), |
| 87 | ("secondary", "major_road", "secondary", 9), |
| 88 | ("secondary_link", "major_road", "secondary_link", 12), |
| 89 | ("tertiary", "major_road", "tertiary", 10), |
| 90 | ("tertiary_link", "major_road", "tertiary_link", 13), |
| 91 | ("residential", "minor_road", "residential", 12), |
| 92 | ("unclassified", "minor_road", "unclassified", 12), |
| 93 | ("living_street", "minor_road", "living_street", 13), |
| 94 | // A service road is every driveway and car-park aisle in the world. There are more of them |
| 95 | // than of every other class combined, and they are street-level detail at best. |
| 96 | ("service", "minor_road", "service", 14), |
| 97 | ("road", "other", "unclassified", 13), |
| 98 | ("track", "path", "track", 14), |
| 99 | ("path", "path", "path", 14), |
| 100 | ("footway", "path", "footway", 14), |
| 101 | ("cycleway", "path", "cycleway", 14), |
| 102 | ("bridleway", "path", "path", 14), |
| 103 | ("steps", "path", "steps", 14), |
| 104 | ("corridor", "path", "corridor", 15), |
| 105 | ("pedestrian", "path", "pedestrian", 13), |
| 106 | ]; |
| 107 | |
| 108 | /// `(OSM railway value, kind_detail, min_zoom)`. |
| 109 | /// |
| 110 | /// One `kind` between them: the style draws every rail the same, and a map that coloured a tram |
| 111 | /// differently from a subway would be a transit diagram rather than a basemap. |
| 112 | const RAILWAYS: &[(&str, &str, u8)] = &[ |
| 113 | ("rail", "rail", 8), |
| 114 | ("subway", "subway", 12), |
| 115 | ("light_rail", "light_rail", 12), |
| 116 | ("tram", "tram", 13), |
| 117 | ("narrow_gauge", "rail", 11), |
| 118 | ("monorail", "light_rail", 13), |
| 119 | ("funicular", "light_rail", 13), |
| 120 | ("turntable", "turntable", 15), |
| 121 | ]; |
| 122 | |
| 123 | pub fn classify(tags: &(impl TagSource + ?Sized)) -> Option<Class> { |
| 124 | if let Some(highway) = tags.get("highway") { |
| 125 | let (_, kind_name, detail_name, min_zoom) = |
| 126 | HIGHWAYS.iter().find(|(value, _, _, _)| *value == highway)?; |
| 127 | return Some(Class { |
| 128 | layer: LAYER_ROADS, |
| 129 | kind: kind(kind_name), |
| 130 | kind_detail: detail(detail_name), |
| 131 | flags: flags(tags, highway), |
| 132 | area: false, |
| 133 | min_zoom: *min_zoom, |
| 134 | min_area_px: 0.0, |
| 135 | }); |
| 136 | } |
| 137 | if let Some(railway) = tags.get("railway") { |
| 138 | let (_, detail_name, min_zoom) = RAILWAYS.iter().find(|(value, _, _)| *value == railway)?; |
| 139 | return Some(Class { |
| 140 | layer: LAYER_ROADS, |
| 141 | kind: kind("rail"), |
| 142 | kind_detail: detail(detail_name), |
| 143 | flags: flags(tags, railway), |
| 144 | area: false, |
| 145 | min_zoom: *min_zoom, |
| 146 | min_area_px: 0.0, |
| 147 | }); |
| 148 | } |
| 149 | // A runway is a line in this layer and a polygon in `landuse`. Both are drawn, because an |
| 150 | // airport reads as a shape with a stripe down it. |
| 151 | if let Some(aeroway) = tags.get("aeroway") { |
| 152 | let detail_name = match aeroway { |
| 153 | "runway" => "runway", |
| 154 | "taxiway" => "taxiway", |
| 155 | _ => return None, |
| 156 | }; |
| 157 | return Some(Class { |
| 158 | layer: LAYER_ROADS, |
| 159 | kind: kind("other"), |
| 160 | kind_detail: detail(detail_name), |
| 161 | flags: flags(tags, aeroway), |
| 162 | area: false, |
| 163 | min_zoom: if aeroway == "runway" { 9 } else { 13 }, |
| 164 | min_area_px: 0.0, |
| 165 | }); |
| 166 | } |
| 167 | // A pier is walkable, so it belongs with the paths rather than with the buildings. |
| 168 | if tags.get("man_made") == Some("pier") { |
| 169 | return Some(Class { |
| 170 | layer: LAYER_ROADS, |
| 171 | kind: kind("path"), |
| 172 | kind_detail: detail("pier"), |
| 173 | flags: flags(tags, "pier"), |
| 174 | area: false, |
| 175 | min_zoom: 13, |
| 176 | min_area_px: 0.0, |
| 177 | }); |
| 178 | } |
| 179 | // A ferry is a route, and drawing it is what stops a coastal map looking disconnected. |
| 180 | if tags.get("route") == Some("ferry") { |
| 181 | return Some(Class::line(LAYER_ROADS, kind("ferry"), 9)); |
| 182 | } |
| 183 | None |
| 184 | } |
| 185 | |
| 186 | /// The four booleans, and the reason this layer was worth redoing. |
| 187 | /// |
| 188 | /// `is_link` is derived from the class name rather than read from a tag, because OSM spells a slip |
| 189 | /// road as `highway=motorway_link` and there is no `link=yes`. That matches what upstream emits and |
| 190 | /// what the style filters with `!has is_link`. |
| 191 | /// |
| 192 | /// `is_oneway` is a flag rather than side-table data because it is what decides whether a |
| 193 | /// carriageway has a centre line at all, and because a flag is part of `coalesce`'s merge key — |
| 194 | /// so a one-way and a two-way of the same class cannot collapse into one feature wearing |
| 195 | /// whichever direction came first. |
| 196 | fn flags(tags: &(impl TagSource + ?Sized), value: &str) -> u8 { |
| 197 | let mut flags = 0u8; |
| 198 | // `tunnel=building_passage` is a tunnel; only `no` is not. Same for a bridge tagged `viaduct` |
| 199 | // or `boardwalk`. |
| 200 | if tags.truthy("tunnel") || tags.get("covered") == Some("yes") { |
| 201 | flags |= FLAG_IS_TUNNEL; |
| 202 | } |
| 203 | if tags.truthy("bridge") { |
| 204 | flags |= FLAG_IS_BRIDGE; |
| 205 | } |
| 206 | if value.ends_with("_link") { |
| 207 | flags |= FLAG_IS_LINK; |
| 208 | } |
| 209 | // `oneway=yes` and only that, which is the test [`osm_ingest::roads::is_oneway`] applies, so a |
| 210 | // road cannot be one-way to the router and two-way to the carriageway drawn under it. |
| 211 | // `oneway=-1` is a direction rather than a flag and neither models it. |
| 212 | if tags.get("oneway") == Some("yes") { |
| 213 | flags |= FLAG_IS_ONEWAY; |
| 214 | } |
| 215 | flags |
| 216 | } |
| 217 | |
| 218 | /// The carriageway lane count baked into a `roads` feature, or zero when the way carries no |
| 219 | /// `lanes` tag. |
| 220 | /// |
| 221 | /// The OSM `lanes` total (both directions), parsed by the same [`osm_ingest::tags::parse_int_tag`] |
| 222 | /// the routing graph and the `roads.pmtiles` layer use, so a road cannot describe its lane count |
| 223 | /// one way to the router and another to the basemap. Capped at [`osm_ingest::tags::MAX_LANES`] |
| 224 | /// (64), which fits a byte — the renderer stores it in the feature record's last byte and expands |
| 225 | /// it into that many parallel lanes with dividers at high zoom. A mistagged `lanes=999999999` |
| 226 | /// therefore becomes the cap rather than an absurd fan. |
| 227 | /// |
| 228 | /// Only the total is baked, not the per-lane `turn:lanes` masks: those are variable length and |
| 229 | /// belong in a side table (the turn-arrow pass), while the count is one byte the parallel-lane |
| 230 | /// geometry needs first. |
| 231 | pub fn lane_count(tags: &(impl TagSource + ?Sized)) -> u8 { |
| 232 | osm_ingest::tags::parse_int_tag(tags.get("lanes")).min(osm_ingest::tags::MAX_LANES) as u8 |
| 233 | } |
| 234 | |
| 235 | /// The per-lane turn-indication masks for a road, `(forward, backward)`, each a left-to-right |
| 236 | /// list of `LANE_*` bit sets from OSM `turn:lanes[:forward|:backward]`. |
| 237 | /// |
| 238 | /// A verbatim reuse of the routing graph's own derivation |
| 239 | /// ([`osm_ingest::roads::lane_masks`]) via a [`RoadTags`](osm_ingest::roads::RoadTags) built from |
| 240 | /// this tag source, so a road cannot describe its lanes one way to the router and another to the |
| 241 | /// arrows drawn over it. Empty vectors when the way has no `turn:lanes`, which is almost every |
| 242 | /// road. Forward lanes are traversed toward the way's end (its junction), backward toward its |
| 243 | /// start — which is where the renderer places each direction's arrows. |
| 244 | pub fn turn_masks(tags: &(impl TagSource + ?Sized)) -> (Vec<u16>, Vec<u16>) { |
| 245 | let rt = osm_ingest::roads::RoadTags { |
| 246 | highway: tags.get("highway"), |
| 247 | lanes: tags.get("lanes"), |
| 248 | lanes_forward: tags.get("lanes:forward"), |
| 249 | lanes_backward: tags.get("lanes:backward"), |
| 250 | turn_lanes: tags.get("turn:lanes"), |
| 251 | turn_lanes_forward: tags.get("turn:lanes:forward"), |
| 252 | turn_lanes_backward: tags.get("turn:lanes:backward"), |
| 253 | oneway: tags.get("oneway"), |
| 254 | ..Default::default() |
| 255 | }; |
| 256 | osm_ingest::roads::lane_masks(&rt) |
| 257 | } |
| 258 | |
| 259 | /// How a road's lanes divide between the two directions, and which of the dividers between them |
| 260 | /// may not be crossed. |
| 261 | /// |
| 262 | /// [`lane_count`] is the total; this is what the surface renderer needs on top of it to put the |
| 263 | /// centre line anywhere but the middle. `forward` runs toward the way's last point and `backward` |
| 264 | /// toward its first, the same convention [`turn_masks`] uses, and `oneway` is decided by |
| 265 | /// [`osm_ingest::roads::is_oneway`] so the split agrees with how the router traverses the road. |
| 266 | /// |
| 267 | /// **An absent split stays absent.** A two-way road tagged only `lanes=4` comes back all-zero |
| 268 | /// rather than as 2/2, because 2/2 is a guess dressed as a survey — the renderer already draws an |
| 269 | /// unknown split down the middle, and on an odd total the guess would be wrong rather than |
| 270 | /// unhelpful. All-zero is also what lets a tile of untagged residential streets carry no |
| 271 | /// carriageway table at all. |
| 272 | pub fn carriageway(tags: &(impl TagSource + ?Sized)) -> Carriageway { |
| 273 | let total = lane_count(tags) as u32; |
| 274 | let forward = osm_ingest::tags::parse_int_tag(tags.get("lanes:forward")); |
| 275 | let backward = osm_ingest::tags::parse_int_tag(tags.get("lanes:backward")); |
| 276 | let (forward, backward) = if tags.get("oneway") == Some("yes") { |
| 277 | // Every lane runs one way, so the total is the forward count even when the way also |
| 278 | // carries a `lanes:forward` that agrees with it. |
| 279 | (if forward > 0 { forward } else { total }, 0) |
| 280 | } else { |
| 281 | // One side tagged implies the other, and the pair is the more common tagging than either |
| 282 | // alone. Saturating rather than wrapping: `lanes=2` with `lanes:backward=3` is a mistagged |
| 283 | // road, and the answer to it is "nothing known about the other side". |
| 284 | ( |
| 285 | if forward > 0 { forward } else { total.saturating_sub(backward) }, |
| 286 | if backward > 0 { backward } else { total.saturating_sub(forward) }, |
| 287 | ) |
| 288 | }; |
| 289 | // A split that does not add up to the total is not a split: the renderer reads the two |
| 290 | // together, so 3 forward and 4 backward of five lanes would be a shape it cannot draw. The |
| 291 | // dividers survive it — they are a property of the total, not of the division. |
| 292 | let (forward, backward) = |
| 293 | if forward + backward == total { (forward, backward) } else { (0, 0) }; |
| 294 | Carriageway { |
| 295 | forward: forward.min(osm_ingest::tags::MAX_LANES) as u8, |
| 296 | backward: backward.min(osm_ingest::tags::MAX_LANES) as u8, |
| 297 | solid_dividers: solid_dividers(tags.get("change:lanes"), total), |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /// The interior dividers a lane change is prohibited across, a bit each from the leftmost. |
| 302 | /// |
| 303 | /// Only the unsuffixed `change:lanes` is read. The `:forward`/`:backward` pair describes each |
| 304 | /// direction's lanes in *that direction's* left-to-right order, so stitching the two into one |
| 305 | /// carriageway-wide ordering needs the driving side — which is a property of the tile rather than |
| 306 | /// of the way and is not known here. The plain tag is already ordered the way this field is. |
| 307 | /// |
| 308 | /// A divider is solid when either lane it separates forbids crossing it: lane `k` tagged |
| 309 | /// `not_right` or `no`, or lane `k + 1` tagged `not_left` or `no`. |
| 310 | fn solid_dividers(spec: Option<&str>, lanes: u32) -> u32 { |
| 311 | let Some(spec) = spec.filter(|s| !s.is_empty()) else { |
| 312 | return 0; |
| 313 | }; |
| 314 | let values: Vec<&str> = spec.split('|').map(str::trim).collect(); |
| 315 | // A list that does not describe this road's lanes describes some other road's, and half of it |
| 316 | // applied to this one would draw solid lines down the wrong gaps. |
| 317 | if values.len() as u32 != lanes { |
| 318 | return 0; |
| 319 | } |
| 320 | let mut bits = 0u32; |
| 321 | // A road with more than 32 interior dividers has none recorded past the 32nd, which no real |
| 322 | // road reaches. |
| 323 | for k in 0..values.len().saturating_sub(1).min(32) { |
| 324 | let blocked = |
| 325 | matches!(values[k], "no" | "not_right") || matches!(values[k + 1], "no" | "not_left"); |
| 326 | if blocked { |
| 327 | bits |= 1 << k; |
| 328 | } |
| 329 | } |
| 330 | bits |
| 331 | } |