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

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

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