Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / reader_extra.rs
reader_extra.rs99 lines · 3.5 KiBRaw
| 1 | pub(crate) fn ru32(b: &[u8], off: usize) -> u32 { |
| 2 | u32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]]) |
| 3 | } |
| 4 | pub(crate) fn ri32(b: &[u8], off: usize) -> i32 { |
| 5 | i32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]]) |
| 6 | } |
| 7 | pub(crate) fn ru64(b: &[u8], off: usize) -> u64 { |
| 8 | let mut a = [0u8; 8]; |
| 9 | a.copy_from_slice(&b[off..off + 8]); |
| 10 | u64::from_le_bytes(a) |
| 11 | } |
| 12 | |
| 13 | /// Read one unsigned LEB128 varint, advancing `pos`. |
| 14 | pub fn read_uvarint(b: &[u8], pos: &mut usize) -> u64 { |
| 15 | let mut result = 0u64; |
| 16 | let mut shift = 0; |
| 17 | loop { |
| 18 | let byte = b[*pos]; |
| 19 | *pos += 1; |
| 20 | result |= ((byte & 0x7f) as u64) << shift; |
| 21 | if byte & 0x80 == 0 { |
| 22 | break; |
| 23 | } |
| 24 | shift += 7; |
| 25 | } |
| 26 | result |
| 27 | } |
| 28 | |
| 29 | pub(crate) fn zigzag(u: u64) -> i64 { |
| 30 | ((u >> 1) as i64) ^ -((u & 1) as i64) |
| 31 | } |
| 32 | |
| 33 | /// One RAIL_LINES entry: a whole, unsimplified rail shape and the route it draws for. |
| 34 | pub struct RailLineRec { |
| 35 | pub feed_idx: u32, |
| 36 | pub route_type: u32, |
| 37 | /// The feed's own `route_color`, or 0 when it gave none. |
| 38 | pub color: u32, |
| 39 | pub name: String, |
| 40 | pub route_id: String, |
| 41 | /// `(lat_e7, lon_e7)`, every `shapes.txt` point in sequence order. |
| 42 | pub points: Vec<(i32, i32)>, |
| 43 | } |
| 44 | |
| 45 | impl crate::reader::Reader { |
| 46 | /// The v7 RAIL_LINES section, in the order it was written (feed, route_id, shape_id). |
| 47 | /// `None` for a v6 pack, which predates it. Panics on a malformed section: the pack is a |
| 48 | /// build input written by this crate, not untrusted data. |
| 49 | pub fn rail_lines(&self) -> Option<Vec<RailLineRec>> { |
| 50 | use crate::index::{NONE, RAIL_LINE_REC_BYTES}; |
| 51 | if self.section_count() as usize <= crate::reader::SEC_RAIL_LINES { |
| 52 | return None; |
| 53 | } |
| 54 | let s = self.sec_bytes(crate::reader::SEC_RAIL_LINES); |
| 55 | let count = ru32(s, 0) as usize; |
| 56 | let recs = 4; |
| 57 | let strings_at = recs + count * RAIL_LINE_REC_BYTES; |
| 58 | let strings_len = ru32(s, strings_at) as usize; |
| 59 | let strings = &s[strings_at + 4..strings_at + 4 + strings_len]; |
| 60 | let blobs = &s[strings_at + 4 + strings_len..]; |
| 61 | let text = |off: u32| -> String { |
| 62 | if off == NONE { |
| 63 | return String::new(); |
| 64 | } |
| 65 | let tail = &strings[off as usize..]; |
| 66 | let end = tail.iter().position(|&b| b == 0).unwrap_or(tail.len()); |
| 67 | String::from_utf8_lossy(&tail[..end]).into_owned() |
| 68 | }; |
| 69 | // Each record decodes on its own; `collect` keeps pack order. |
| 70 | use rayon::prelude::*; |
| 71 | let out = (0..count) |
| 72 | .into_par_iter() |
| 73 | .map(|i| { |
| 74 | let at = recs + i * RAIL_LINE_REC_BYTES; |
| 75 | let field = |k: usize| ru32(s, at + k * 4); |
| 76 | let mut pos = field(5) as usize; |
| 77 | let n = ru32(blobs, pos) as usize; |
| 78 | pos += 4; |
| 79 | let (mut lat, mut lon) = (0i64, 0i64); |
| 80 | let points = (0..n) |
| 81 | .map(|_| { |
| 82 | lat += zigzag(read_uvarint(blobs, &mut pos)); |
| 83 | lon += zigzag(read_uvarint(blobs, &mut pos)); |
| 84 | (lat as i32, lon as i32) |
| 85 | }) |
| 86 | .collect(); |
| 87 | RailLineRec { |
| 88 | feed_idx: field(0), |
| 89 | route_type: field(1), |
| 90 | color: field(2), |
| 91 | name: text(field(3)), |
| 92 | route_id: text(field(4)), |
| 93 | points, |
| 94 | } |
| 95 | }) |
| 96 | .collect(); |
| 97 | Some(out) |
| 98 | } |
| 99 | } |