Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / roads_extra.rs
roads_extra.rs280 lines · 12.8 KiBRaw
| 1 | //! Tests for [`super::roads`], kept beside the schema rules they pin. |
| 2 | #[cfg(test)] |
| 3 | mod tests { |
| 4 | use super::super::roads::{carriageway, classify, lane_count, turn_masks}; |
| 5 | use super::super::Class; |
| 6 | use tilecodec::mamaps::body::{ |
| 7 | Carriageway, FLAG_IS_BRIDGE, FLAG_IS_LINK, FLAG_IS_ONEWAY, FLAG_IS_TUNNEL, |
| 8 | }; |
| 9 | use tilecodec::mamaps::dict; |
| 10 | |
| 11 | fn classify_tags(pairs: &[(&str, &str)]) -> Option<Class> { |
| 12 | classify(pairs) |
| 13 | } |
| 14 | |
| 15 | fn names(class: &Class) -> (&'static str, &'static str) { |
| 16 | (dict::KINDS[class.kind as usize - 1], dict::DETAILS[class.kind_detail as usize - 1]) |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn a_road_carries_a_coarse_kind_and_its_exact_osm_class() { |
| 21 | let motorway = classify_tags(&[("highway", "motorway")]).expect("motorway"); |
| 22 | assert_eq!(names(&motorway), ("highway", "motorway")); |
| 23 | assert_eq!(motorway.layer, dict::LAYER_ROADS); |
| 24 | assert!(!motorway.area, "a road is a line"); |
| 25 | |
| 26 | // Four OSM classes collapse to one drawn colour, and each keeps its own detail. |
| 27 | for (value, detail) in [ |
| 28 | ("trunk", "trunk"), |
| 29 | ("primary", "primary"), |
| 30 | ("secondary", "secondary"), |
| 31 | ("tertiary", "tertiary"), |
| 32 | ] { |
| 33 | let class = classify_tags(&[("highway", value)]).expect(value); |
| 34 | assert_eq!(names(&class), ("major_road", detail)); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// **The fix this layer was redone for.** Without these three bits a bridge is tarmac laid over |
| 39 | /// a river and a tunnel is a road on the surface. |
| 40 | #[test] |
| 41 | fn a_bridge_a_tunnel_and_a_slip_road_are_all_flagged() { |
| 42 | let bridge = classify_tags(&[("highway", "primary"), ("bridge", "yes")]).expect("bridge"); |
| 43 | assert_eq!(bridge.flags, FLAG_IS_BRIDGE); |
| 44 | let tunnel = classify_tags(&[("highway", "primary"), ("tunnel", "yes")]).expect("tunnel"); |
| 45 | assert_eq!(tunnel.flags, FLAG_IS_TUNNEL); |
| 46 | // A slip road is spelled in the class, not in a tag of its own. |
| 47 | let link = classify_tags(&[("highway", "motorway_link")]).expect("link"); |
| 48 | assert_eq!(link.flags, FLAG_IS_LINK); |
| 49 | assert_eq!(names(&link), ("highway", "motorway_link")); |
| 50 | // And they combine: a flyover slip road is both. |
| 51 | let both = |
| 52 | classify_tags(&[("highway", "motorway_link"), ("bridge", "viaduct")]).expect("both"); |
| 53 | assert_eq!(both.flags, FLAG_IS_LINK | FLAG_IS_BRIDGE); |
| 54 | // Only `no` is not a bridge. `bridge=viaduct` and `bridge=boardwalk` are. |
| 55 | let flat = classify_tags(&[("highway", "primary"), ("bridge", "no")]).expect("flat"); |
| 56 | assert_eq!(flat.flags, 0); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn a_covered_way_counts_as_a_tunnel() { |
| 61 | // `covered=yes` is how an arcade or a building passage is tagged when it is not a tunnel |
| 62 | // proper, and it draws the same way. |
| 63 | let covered = |
| 64 | classify_tags(&[("highway", "footway"), ("covered", "yes")]).expect("covered"); |
| 65 | assert_eq!(covered.flags, FLAG_IS_TUNNEL); |
| 66 | } |
| 67 | |
| 68 | /// The most consequential column in this crate. A motorway carries a continent; a service road |
| 69 | /// is every driveway in the world and there are more of them than of everything else combined. |
| 70 | #[test] |
| 71 | fn the_minimum_zooms_run_from_continent_to_street() { |
| 72 | let at = |value: &str| classify_tags(&[("highway", value)]).expect(value).min_zoom; |
| 73 | assert_eq!(at("motorway"), 3, "a motorway is a continental feature"); |
| 74 | assert!(at("trunk") < at("primary")); |
| 75 | assert!(at("primary") < at("secondary")); |
| 76 | assert!(at("secondary") < at("tertiary")); |
| 77 | assert!(at("tertiary") < at("residential")); |
| 78 | assert!(at("residential") < at("service")); |
| 79 | assert_eq!(at("service"), 14, "a driveway is street-level at best"); |
| 80 | // A slip road is not carried shallower than the road it joins, which would draw a |
| 81 | // disembodied stub. |
| 82 | assert!(at("motorway_link") > at("motorway")); |
| 83 | assert!(at("primary_link") > at("primary")); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn every_railway_draws_as_one_kind_with_its_own_detail() { |
| 88 | for (value, expected) in |
| 89 | [("rail", "rail"), ("subway", "subway"), ("tram", "tram"), ("narrow_gauge", "rail")] |
| 90 | { |
| 91 | let class = classify_tags(&[("railway", value)]).expect(value); |
| 92 | assert_eq!(names(&class), ("rail", expected)); |
| 93 | } |
| 94 | // A rail line is a country-scale feature; a tram is not. |
| 95 | let rail = classify_tags(&[("railway", "rail")]).expect("rail"); |
| 96 | let tram = classify_tags(&[("railway", "tram")]).expect("tram"); |
| 97 | assert!(rail.min_zoom < tram.min_zoom); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn a_runway_a_pier_and_a_ferry_are_carried_in_this_layer() { |
| 102 | let runway = classify_tags(&[("aeroway", "runway")]).expect("runway"); |
| 103 | assert_eq!(names(&runway), ("other", "runway")); |
| 104 | let pier = classify_tags(&[("man_made", "pier")]).expect("pier"); |
| 105 | assert_eq!(names(&pier), ("path", "pier"), "a pier is walkable"); |
| 106 | let ferry = classify_tags(&[("route", "ferry")]).expect("ferry"); |
| 107 | assert_eq!(dict::KINDS[ferry.kind as usize - 1], "ferry"); |
| 108 | } |
| 109 | |
| 110 | /// `highway` is asked before `railway`, which matters at a level crossing: a way tagged both is |
| 111 | /// the road, because that is what carries traffic. |
| 112 | #[test] |
| 113 | fn highway_is_asked_before_railway() { |
| 114 | let crossing = |
| 115 | classify_tags(&[("railway", "rail"), ("highway", "residential")]).expect("crossing"); |
| 116 | assert_eq!(names(&crossing).0, "minor_road"); |
| 117 | } |
| 118 | |
| 119 | /// The carriageway lane count baked for the renderer's parallel-lane draw: the OSM `lanes` |
| 120 | /// total, zero when absent, and capped so a mistagged count cannot become an absurd fan. |
| 121 | #[test] |
| 122 | fn the_lane_count_is_the_capped_osm_lanes_total() { |
| 123 | assert_eq!(lane_count(&[("highway", "primary"), ("lanes", "4")][..]), 4); |
| 124 | assert_eq!(lane_count(&[("highway", "residential")][..]), 0, "no tag, no lanes"); |
| 125 | assert_eq!(lane_count(&[("highway", "primary"), ("lanes", "0")][..]), 0); |
| 126 | // A byte, and never past the shared MAX_LANES the router and pmtiles layer use. |
| 127 | assert_eq!( |
| 128 | lane_count(&[("highway", "motorway"), ("lanes", "999999999")][..]), |
| 129 | osm_ingest::tags::MAX_LANES as u8, |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /// The per-lane turn masks reuse the routing graph's derivation, so the arrows drawn over a |
| 134 | /// road agree with how it is routed: a oneway's plain `turn:lanes` is forward, a two-way's is |
| 135 | /// neither, and the masks are the graph's `LANE_*` bits left to right. |
| 136 | #[test] |
| 137 | fn the_turn_masks_match_the_routing_graphs_derivation() { |
| 138 | use osm_ingest::tags::{LANE_LEFT, LANE_RIGHT, LANE_THROUGH}; |
| 139 | // A oneway carries its plain `turn:lanes` as forward, none backward. |
| 140 | let oneway: &[(&str, &str)] = &[ |
| 141 | ("highway", "primary"), |
| 142 | ("oneway", "yes"), |
| 143 | ("turn:lanes", "left|through|through;right"), |
| 144 | ]; |
| 145 | let (fwd, bwd) = turn_masks(oneway); |
| 146 | assert_eq!(fwd, vec![LANE_LEFT, LANE_THROUGH, LANE_THROUGH | LANE_RIGHT]); |
| 147 | assert!(bwd.is_empty()); |
| 148 | // Explicit forward/backward split on a two-way street. |
| 149 | let split: &[(&str, &str)] = &[ |
| 150 | ("highway", "secondary"), |
| 151 | ("turn:lanes:forward", "through|right"), |
| 152 | ("turn:lanes:backward", "left"), |
| 153 | ]; |
| 154 | let (fwd, bwd) = turn_masks(split); |
| 155 | assert_eq!(fwd, vec![LANE_THROUGH, LANE_RIGHT]); |
| 156 | assert_eq!(bwd, vec![LANE_LEFT]); |
| 157 | // A road with no turn tags carries nothing either way. |
| 158 | let (fwd, bwd) = turn_masks(&[("highway", "residential")][..]); |
| 159 | assert!(fwd.is_empty() && bwd.is_empty()); |
| 160 | } |
| 161 | |
| 162 | /// A one-way is a flag rather than side-table data: it is what decides whether the carriageway |
| 163 | /// has a centre line at all, and `coalesce` keys on flags — so a one-way and a two-way of the |
| 164 | /// same class cannot merge into one feature wearing whichever direction came first. |
| 165 | #[test] |
| 166 | fn a_oneway_is_flagged_and_only_oneway_yes_counts() { |
| 167 | let oneway = classify_tags(&[("highway", "primary"), ("oneway", "yes")]).expect("oneway"); |
| 168 | assert_eq!(oneway.flags, FLAG_IS_ONEWAY); |
| 169 | // The same test the routing graph applies: `-1` is a direction rather than a flag, and |
| 170 | // neither models it. |
| 171 | for value in ["no", "-1", "reversible", "alternating", ""] { |
| 172 | let class = classify_tags(&[("highway", "primary"), ("oneway", value)]).expect(value); |
| 173 | assert_eq!(class.flags, 0, "oneway={value} is not a one-way here"); |
| 174 | } |
| 175 | // And it combines with the other three. |
| 176 | let ramp = |
| 177 | classify_tags(&[("highway", "motorway_link"), ("oneway", "yes"), ("bridge", "yes")]) |
| 178 | .expect("ramp"); |
| 179 | assert_eq!(ramp.flags, FLAG_IS_LINK | FLAG_IS_BRIDGE | FLAG_IS_ONEWAY); |
| 180 | } |
| 181 | |
| 182 | /// The directional split the surface renderer places a centre line from. A one-way puts every |
| 183 | /// lane forward; a two-way needs one side tagged and infers the other from the total. |
| 184 | #[test] |
| 185 | fn the_carriageway_divides_the_lane_total_between_the_directions() { |
| 186 | let split = |pairs: &[(&str, &str)]| carriageway(pairs); |
| 187 | assert_eq!( |
| 188 | split(&[("highway", "motorway"), ("oneway", "yes"), ("lanes", "3")]), |
| 189 | Carriageway { forward: 3, backward: 0, solid_dividers: 0 }, |
| 190 | "every lane of a one-way runs forward", |
| 191 | ); |
| 192 | assert_eq!( |
| 193 | split(&[("highway", "primary"), ("lanes", "4"), ("lanes:backward", "1")]), |
| 194 | Carriageway { forward: 3, backward: 1, solid_dividers: 0 }, |
| 195 | "one side tagged implies the other", |
| 196 | ); |
| 197 | assert_eq!( |
| 198 | split(&[ |
| 199 | ("highway", "primary"), |
| 200 | ("lanes", "5"), |
| 201 | ("lanes:forward", "3"), |
| 202 | ("lanes:backward", "2"), |
| 203 | ]), |
| 204 | Carriageway { forward: 3, backward: 2, solid_dividers: 0 }, |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | /// **An absent split stays absent.** 2/2 for a road tagged only `lanes=4` would be a guess |
| 209 | /// dressed as a survey, and the renderer already draws an unknown split down the middle. It is |
| 210 | /// also what lets a tile of untagged residential streets carry no carriageway table at all. |
| 211 | #[test] |
| 212 | fn a_split_that_was_never_surveyed_or_does_not_add_up_is_left_unknown() { |
| 213 | let split = |pairs: &[(&str, &str)]| carriageway(pairs); |
| 214 | assert_eq!( |
| 215 | split(&[("highway", "primary"), ("lanes", "4")]), |
| 216 | Carriageway::default(), |
| 217 | "a bare total says nothing about the division", |
| 218 | ); |
| 219 | assert_eq!(split(&[("highway", "residential")][..]), Carriageway::default()); |
| 220 | // Mistagged: three forward and three backward of four lanes is a shape nothing can draw. |
| 221 | assert_eq!( |
| 222 | split(&[ |
| 223 | ("highway", "primary"), |
| 224 | ("lanes", "4"), |
| 225 | ("lanes:forward", "3"), |
| 226 | ("lanes:backward", "3"), |
| 227 | ]), |
| 228 | Carriageway::default(), |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | /// `change:lanes` marks the gaps a lane change is prohibited across, one bit per interior |
| 233 | /// divider from the leftmost. Either lane can forbid the crossing. |
| 234 | #[test] |
| 235 | fn a_prohibited_lane_change_becomes_a_solid_divider() { |
| 236 | let dividers = |pairs: &[(&str, &str)]| carriageway(pairs).solid_dividers; |
| 237 | // Four lanes, three interior dividers. `not_right` on lane 0 makes divider 0 solid, `no` |
| 238 | // on lane 2 makes dividers 1 and 2 solid. |
| 239 | assert_eq!( |
| 240 | dividers(&[ |
| 241 | ("highway", "primary"), |
| 242 | ("lanes", "4"), |
| 243 | ("change:lanes", "not_right|yes|no|yes"), |
| 244 | ]), |
| 245 | 0b111, |
| 246 | ); |
| 247 | assert_eq!( |
| 248 | dividers(&[("highway", "primary"), ("lanes", "2"), ("change:lanes", "yes|yes")]), |
| 249 | 0, |
| 250 | "a road nothing is prohibited on has no solid dividers", |
| 251 | ); |
| 252 | // A list that does not describe this road's lanes describes some other road's, and half of |
| 253 | // it applied here would draw solid lines down the wrong gaps. |
| 254 | assert_eq!( |
| 255 | dividers(&[("highway", "primary"), ("lanes", "4"), ("change:lanes", "no|no")]), |
| 256 | 0, |
| 257 | ); |
| 258 | // The dividers are a property of the total, so they survive an unusable split. |
| 259 | assert_eq!( |
| 260 | dividers(&[("highway", "primary"), ("lanes", "3"), ("change:lanes", "yes|no|yes")]), |
| 261 | 0b11, |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn an_unrecognised_value_is_not_a_road() { |
| 267 | for tags in [ |
| 268 | vec![("highway", "bus_stop")], |
| 269 | vec![("highway", "street_lamp")], |
| 270 | vec![("railway", "abandoned")], |
| 271 | vec![("aeroway", "gate")], |
| 272 | vec![("man_made", "tower")], |
| 273 | vec![("route", "bicycle")], |
| 274 | vec![("building", "yes")], |
| 275 | vec![], |
| 276 | ] { |
| 277 | assert!(classify_tags(&tags).is_none(), "{tags:?} should not be a road"); |
| 278 | } |
| 279 | } |
| 280 | } |