Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / transit_s1.rs
transit_s1.rs266 lines · 11.6 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | use tilecodec::mamaps::dict; |
| 5 | |
| 6 | fn station_mode(pairs: &[(&str, &str)]) -> &'static str { |
| 7 | super::station_detail(pairs) |
| 8 | } |
| 9 | |
| 10 | /// One `LineString` per line, written by `transit_shapes`. |
| 11 | fn feature(color: &str, mode: &str, coords: &str) -> String { |
| 12 | format!( |
| 13 | "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ |
| 14 | \"coordinates\":[{coords}]}},\"properties\":{{\"color\":\"{color}\",\ |
| 15 | \"mode\":\"{mode}\"}}}}" |
| 16 | ) |
| 17 | } |
| 18 | |
| 19 | /// The same, carrying the corridor lane inputs. |
| 20 | fn slotted(color: &str, mode: &str, ordinal: u8, lanes: u8, taper: u8, coords: &str) -> String { |
| 21 | format!( |
| 22 | "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ |
| 23 | \"coordinates\":[{coords}]}},\"properties\":{{\"color\":\"{color}\",\ |
| 24 | \"mode\":\"{mode}\",\"ordinal\":{ordinal},\"lanes\":{lanes},\ |
| 25 | \"taper\":{taper}}}}}" |
| 26 | ) |
| 27 | } |
| 28 | |
| 29 | fn scratch(tag: &str) -> std::path::PathBuf { |
| 30 | std::env::temp_dir().join(format!("mamaps_transit_{tag}_{}", std::process::id())) |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn every_mode_has_a_class_and_they_all_share_one_floor() { |
| 35 | for mode in MODES { |
| 36 | let class = transit_class(mode, false).expect("a class"); |
| 37 | assert_eq!(class.layer, dict::LAYER_TRANSIT); |
| 38 | assert_eq!(dict::KINDS[class.kind as usize - 1], "rail"); |
| 39 | assert_eq!(dict::DETAILS[class.kind_detail as usize - 1], *mode); |
| 40 | assert!(!class.area, "a line, even looped"); |
| 41 | // The whole network appears at once; see [`MIN_ZOOM`]. The style's `transit-rail` |
| 42 | // carries the same 8, and a floor deeper than that here would leave it asking for |
| 43 | // features the archive does not hold. |
| 44 | assert_eq!(class.min_zoom, 8, "{mode}"); |
| 45 | } |
| 46 | assert!(transit_class("bus", false).is_none()); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn an_intercity_line_surfaces_at_country_zoom() { |
| 51 | let class = transit_class("train", true).expect("a class"); |
| 52 | assert_eq!(dict::DETAILS[class.kind_detail as usize - 1], "intercity"); |
| 53 | assert_eq!(class.min_zoom, 3); |
| 54 | assert_eq!(dict::KINDS[class.kind as usize - 1], "rail", "still drawn by transit-rail"); |
| 55 | assert!(transit_class("bus", true).is_none(), "still only the rail modes"); |
| 56 | |
| 57 | let path = scratch("intercity.geojsonseq"); |
| 58 | let coords = "[-122.4,37.7],[-118.2,34.0]"; |
| 59 | std::fs::write( |
| 60 | &path, |
| 61 | format!( |
| 62 | "{}\n{}\n", |
| 63 | feature("0057A8", "train", coords) |
| 64 | .replace("\"mode\":\"train\"", "\"mode\":\"train\",\"intercity\":1"), |
| 65 | feature("0057A8", "train", coords), |
| 66 | ), |
| 67 | ) |
| 68 | .expect("write"); |
| 69 | let spill = scratch("intercity.features"); |
| 70 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 71 | assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 2); |
| 72 | let store = sink.finish(&spill).expect("finish"); |
| 73 | let mut reader = store.reader().expect("reader"); |
| 74 | let floors: Vec<u8> = |
| 75 | std::iter::from_fn(|| reader.next().expect("read")).map(|f| f.class.min_zoom).collect(); |
| 76 | assert_eq!(floors, vec![3, 8], "flagged, then an ordinary line"); |
| 77 | |
| 78 | std::fs::write( |
| 79 | &path, |
| 80 | feature("0057A8", "train", coords) |
| 81 | .replace("\"mode\":\"train\"", "\"mode\":\"train\",\"intercity\":2") |
| 82 | + "\n", |
| 83 | ) |
| 84 | .expect("write"); |
| 85 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 86 | assert!( |
| 87 | stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).is_err(), |
| 88 | "2 is not a flag" |
| 89 | ); |
| 90 | let _ = std::fs::remove_file(&path); |
| 91 | let _ = std::fs::remove_file(&spill); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn colours_are_nonzero_bare_hex() { |
| 96 | assert_eq!(hex_rrggbb("E4002B"), Some(0xE4002B)); |
| 97 | assert_eq!(hex_rrggbb(" e4002b "), Some(0xE4002B)); |
| 98 | assert_eq!(hex_rrggbb("#E4002B"), None, "the exporter writes bare hex"); |
| 99 | assert_eq!(hex_rrggbb("F00"), None, "no short form"); |
| 100 | assert_eq!(hex_rrggbb("GGGGGG"), None); |
| 101 | // Zero means "no colour" on the wire, so it must never reach `push_transit`. |
| 102 | assert_eq!(hex_rrggbb("000000"), None); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn a_prepared_route_streams_in_as_a_coloured_transit_line() { |
| 107 | let path = scratch("routes.geojsonseq"); |
| 108 | std::fs::write( |
| 109 | &path, |
| 110 | format!( |
| 111 | "{}\n{}\n", |
| 112 | feature("0054A5", "subway", "[-122.4,37.7],[-122.39,37.71]"), |
| 113 | feature("E31E24", "light_rail", "[-122.4,37.7],[-122.41,37.72]"), |
| 114 | ), |
| 115 | ) |
| 116 | .expect("write"); |
| 117 | let spill = scratch("routes.features"); |
| 118 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 119 | assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 2,); |
| 120 | let store = sink.finish(&spill).expect("finish"); |
| 121 | |
| 122 | let mut reader = store.reader().expect("reader"); |
| 123 | let first = reader.next().expect("read").expect("a feature"); |
| 124 | assert_eq!(first.class.layer, dict::LAYER_TRANSIT); |
| 125 | assert_eq!(dict::KINDS[first.class.kind as usize - 1], "rail"); |
| 126 | assert_eq!(dict::DETAILS[first.class.kind_detail as usize - 1], "subway"); |
| 127 | assert_eq!(first.transit_color, 0x00_54_A5, "the agency's own colour, verbatim"); |
| 128 | assert_eq!( |
| 129 | (first.transit_ordinal, first.transit_lanes, first.transit_taper), |
| 130 | (0, 1, 255), |
| 131 | "absent lane inputs are a route on its own alignment", |
| 132 | ); |
| 133 | let second = reader.next().expect("read").expect("a feature"); |
| 134 | assert_eq!(second.transit_color, 0xE3_1E_24); |
| 135 | assert_eq!(second.class.min_zoom, 8, "the network's single floor"); |
| 136 | |
| 137 | let _ = std::fs::remove_file(&path); |
| 138 | let _ = std::fs::remove_file(&spill); |
| 139 | } |
| 140 | |
| 141 | /// The lane inputs ride through to the feature untouched — the renderer, not the exporter, |
| 142 | /// decides how many lanes to use, so nothing here clamps the count. |
| 143 | #[test] |
| 144 | fn the_corridor_lane_inputs_stream_through_to_the_feature() { |
| 145 | let path = scratch("slots.geojsonseq"); |
| 146 | std::fs::write( |
| 147 | &path, |
| 148 | format!( |
| 149 | "{}\n{}\n{}\n", |
| 150 | slotted("0054A5", "subway", 0, 10, 255, "[-122.4,37.7],[-122.39,37.71]"), |
| 151 | slotted("0054A5", "subway", 9, 10, 128, "[-122.4,37.7],[-122.39,37.71]"), |
| 152 | slotted("0054A5", "subway", 0, 1, 255, "[-122.4,37.7],[-122.39,37.71]"), |
| 153 | ), |
| 154 | ) |
| 155 | .expect("write"); |
| 156 | let spill = scratch("slots.features"); |
| 157 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 158 | assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 3); |
| 159 | let store = sink.finish(&spill).expect("finish"); |
| 160 | |
| 161 | let mut reader = store.reader().expect("reader"); |
| 162 | let mut read = Vec::new(); |
| 163 | while let Some(feature) = reader.next().expect("read") { |
| 164 | read.push((feature.transit_ordinal, feature.transit_lanes, feature.transit_taper)); |
| 165 | } |
| 166 | assert_eq!(read, vec![(0, 10, 255), (9, 10, 128), (0, 1, 255)]); |
| 167 | |
| 168 | let _ = std::fs::remove_file(&path); |
| 169 | let _ = std::fs::remove_file(&spill); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn a_lane_input_that_is_not_a_byte_is_an_error() { |
| 174 | let spill = scratch("badslot.features"); |
| 175 | let path = scratch("badslot.geojsonseq"); |
| 176 | for body in ["\"ordinal\":\"left\"", "\"ordinal\":1.5", "\"lanes\":99999", "\"taper\":-1"] { |
| 177 | std::fs::write( |
| 178 | &path, |
| 179 | format!( |
| 180 | "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ |
| 181 | \"coordinates\":[[-122.4,37.7],[-122.39,37.71]]}},\"properties\":\ |
| 182 | {{\"color\":\"0054A5\",\"mode\":\"subway\",{body}}}}}\n" |
| 183 | ), |
| 184 | ) |
| 185 | .expect("write"); |
| 186 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 187 | assert!( |
| 188 | stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).is_err(), |
| 189 | "{body} was accepted", |
| 190 | ); |
| 191 | } |
| 192 | let _ = std::fs::remove_file(&path); |
| 193 | let _ = std::fs::remove_file(&spill); |
| 194 | } |
| 195 | |
| 196 | /// The same clip `earth` does, for the same reason: the file covers whatever regions were |
| 197 | /// exported, and a corridor build must not carry lines from outside it. |
| 198 | #[test] |
| 199 | fn a_route_outside_the_build_area_is_not_carried() { |
| 200 | let path = scratch("clip.geojsonseq"); |
| 201 | std::fs::write( |
| 202 | &path, |
| 203 | format!( |
| 204 | "{}\n{}\n", |
| 205 | feature("0054A5", "subway", "[-122.4,37.7],[-122.39,37.71]"), |
| 206 | feature("0054A5", "train", "[-9.0,38.0],[-8.9,38.1]"), |
| 207 | ), |
| 208 | ) |
| 209 | .expect("write"); |
| 210 | let spill = scratch("clip.features"); |
| 211 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 212 | let california = (-125.0, 32.0, -114.0, 42.0); |
| 213 | assert_eq!(stream_routes(&path, california, &mut sink).expect("read"), 1); |
| 214 | |
| 215 | // And an area with no route in it is an error, not an empty layer. |
| 216 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 217 | assert!(stream_routes(&path, (-160.0, 0.0, -150.0, 10.0), &mut sink).is_err()); |
| 218 | |
| 219 | let _ = std::fs::remove_file(&path); |
| 220 | let _ = std::fs::remove_file(&spill); |
| 221 | } |
| 222 | |
| 223 | /// Every way this file can be the wrong file. Each is an error naming the line, because a |
| 224 | /// transit layer that is quietly half a city is what this change exists to stop shipping. |
| 225 | #[test] |
| 226 | fn a_line_this_cannot_read_is_an_error_rather_than_a_skip() { |
| 227 | let spill = scratch("bad.features"); |
| 228 | let world = (-180.0, -90.0, 180.0, 90.0); |
| 229 | for (tag, body) in [ |
| 230 | ("nonsense", "not json at all".to_string()), |
| 231 | ( |
| 232 | "polygon", |
| 233 | "{\"type\":\"Feature\",\"properties\":{\"color\":\"0054A5\",\"mode\":\"subway\"},\ |
| 234 | \"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],\ |
| 235 | [1.0,1.0],[0.0,0.0]]]}}" |
| 236 | .to_string(), |
| 237 | ), |
| 238 | ("no_mode", feature("0054A5", "", "[-122.4,37.7],[-122.39,37.71]")), |
| 239 | ("bus", feature("0054A5", "bus", "[-122.4,37.7],[-122.39,37.71]")), |
| 240 | ("black", feature("000000", "subway", "[-122.4,37.7],[-122.39,37.71]")), |
| 241 | ("no_colour", feature("nope", "subway", "[-122.4,37.7],[-122.39,37.71]")), |
| 242 | ] { |
| 243 | let path = scratch(&format!("bad_{tag}.geojsonseq")); |
| 244 | std::fs::write(&path, format!("{body}\n")).expect("write"); |
| 245 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 246 | assert!(stream_routes(&path, world, &mut sink).is_err(), "{tag} was accepted"); |
| 247 | let _ = std::fs::remove_file(&path); |
| 248 | } |
| 249 | // A missing file too, rather than an empty layer. |
| 250 | let mut sink = crate::store::Sink::create(&spill).expect("sink"); |
| 251 | assert!(stream_routes(Path::new("no_such_routes.geojsonseq"), world, &mut sink).is_err()); |
| 252 | let _ = std::fs::remove_file(&spill); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn stations_name_their_mode() { |
| 257 | assert_eq!(station_mode(&[("railway", "station")]), "station"); |
| 258 | assert_eq!(station_mode(&[("railway", "halt")]), "halt"); |
| 259 | assert_eq!(station_mode(&[("railway", "station"), ("station", "subway")]), "subway"); |
| 260 | assert_eq!( |
| 261 | station_mode(&[("station", "light_rail")]), |
| 262 | "light_rail", |
| 263 | "a station tag alone counts" |
| 264 | ); |
| 265 | } |
| 266 | } |