Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / testpbf_part1.rs
testpbf_part1.rs451 lines · 16.0 KiBRaw
| 1 | /// Dense-node group for an arbitrary node list. Ids must ascend, as they do in a |
| 2 | /// real PBF. |
| 3 | fn dense_group(nodes: &[FixtureNode]) -> Vec<u8> { |
| 4 | let mut keys_vals: Vec<u32> = Vec::new(); |
| 5 | for (_, _, _, tags) in nodes { |
| 6 | for (k, v) in tags { |
| 7 | keys_vals.push(*k); |
| 8 | keys_vals.push(*v); |
| 9 | } |
| 10 | keys_vals.push(ST_EMPTY); |
| 11 | } |
| 12 | let mut out = Vec::new(); |
| 13 | packed_delta(1, &nodes.iter().map(|n| n.0).collect::<Vec<_>>(), &mut out); |
| 14 | packed_delta(8, &nodes.iter().map(|n| n.1 as i64).collect::<Vec<_>>(), &mut out); |
| 15 | packed_delta(9, &nodes.iter().map(|n| n.2 as i64).collect::<Vec<_>>(), &mut out); |
| 16 | packed_u32(10, &keys_vals, &mut out); |
| 17 | out |
| 18 | } |
| 19 | |
| 20 | /// The `PrimitiveBlock` for the vector-layer fixture. All nodes sit in San |
| 21 | /// Francisco, around 37.77N 122.41W, so a plausible `--bbox` includes them and an |
| 22 | /// Atlantic one does not. |
| 23 | pub fn layers_block() -> Vec<u8> { |
| 24 | let mut st = StringTable::new(); |
| 25 | |
| 26 | let k_highway = st.id("highway"); |
| 27 | let k_man_made = st.id("man_made"); |
| 28 | let k_surveillance_type = st.id("surveillance:type"); |
| 29 | let k_operator = st.id("operator"); |
| 30 | let k_direction = st.id("direction"); |
| 31 | let k_amenity = st.id("amenity"); |
| 32 | let k_name = st.id("name"); |
| 33 | let k_maxspeed = st.id("maxspeed"); |
| 34 | let k_maxspeed_forward = st.id("maxspeed:forward"); |
| 35 | let k_railway = st.id("railway"); |
| 36 | let k_type = st.id("type"); |
| 37 | let k_route = st.id("route"); |
| 38 | let k_ref = st.id("ref"); |
| 39 | let k_colour = st.id("colour"); |
| 40 | let k_boundary = st.id("boundary"); |
| 41 | let k_admin_level = st.id("admin_level"); |
| 42 | // roads: the attributes the `roads` layer carries beyond geometry and class. |
| 43 | let k_lanes = st.id("lanes"); |
| 44 | let k_turn_lanes_forward = st.id("turn:lanes:forward"); |
| 45 | let k_oneway = st.id("oneway"); |
| 46 | let k_width = st.id("width"); |
| 47 | let k_bridge = st.id("bridge"); |
| 48 | let k_layer = st.id("layer"); |
| 49 | |
| 50 | let v_speed_camera = st.id("speed_camera"); |
| 51 | let v_surveillance = st.id("surveillance"); |
| 52 | let v_alpr = st.id("ALPR"); |
| 53 | let v_flock = st.id("Flock Safety"); |
| 54 | let v_forward = st.id("forward"); |
| 55 | let v_stop = st.id("stop"); |
| 56 | let v_traffic_signals = st.id("traffic_signals"); |
| 57 | let v_cafe = st.id("cafe"); |
| 58 | let v_corner_cafe = st.id("Corner Cafe"); |
| 59 | let v_residential = st.id("residential"); |
| 60 | let v_motorway = st.id("motorway"); |
| 61 | let v_service = st.id("service"); |
| 62 | let v_25mph = st.id("25 mph"); |
| 63 | let v_none = st.id("none"); |
| 64 | let v_30mph = st.id("30 mph"); |
| 65 | let v_main_st = st.id("Main St"); |
| 66 | let v_subway = st.id("subway"); |
| 67 | let v_narrow_gauge = st.id("narrow_gauge"); |
| 68 | let v_platform = st.id("platform"); |
| 69 | let v_market_st = st.id("Market St Subway"); |
| 70 | let v_route = st.id("route"); |
| 71 | let v_bus = st.id("bus"); |
| 72 | let v_red_line = st.id("Red Line"); |
| 73 | let v_red = st.id("Red"); |
| 74 | let v_da291c = st.id("#DA291C"); |
| 75 | let v_boundary = st.id("boundary"); |
| 76 | let v_administrative = st.id("administrative"); |
| 77 | let v_eight = st.id("8"); |
| 78 | let v_six = st.id("6"); |
| 79 | let v_oakland = st.id("Oakland"); |
| 80 | let v_alameda = st.id("Alameda County"); |
| 81 | let v_three = st.id("3"); |
| 82 | let v_through_through_right = st.id("through|through|right"); |
| 83 | let v_yes = st.id("yes"); |
| 84 | let v_12m = st.id("12 m"); |
| 85 | let v_one = st.id("1"); |
| 86 | let role_outer = st.id("outer"); |
| 87 | let role_inner = st.id("inner"); |
| 88 | let role_platform = st.id("platform"); |
| 89 | // An empty member role is string-table index 0, per the PBF spec. |
| 90 | let role_empty = ST_EMPTY; |
| 91 | |
| 92 | // Ids ascend, as they do in a real PBF. |
| 93 | let nodes: Vec<FixtureNode> = vec![ |
| 94 | ( |
| 95 | CAMERA_NODE_ID, |
| 96 | 377_749_000, |
| 97 | -1_224_194_000, |
| 98 | vec![(k_highway, v_speed_camera), (k_direction, v_forward)], |
| 99 | ), |
| 100 | ( |
| 101 | ALPR_NODE_ID, |
| 102 | 377_770_000, |
| 103 | -1_224_170_000, |
| 104 | vec![ |
| 105 | (k_man_made, v_surveillance), |
| 106 | (k_surveillance_type, v_alpr), |
| 107 | (k_operator, v_flock), |
| 108 | ], |
| 109 | ), |
| 110 | ( |
| 111 | STOP_SIGN_NODE_ID, |
| 112 | 377_800_000, |
| 113 | -1_224_150_000, |
| 114 | vec![(k_highway, v_stop)], |
| 115 | ), |
| 116 | ( |
| 117 | SIGNALS_NODE_ID, |
| 118 | 377_810_000, |
| 119 | -1_224_140_000, |
| 120 | vec![(k_highway, v_traffic_signals)], |
| 121 | ), |
| 122 | ( |
| 123 | LAYERS_CAFE_NODE_ID, |
| 124 | 377_820_000, |
| 125 | -1_224_130_000, |
| 126 | vec![(k_amenity, v_cafe), (k_name, v_corner_cafe)], |
| 127 | ), |
| 128 | // Untagged nodes the ways are built from, laid out along 37.79N. |
| 129 | (WAY_NODE_IDS[0], 377_900_000, -1_224_300_000, vec![]), |
| 130 | (WAY_NODE_IDS[1], 377_900_000, -1_224_200_000, vec![]), |
| 131 | (WAY_NODE_IDS[2], 377_900_000, -1_224_100_000, vec![]), |
| 132 | (WAY_NODE_IDS[3], 377_910_000, -1_224_100_000, vec![]), |
| 133 | (WAY_NODE_IDS[4], 377_920_000, -1_224_100_000, vec![]), |
| 134 | (WAY_NODE_IDS[5], 377_930_000, -1_224_100_000, vec![]), |
| 135 | ]; |
| 136 | let mut nodes = nodes; |
| 137 | for (id, lat, lon) in ADMIN_NODES.into_iter().chain(ROUTE_PLATFORM_NODES) { |
| 138 | nodes.push((id, lat, lon, vec![])); |
| 139 | } |
| 140 | let dense = dense_group(&nodes); |
| 141 | |
| 142 | let ways = [ |
| 143 | // maxspeed: a raw "25 mph", with a highway and a name to carry. |
| 144 | way( |
| 145 | MAXSPEED_WAY_ID, |
| 146 | &[ |
| 147 | (k_highway, v_residential), |
| 148 | (k_maxspeed, v_25mph), |
| 149 | (k_name, v_main_st), |
| 150 | ], |
| 151 | &WAY_NODE_IDS[..3], |
| 152 | ), |
| 153 | // maxspeed=none, plus a directional tag that must NOT win over it. Also the |
| 154 | // `roads` layer's fully-attributed way: a oneway bridge with lanes, turn |
| 155 | // lanes, a width and a layer. |
| 156 | way( |
| 157 | MAXSPEED_NONE_WAY_ID, |
| 158 | &[ |
| 159 | (k_highway, v_motorway), |
| 160 | (k_maxspeed, v_none), |
| 161 | (k_maxspeed_forward, v_30mph), |
| 162 | (k_lanes, v_three), |
| 163 | (k_turn_lanes_forward, v_through_through_right), |
| 164 | (k_oneway, v_yes), |
| 165 | (k_width, v_12m), |
| 166 | (k_bridge, v_yes), |
| 167 | (k_layer, v_one), |
| 168 | ], |
| 169 | &WAY_NODE_IDS[2..5], |
| 170 | ), |
| 171 | // No speed limit at all. |
| 172 | way(NO_MAXSPEED_WAY_ID, &[(k_highway, v_service)], &WAY_NODE_IDS[..2]), |
| 173 | // transit_lines: a subway way with a name, and a member of the route below. |
| 174 | way( |
| 175 | RAILWAY_WAY_ID, |
| 176 | &[(k_railway, v_subway), (k_name, v_market_st)], |
| 177 | &WAY_NODE_IDS[..3], |
| 178 | ), |
| 179 | // narrow_gauge folds into rail; also the route's second member. |
| 180 | way( |
| 181 | NARROW_GAUGE_WAY_ID, |
| 182 | &[(k_railway, v_narrow_gauge)], |
| 183 | &WAY_NODE_IDS[3..6], |
| 184 | ), |
| 185 | // A platform is not a line. |
| 186 | way(PLATFORM_WAY_ID, &[(k_railway, v_platform)], &WAY_NODE_IDS[..2]), |
| 187 | // And a closed one, which the route relation below claims as `role=platform`. |
| 188 | way( |
| 189 | ROUTE_PLATFORM_WAY_ID, |
| 190 | &[(k_railway, v_platform)], |
| 191 | &[2201, 2202, 2203, 2204, 2201], |
| 192 | ), |
| 193 | // admin_city: the outer ring split across two ways, so the assembler has to |
| 194 | // stitch them, and one of them is reversed so it has to flip one too. |
| 195 | way(ADMIN_OUTER_WAY_A, &[], &[2101, 2102, 2103]), |
| 196 | way(ADMIN_OUTER_WAY_B, &[], &[2101, 2104, 2103]), |
| 197 | // The hole, closed on its own. |
| 198 | way(ADMIN_INNER_WAY, &[], &[2111, 2112, 2113, 2114, 2111]), |
| 199 | ]; |
| 200 | |
| 201 | let rels = [ |
| 202 | // A route relation over two member ways, so its geometry is a genuine |
| 203 | // MultiLineString rather than a single part. |
| 204 | relation( |
| 205 | ROUTE_RELATION_ID, |
| 206 | &[ |
| 207 | (k_type, v_route), |
| 208 | (k_route, v_subway), |
| 209 | (k_name, v_red_line), |
| 210 | (k_ref, v_red), |
| 211 | (k_colour, v_da291c), |
| 212 | ], |
| 213 | &[ |
| 214 | (RAILWAY_WAY_ID, role_empty, 1), |
| 215 | (NARROW_GAUGE_WAY_ID, role_empty, 1), |
| 216 | (ROUTE_PLATFORM_WAY_ID, role_platform, 1), |
| 217 | ], |
| 218 | ), |
| 219 | // A bus route: this layer is the rail-like modes only. |
| 220 | relation( |
| 221 | BUS_RELATION_ID, |
| 222 | &[(k_type, v_route), (k_route, v_bus)], |
| 223 | &[(RAILWAY_WAY_ID, role_empty, 1)], |
| 224 | ), |
| 225 | // An admin_level=8 boundary. One member is unroled, which the OSM boundary |
| 226 | // convention says is outer -- the case that matters most, since most real |
| 227 | // members are unroled. |
| 228 | relation( |
| 229 | ADMIN_CITY_RELATION_ID, |
| 230 | &[ |
| 231 | (k_type, v_boundary), |
| 232 | (k_boundary, v_administrative), |
| 233 | (k_admin_level, v_eight), |
| 234 | (k_name, v_oakland), |
| 235 | ], |
| 236 | &[ |
| 237 | (ADMIN_OUTER_WAY_A, role_outer, 1), |
| 238 | (ADMIN_OUTER_WAY_B, role_empty, 1), |
| 239 | (ADMIN_INNER_WAY, role_inner, 1), |
| 240 | ], |
| 241 | ), |
| 242 | // A county: same shape, wrong level. |
| 243 | relation( |
| 244 | ADMIN_COUNTY_RELATION_ID, |
| 245 | &[ |
| 246 | (k_type, v_boundary), |
| 247 | (k_boundary, v_administrative), |
| 248 | (k_admin_level, v_six), |
| 249 | (k_name, v_alameda), |
| 250 | ], |
| 251 | &[(ADMIN_OUTER_WAY_A, role_outer, 1), (ADMIN_OUTER_WAY_B, role_empty, 1)], |
| 252 | ), |
| 253 | ]; |
| 254 | |
| 255 | let mut node_group = Vec::new(); |
| 256 | bytes_field(2, &dense, &mut node_group); |
| 257 | let mut way_group = Vec::new(); |
| 258 | for w in &ways { |
| 259 | bytes_field(3, w, &mut way_group); |
| 260 | } |
| 261 | let mut rel_group = Vec::new(); |
| 262 | for r in &rels { |
| 263 | bytes_field(4, r, &mut rel_group); |
| 264 | } |
| 265 | |
| 266 | let mut block = Vec::new(); |
| 267 | bytes_field(1, &st.encode(), &mut block); |
| 268 | bytes_field(2, &node_group, &mut block); |
| 269 | bytes_field(2, &way_group, &mut block); |
| 270 | bytes_field(2, &rel_group, &mut block); |
| 271 | varint_field(17, 100, &mut block); // granularity |
| 272 | block |
| 273 | } |
| 274 | |
| 275 | /// Write the vector-layer fixture to a unique temp directory. |
| 276 | pub fn write_layers_sample(tag: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
| 277 | write_pbf(tag, &pbf_from_block(&layers_block())) |
| 278 | } |
| 279 | |
| 280 | // ---- the dangling-reference fixture -------------------------------------- |
| 281 | // |
| 282 | // A THIRD fixture, for one hole: a routable way referencing a node the file never |
| 283 | // defines. Real extracts are full of these — every way clipped at an extract's |
| 284 | // boundary keeps refs to nodes outside it — and the road graph's whole handling |
| 285 | // of them is "the pair fails to resolve, so skip it", which nothing tested. |
| 286 | // |
| 287 | // It matters more than it looks. The node-id bitset is marked from way refs, so a |
| 288 | // dangling ref *is* marked, and any scheme that treats the marked set as the node |
| 289 | // address space hands it a slot. A slot that never receives coordinates reads as |
| 290 | // (0, 0) — null island — which corrupts distances, spatial keys and polylines |
| 291 | // while leaving every count plausible. |
| 292 | |
| 293 | /// A node id no element in this fixture defines, referenced from the middle of a |
| 294 | /// routable way. |
| 295 | pub const DANGLING_REF_ID: i64 = 999; |
| 296 | pub const DANGLING_WAY_ID: i64 = 103; |
| 297 | |
| 298 | /// `(id, lat_e7, lon_e7)` of the nodes this fixture does define. |
| 299 | pub const DANGLING_NODES: [(i64, i32, i32); 3] = [ |
| 300 | (1, 370_000_000, -1_220_000_000), |
| 301 | (2, 370_010_000, -1_220_000_000), |
| 302 | (3, 370_020_000, -1_220_000_000), |
| 303 | ]; |
| 304 | |
| 305 | /// One routable way over `[1, 2, 999, 3]`, so the dangling ref sits between two |
| 306 | /// present nodes and breaks two consecutive pairs rather than one. |
| 307 | fn dangling_block() -> Vec<u8> { |
| 308 | let mut st = StringTable::new(); |
| 309 | let k_hw = st.id("highway"); |
| 310 | let k_name = st.id("name"); |
| 311 | let v_residential = st.id("residential"); |
| 312 | let v_gap = st.id("Gap St"); |
| 313 | |
| 314 | let nodes: Vec<FixtureNode> = DANGLING_NODES |
| 315 | .iter() |
| 316 | .map(|(id, lat, lon)| (*id, *lat, *lon, Vec::new())) |
| 317 | .collect(); |
| 318 | let dense = dense_group(&nodes); |
| 319 | let ways = [way( |
| 320 | DANGLING_WAY_ID, |
| 321 | &[(k_hw, v_residential), (k_name, v_gap)], |
| 322 | &[1, 2, DANGLING_REF_ID, 3], |
| 323 | )]; |
| 324 | |
| 325 | let mut node_group = Vec::new(); |
| 326 | bytes_field(2, &dense, &mut node_group); |
| 327 | let mut way_group = Vec::new(); |
| 328 | for w in &ways { |
| 329 | bytes_field(3, w, &mut way_group); |
| 330 | } |
| 331 | |
| 332 | let mut block = Vec::new(); |
| 333 | bytes_field(1, &st.encode(), &mut block); |
| 334 | bytes_field(2, &node_group, &mut block); |
| 335 | bytes_field(2, &way_group, &mut block); |
| 336 | varint_field(17, 100, &mut block); // granularity |
| 337 | block |
| 338 | } |
| 339 | |
| 340 | /// Write the dangling-reference fixture to a unique temp directory. |
| 341 | pub fn write_dangling_sample(tag: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
| 342 | write_pbf(tag, &pbf_from_block(&dangling_block())) |
| 343 | } |
| 344 | |
| 345 | // ---- an arbitrary graph shape -------------------------------------------- |
| 346 | // |
| 347 | // The three fixtures above each say something specific and the tests assert exact |
| 348 | // counts against them, so growing one to cover a new topology makes every test |
| 349 | // that reads it slightly less about what it was testing. Chain cutting is all |
| 350 | // topology, though, and needs many shapes: a ring, a self-touching way, two ways |
| 351 | // meeting end-to-end, a repeated ref. This builds a PBF from a shape stated |
| 352 | // directly in the test. |
| 353 | |
| 354 | /// A PBF of untagged nodes plus `highway=residential` ways over them. |
| 355 | /// |
| 356 | /// Deliberately minimal: no names, no lanes, no speed limits and no oneways, so |
| 357 | /// every way agrees with every other on attributes and the *only* thing deciding |
| 358 | /// where chains are cut is the graph's shape. |
| 359 | pub fn write_shape_sample( |
| 360 | tag: &str, |
| 361 | nodes: &[(i64, i32, i32)], |
| 362 | ways: &[(i64, &[i64])], |
| 363 | ) -> (std::path::PathBuf, std::path::PathBuf) { |
| 364 | let mut st = StringTable::new(); |
| 365 | let k_hw = st.id("highway"); |
| 366 | let v_residential = st.id("residential"); |
| 367 | |
| 368 | let fixture: Vec<FixtureNode> = nodes |
| 369 | .iter() |
| 370 | .map(|(id, lat, lon)| (*id, *lat, *lon, Vec::new())) |
| 371 | .collect(); |
| 372 | let dense = dense_group(&fixture); |
| 373 | |
| 374 | let mut node_group = Vec::new(); |
| 375 | bytes_field(2, &dense, &mut node_group); |
| 376 | let mut way_group = Vec::new(); |
| 377 | for (id, refs) in ways { |
| 378 | bytes_field(3, &way(*id, &[(k_hw, v_residential)], refs), &mut way_group); |
| 379 | } |
| 380 | |
| 381 | let mut block = Vec::new(); |
| 382 | bytes_field(1, &st.encode(), &mut block); |
| 383 | bytes_field(2, &node_group, &mut block); |
| 384 | bytes_field(2, &way_group, &mut block); |
| 385 | varint_field(17, 100, &mut block); // granularity |
| 386 | write_pbf(tag, &pbf_from_block(&block)) |
| 387 | } |
| 388 | |
| 389 | /// [`write_shape_sample`] with a `highway=*` value per way. |
| 390 | pub fn write_highway_sample( |
| 391 | tag: &str, |
| 392 | nodes: &[(i64, i32, i32)], |
| 393 | ways: &[(i64, &str, &[i64])], |
| 394 | ) -> (std::path::PathBuf, std::path::PathBuf) { |
| 395 | let mut st = StringTable::new(); |
| 396 | let k_hw = st.id("highway"); |
| 397 | let values: Vec<u32> = ways.iter().map(|(_, hw, _)| st.id(hw)).collect(); |
| 398 | |
| 399 | let fixture: Vec<FixtureNode> = nodes |
| 400 | .iter() |
| 401 | .map(|(id, lat, lon)| (*id, *lat, *lon, Vec::new())) |
| 402 | .collect(); |
| 403 | let dense = dense_group(&fixture); |
| 404 | |
| 405 | let mut node_group = Vec::new(); |
| 406 | bytes_field(2, &dense, &mut node_group); |
| 407 | let mut way_group = Vec::new(); |
| 408 | for ((id, _, refs), v) in ways.iter().zip(values) { |
| 409 | bytes_field(3, &way(*id, &[(k_hw, v)], refs), &mut way_group); |
| 410 | } |
| 411 | |
| 412 | let mut block = Vec::new(); |
| 413 | bytes_field(1, &st.encode(), &mut block); |
| 414 | bytes_field(2, &node_group, &mut block); |
| 415 | bytes_field(2, &way_group, &mut block); |
| 416 | varint_field(17, 100, &mut block); // granularity |
| 417 | write_pbf(tag, &pbf_from_block(&block)) |
| 418 | } |
| 419 | |
| 420 | #[cfg(test)] |
| 421 | mod tests { |
| 422 | use super::*; |
| 423 | |
| 424 | #[test] |
| 425 | fn stored_deflate_round_trips_through_miniz() { |
| 426 | for payload in [ |
| 427 | b"".to_vec(), |
| 428 | b"hello".to_vec(), |
| 429 | (0..200_000u32).map(|i| i as u8).collect::<Vec<u8>>(), |
| 430 | ] { |
| 431 | let z = zlib_store(&payload); |
| 432 | let back = miniz_oxide::inflate::decompress_to_vec_zlib(&z).unwrap(); |
| 433 | assert_eq!(back, payload, "len {}", payload.len()); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | #[test] |
| 438 | fn adler32_matches_the_reference_value() { |
| 439 | // The canonical zlib test vector. |
| 440 | assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398); |
| 441 | assert_eq!(adler32(b""), 1); |
| 442 | } |
| 443 | |
| 444 | #[test] |
| 445 | fn a_corrupt_adler_trailer_is_rejected() { |
| 446 | let mut z = zlib_store(b"payload"); |
| 447 | let last = z.len() - 1; |
| 448 | z[last] ^= 0xFF; |
| 449 | assert!(miniz_oxide::inflate::decompress_to_vec_zlib(&z).is_err()); |
| 450 | } |
| 451 | } |