Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / testpbf.rs
testpbf.rs445 lines · 15.3 KiBRaw
| 1 | //! A hand-built `.osm.pbf` used by the crate's tests. |
| 2 | //! |
| 3 | //! Encoding the fixture by hand (rather than checking in a binary) keeps the |
| 4 | //! test readable and, more importantly, exercises the decoder against bytes |
| 5 | //! produced independently of it. The DEFLATE side uses *stored* blocks, so the |
| 6 | //! zlib framing and Adler-32 trailer are real without pulling in a compressor. |
| 7 | |
| 8 | const ST_EMPTY: u32 = 0; |
| 9 | |
| 10 | /// Node ids 1..=6 are laid out as a 4-node road, one detached bus stop and one |
| 11 | /// cafe. |
| 12 | pub const NODE_COUNT: usize = 6; |
| 13 | /// A `highway=bus_stop` node that no way references, so it lands outside the |
| 14 | /// largest connected component and must be reconnected synthetically. |
| 15 | pub const STOP_NODE_ID: i64 = 5; |
| 16 | pub const CAFE_NODE_ID: i64 = 6; |
| 17 | pub const MAIN_WAY_ID: i64 = 100; |
| 18 | pub const SERVICE_WAY_ID: i64 = 101; |
| 19 | pub const AREA_WAY_ID: i64 = 102; |
| 20 | pub const RELATION_ID: i64 = 200; |
| 21 | |
| 22 | /// `(id, lat_e7, lon_e7)` of every node in the fixture. The road runs north |
| 23 | /// along 122°W near 37°N; the bus stop sits just off it and the cafe beyond it. |
| 24 | pub const NODES: [(i64, i32, i32); NODE_COUNT] = [ |
| 25 | (1, 370_000_000, -1_220_000_000), |
| 26 | (2, 370_010_000, -1_220_000_000), |
| 27 | (3, 370_020_000, -1_220_000_000), |
| 28 | (4, 370_030_000, -1_220_000_000), |
| 29 | (STOP_NODE_ID, 370_005_000, -1_220_005_000), |
| 30 | (CAFE_NODE_ID, 370_040_000, -1_220_010_000), |
| 31 | ]; |
| 32 | |
| 33 | // ---- protobuf encoding helpers ------------------------------------------- |
| 34 | |
| 35 | fn uvarint(mut v: u64, out: &mut Vec<u8>) { |
| 36 | loop { |
| 37 | let b = (v & 0x7F) as u8; |
| 38 | v >>= 7; |
| 39 | if v == 0 { |
| 40 | out.push(b); |
| 41 | return; |
| 42 | } |
| 43 | out.push(b | 0x80); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn zz(v: i64) -> u64 { |
| 48 | ((v << 1) ^ (v >> 63)) as u64 |
| 49 | } |
| 50 | |
| 51 | fn varint_field(field: u32, v: u64, out: &mut Vec<u8>) { |
| 52 | uvarint((field as u64) << 3, out); |
| 53 | uvarint(v, out); |
| 54 | } |
| 55 | |
| 56 | fn bytes_field(field: u32, data: &[u8], out: &mut Vec<u8>) { |
| 57 | uvarint(((field as u64) << 3) | 2, out); |
| 58 | uvarint(data.len() as u64, out); |
| 59 | out.extend_from_slice(data); |
| 60 | } |
| 61 | |
| 62 | fn packed_u32(field: u32, values: &[u32], out: &mut Vec<u8>) { |
| 63 | if values.is_empty() { |
| 64 | return; |
| 65 | } |
| 66 | let mut payload = Vec::new(); |
| 67 | for v in values { |
| 68 | uvarint(*v as u64, &mut payload); |
| 69 | } |
| 70 | bytes_field(field, &payload, out); |
| 71 | } |
| 72 | |
| 73 | fn packed_delta(field: u32, values: &[i64], out: &mut Vec<u8>) { |
| 74 | if values.is_empty() { |
| 75 | return; |
| 76 | } |
| 77 | let mut payload = Vec::new(); |
| 78 | let mut prev = 0i64; |
| 79 | for v in values { |
| 80 | uvarint(zz(v - prev), &mut payload); |
| 81 | prev = *v; |
| 82 | } |
| 83 | bytes_field(field, &payload, out); |
| 84 | } |
| 85 | |
| 86 | // ---- zlib with stored DEFLATE blocks ------------------------------------- |
| 87 | |
| 88 | fn adler32(data: &[u8]) -> u32 { |
| 89 | let mut a: u32 = 1; |
| 90 | let mut b: u32 = 0; |
| 91 | for &x in data { |
| 92 | a = (a + x as u32) % 65521; |
| 93 | b = (b + a) % 65521; |
| 94 | } |
| 95 | (b << 16) | a |
| 96 | } |
| 97 | |
| 98 | fn zlib_store(data: &[u8]) -> Vec<u8> { |
| 99 | let mut out = vec![0x78, 0x01]; |
| 100 | let mut offset = 0usize; |
| 101 | loop { |
| 102 | let len = (data.len() - offset).min(0xFFFF); |
| 103 | let last = offset + len >= data.len(); |
| 104 | out.push(if last { 1 } else { 0 }); // BFINAL, BTYPE = stored |
| 105 | out.extend_from_slice(&(len as u16).to_le_bytes()); |
| 106 | out.extend_from_slice(&(!(len as u16)).to_le_bytes()); |
| 107 | out.extend_from_slice(&data[offset..offset + len]); |
| 108 | offset += len; |
| 109 | if last { |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | out.extend_from_slice(&adler32(data).to_be_bytes()); |
| 114 | out |
| 115 | } |
| 116 | |
| 117 | // ---- string table -------------------------------------------------------- |
| 118 | |
| 119 | struct StringTable { |
| 120 | strings: Vec<Vec<u8>>, |
| 121 | } |
| 122 | |
| 123 | impl StringTable { |
| 124 | fn new() -> Self { |
| 125 | // Index 0 must be unused/empty per the PBF spec. |
| 126 | StringTable { |
| 127 | strings: vec![Vec::new()], |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn id(&mut self, s: &str) -> u32 { |
| 132 | if let Some(i) = self.strings.iter().position(|x| x == s.as_bytes()) { |
| 133 | return i as u32; |
| 134 | } |
| 135 | self.strings.push(s.as_bytes().to_vec()); |
| 136 | (self.strings.len() - 1) as u32 |
| 137 | } |
| 138 | |
| 139 | fn encode(&self) -> Vec<u8> { |
| 140 | let mut out = Vec::new(); |
| 141 | for s in &self.strings { |
| 142 | bytes_field(1, s, &mut out); |
| 143 | } |
| 144 | out |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // ---- the fixture itself -------------------------------------------------- |
| 149 | |
| 150 | fn dense_nodes(st: &mut StringTable) -> Vec<u8> { |
| 151 | let stop_tags = [ |
| 152 | (st.id("highway"), st.id("bus_stop")), |
| 153 | (st.id("name"), st.id("Test Stop")), |
| 154 | ]; |
| 155 | let cafe_tags = [ |
| 156 | (st.id("amenity"), st.id("cafe")), |
| 157 | (st.id("name"), st.id("Corner Cafe")), |
| 158 | // Sidecar attributes (poi_attrs.bin). `24/7` earns its place here: it used |
| 159 | // to parse as permanently closed, so it is the case worth pinning. |
| 160 | (st.id("opening_hours"), st.id("24/7")), |
| 161 | (st.id("contact:phone"), st.id("+1-555-0100")), |
| 162 | (st.id("website"), st.id("https://cafe.example")), |
| 163 | (st.id("addr:housenumber"), st.id("120")), |
| 164 | (st.id("addr:street"), st.id("Market St")), |
| 165 | (st.id("cuisine"), st.id("coffee_shop")), |
| 166 | ]; |
| 167 | |
| 168 | let mut keys_vals: Vec<u32> = Vec::new(); |
| 169 | for (id, _, _) in NODES { |
| 170 | let tags: &[(u32, u32)] = match id { |
| 171 | STOP_NODE_ID => &stop_tags, |
| 172 | CAFE_NODE_ID => &cafe_tags, |
| 173 | _ => &[], |
| 174 | }; |
| 175 | for (k, v) in tags { |
| 176 | keys_vals.push(*k); |
| 177 | keys_vals.push(*v); |
| 178 | } |
| 179 | keys_vals.push(ST_EMPTY); |
| 180 | } |
| 181 | |
| 182 | let mut out = Vec::new(); |
| 183 | packed_delta(1, &NODES.map(|n| n.0), &mut out); |
| 184 | packed_delta(8, &NODES.map(|n| n.1 as i64), &mut out); |
| 185 | packed_delta(9, &NODES.map(|n| n.2 as i64), &mut out); |
| 186 | packed_u32(10, &keys_vals, &mut out); |
| 187 | out |
| 188 | } |
| 189 | |
| 190 | fn way(id: i64, tags: &[(u32, u32)], refs: &[i64]) -> Vec<u8> { |
| 191 | let mut out = Vec::new(); |
| 192 | varint_field(1, id as u64, &mut out); |
| 193 | packed_u32(2, &tags.iter().map(|t| t.0).collect::<Vec<_>>(), &mut out); |
| 194 | packed_u32(3, &tags.iter().map(|t| t.1).collect::<Vec<_>>(), &mut out); |
| 195 | packed_delta(8, refs, &mut out); |
| 196 | out |
| 197 | } |
| 198 | |
| 199 | fn relation(id: i64, tags: &[(u32, u32)], members: &[(i64, u32, u32)]) -> Vec<u8> { |
| 200 | let mut out = Vec::new(); |
| 201 | varint_field(1, id as u64, &mut out); |
| 202 | packed_u32(2, &tags.iter().map(|t| t.0).collect::<Vec<_>>(), &mut out); |
| 203 | packed_u32(3, &tags.iter().map(|t| t.1).collect::<Vec<_>>(), &mut out); |
| 204 | packed_u32(8, &members.iter().map(|m| m.1).collect::<Vec<_>>(), &mut out); |
| 205 | packed_delta(9, &members.iter().map(|m| m.0).collect::<Vec<_>>(), &mut out); |
| 206 | packed_u32(10, &members.iter().map(|m| m.2).collect::<Vec<_>>(), &mut out); |
| 207 | out |
| 208 | } |
| 209 | |
| 210 | /// The `PrimitiveBlock` message for the fixture: one dense-node group, one way |
| 211 | /// group and one relation group. |
| 212 | pub fn primitive_block() -> Vec<u8> { |
| 213 | let mut st = StringTable::new(); |
| 214 | let dense = dense_nodes(&mut st); |
| 215 | |
| 216 | let (k_hw, k_name, k_lanes, k_tlf, k_oneway, k_maxspeed, k_amenity, k_type, k_leisure) = ( |
| 217 | st.id("highway"), |
| 218 | st.id("name"), |
| 219 | st.id("lanes"), |
| 220 | st.id("turn:lanes:forward"), |
| 221 | st.id("oneway"), |
| 222 | st.id("maxspeed"), |
| 223 | st.id("amenity"), |
| 224 | st.id("type"), |
| 225 | st.id("leisure"), |
| 226 | ); |
| 227 | let v_residential = st.id("residential"); |
| 228 | let v_main = st.id("Main St"); |
| 229 | let v_two = st.id("2"); |
| 230 | let v_left_through = st.id("left|through"); |
| 231 | let v_service = st.id("service"); |
| 232 | let v_yes = st.id("yes"); |
| 233 | let v_30mph = st.id("30 mph"); |
| 234 | let v_cafe = st.id("cafe"); |
| 235 | let v_plaza = st.id("Plaza"); |
| 236 | let v_multipolygon = st.id("multipolygon"); |
| 237 | let v_park = st.id("Riverside Park"); |
| 238 | let v_leisure_park = st.id("park"); |
| 239 | let role_outer = st.id("outer"); |
| 240 | |
| 241 | let ways = [ |
| 242 | // A 4-node bidirectional residential road with real turn:lanes. |
| 243 | way( |
| 244 | MAIN_WAY_ID, |
| 245 | &[ |
| 246 | (k_hw, v_residential), |
| 247 | (k_name, v_main), |
| 248 | (k_lanes, v_two), |
| 249 | (k_tlf, v_left_through), |
| 250 | ], |
| 251 | &[1, 2, 3, 4], |
| 252 | ), |
| 253 | // A oneway service road, mph speed limit, no name. |
| 254 | way( |
| 255 | SERVICE_WAY_ID, |
| 256 | &[(k_hw, v_service), (k_oneway, v_yes), (k_maxspeed, v_30mph)], |
| 257 | &[4, 2], |
| 258 | ), |
| 259 | // A closed, unrouted area: a POI polygon, invisible to the road graph. |
| 260 | way( |
| 261 | AREA_WAY_ID, |
| 262 | &[(k_amenity, v_cafe), (k_name, v_plaza)], |
| 263 | &[1, 2, 3, 4, 1], |
| 264 | ), |
| 265 | ]; |
| 266 | let rels = [relation( |
| 267 | RELATION_ID, |
| 268 | &[ |
| 269 | (k_type, v_multipolygon), |
| 270 | (k_name, v_park), |
| 271 | (k_leisure, v_leisure_park), |
| 272 | ], |
| 273 | // Outer ring = the closed way above, the case where the centroid can be |
| 274 | // reproduced exactly. |
| 275 | &[(AREA_WAY_ID, role_outer, 1)], |
| 276 | )]; |
| 277 | |
| 278 | let mut node_group = Vec::new(); |
| 279 | bytes_field(2, &dense, &mut node_group); |
| 280 | let mut way_group = Vec::new(); |
| 281 | for w in &ways { |
| 282 | bytes_field(3, w, &mut way_group); |
| 283 | } |
| 284 | let mut rel_group = Vec::new(); |
| 285 | for r in &rels { |
| 286 | bytes_field(4, r, &mut rel_group); |
| 287 | } |
| 288 | |
| 289 | let mut block = Vec::new(); |
| 290 | bytes_field(1, &st.encode(), &mut block); |
| 291 | bytes_field(2, &node_group, &mut block); |
| 292 | bytes_field(2, &way_group, &mut block); |
| 293 | bytes_field(2, &rel_group, &mut block); |
| 294 | varint_field(17, 100, &mut block); // granularity |
| 295 | block |
| 296 | } |
| 297 | |
| 298 | /// The `Blob` message wrapping the fixture's `PrimitiveBlock`. |
| 299 | pub fn sample_data_blob() -> Vec<u8> { |
| 300 | data_blob(&primitive_block()) |
| 301 | } |
| 302 | |
| 303 | fn data_blob(payload: &[u8]) -> Vec<u8> { |
| 304 | let mut blob = Vec::new(); |
| 305 | varint_field(2, payload.len() as u64, &mut blob); // raw_size |
| 306 | bytes_field(3, &zlib_store(payload), &mut blob); // zlib_data |
| 307 | blob |
| 308 | } |
| 309 | |
| 310 | fn framed(kind: &str, blob: &[u8], out: &mut Vec<u8>) { |
| 311 | let mut header = Vec::new(); |
| 312 | bytes_field(1, kind.as_bytes(), &mut header); |
| 313 | varint_field(3, blob.len() as u64, &mut header); |
| 314 | out.extend_from_slice(&(header.len() as u32).to_be_bytes()); |
| 315 | out.extend_from_slice(&header); |
| 316 | out.extend_from_slice(blob); |
| 317 | } |
| 318 | |
| 319 | /// A complete `.osm.pbf`: one `OSMHeader` blob (which every reader must skip) |
| 320 | /// followed by one `OSMData` blob. |
| 321 | pub fn sample_pbf() -> Vec<u8> { |
| 322 | pbf_from_block(&primitive_block()) |
| 323 | } |
| 324 | |
| 325 | fn pbf_from_block(block: &[u8]) -> Vec<u8> { |
| 326 | pbf_from_blocks(std::iter::once(block)) |
| 327 | } |
| 328 | |
| 329 | /// A complete `.osm.pbf` with one `OSMData` blob per block. |
| 330 | /// |
| 331 | /// Exists so a test can produce more than the one chunk `CHUNK_BLOBS` groups blobs |
| 332 | /// into. Everything about `run_pass_sink` that is interesting under concurrency — the |
| 333 | /// reorder buffer, its window, the ordered drain — is inert on a single-chunk file. |
| 334 | pub fn pbf_from_blocks<'a>(blocks: impl IntoIterator<Item = &'a [u8]>) -> Vec<u8> { |
| 335 | let mut header_block = Vec::new(); |
| 336 | bytes_field(4, b"OsmSchema-V0.6", &mut header_block); |
| 337 | bytes_field(4, b"DenseNodes", &mut header_block); |
| 338 | bytes_field(16, b"osm_ingest test fixture", &mut header_block); |
| 339 | let mut header_blob = Vec::new(); |
| 340 | varint_field(2, header_block.len() as u64, &mut header_blob); |
| 341 | bytes_field(3, &zlib_store(&header_block), &mut header_blob); |
| 342 | |
| 343 | let mut out = Vec::new(); |
| 344 | framed("OSMHeader", &header_blob, &mut out); |
| 345 | for block in blocks { |
| 346 | framed("OSMData", &data_blob(block), &mut out); |
| 347 | } |
| 348 | out |
| 349 | } |
| 350 | |
| 351 | /// Write the fixture to a unique temp directory and return the path plus the |
| 352 | /// directory (which the caller may use for outputs). |
| 353 | pub fn write_sample(tag: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
| 354 | write_pbf(tag, &sample_pbf()) |
| 355 | } |
| 356 | |
| 357 | fn write_pbf(tag: &str, bytes: &[u8]) -> (std::path::PathBuf, std::path::PathBuf) { |
| 358 | let dir = std::env::temp_dir().join(format!("osm_ingest_{tag}")); |
| 359 | let _ = std::fs::remove_dir_all(&dir); |
| 360 | std::fs::create_dir_all(&dir).unwrap(); |
| 361 | let path = dir.join("sample.osm.pbf"); |
| 362 | std::fs::write(&path, bytes).unwrap(); |
| 363 | (path, dir) |
| 364 | } |
| 365 | |
| 366 | // ---- the vector-layer fixture -------------------------------------------- |
| 367 | // |
| 368 | // A SECOND fixture rather than more features in the one above. The road-graph and |
| 369 | // POI tests assert exact counts against it (`ways.len() == 3`, `records == 3`, |
| 370 | // `NODE_COUNT`), so every addition there ripples into four modules' tests and |
| 371 | // makes each of them slightly less about what it is testing. The vector layers |
| 372 | // need different elements anyway. |
| 373 | |
| 374 | /// `highway=speed_camera` with a direction. |
| 375 | pub const CAMERA_NODE_ID: i64 = 1001; |
| 376 | /// `man_made=surveillance` + `surveillance:type=ALPR` + a Flock operator, so it |
| 377 | /// hits the ALPR branch by two independent signals. |
| 378 | pub const ALPR_NODE_ID: i64 = 1003; |
| 379 | pub const STOP_SIGN_NODE_ID: i64 = 1006; |
| 380 | pub const SIGNALS_NODE_ID: i64 = 1007; |
| 381 | /// A named cafe: tagged, but not road furniture. The negative control. |
| 382 | pub const LAYERS_CAFE_NODE_ID: i64 = 1008; |
| 383 | |
| 384 | /// `highway=residential` + `maxspeed=25 mph`, the raw-string case. |
| 385 | pub const MAXSPEED_WAY_ID: i64 = 3001; |
| 386 | /// `highway=motorway` + `maxspeed=none`, which the graph's parser would collapse |
| 387 | /// to 0 and this layer must keep verbatim. Also the `roads` layer's |
| 388 | /// fully-attributed way: a oneway bridge with lanes, turn lanes, width and layer. |
| 389 | pub const MAXSPEED_NONE_WAY_ID: i64 = 3002; |
| 390 | /// `highway=service` with no speed limit at all. The negative control. |
| 391 | pub const NO_MAXSPEED_WAY_ID: i64 = 3003; |
| 392 | /// `railway=subway`, a transit line in its own right. |
| 393 | pub const RAILWAY_WAY_ID: i64 = 5001; |
| 394 | /// `railway=narrow_gauge`, which folds into `rail`. |
| 395 | pub const NARROW_GAUGE_WAY_ID: i64 = 5002; |
| 396 | /// `railway=platform`, which is not a line. The negative control. |
| 397 | pub const PLATFORM_WAY_ID: i64 = 5003; |
| 398 | /// A **closed** `railway=platform` way, carried by the route relation below as a |
| 399 | /// `role=platform` member. The shape that drew a box around every station. |
| 400 | pub const ROUTE_PLATFORM_WAY_ID: i64 = 5004; |
| 401 | /// `type=route` + `route=subway`, with a colour and a ref, over two member ways |
| 402 | /// plus a `role=platform` member that is not part of the line. |
| 403 | pub const ROUTE_RELATION_ID: i64 = 9001; |
| 404 | /// `type=route` + `route=bus`, which this layer drops. |
| 405 | pub const BUS_RELATION_ID: i64 = 9002; |
| 406 | /// `type=boundary` + `boundary=administrative` + `admin_level=8`, with an outer |
| 407 | /// ring split across two ways and one inner ring (a hole). |
| 408 | pub const ADMIN_CITY_RELATION_ID: i64 = 9101; |
| 409 | /// The same, but `admin_level=6`: a county, which the city layer drops. |
| 410 | pub const ADMIN_COUNTY_RELATION_ID: i64 = 9102; |
| 411 | |
| 412 | /// The corners of [`ROUTE_PLATFORM_WAY_ID`], well clear of every other fixture |
| 413 | /// coordinate so its absence from the route's geometry can be asserted directly. |
| 414 | const ROUTE_PLATFORM_NODES: [(i64, i32, i32); 4] = [ |
| 415 | (2201, 377_950_000, -1_224_050_000), |
| 416 | (2202, 377_950_000, -1_224_040_000), |
| 417 | (2203, 377_960_000, -1_224_040_000), |
| 418 | (2204, 377_960_000, -1_224_050_000), |
| 419 | ]; |
| 420 | |
| 421 | /// Way ids the admin boundary is assembled from. |
| 422 | pub const ADMIN_OUTER_WAY_A: i64 = 7001; |
| 423 | pub const ADMIN_OUTER_WAY_B: i64 = 7002; |
| 424 | pub const ADMIN_INNER_WAY: i64 = 7003; |
| 425 | |
| 426 | /// Node ids the vector-layer fixture's ways are built from. |
| 427 | const WAY_NODE_IDS: [i64; 6] = [2001, 2002, 2003, 2004, 2005, 2006]; |
| 428 | |
| 429 | /// The admin boundary's outer ring corners, then its hole's corners. A 0.04-degree |
| 430 | /// square near the other fixture geometry, with a 0.01-degree hole inside it. |
| 431 | const ADMIN_NODES: [(i64, i32, i32); 8] = [ |
| 432 | (2101, 378_000_000, -1_224_000_000), |
| 433 | (2102, 378_000_000, -1_223_600_000), |
| 434 | (2103, 378_400_000, -1_223_600_000), |
| 435 | (2104, 378_400_000, -1_224_000_000), |
| 436 | (2111, 378_100_000, -1_223_900_000), |
| 437 | (2112, 378_100_000, -1_223_800_000), |
| 438 | (2113, 378_200_000, -1_223_800_000), |
| 439 | (2114, 378_200_000, -1_223_900_000), |
| 440 | ]; |
| 441 | |
| 442 | /// One node in the vector-layer fixture: `(id, lat_e7, lon_e7, tags)`. |
| 443 | type FixtureNode = (i64, i32, i32, Vec<(u32, u32)>); |
| 444 | |
| 445 | include!("testpbf_part1.rs"); |