Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / testpbf.rs
testpbf.rs384 lines · 13.0 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 { strings: vec![Vec::new()] } |
| 127 | } |
| 128 | |
| 129 | fn id(&mut self, s: &str) -> u32 { |
| 130 | if let Some(i) = self.strings.iter().position(|x| x == s.as_bytes()) { |
| 131 | return i as u32; |
| 132 | } |
| 133 | self.strings.push(s.as_bytes().to_vec()); |
| 134 | (self.strings.len() - 1) as u32 |
| 135 | } |
| 136 | |
| 137 | fn encode(&self) -> Vec<u8> { |
| 138 | let mut out = Vec::new(); |
| 139 | for s in &self.strings { |
| 140 | bytes_field(1, s, &mut out); |
| 141 | } |
| 142 | out |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // ---- the fixture itself -------------------------------------------------- |
| 147 | |
| 148 | fn dense_nodes(st: &mut StringTable) -> Vec<u8> { |
| 149 | let stop_tags = [(st.id("highway"), st.id("bus_stop")), (st.id("name"), st.id("Test Stop"))]; |
| 150 | let cafe_tags = [ |
| 151 | (st.id("amenity"), st.id("cafe")), |
| 152 | (st.id("name"), st.id("Corner Cafe")), |
| 153 | // Sidecar attributes (poi_attrs.bin). `24/7` earns its place here: it used |
| 154 | // to parse as permanently closed, so it is the case worth pinning. |
| 155 | (st.id("opening_hours"), st.id("24/7")), |
| 156 | (st.id("contact:phone"), st.id("+1-555-0100")), |
| 157 | (st.id("website"), st.id("https://cafe.example")), |
| 158 | (st.id("addr:housenumber"), st.id("120")), |
| 159 | (st.id("addr:street"), st.id("Market St")), |
| 160 | (st.id("cuisine"), st.id("coffee_shop")), |
| 161 | ]; |
| 162 | |
| 163 | let mut keys_vals: Vec<u32> = Vec::new(); |
| 164 | for (id, _, _) in NODES { |
| 165 | let tags: &[(u32, u32)] = match id { |
| 166 | STOP_NODE_ID => &stop_tags, |
| 167 | CAFE_NODE_ID => &cafe_tags, |
| 168 | _ => &[], |
| 169 | }; |
| 170 | for (k, v) in tags { |
| 171 | keys_vals.push(*k); |
| 172 | keys_vals.push(*v); |
| 173 | } |
| 174 | keys_vals.push(ST_EMPTY); |
| 175 | } |
| 176 | |
| 177 | let mut out = Vec::new(); |
| 178 | packed_delta(1, &NODES.map(|n| n.0), &mut out); |
| 179 | packed_delta(8, &NODES.map(|n| n.1 as i64), &mut out); |
| 180 | packed_delta(9, &NODES.map(|n| n.2 as i64), &mut out); |
| 181 | packed_u32(10, &keys_vals, &mut out); |
| 182 | out |
| 183 | } |
| 184 | |
| 185 | fn way(id: i64, tags: &[(u32, u32)], refs: &[i64]) -> Vec<u8> { |
| 186 | let mut out = Vec::new(); |
| 187 | varint_field(1, id as u64, &mut out); |
| 188 | packed_u32(2, &tags.iter().map(|t| t.0).collect::<Vec<_>>(), &mut out); |
| 189 | packed_u32(3, &tags.iter().map(|t| t.1).collect::<Vec<_>>(), &mut out); |
| 190 | packed_delta(8, refs, &mut out); |
| 191 | out |
| 192 | } |
| 193 | |
| 194 | fn relation(id: i64, tags: &[(u32, u32)], members: &[(i64, u32, u32)]) -> Vec<u8> { |
| 195 | let mut out = Vec::new(); |
| 196 | varint_field(1, id as u64, &mut out); |
| 197 | packed_u32(2, &tags.iter().map(|t| t.0).collect::<Vec<_>>(), &mut out); |
| 198 | packed_u32(3, &tags.iter().map(|t| t.1).collect::<Vec<_>>(), &mut out); |
| 199 | packed_u32(8, &members.iter().map(|m| m.1).collect::<Vec<_>>(), &mut out); |
| 200 | packed_delta(9, &members.iter().map(|m| m.0).collect::<Vec<_>>(), &mut out); |
| 201 | packed_u32(10, &members.iter().map(|m| m.2).collect::<Vec<_>>(), &mut out); |
| 202 | out |
| 203 | } |
| 204 | |
| 205 | /// The `PrimitiveBlock` message for the fixture: one dense-node group, one way |
| 206 | /// group and one relation group. |
| 207 | pub fn primitive_block() -> Vec<u8> { |
| 208 | let mut st = StringTable::new(); |
| 209 | let dense = dense_nodes(&mut st); |
| 210 | |
| 211 | let (k_hw, k_name, k_lanes, k_tlf, k_oneway, k_maxspeed, k_amenity, k_type, k_leisure) = ( |
| 212 | st.id("highway"), |
| 213 | st.id("name"), |
| 214 | st.id("lanes"), |
| 215 | st.id("turn:lanes:forward"), |
| 216 | st.id("oneway"), |
| 217 | st.id("maxspeed"), |
| 218 | st.id("amenity"), |
| 219 | st.id("type"), |
| 220 | st.id("leisure"), |
| 221 | ); |
| 222 | let v_residential = st.id("residential"); |
| 223 | let v_main = st.id("Main St"); |
| 224 | let v_two = st.id("2"); |
| 225 | let v_left_through = st.id("left|through"); |
| 226 | let v_service = st.id("service"); |
| 227 | let v_yes = st.id("yes"); |
| 228 | let v_30mph = st.id("30 mph"); |
| 229 | let v_cafe = st.id("cafe"); |
| 230 | let v_plaza = st.id("Plaza"); |
| 231 | let v_multipolygon = st.id("multipolygon"); |
| 232 | let v_park = st.id("Riverside Park"); |
| 233 | let v_leisure_park = st.id("park"); |
| 234 | let role_outer = st.id("outer"); |
| 235 | |
| 236 | let ways = [ |
| 237 | // A 4-node bidirectional residential road with real turn:lanes. |
| 238 | way( |
| 239 | MAIN_WAY_ID, |
| 240 | &[(k_hw, v_residential), (k_name, v_main), (k_lanes, v_two), (k_tlf, v_left_through)], |
| 241 | &[1, 2, 3, 4], |
| 242 | ), |
| 243 | // A oneway service road, mph speed limit, no name. |
| 244 | way( |
| 245 | SERVICE_WAY_ID, |
| 246 | &[(k_hw, v_service), (k_oneway, v_yes), (k_maxspeed, v_30mph)], |
| 247 | &[4, 2], |
| 248 | ), |
| 249 | // A closed, unrouted area: a POI polygon, invisible to the road graph. |
| 250 | way(AREA_WAY_ID, &[(k_amenity, v_cafe), (k_name, v_plaza)], &[1, 2, 3, 4, 1]), |
| 251 | ]; |
| 252 | let rels = [relation( |
| 253 | RELATION_ID, |
| 254 | &[(k_type, v_multipolygon), (k_name, v_park), (k_leisure, v_leisure_park)], |
| 255 | // Outer ring = the closed way above, the case where the centroid can be |
| 256 | // reproduced exactly. |
| 257 | &[(AREA_WAY_ID, role_outer, 1)], |
| 258 | )]; |
| 259 | |
| 260 | let mut node_group = Vec::new(); |
| 261 | bytes_field(2, &dense, &mut node_group); |
| 262 | let mut way_group = Vec::new(); |
| 263 | for w in &ways { |
| 264 | bytes_field(3, w, &mut way_group); |
| 265 | } |
| 266 | let mut rel_group = Vec::new(); |
| 267 | for r in &rels { |
| 268 | bytes_field(4, r, &mut rel_group); |
| 269 | } |
| 270 | |
| 271 | let mut block = Vec::new(); |
| 272 | bytes_field(1, &st.encode(), &mut block); |
| 273 | bytes_field(2, &node_group, &mut block); |
| 274 | bytes_field(2, &way_group, &mut block); |
| 275 | bytes_field(2, &rel_group, &mut block); |
| 276 | varint_field(17, 100, &mut block); // granularity |
| 277 | block |
| 278 | } |
| 279 | |
| 280 | /// The `Blob` message wrapping the fixture's `PrimitiveBlock`. |
| 281 | pub fn sample_data_blob() -> Vec<u8> { |
| 282 | data_blob(&primitive_block()) |
| 283 | } |
| 284 | |
| 285 | fn data_blob(payload: &[u8]) -> Vec<u8> { |
| 286 | let mut blob = Vec::new(); |
| 287 | varint_field(2, payload.len() as u64, &mut blob); // raw_size |
| 288 | bytes_field(3, &zlib_store(payload), &mut blob); // zlib_data |
| 289 | blob |
| 290 | } |
| 291 | |
| 292 | fn framed(kind: &str, blob: &[u8], out: &mut Vec<u8>) { |
| 293 | let mut header = Vec::new(); |
| 294 | bytes_field(1, kind.as_bytes(), &mut header); |
| 295 | varint_field(3, blob.len() as u64, &mut header); |
| 296 | out.extend_from_slice(&(header.len() as u32).to_be_bytes()); |
| 297 | out.extend_from_slice(&header); |
| 298 | out.extend_from_slice(blob); |
| 299 | } |
| 300 | |
| 301 | /// A complete `.osm.pbf`: one `OSMHeader` blob (which every reader must skip) |
| 302 | /// followed by one `OSMData` blob. |
| 303 | pub fn sample_pbf() -> Vec<u8> { |
| 304 | pbf_from_block(&primitive_block()) |
| 305 | } |
| 306 | |
| 307 | fn pbf_from_block(block: &[u8]) -> Vec<u8> { |
| 308 | pbf_from_blocks(std::iter::once(block)) |
| 309 | } |
| 310 | |
| 311 | /// A complete `.osm.pbf` with one `OSMData` blob per block. |
| 312 | /// |
| 313 | /// Exists so a test can produce more than the one chunk `CHUNK_BLOBS` groups blobs |
| 314 | /// into. Everything about `run_pass_sink` that is interesting under concurrency — the |
| 315 | /// reorder buffer, its window, the ordered drain — is inert on a single-chunk file. |
| 316 | pub fn pbf_from_blocks<'a>(blocks: impl IntoIterator<Item = &'a [u8]>) -> Vec<u8> { |
| 317 | let mut header_block = Vec::new(); |
| 318 | bytes_field(4, b"OsmSchema-V0.6", &mut header_block); |
| 319 | bytes_field(4, b"DenseNodes", &mut header_block); |
| 320 | bytes_field(16, b"osm_ingest test fixture", &mut header_block); |
| 321 | let mut header_blob = Vec::new(); |
| 322 | varint_field(2, header_block.len() as u64, &mut header_blob); |
| 323 | bytes_field(3, &zlib_store(&header_block), &mut header_blob); |
| 324 | |
| 325 | let mut out = Vec::new(); |
| 326 | framed("OSMHeader", &header_blob, &mut out); |
| 327 | for block in blocks { |
| 328 | framed("OSMData", &data_blob(block), &mut out); |
| 329 | } |
| 330 | out |
| 331 | } |
| 332 | |
| 333 | /// Write the fixture to a unique temp directory and return the path plus the |
| 334 | /// directory (which the caller may use for outputs). |
| 335 | pub fn write_sample(tag: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
| 336 | write_pbf(tag, &sample_pbf()) |
| 337 | } |
| 338 | |
| 339 | fn write_pbf(tag: &str, bytes: &[u8]) -> (std::path::PathBuf, std::path::PathBuf) { |
| 340 | let dir = std::env::temp_dir().join(format!("osm_ingest_{tag}")); |
| 341 | let _ = std::fs::remove_dir_all(&dir); |
| 342 | std::fs::create_dir_all(&dir).unwrap(); |
| 343 | let path = dir.join("sample.osm.pbf"); |
| 344 | std::fs::write(&path, bytes).unwrap(); |
| 345 | (path, dir) |
| 346 | } |
| 347 | |
| 348 | // ---- the POI region-link fixture ------------------------------------------- |
| 349 | // |
| 350 | // A FOURTH fixture, for `poi_build`'s boundary links. The road-graph, vector and |
| 351 | // POI tests assert exact counts against the fixtures above, so this one is |
| 352 | // separate: a `type=boundary` + `boundary=administrative` relation that is |
| 353 | // itself a POI (`leisure=park` gives it a type, so it self-links), a |
| 354 | // `place=town` label carried as a `label` member OUTSIDE the ring (only the |
| 355 | // member link, never containment, can stamp it), one cafe inside the ring |
| 356 | // (containment) and one outside it (unstamped). |
| 357 | |
| 358 | /// `type=boundary` + `boundary=administrative` + `admin_level=8`, itself a park |
| 359 | /// POI. |
| 360 | pub const REGION_BOUNDARY_ID: i64 = 300; |
| 361 | /// The boundary's single closed outer ring. |
| 362 | pub const REGION_RING_WAY_ID: i64 = 400; |
| 363 | /// `amenity=cafe` inside the ring. |
| 364 | pub const REGION_INSIDE_CAFE_ID: i64 = 15; |
| 365 | /// `amenity=cafe` outside the ring. |
| 366 | pub const REGION_OUTSIDE_CAFE_ID: i64 = 16; |
| 367 | /// `place=town` carried as a `label` member, deliberately outside the ring. |
| 368 | pub const REGION_TOWN_NODE_ID: i64 = 17; |
| 369 | |
| 370 | /// `(id, lat_e7, lon_e7)` of the ring corners plus the POI nodes: a |
| 371 | /// 0.04-degree square, with the cafes and the town on its middle parallel. |
| 372 | pub const REGION_NODES: [(i64, i32, i32); 7] = [ |
| 373 | (11, 371_000_000, -1_221_000_000), |
| 374 | (12, 371_000_000, -1_220_600_000), |
| 375 | (13, 371_040_000, -1_220_600_000), |
| 376 | (14, 371_040_000, -1_221_000_000), |
| 377 | (REGION_INSIDE_CAFE_ID, 371_020_000, -1_220_800_000), |
| 378 | (REGION_OUTSIDE_CAFE_ID, 371_100_000, -1_220_800_000), |
| 379 | (REGION_TOWN_NODE_ID, 371_120_000, -1_220_850_000), |
| 380 | ]; |
| 381 | |
| 382 | include!("testpbf_s1.rs"); |
| 383 | include!("testpbf_part2.rs"); |
| 384 | include!("testpbf_part1.rs"); |