gtfs_ingest: hash-keyed dedup, flat grids, streamed pack write
- Commit
3c72bcc11baff666ade9858343d3ef164143bf48- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-21 20:45 UTC (3 months ago)
- Parents
085dcde
Diff
scripts/maps/gtfs_ingest/Cargo.toml
diff --git a/scripts/maps/gtfs_ingest/Cargo.toml b/scripts/maps/gtfs_ingest/Cargo.tomlindex 26bb819401..83562addd4 100644--- a/scripts/maps/gtfs_ingest/Cargo.toml+++ b/scripts/maps/gtfs_ingest/Cargo.toml@@ -13,8 +13,8 @@ edition = "2021" publish = false license = "GPL-2.0-only" description = "GTFS -> compact on-device transit index for the Maps offline packs"-# The crate carries two binaries, so a bare `cargo run` would be ambiguous. Name-# the pack builder as the default so existing callers keep working.+# The crate carries several binaries, so a bare `cargo run` would be ambiguous.+# Name the pack builder as the default so existing callers keep working. default-run = "gtfs_ingest" [[bin]]@@ -29,6 +29,9 @@ path = "src/bin/transit_stops.rs" [[bin]] name = "resolve_feeds" path = "src/bin/resolve_feeds.rs"+[[bin]]+name = "transit_dump"+path = "src/bin/transit_dump.rs" [dependencies]
scripts/maps/gtfs_ingest/src/bin/transit_dump.rs
diff --git a/scripts/maps/gtfs_ingest/src/bin/transit_dump.rs b/scripts/maps/gtfs_ingest/src/bin/transit_dump.rsnew file mode 100644index 0000000000..faaa6028e1--- /dev/null+++ b/scripts/maps/gtfs_ingest/src/bin/transit_dump.rs@@ -0,0 +1,266 @@+//! `transit_dump` — a sort-stable, semantic text dump of a `.transit` pack.+//!+//! The pack's *bytes* are deliberately non-reproducible: trips are grouped out of+//! a `HashMap`, so RAPTOR route order, profile ids and service indices all vary+//! run to run (see the crate README). Byte-diffing two packs therefore proves+//! nothing. This dump exists so a refactor of the builder can be verified the+//! only way that is actually valid: dump before, dump after, `diff`, expect+//! empty.+//!+//! Everything whose order is a `HashMap` artefact is either sorted (route blocks,+//! trip lines) or replaced by a content signature (services, referenced by their+//! calendar + exceptions rather than by index). Everything whose order *is*+//! contractual — stop indices, grid cells, a route's stop pattern, a route's+//! trips by start time — is dumped in that order, so a reordering shows up.+//!+//! Usage: `transit_dump <pack.transit> [out.txt]` (stdout when `out.txt` is+//! omitted). Output is one line per stop and one per trip, so it is much larger+//! than the pack; it is meant for the small verification corpora, not `world`.++use gtfs_ingest::reader::{Reader, RouteRec};+use std::collections::HashMap;+use std::io::{BufWriter, Write};+use std::process::ExitCode;++fn main() -> ExitCode {+ let args: Vec<String> = std::env::args().collect();+ if args.len() < 2 || args.len() > 3 {+ eprintln!("usage: transit_dump <pack.transit> [out.txt]");+ return ExitCode::from(2);+ }+ let reader = match Reader::open(std::path::Path::new(&args[1])) {+ Ok(r) => r,+ Err(e) => {+ eprintln!("transit_dump: {e}");+ return ExitCode::FAILURE;+ }+ };+ let result = match args.get(2) {+ Some(path) => match std::fs::File::create(path) {+ Ok(f) => dump(&reader, &mut BufWriter::new(f)),+ Err(e) => Err(format!("cannot create {path}: {e}")),+ },+ None => {+ let stdout = std::io::stdout();+ dump(&reader, &mut BufWriter::new(stdout.lock()))+ }+ };+ match result {+ Ok(()) => ExitCode::SUCCESS,+ Err(e) => {+ eprintln!("transit_dump: {e}");+ ExitCode::FAILURE+ }+ }+}++fn dump(r: &Reader, out: &mut impl Write) -> Result<(), String> {+ let write = |out: &mut dyn Write, s: String| -> Result<(), String> {+ out.write_all(s.as_bytes()).map_err(|e| format!("write failed: {e}"))+ };++ write(out, format!("pack {:?}\n", r.pack_name()))?;+ write(out, format!("version {} sections {}\n", r.version(), r.section_count()))?;+ write(+ out,+ format!(+ "counts stops={} routes={} trips={} services={} profiles={} feeds={} grid_cells={}\n",+ r.stop_count(),+ r.route_count(),+ r.trip_count(),+ r.service_count(),+ r.profile_count(),+ r.feed_count(),+ r.grid_cell_count(),+ ),+ )?;+ let (min_lat, min_lon, max_lat, max_lon) = r.bbox();+ write(out, format!("bbox {min_lat} {min_lon} {max_lat} {max_lon}\n"))?;+ let g = r.grid();+ write(+ out,+ format!(+ "grid origin={},{} cell={} cols={} rows={}\n",+ g.lat0_e7, g.lon0_e7, g.cell_e7, g.cols, g.rows+ ),+ )?;++ // Services are referenced by content, not index: a service that only ever+ // appears on a trip is registered while iterating a `HashMap`, so its index+ // is one of the things that legitimately moves between runs.+ let sigs: Vec<String> = (0..r.service_count()).map(|s| service_sig(r, s)).collect();++ // --- Routes, grouped by feed and sorted within it ---+ let mut per_feed: Vec<Vec<String>> = vec![Vec::new(); r.feed_count() as usize];+ for ridx in 0..r.route_count() {+ let rec = r.route(ridx);+ per_feed[rec.feed_idx as usize].push(route_block(r, ridx, &rec, &sigs));+ }+ for feed in 0..r.feed_count() {+ write(+ out,+ format!(+ "feed {feed} name={:?} tz={:?} motis={:?}\n",+ r.feed_name(feed),+ r.feed_tz(feed),+ r.feed_motis_prefix(feed)+ ),+ )?;+ let blocks = &mut per_feed[feed as usize];+ blocks.sort();+ for b in blocks.iter() {+ write(out, b.clone())?;+ }+ }++ // --- Stops, in index order (per-feed contiguous, stops.txt order within) ---+ for s in 0..r.stop_count() {+ let (lat, lon) = r.stop_ll_e7(s);+ // A stop's routes come out of the builder in route order, which moves;+ // name them and sort.+ let mut routes: Vec<String> = r+ .stop_routes(s)+ .iter()+ .map(|&(route, pos)| format!("{:?}@{pos}", r.read_str(r.route(route).name_off)))+ .collect();+ routes.sort();+ let transfers: Vec<String> =+ r.transfers(s).iter().map(|&(to, secs)| format!("{to}:{secs}")).collect();+ write(+ out,+ format!(+ "stop {s} gtfs={:?} name={:?} code={:?} ll={lat},{lon} routes=[{}] transfers=[{}]\n",+ r.stop_gtfs_id(s),+ r.stop_name(s),+ r.stop_code(s),+ routes.join(" "),+ transfers.join(" "),+ ),+ )?;+ }++ // --- Distinct service definitions, sorted, with how many services share one ---+ let mut sig_counts: HashMap<&str, usize> = HashMap::new();+ for sig in &sigs {+ *sig_counts.entry(sig.as_str()).or_insert(0) += 1;+ }+ let mut distinct: Vec<(&&str, &usize)> = sig_counts.iter().collect();+ distinct.sort();+ for (sig, n) in distinct {+ write(out, format!("service x{n} {sig}\n"))?;+ }++ // --- Spatial grid, in file order (the reader binary-searches it ascending) ---+ let cells = r.grid_cells();+ assert!(+ cells.windows(2).all(|w| w[0].0 < w[1].0),+ "GRID_CELL_IDS must be strictly ascending or the device's binary search breaks"+ );+ for (cid, stops) in &cells {+ let list: Vec<String> = stops.iter().map(|s| s.to_string()).collect();+ write(out, format!("cell {cid} stops=[{}]\n", list.join(" ")))?;+ }++ out.flush().map_err(|e| format!("flush failed: {e}"))+}++/// One service's calendar plus its exceptions, as a content signature.+fn service_sig(r: &Reader, service_idx: u32) -> String {+ let svc = r.service(service_idx);+ let exc = r.service_exceptions(service_idx);+ assert!(+ exc.windows(2).all(|w| w[0].1 < w[1].1),+ "service {service_idx} exceptions must be date-ascending and deduped, got {exc:?}"+ );+ let dates: Vec<String> = exc+ .iter()+ .map(|&(sidx, date, added)| {+ assert_eq!(sidx, service_idx, "EXCEPTIONS row outside its CSR range");+ format!("{}{date}", if added == 1 { '+' } else { '-' })+ })+ .collect();+ format!(+ "days={:07b} {}..{} exc=[{}]",+ svc.weekday_mask,+ svc.start_date,+ svc.end_date,+ dates.join(" ")+ )+}++/// A whole route: its identity, geometry summary, stop pattern and every trip+/// with profile-expanded absolute times.+fn route_block(r: &Reader, ridx: u32, rec: &RouteRec, sigs: &[String]) -> String {+ let stops = r.route_stops(rec);+ let pattern: Vec<String> = stops+ .iter()+ .enumerate()+ .map(|(pos, &s)| {+ let v = r.route_stop_shape(rec.first_route_stop + pos as u32);+ if v == u32::MAX {+ format!("{s}")+ } else {+ format!("{s}@{v}")+ }+ })+ .collect();+ let shape = match r.route_shape_off(ridx) {+ None => "none".to_string(),+ Some(off) => {+ let pts = r.shape_points(off);+ format!("n={},sum={:016x}", pts.len(), fnv1a_points(&pts))+ }+ };+ let mut lines = vec![format!(+ " route name={:?} type={} color={:06x} shape={shape} stops=[{}]\n",+ r.read_str(rec.name_off),+ rec.route_type,+ rec.color,+ pattern.join(" "),+ )];++ // Trips are start-time ordered by construction, but ties are `HashMap`+ // order, so sort the rendered lines. Zero-padding keeps that a numeric sort.+ let mut trips: Vec<String> = r+ .route_trips(rec)+ .iter()+ .map(|t| {+ let start = t.start_time as i64;+ let times: Vec<String> = r+ .profile(t.profile_id)+ .iter()+ .map(|&(arr, dep)| format!("{}:{}", start + arr, start + dep))+ .collect();+ assert_eq!(+ times.len(),+ stops.len(),+ "route {ridx} trip at {start} has {} profile entries for {} stops",+ times.len(),+ stops.len()+ );+ format!(+ " trip start={:08} svc={} headsign={:?} times=[{}]\n",+ t.start_time,+ sigs[t.service_idx as usize],+ r.read_str(t.headsign_off),+ times.join(" "),+ )+ })+ .collect();+ trips.sort();+ lines.append(&mut trips);+ lines.concat()+}++/// FNV-1a over a polyline's coordinates: a full comparison of the geometry+/// without printing every vertex of every route.+fn fnv1a_points(pts: &[(i32, i32)]) -> u64 {+ let mut h: u64 = 0xcbf2_9ce4_8422_2325;+ for &(lat, lon) in pts {+ for b in lat.to_le_bytes().iter().chain(lon.to_le_bytes().iter()) {+ h ^= *b as u64;+ h = h.wrapping_mul(0x100_0000_01b3);+ }+ }+ h+}
scripts/maps/gtfs_ingest/src/index.rs
diff --git a/scripts/maps/gtfs_ingest/src/index.rs b/scripts/maps/gtfs_ingest/src/index.rsindex 37805220d5..b2fce7c7fd 100644--- a/scripts/maps/gtfs_ingest/src/index.rs+++ b/scripts/maps/gtfs_ingest/src/index.rs@@ -125,9 +125,13 @@ use crate::gtfs::{parse_gtfs_date, parse_gtfs_time, Csv, Shape}; use crate::shapes; use std::collections::HashMap;+use std::io::Write; pub const MAGIC: u32 = 0x5452_4958; // "TRIX" pub const VERSION: u32 = 5;+/// Oldest version the device reader still accepts, mirrored from+/// `maps/src/main/rust/src/transit.rs` so the host reader agrees on the range.+pub const VERSION_MIN: u32 = 3; pub const NONE: u32 = 0xFFFF_FFFF; pub const SECTION_COUNT: usize = 25; pub const HEADER_LEN: usize = 80;@@ -156,32 +160,173 @@ fn write_uvarint(v: &mut Vec<u8>, mut x: u64) { } } +/// FNV-1a, 64-bit. Only ever used to *find* dedup candidates, which are then+/// confirmed byte-for-byte against the buffer that already holds them — so a+/// collision costs a few duplicated bytes and never a wrong merge.+fn fnv1a(bytes: &[u8]) -> u64 {+ fnv1a_mix(0xcbf2_9ce4_8422_2325, bytes)+}++/// Continue an FNV-1a hash, for keys built from more than one piece.+fn fnv1a_mix(mut h: u64, bytes: &[u8]) -> u64 {+ for &b in bytes {+ h ^= b as u64;+ h = h.wrapping_mul(0x100_0000_01b3);+ }+ h+}+ /// Interning string pool producing byte offsets into the STRINGS section.+///+/// The pool does **not** keep a copy of what it interns: `bytes` is the only+/// copy, and the index is `hash -> offsets`, each candidate confirmed by+/// comparing it against `bytes`. On a world pack the second copy cost several+/// gigabytes on its own. struct StringPool { bytes: Vec<u8>,- map: HashMap<String, u32>,+ by_hash: HashMap<u64, Vec<u32>>,+ /// Set when the pool would pass the 4 GiB that a u32 STRINGS offset can+ /// address. Checked once before the pack is assembled: silently wrapping+ /// here produces a pack the device will happily mmap and mis-read.+ overflowed: bool, } impl StringPool { fn new() -> StringPool { // Byte 0 is a lone NUL so offset 0 is a valid empty string, keeping // NONE (0xFFFFFFFF) unambiguous.- StringPool { bytes: vec![0], map: HashMap::new() }+ StringPool { bytes: vec![0], by_hash: HashMap::new(), overflowed: false } } fn intern(&mut self, s: &str) -> u32 { if s.is_empty() { return NONE; }- if let Some(&off) = self.map.get(s) {+ let h = fnv1a(s.as_bytes());+ if let Some(off) =+ self.by_hash.get(&h).and_then(|offs| {+ offs.iter().copied().find(|&off| self.matches_at(off, s))+ })+ { return off; }+ // `+ 1` for the terminating NUL; the last valid offset must still be a+ // u32, and NONE is reserved.+ if self.bytes.len() + s.len() + 1 >= NONE as usize {+ self.overflowed = true;+ return NONE;+ } let off = self.bytes.len() as u32; self.bytes.extend_from_slice(s.as_bytes()); self.bytes.push(0);- self.map.insert(s.to_string(), off);+ self.by_hash.entry(h).or_default().push(off); off }++ /// Whether the interned string at `off` is exactly `s`. The NUL check is+ /// what stops `s` matching a prefix of a longer entry.+ fn matches_at(&self, off: u32, s: &str) -> bool {+ let start = off as usize;+ let end = start + s.len();+ end < self.bytes.len()+ && self.bytes[end] == 0+ && &self.bytes[start..end] == s.as_bytes()+ }+}++/// The PROFILES section plus its dedup index: run-time shapes are shared by+/// every trip that runs them, which is what removed v1's per-trip stop-time+/// table. Like [`StringPool`], the index is `hash -> ids` and holds no second+/// copy of the bodies.+struct ProfileTable {+ bytes: Vec<u8>,+ /// Byte offset into `bytes` per profile id.+ offsets: Vec<u32>,+ by_hash: HashMap<u64, Vec<u32>>,+ /// Reused across trips so encoding one does not allocate.+ scratch: Vec<u8>,+}++impl ProfileTable {+ fn new() -> ProfileTable {+ ProfileTable {+ bytes: Vec::new(),+ offsets: Vec::new(),+ by_hash: HashMap::new(),+ scratch: Vec::new(),+ }+ }++ /// Encode a trip's `(arr, dep)` sequence as a profile body and return its id,+ /// reusing an identical existing profile.+ fn intern(&mut self, sts: &[(u32, u32)]) -> Result<u32, String> {+ let n = sts.len();+ self.scratch.clear();+ write_uvarint(&mut self.scratch, n as u64);+ let (arr0, dep0) = sts[0];+ write_uvarint(&mut self.scratch, dep0.saturating_sub(arr0) as u64);+ let mut prev_dep = dep0;+ for k in 1..n {+ let (arr, dep) = sts[k];+ write_uvarint(&mut self.scratch, arr.saturating_sub(prev_dep) as u64);+ write_uvarint(&mut self.scratch, dep.saturating_sub(arr) as u64);+ prev_dep = dep;+ }+ let h = fnv1a(&self.scratch);+ if let Some(id) = self.by_hash.get(&h).and_then(|ids| {+ ids.iter().copied().find(|&id| {+ let start = self.offsets[id as usize] as usize;+ self.bytes[start..].starts_with(&self.scratch)+ })+ }) {+ return Ok(id);+ }+ // PROFILES_IDX entries are u32 byte offsets into PROFILES.+ let off = u32::try_from(self.bytes.len()).map_err(|_| {+ "PROFILES exceeded 4 GiB, which a u32 PROFILES_IDX offset cannot address"+ .to_string()+ })?;+ let id = self.offsets.len() as u32;+ self.offsets.push(off);+ self.bytes.extend_from_slice(&self.scratch);+ self.by_hash.entry(h).or_default().push(id);+ Ok(id)+ }+}++/// The SHAPE_COORDS section plus its dedup index, so routes producing identical+/// geometry share one blob. Blobs are self-delimiting, so a candidate is+/// confirmed by a prefix comparison at its offset.+struct ShapeBlobs {+ bytes: Vec<u8>,+ by_hash: HashMap<u64, Vec<u32>>,+}++impl ShapeBlobs {+ fn new() -> ShapeBlobs {+ ShapeBlobs { bytes: Vec::new(), by_hash: HashMap::new() }+ }++ fn intern(&mut self, blob: &[u8]) -> Result<u32, String> {+ let h = fnv1a(blob);+ if let Some(off) = self.by_hash.get(&h).and_then(|offs| {+ offs.iter().copied().find(|&off| self.bytes[off as usize..].starts_with(blob))+ }) {+ return Ok(off);+ }+ // ROUTE_SHAPE_IDX holds u32 byte offsets and reserves NONE, so the+ // section itself has to stay below that.+ if self.bytes.len() >= NONE as usize {+ return Err(+ "SHAPE_COORDS exceeded the 4 GiB a u32 ROUTE_SHAPE_IDX offset can address"+ .to_string(),+ );+ }+ let off = self.bytes.len() as u32;+ self.bytes.extend_from_slice(blob);+ self.by_hash.entry(h).or_default().push(off);+ Ok(off)+ } } struct StopTime {@@ -260,6 +405,10 @@ fn dist_m(lat1_e7: i32, lon1_e7: i32, lat2_e7: i32, lon2_e7: i32) -> f64 { /// A RAPTOR route: trips grouped by identical (feed, gtfs route, stop pattern). struct RaptorRoute { feed_idx: u32,+ /// The feed's own `route_id`. Kept because grouping is by+ /// `(route_id, stop_pattern)`: two GTFS routes over the same stops are two+ /// RAPTOR routes, and `pattern_index` confirms a candidate against this.+ route_id: String, name_off: u32, color: u32, route_type: u32,@@ -301,10 +450,24 @@ fn ensure_service( /// Build the full index blob from one or more GTFS feeds. `pack_name` is stored /// in the string pool and surfaced to the on-device planner as a fallback.+///+/// Buffers the whole pack in memory; prefer [`build_index_to`] for anything+/// larger than a test corpus. pub fn build_index( pack_name: &str, feeds: &[FeedInput], ) -> Result<(Vec<u8>, BuildStats), String> {+ let mut blob = Vec::new();+ let stats = build_index_to(pack_name, feeds, &mut blob)?;+ Ok((blob, stats))+}++/// Build the index straight into `out` (a `BufWriter<File>` in the host tool).+pub fn build_index_to(+ pack_name: &str,+ feeds: &[FeedInput],+ out: &mut impl Write,+) -> Result<BuildStats, String> { let mut pool = StringPool::new(); let feed_name_off = pool.intern(pack_name); @@ -508,7 +671,13 @@ pub fn build_index( } // --- Group this feed's trips into RAPTOR routes ---- let mut pattern_key_to_route: HashMap<String, usize> = HashMap::new();+ // Keyed by a hash of `(route_id, stop_pattern)`, with every candidate+ // compared in full. Unlike the other dedup tables a false match here+ // would fuse two distinct patterns into one route — wrong departures+ // rather than wasted bytes — so a hash alone is not enough. Comparing+ // against the pattern the route already stores also removes the ~240-byte+ // key string this used to allocate per trip.+ let mut pattern_index: HashMap<u64, Vec<usize>> = HashMap::new(); for (trip_id, mut sts) in trip_stoptimes { if sts.len() < 2 { continue;@@ -526,26 +695,35 @@ pub fn build_index( &mut svc_start, &mut svc_end, );- let key = format!(- "{}|{}",- tmeta.route_id,- pattern.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(",")- );- let route_idx = *pattern_key_to_route.entry(key).or_insert_with(|| {- let (name_off, color, rtype) = match route_meta.get(&tmeta.route_id) {- Some(m) => (m.name_off, m.color, m.route_type),- None => (NONE, 0, 3),- };- raptor_routes.push(RaptorRoute {- feed_idx,- name_off,- color,- route_type: rtype,- stop_pattern: pattern.clone(),- trips: Vec::new(),- });- raptor_routes.len() - 1- });+ let mut hasher = fnv1a(tmeta.route_id.as_bytes());+ for &s in &pattern {+ hasher = fnv1a_mix(hasher, &s.to_le_bytes());+ }+ let slot = pattern_index.entry(hasher).or_default();+ let route_idx = match slot.iter().copied().find(|&i| {+ raptor_routes[i].route_id == tmeta.route_id+ && raptor_routes[i].stop_pattern == pattern+ }) {+ Some(i) => i,+ None => {+ let (name_off, color, rtype) = match route_meta.get(&tmeta.route_id) {+ Some(m) => (m.name_off, m.color, m.route_type),+ None => (NONE, 0, 3),+ };+ raptor_routes.push(RaptorRoute {+ feed_idx,+ route_id: tmeta.route_id.clone(),+ name_off,+ color,+ route_type: rtype,+ stop_pattern: pattern,+ trips: Vec::new(),+ });+ let i = raptor_routes.len() - 1;+ slot.push(i);+ i+ }+ }; let bt = BuiltTrip { service_idx, headsign_off: tmeta.headsign_off,@@ -579,35 +757,7 @@ pub fn build_index( } // --- Profiles: dedup run-time shapes into a varint table ---- let mut profile_bytes: Vec<u8> = Vec::new();- let mut profile_offsets: Vec<u32> = Vec::new(); // byte offset per profile id- let mut profile_key_to_id: HashMap<Vec<u8>, u32> = HashMap::new();- // Encode a trip's (arr,dep) sequence into a profile body; return its id.- let mut intern_profile = |sts: &[(u32, u32)]| -> u32 {- let n = sts.len();- let mut body: Vec<u8> = Vec::new();- write_uvarint(&mut body, n as u64);- let (arr0, dep0) = sts[0];- let dwell0 = dep0.saturating_sub(arr0);- write_uvarint(&mut body, dwell0 as u64);- let mut prev_dep = dep0;- for k in 1..n {- let (arr, dep) = sts[k];- let hop = arr.saturating_sub(prev_dep);- let dwell = dep.saturating_sub(arr);- write_uvarint(&mut body, hop as u64);- write_uvarint(&mut body, dwell as u64);- prev_dep = dep;- }- if let Some(&id) = profile_key_to_id.get(&body) {- return id;- }- let id = profile_offsets.len() as u32;- profile_offsets.push(profile_bytes.len() as u32);- profile_bytes.extend_from_slice(&body);- profile_key_to_id.insert(body, id);- id- };+ let mut profiles = ProfileTable::new(); // --- Flatten stops --- let mut sec_stops = Vec::with_capacity(stop_count * 16);@@ -629,12 +779,9 @@ pub fn build_index( let mut trip_total: usize = 0; let route_count = raptor_routes.len(); // --- v4 ride geometry ---- let mut sec_shape_coords: Vec<u8> = Vec::new();+ let mut shape_blobs = ShapeBlobs::new(); let mut sec_route_shape_idx = Vec::new(); let mut sec_route_stop_shape = Vec::new();- // Deduplicated by encoded blob, so routes producing identical geometry- // (patterns sharing a `shape_id` and stop list) store it once.- let mut shape_blob_offsets: HashMap<Vec<u8>, u32> = HashMap::new(); let mut shaped_routes = 0usize; let mut multi_shape_routes = 0usize; let mut dropped_shape_routes = 0usize;@@ -683,18 +830,7 @@ pub fn build_index( } let shape_off = match &fitted { None => NONE,- Some(f) => {- let blob = shapes::encode(&f.points);- match shape_blob_offsets.get(&blob) {- Some(&off) => off,- None => {- let off = sec_shape_coords.len() as u32;- sec_shape_coords.extend_from_slice(&blob);- shape_blob_offsets.insert(blob, off);- off- }- }- }+ Some(f) => shape_blobs.intern(&shapes::encode(&f.points))?, }; append_u32(&mut sec_route_shape_idx, shape_off); @@ -719,11 +855,15 @@ pub fn build_index( // Sort this route's trips by first-stop departure, then varint-pack. let mut trip_order = rr.trips.clone(); trip_order.sort_by_key(|&ti| built_trips[ti].start_time);- let trips_off = sec_route_trips.len() as u32;+ // RouteRec.trips_off is a u32 byte offset into ROUTE_TRIPS.+ let trips_off = u32::try_from(sec_route_trips.len()).map_err(|_| {+ "ROUTE_TRIPS exceeded 4 GiB, which a u32 RouteRec.trips_off cannot address"+ .to_string()+ })?; let mut prev_start: u32 = 0; for &ti in &trip_order { let bt = &built_trips[ti];- let profile_id = intern_profile(&bt.stoptimes);+ let profile_id = profiles.intern(&bt.stoptimes)?; let start_delta = bt.start_time.saturating_sub(prev_start); write_uvarint(&mut sec_route_trips, start_delta as u64); write_uvarint(&mut sec_route_trips, profile_id as u64);@@ -744,15 +884,25 @@ pub fn build_index( append_u32(&mut sec_routes, trips_off); } // Terminating bound the reader validates offsets against.- append_u32(&mut sec_route_shape_idx, sec_shape_coords.len() as u32);+ append_u32(&mut sec_route_shape_idx, shape_blobs.bytes.len() as u32); // Profiles index (byte offset per id, plus terminating length). let mut sec_profiles_idx = Vec::new();- for &off in &profile_offsets {+ for &off in &profiles.offsets { append_u32(&mut sec_profiles_idx, off); }- append_u32(&mut sec_profiles_idx, profile_bytes.len() as u32);- let profile_count = profile_offsets.len();+ append_u32(&mut sec_profiles_idx, profiles.bytes.len() as u32);+ let profile_count = profiles.offsets.len();++ // A wrapped STRINGS offset would name the wrong stop rather than fail, so+ // this is checked before anything is written.+ if pool.overflowed {+ return Err(+ "STRINGS exceeded the 4 GiB a u32 string offset can address; the pack cannot \+ represent this many feeds"+ .to_string(),+ );+ } // --- stop_routes flattened + index (+ parallel stop positions) --- let mut sec_stop_routes = Vec::new();@@ -770,16 +920,26 @@ pub fn build_index( append_u32(&mut sec_stop_routes_idx, acc); // --- Footpath transfers via a coarse spatial grid (cross-feed for free) ---- let mut xgrid: HashMap<(i32, i32), Vec<u32>> = HashMap::new();+ // A flat `(cell, stop)` vector sorted once, not a `HashMap` of buckets: on a+ // world pack the map is millions of cells each holding a tiny `Vec`, whose+ // headers and allocations cost far more than the stop ids in them. Sorting+ // by `(cell, stop)` keeps each cell's stops ascending, which is the order the+ // old bucket pushes produced and which the distance sort below tie-breaks on. let xcell = |lat_e7: i32, lon_e7: i32| -> (i32, i32) { ( (lat_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, (lon_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, ) };- for i in 0..stop_count {- xgrid.entry(xcell(stop_lat[i], stop_lon[i])).or_default().push(i as u32);- }+ let mut xgrid: Vec<((i32, i32), u32)> = (0..stop_count)+ .map(|i| (xcell(stop_lat[i], stop_lon[i]), i as u32))+ .collect();+ xgrid.sort_unstable();+ let xbucket = |key: (i32, i32)| -> &[((i32, i32), u32)] {+ let lo = xgrid.partition_point(|&(k, _)| k < key);+ let hi = xgrid.partition_point(|&(k, _)| k <= key);+ &xgrid[lo..hi]+ }; let mut transfer_total = 0usize; let mut sec_transfers = Vec::new(); let mut sec_transfers_idx = Vec::new();@@ -790,24 +950,20 @@ pub fn build_index( let mut cand: Vec<(u32, f64)> = Vec::new(); for dx in -1..=1 { for dy in -1..=1 {- if let Some(bucket) = xgrid.get(&(cx + dx, cy + dy)) {- for &j in bucket {- if j as usize == i {- continue;- }- let d = dist_m(- stop_lat[i],- stop_lon[i],- stop_lat[j as usize],- stop_lon[j as usize],- );- if d <= MAX_TRANSFER_M {- cand.push((j, d));- }+ for &(_, j) in xbucket((cx + dx, cy + dy)) {+ if j as usize == i {+ continue;+ }+ let d =+ dist_m(stop_lat[i], stop_lon[i], stop_lat[j as usize], stop_lon[j as usize]);+ if d <= MAX_TRANSFER_M {+ cand.push((j, d)); } } } }+ // Stable, so equidistant candidates keep the dx/dy/stop order above and+ // `truncate` drops a deterministic tail. cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)); cand.truncate(MAX_TRANSFERS_PER_STOP); for (j, d) in cand {@@ -872,29 +1028,30 @@ pub fn build_index( }; let grid_cols = (cell_col(max_lon) + 1) as u32; let grid_rows = (cell_row(max_lat) + 1) as u32;- let mut grid: HashMap<u32, Vec<u32>> = HashMap::new();- for i in 0..stop_count {- let row = cell_row(stop_lat[i]) as u32;- let col = cell_col(stop_lon[i]) as u32;- let cell_id = row * grid_cols + col;- grid.entry(cell_id).or_default().push(i as u32);- }- let mut cell_ids: Vec<u32> = grid.keys().copied().collect();- cell_ids.sort_unstable();- let mut sec_grid_cell_ids = Vec::with_capacity(cell_ids.len() * 4);- let mut sec_grid_cell_off = Vec::with_capacity((cell_ids.len() + 1) * 4);+ // Sorted by `(cell_id, stop)`, which is exactly the two orderings the device+ // requires: GRID_CELL_IDS ascending, because the reader binary-searches it,+ // and each cell's GRID_STOPS run stop-ascending.+ let mut grid: Vec<(u32, u32)> = (0..stop_count)+ .map(|i| {+ let row = cell_row(stop_lat[i]) as u32;+ let col = cell_col(stop_lon[i]) as u32;+ (row * grid_cols + col, i as u32)+ })+ .collect();+ grid.sort_unstable();+ let mut sec_grid_cell_ids = Vec::new();+ let mut sec_grid_cell_off = Vec::new(); let mut sec_grid_stops = Vec::with_capacity(stop_count * 4);- let mut gacc: u32 = 0;- for &cid in &cell_ids {- append_u32(&mut sec_grid_cell_ids, cid);- append_u32(&mut sec_grid_cell_off, gacc);- for &s in &grid[&cid] {- append_u32(&mut sec_grid_stops, s);+ let mut grid_cell_count = 0usize;+ for (k, &(cid, stop)) in grid.iter().enumerate() {+ if k == 0 || cid != grid[k - 1].0 {+ append_u32(&mut sec_grid_cell_ids, cid);+ append_u32(&mut sec_grid_cell_off, k as u32);+ grid_cell_count += 1; }- gacc += grid[&cid].len() as u32;+ append_u32(&mut sec_grid_stops, stop); }- append_u32(&mut sec_grid_cell_off, gacc);- let grid_cell_count = cell_ids.len();+ append_u32(&mut sec_grid_cell_off, grid.len() as u32); // --- Feeds --- let mut sec_feeds = Vec::with_capacity(feed_name_offs.len() * 4);@@ -948,7 +1105,7 @@ pub fn build_index( &sec_routes, &sec_route_stops, &sec_route_trips,- &profile_bytes,+ &profiles.bytes, &sec_profiles_idx, &sec_stop_routes, &sec_stop_routes_idx,@@ -963,7 +1120,7 @@ pub fn build_index( &sec_feed_tz, &sec_exceptions_idx, &sec_stop_route_pos,- &sec_shape_coords,+ &shape_blobs.bytes, &sec_route_shape_idx, &sec_route_stop_shape, &sec_feed_motis_prefix,@@ -973,50 +1130,42 @@ pub fn build_index( let dir_len = SECTION_COUNT * 16; let mut data_off = HEADER_LEN + dir_len; let align = |o: usize| (o + 7) & !7;- let mut dir: Vec<(u64, u64)> = Vec::with_capacity(SECTION_COUNT); for s in sections.iter() {- data_off = align(data_off);- dir.push((data_off as u64, s.len() as u64));- data_off += s.len();+ data_off = align(data_off) + s.len(); } let total = data_off; - let mut out = Vec::with_capacity(total);- append_u32(&mut out, MAGIC);- append_u32(&mut out, VERSION);- append_u32(&mut out, SECTION_COUNT as u32);- append_u32(&mut out, stop_count as u32);- append_u32(&mut out, route_count as u32);- append_u32(&mut out, trip_total as u32);- append_u32(&mut out, svc_mask.len() as u32);- append_u32(&mut out, profile_count as u32);- append_u32(&mut out, feed_name_offs.len() as u32);- append_u32(&mut out, grid_cell_count as u32);- append_u32(&mut out, feed_name_off);- append_i32(&mut out, min_lat);- append_i32(&mut out, min_lon);- append_i32(&mut out, max_lat);- append_i32(&mut out, max_lon);- append_i32(&mut out, grid_lat0);- append_i32(&mut out, grid_lon0);- append_u32(&mut out, grid_cell_e7);- append_u32(&mut out, grid_cols);- append_u32(&mut out, grid_rows);- debug_assert_eq!(out.len(), HEADER_LEN);- for (off, len) in &dir {- out.extend_from_slice(&off.to_le_bytes());- out.extend_from_slice(&len.to_le_bytes());- }- for s in sections.iter() {- let pad = align(out.len()) - out.len();- out.extend(std::iter::repeat(0u8).take(pad));- out.extend_from_slice(s);- }+ let mut header: Vec<u8> = Vec::with_capacity(HEADER_LEN);+ append_u32(&mut header, MAGIC);+ append_u32(&mut header, VERSION);+ append_u32(&mut header, SECTION_COUNT as u32);+ append_u32(&mut header, stop_count as u32);+ append_u32(&mut header, route_count as u32);+ append_u32(&mut header, trip_total as u32);+ append_u32(&mut header, svc_mask.len() as u32);+ append_u32(&mut header, profile_count as u32);+ append_u32(&mut header, feed_name_offs.len() as u32);+ append_u32(&mut header, grid_cell_count as u32);+ append_u32(&mut header, feed_name_off);+ append_i32(&mut header, min_lat);+ append_i32(&mut header, min_lon);+ append_i32(&mut header, max_lat);+ append_i32(&mut header, max_lon);+ append_i32(&mut header, grid_lat0);+ append_i32(&mut header, grid_lon0);+ append_u32(&mut header, grid_cell_e7);+ append_u32(&mut header, grid_cols);+ append_u32(&mut header, grid_rows);+ debug_assert_eq!(header.len(), HEADER_LEN);++ let written =+ write_pack(out, &header, §ions).map_err(|e| format!("cannot write the pack: {e}"))?;+ debug_assert_eq!(written, total); let section_sizes: Vec<(&'static str, usize)> = section_names.iter().zip(sections.iter()).map(|(&n, s)| (n, s.len())).collect(); - let stats = BuildStats {+ Ok(BuildStats { stops: stop_count, routes: route_count, trips: trip_total,@@ -1027,269 +1176,64 @@ pub fn build_index( min_lon_e7: min_lon, max_lat_e7: max_lat, max_lon_e7: max_lon,- size_bytes: out.len(),+ size_bytes: written, section_sizes, shaped_routes, multi_shape_routes, dropped_shape_routes,- };- Ok((out, stats))+ })+}++/// Write header + section directory + 8-byte-aligned payloads to `out`, and+/// return the total byte length.+///+/// Every section's length is known by the time this runs, so the directory is+/// computed up front: nothing is reserved and seeked back to, which is what lets+/// the pack go straight to a `BufWriter` instead of being concatenated into one+/// multi-gigabyte `Vec` first.+fn write_pack(+ out: &mut impl Write,+ header: &[u8],+ sections: &[&[u8]; SECTION_COUNT],+) -> std::io::Result<usize> {+ const PAD: [u8; 8] = [0; 8];+ let align = |o: usize| (o + 7) & !7;++ let mut off = HEADER_LEN + SECTION_COUNT * 16;+ let mut dir = Vec::with_capacity(SECTION_COUNT * 16);+ for s in sections {+ off = align(off);+ dir.extend_from_slice(&(off as u64).to_le_bytes());+ dir.extend_from_slice(&(s.len() as u64).to_le_bytes());+ off += s.len();+ }+ let total = off;++ out.write_all(header)?;+ out.write_all(&dir)?;+ let mut pos = header.len() + dir.len();+ for s in sections {+ let pad = align(pos) - pos;+ out.write_all(&PAD[..pad])?;+ out.write_all(s)?;+ pos += pad + s.len();+ }+ debug_assert_eq!(pos, total);+ Ok(total) } #[cfg(test)] mod tests { use super::*; use crate::gtfs::parse_csv;-- // --- Minimal TRX2 reader mirroring maps/src/main/rust/src/transit.rs ---- struct Reader {- buf: Vec<u8>,- sec: Vec<(usize, usize)>,- }- fn ru32(b: &[u8], off: usize) -> u32 {- u32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])- }- fn ri32(b: &[u8], off: usize) -> i32 {- i32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])- }- fn ru64(b: &[u8], off: usize) -> u64 {- let mut a = [0u8; 8];- a.copy_from_slice(&b[off..off + 8]);- u64::from_le_bytes(a)- }- fn read_uvarint(b: &[u8], pos: &mut usize) -> u64 {- let mut result = 0u64;- let mut shift = 0;- loop {- let byte = b[*pos];- *pos += 1;- result |= ((byte & 0x7f) as u64) << shift;- if byte & 0x80 == 0 {- break;- }- shift += 7;- }- result- }- impl Reader {- fn new(buf: Vec<u8>) -> Reader {- let sc = ru32(&buf, 8) as usize;- let mut sec = Vec::new();- for i in 0..sc {- let base = HEADER_LEN + i * 16;- sec.push((ru64(&buf, base) as usize, ru64(&buf, base + 8) as usize));- }- Reader { buf, sec }- }- fn h(&self, idx: usize) -> u32 {- ru32(&self.buf, idx * 4)- }- fn sec_bytes(&self, s: usize) -> &[u8] {- let (o, l) = self.sec[s];- &self.buf[o..o + l]- }- fn read_str(&self, off: u32) -> String {- if off == NONE {- return String::new();- }- let s = self.sec_bytes(0);- let start = off as usize;- let mut n = 0;- while start + n < s.len() && s[start + n] != 0 {- n += 1;- }- String::from_utf8_lossy(&s[start..start + n]).into_owned()- }- fn stop_ll(&self, i: u32) -> (f64, f64) {- let s = self.sec_bytes(1);- let base = i as usize * 16;- (ri32(s, base) as f64 * 1e-7, ri32(s, base + 4) as f64 * 1e-7)- }- // RouteRec fields- fn route(&self, r: u32) -> [u32; 8] {- let s = self.sec_bytes(2);- let base = r as usize * 32;- let mut out = [0u32; 8];- for (k, o) in out.iter_mut().enumerate() {- *o = ru32(s, base + k * 4);- }- out- }- fn route_stop(&self, i: u32) -> u32 {- ru32(self.sec_bytes(3), i as usize * 4)- }- // Decode a route's varint trips -> (start_time, profile_id, service_idx, headsign_off).- fn route_trips(&self, rec: &[u32; 8]) -> Vec<(u32, u32, u32, u32)> {- let n = rec[6];- let trips_off = rec[7] as usize;- let s = self.sec_bytes(4);- let mut pos = trips_off;- let mut prev = 0u32;- let mut out = Vec::new();- for _ in 0..n {- let start = prev + read_uvarint(s, &mut pos) as u32;- let pid = read_uvarint(s, &mut pos) as u32;- let svc = read_uvarint(s, &mut pos) as u32;- let head = read_uvarint(s, &mut pos) as u32;- out.push((start, pid, svc, head));- prev = start;- }- out- }- // Decode profile -> per-stop (arr_rel, dep_rel) relative to start_time.- fn profile(&self, pid: u32) -> Vec<(i64, i64)> {- let idx = self.sec_bytes(6);- let off = ru32(idx, pid as usize * 4) as usize;- let s = self.sec_bytes(5);- let mut pos = off;- let n = read_uvarint(s, &mut pos) as usize;- let dwell0 = read_uvarint(s, &mut pos) as i64;- let mut out = Vec::with_capacity(n);- out.push((-dwell0, 0i64));- let mut prev_dep = 0i64;- for _ in 1..n {- let hop = read_uvarint(s, &mut pos) as i64;- let dwell = read_uvarint(s, &mut pos) as i64;- let arr = prev_dep + hop;- let dep = arr + dwell;- out.push((arr, dep));- prev_dep = dep;- }- out- }- fn feed_name(&self, feed_idx: u32) -> String {- let s = self.sec_bytes(16);- self.read_str(ru32(s, feed_idx as usize * 4))- }- fn feed_tz(&self, feed_idx: u32) -> String {- let s = self.sec_bytes(17);- self.read_str(ru32(s, feed_idx as usize * 4))- }- fn feed_motis_prefix(&self, feed_idx: u32) -> String {- let s = self.sec_bytes(23);- self.read_str(ru32(s, feed_idx as usize * 4))- }- fn stop_gtfs_id(&self, stop_idx: u32) -> String {- let s = self.sec_bytes(24);- self.read_str(ru32(s, stop_idx as usize * 4))- }- /// (service_idx, date, added) exceptions for one service, via the CSR.- fn service_exceptions(&self, service_idx: u32) -> Vec<(u32, u32, u32)> {- let idx = self.sec_bytes(18);- let s = self.sec_bytes(12);- let lo = ru32(idx, service_idx as usize * 4) as usize;- let hi = ru32(idx, (service_idx as usize + 1) * 4) as usize;- (lo..hi)- .map(|i| (ru32(s, i * 12), ru32(s, i * 12 + 4), ru32(s, i * 12 + 8)))- .collect()- }- /// (route_idx, stop_pos) pairs serving `stop`, via STOP_ROUTES(+_POS).- fn stop_routes(&self, stop: u32) -> Vec<(u32, u32)> {- let idx = self.sec_bytes(8);- let routes = self.sec_bytes(7);- let pos = self.sec_bytes(19);- let lo = ru32(idx, stop as usize * 4) as usize;- let hi = ru32(idx, (stop as usize + 1) * 4) as usize;- (lo..hi).map(|i| (ru32(routes, i * 4), ru32(pos, i * 4))).collect()- }- /// Byte offset of route `r`'s shape blob, or `None` when it has none.- fn route_shape_off(&self, r: u32) -> Option<usize> {- let idx = self.sec_bytes(21);- let route_count = self.h(4) as usize;- assert_eq!(idx.len(), (route_count + 1) * 4, "ROUTE_SHAPE_IDX size");- let off = ru32(idx, r as usize * 4);- if off == NONE {- return None;- }- let bound = ru32(idx, route_count * 4) as usize;- assert_eq!(bound, self.sec_bytes(20).len(), "ROUTE_SHAPE_IDX bound");- assert!(off as usize + 4 <= bound, "shape offset {off} out of SHAPE_COORDS");- Some(off as usize)- }- /// Decode the whole polyline at byte offset `off` in SHAPE_COORDS.- fn shape_points(&self, off: usize) -> Vec<(i32, i32)> {- let s = self.sec_bytes(20);- let n = ru32(s, off) as usize;- let mut pos = off + 4;- let (mut lat, mut lon) = (0i64, 0i64);- (0..n)- .map(|_| {- lat += zigzag(read_uvarint(s, &mut pos));- lon += zigzag(read_uvarint(s, &mut pos));- (lat as i32, lon as i32)- })- .collect()- }- /// Vertex index for ROUTE_STOPS entry `i`.- fn route_stop_shape(&self, i: u32) -> u32 {- ru32(self.sec_bytes(22), i as usize * 4)- }- // Sparse-grid nearest stop to (lat, lon).- fn nearest(&self, lat: f64, lon: f64) -> Option<u32> {- let lat0 = self.h(15) as i32; // grid_lat0_e7 (index 15)- let lon0 = self.h(16) as i32;- let cell = self.h(17) as i64; // grid_cell_e7- let cols = self.h(18) as i64;- let lat_e7 = (lat * 1e7) as i64;- let lon_e7 = (lon * 1e7) as i64;- let row0 = ((lat_e7 - lat0 as i64) / cell).max(0);- let col0 = ((lon_e7 - lon0 as i64) / cell).max(0);- let ids = self.sec_bytes(13);- let offs = self.sec_bytes(14);- let stops = self.sec_bytes(15);- let n_cells = ids.len() / 4;- let find = |cid: u32| -> Option<usize> {- let (mut lo, mut hi) = (0usize, n_cells);- while lo < hi {- let mid = (lo + hi) / 2;- let v = ru32(ids, mid * 4);- if v == cid {- return Some(mid);- } else if v < cid {- lo = mid + 1;- } else {- hi = mid;- }- }- None- };- let mut best = None;- let mut bestd = f64::MAX;- for dr in -1..=1 {- for dc in -1..=1 {- let r = row0 + dr;- let c = col0 + dc;- if r < 0 || c < 0 || c >= cols {- continue;- }- let cid = (r * cols + c) as u32;- if let Some(ci) = find(cid) {- let s = ru32(offs, ci * 4) as usize;- let e = ru32(offs, (ci + 1) * 4) as usize;- for k in s..e {- let sid = ru32(stops, k * 4);- let (slat, slon) = self.stop_ll(sid);- let d = (slat - lat).powi(2) + (slon - lon).powi(2);- if d < bestd {- bestd = d;- best = Some(sid);- }- }- }- }- }- best- }- }+ use crate::reader::{+ Reader, SEC_FEED_MOTIS_PREFIX, SEC_SHAPE_COORDS, SEC_STOP_GTFS_ID,+ }; fn agency(tz: &str) -> Csv { parse_csv(&format!("agency_id,agency_name,agency_timezone\nA,Agency,{tz}\n")) } - fn zigzag(u: u64) -> i64 {- ((u >> 1) as i64) ^ -((u & 1) as i64)- }- /// `(shape_id, points as (lat, lon), optional shape_dist_traveled)`. type ShapeSpec<'a> = (&'a str, Vec<(f64, f64)>, Option<Vec<f64>>); @@ -1312,9 +1256,9 @@ mod tests { /// Every route's vertex indices must be non-decreasing and inside its blob, /// or the device's `shape[vertex(board)..=vertex(alight)]` slice is garbage. fn assert_shape_invariants(r: &Reader) {- for route in 0..r.h(4) {+ for route in 0..r.route_count() { let rec = r.route(route);- let (n_stops, first) = (rec[4], rec[5]);+ let (n_stops, first) = (rec.n_stops, rec.first_route_stop); let vertices: Vec<u32> = (0..n_stops).map(|p| r.route_stop_shape(first + p)).collect(); match r.route_shape_off(route) {@@ -1438,18 +1382,17 @@ mod tests { }, ]; let (blob, stats) = build_index("world", &feeds).expect("build");- let r = Reader::new(blob);-- // Header sanity.- assert_eq!(r.h(0), MAGIC);- assert_eq!(r.h(1), VERSION);- assert_eq!(r.h(2), SECTION_COUNT as u32);- assert_eq!(r.h(3), 5, "stop_count"); // 3 + 2- assert_eq!(r.h(4), 2, "route_count");- assert_eq!(r.h(5), 3, "trip_count"); // 2 + 1- assert_eq!(r.h(8), 2, "feed_count");+ let r = Reader::new(blob).expect("read back the pack");++ // Header sanity. `Reader::new` has already checked the magic.+ assert_eq!(r.version(), VERSION);+ assert_eq!(r.section_count(), SECTION_COUNT as u32);+ assert_eq!(r.stop_count(), 5, "stop_count"); // 3 + 2+ assert_eq!(r.route_count(), 2, "route_count");+ assert_eq!(r.trip_count(), 3, "trip_count"); // 2 + 1+ assert_eq!(r.feed_count(), 2, "feed_count"); // Feed A's two trips share one shape; feed B has its own -> 2 profiles.- assert_eq!(r.h(7), 2, "profile_count");+ assert_eq!(r.profile_count(), 2, "profile_count"); assert_eq!(stats.profiles, 2); // Feeds table namespacing.@@ -1467,28 +1410,28 @@ mod tests { assert_eq!(r.stop_gtfs_id(1), "S2"); assert_eq!(r.stop_gtfs_id(2), "S3"); assert_ne!(- r.sec_bytes(23).len(),+ r.sec_bytes(SEC_FEED_MOTIS_PREFIX).len(), 0, "FEED_MOTIS_PREFIX must be populated so the manifest proves v5 landed" );- assert_eq!(r.sec_bytes(24).len(), 5 * 4, "STOP_GTFS_ID is u32[stop_count]");+ assert_eq!(r.sec_bytes(SEC_STOP_GTFS_ID).len(), 5 * 4, "STOP_GTFS_ID is u32[stop_count]"); // Route 0 = feed A's N-Judah. Decode its trips + profile. let rec0 = r.route(0);- assert_eq!(rec0[3], 0, "route0 feed_idx");- assert_eq!(rec0[4], 3, "route0 n_stops");- assert_eq!(r.read_str(rec0[0]), "N");+ assert_eq!(rec0.feed_idx, 0, "route0 feed_idx");+ assert_eq!(rec0.n_stops, 3, "route0 n_stops");+ assert_eq!(r.read_str(rec0.name_off), "N"); let trips = r.route_trips(&rec0); assert_eq!(trips.len(), 2); // Sorted by start_time: 08:00 then 09:00.- assert_eq!(trips[0].0, 28800);- assert_eq!(trips[1].0, 32400);+ assert_eq!(trips[0].start_time, 28800);+ assert_eq!(trips[1].start_time, 32400); // Both reference the same profile id (dedup).- assert_eq!(trips[0].1, trips[1].1);+ assert_eq!(trips[0].profile_id, trips[1].profile_id); // Reconstruct absolute arr/dep for trip T2 (start 09:00) and compare.- let prof = r.profile(trips[1].1);- let start = trips[1].0 as i64;+ let prof = r.profile(trips[1].profile_id);+ let start = trips[1].start_time as i64; let abs: Vec<(i64, i64)> = prof.iter().map(|&(a, d)| (start + a, start + d)).collect(); assert_eq!(abs[0], (32400, 32400)); // S1 09:00/09:00@@ -1497,12 +1440,12 @@ mod tests { // Route 1 belongs to feed B. let rec1 = r.route(1);- assert_eq!(rec1[3], 1, "route1 feed_idx");- assert_eq!(rec1[4], 2, "route1 n_stops");+ assert_eq!(rec1.feed_idx, 1, "route1 feed_idx");+ assert_eq!(rec1.n_stops, 2, "route1 n_stops"); // Route stops of route 0 point at the first three (feed A) stops.- for pos in 0..rec0[4] {- let s = r.route_stop(rec0[5] + pos);+ for pos in 0..rec0.n_stops {+ let s = r.route_stop(rec0.first_route_stop + pos); assert!(s < 3, "route0 stop {s} should be a feed-A stop"); } @@ -1512,30 +1455,34 @@ mod tests { // v3: STOP_ROUTE_POS is parallel to STOP_ROUTES and gives the stop's // position in the pattern, so pos matches a ROUTE_STOPS lookup.- for stop in 0..r.h(3) {+ for stop in 0..r.stop_count() { for (route, pos) in r.stop_routes(stop) { let rec = r.route(route);- assert!(pos < rec[4], "stop {stop} pos {pos} out of route {route}");- assert_eq!(r.route_stop(rec[5] + pos), stop, "stop {stop} route {route}");+ assert!(pos < rec.n_stops, "stop {stop} pos {pos} out of route {route}");+ assert_eq!(+ r.route_stop(rec.first_route_stop + pos),+ stop,+ "stop {stop} route {route}"+ ); } } // v3: exceptions are date-sorted per service and collapsed to one row // per (service, date), with the last CSV row winning.- let svc = r.route_trips(&rec0)[0].2;+ let svc = r.route_trips(&rec0)[0].service_idx; assert_eq!( r.service_exceptions(svc), vec![(svc, 20240101, 0), (svc, 20240704, 0)], "sorted, deduped, last-row-wins" ); // Feed B contributes no exceptions, so its services have empty ranges.- let svc_b = r.route_trips(&rec1)[0].2;+ let svc_b = r.route_trips(&rec1)[0].service_idx; assert!(r.service_exceptions(svc_b).is_empty()); // v4: no shapes.txt anywhere, so every route falls back to stop-to-stop. assert!(r.route_shape_off(0).is_none()); assert!(r.route_shape_off(1).is_none());- assert_eq!(r.sec_bytes(20).len(), 0, "SHAPE_COORDS is empty");+ assert_eq!(r.sec_bytes(SEC_SHAPE_COORDS).len(), 0, "SHAPE_COORDS is empty"); assert_shape_invariants(&r); } @@ -1630,7 +1577,7 @@ mod tests { let sh = detour_shape(false); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.shaped_routes, 1); assert_eq!(stats.dropped_shape_routes, 0); assert_eq!(stats.multi_shape_routes, 0);@@ -1639,14 +1586,15 @@ mod tests { let off = r.route_shape_off(0).expect("route 0 is shaped"); let pts = r.shape_points(off); let rec = r.route(0);- let vertices: Vec<u32> = (0..rec[4]).map(|p| r.route_stop_shape(rec[5] + p)).collect();+ let vertices: Vec<u32> =+ (0..rec.n_stops).map(|p| r.route_stop_shape(rec.first_route_stop + p)).collect(); // Trimmed to the boarded extent: first stop is vertex 0, last is the end. assert_eq!(vertices[0], 0); assert_eq!(*vertices.last().unwrap() as usize, pts.len() - 1); // Each stop's vertex is the stop's own projection, so a boarded span // starts exactly on the stop. for (p, &v) in vertices.iter().enumerate() {- let stop = r.route_stop(rec[5] + p as u32);+ let stop = r.route_stop(rec.first_route_stop + p as u32); let (slat, slon) = r.stop_ll(stop); let (vlat, vlon) = pts[v as usize]; assert!(@@ -1669,11 +1617,12 @@ mod tests { let sh = detour_shape(true); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.shaped_routes, 1); assert_shape_invariants(&r); let rec = r.route(0);- let vertices: Vec<u32> = (0..rec[4]).map(|p| r.route_stop_shape(rec[5] + p)).collect();+ let vertices: Vec<u32> =+ (0..rec.n_stops).map(|p| r.route_stop_shape(rec.first_route_stop + p)).collect(); assert!(vertices.windows(2).all(|w| w[1] > w[0]), "distinct stops, distinct vertices"); } @@ -1689,11 +1638,11 @@ mod tests { )]); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.shaped_routes, 0); assert_eq!(stats.dropped_shape_routes, 1); assert!(r.route_shape_off(0).is_none(), "a bad shape must not be stored");- assert_eq!(r.sec_bytes(20).len(), 0);+ assert_eq!(r.sec_bytes(SEC_SHAPE_COORDS).len(), 0); assert_shape_invariants(&r); } @@ -1705,7 +1654,7 @@ mod tests { let sh = shape_map(vec![("OTHER", vec![(37.700, -122.400), (37.710, -122.400)], None)]); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.shaped_routes, 0); assert_eq!(stats.dropped_shape_routes, 0, "an absent shape is not a validation drop"); assert!(r.route_shape_off(0).is_none());@@ -1756,7 +1705,7 @@ mod tests { ]); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.multi_shape_routes, 1, "the disagreement is reported"); assert_eq!(stats.shaped_routes, 1); let pts = r.shape_points(r.route_shape_off(0).expect("shaped"));@@ -1806,14 +1755,14 @@ mod tests { )]); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);- assert_eq!(r.h(4), 2, "route_count");+ let r = Reader::new(blob).expect("read back the pack");+ assert_eq!(r.route_count(), 2, "route_count"); assert_eq!(stats.shaped_routes, 2); let a = r.route_shape_off(0).expect("route 0 shaped"); let b = r.route_shape_off(1).expect("route 1 shaped"); assert_eq!(a, b, "identical geometry must be stored once"); assert_eq!(- r.sec_bytes(20).len(),+ r.sec_bytes(SEC_SHAPE_COORDS).len(), shapes::encode(&r.shape_points(a)).len(), "SHAPE_COORDS holds exactly one blob" );@@ -1855,7 +1804,7 @@ mod tests { )]); let (blob, stats) = build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build");- let r = Reader::new(blob);+ let r = Reader::new(blob).expect("read back the pack"); assert_eq!(stats.shaped_routes, 1); let pts = r.shape_points(r.route_shape_off(0).expect("shaped")); assert_eq!(pts.first(), Some(&(377_000_000, -1_224_000_000)));
scripts/maps/gtfs_ingest/src/lib.rs
diff --git a/scripts/maps/gtfs_ingest/src/lib.rs b/scripts/maps/gtfs_ingest/src/lib.rsindex 15bd351c1e..71e6ee9baa 100644--- a/scripts/maps/gtfs_ingest/src/lib.rs+++ b/scripts/maps/gtfs_ingest/src/lib.rs@@ -1,10 +1,11 @@ //! Shared GTFS ingest internals for the Maps offline packs. //! //! Exists so the crate can carry more than one host binary over the same GTFS-//! parser: `gtfs_ingest` builds the on-device `.transit` index, `transit_stops`-//! emits the basemap stop layer, and `resolve_feeds` turns the Transitous and+//! `gtfs_ingest` builds the on-device `.transit` index, `transit_stops`+//! emits the basemap stop layer, `resolve_feeds` turns the Transitous and //! transitland-atlas registries into a download plan whose feeds can carry MOTIS-//! stop ids. Mirrors `osm_ingest`'s lib + `src/bin/*` shape.+//! stop ids, and `transit_dump` renders a built pack as diffable text. Mirrors+//! `osm_ingest`'s lib + `src/bin/*` shape. //! //! **Zero dependencies, deliberately.** That includes the JSON reader in [`json`]: //! pulling in serde for two registry formats would trade away the property that@@ -13,5 +14,6 @@ pub mod gtfs; pub mod index; pub mod json; pub mod manifest;+pub mod reader; pub mod registry; pub mod shapes;
scripts/maps/gtfs_ingest/src/main.rs
diff --git a/scripts/maps/gtfs_ingest/src/main.rs b/scripts/maps/gtfs_ingest/src/main.rsindex e1aebd2f78..507de506ae 100644--- a/scripts/maps/gtfs_ingest/src/main.rs+++ b/scripts/maps/gtfs_ingest/src/main.rs@@ -34,6 +34,7 @@ use gtfs_ingest::gtfs; use gtfs_ingest::index; use gtfs_ingest::index::FeedInput; use gtfs_ingest::manifest::{read_manifest, parse_feed_spec, FeedSpec};+use std::io::Write; use std::path::{Path, PathBuf}; use std::process::ExitCode; @@ -166,13 +167,20 @@ fn run(out_dir: &Path, pack_name: &str, specs: &[FeedSpec]) -> Result<(), String }) .collect(); - let (blob, stats) = index::build_index(pack_name, &feeds)?;- std::fs::create_dir_all(out_dir) .map_err(|e| format!("cannot create out dir {}: {e}", out_dir.display()))?; let index_path = out_dir.join(format!("{pack_name}.transit"));- std::fs::write(&index_path, &blob)- .map_err(|e| format!("cannot write {}: {e}", index_path.display()))?;+ // Straight to disk through a BufWriter: the pack is 1.5-4 GB for a world+ // build, and holding a second copy of it in a `Vec` to then `fs::write` was+ // pure overhead.+ let file = std::fs::File::create(&index_path)+ .map_err(|e| format!("cannot create {}: {e}", index_path.display()))?;+ let mut writer = std::io::BufWriter::new(file);+ let stats = index::build_index_to(pack_name, &feeds, &mut writer)?;+ writer+ .flush()+ .map_err(|e| format!("cannot finish writing {}: {e}", index_path.display()))?;+ drop(writer); // Manifest (hand-written JSON, no serde dep). Includes a per-section size // breakdown so the compression win (profiles vs the old stoptimes) is
scripts/maps/gtfs_ingest/src/reader.rs
diff --git a/scripts/maps/gtfs_ingest/src/reader.rs b/scripts/maps/gtfs_ingest/src/reader.rsnew file mode 100644index 0000000000..7e09be85f1--- /dev/null+++ b/scripts/maps/gtfs_ingest/src/reader.rs@@ -0,0 +1,484 @@+//! A host-side reader for the `.transit` (TRX2) packs [`crate::index`] writes.+//!+//! This is a **deliberate reimplementation** of the on-device reader+//! `maps/src/main/rust/src/transit.rs`, not shared code. Two independent+//! decoders of the same bytes must agree; keeping them separate is what makes a+//! layout mistake in either one show up as a failing test or a dump diff instead+//! of a pack the device silently mis-reads. Anything learned here must be+//! mirrored there by hand — that is the point.+//!+//! Used by `index`'s tests and by `src/bin/transit_dump.rs`.++use crate::index::{HEADER_LEN, MAGIC, NONE, SECTION_COUNT, VERSION, VERSION_MIN};++// Section slot numbers. Slot `i` must describe section `i`; the device reader+// hardcodes the same numbers.+pub const SEC_STRINGS: usize = 0;+pub const SEC_STOPS: usize = 1;+pub const SEC_ROUTES: usize = 2;+pub const SEC_ROUTE_STOPS: usize = 3;+pub const SEC_ROUTE_TRIPS: usize = 4;+pub const SEC_PROFILES: usize = 5;+pub const SEC_PROFILES_IDX: usize = 6;+pub const SEC_STOP_ROUTES: usize = 7;+pub const SEC_STOP_ROUTES_IDX: usize = 8;+pub const SEC_TRANSFERS: usize = 9;+pub const SEC_TRANSFERS_IDX: usize = 10;+pub const SEC_SERVICES: usize = 11;+pub const SEC_EXCEPTIONS: usize = 12;+pub const SEC_GRID_CELL_IDS: usize = 13;+pub const SEC_GRID_CELL_OFF: usize = 14;+pub const SEC_GRID_STOPS: usize = 15;+pub const SEC_FEEDS: usize = 16;+pub const SEC_FEED_TZ: usize = 17;+pub const SEC_EXCEPTIONS_IDX: usize = 18;+pub const SEC_STOP_ROUTE_POS: usize = 19;+pub const SEC_SHAPE_COORDS: usize = 20;+pub const SEC_ROUTE_SHAPE_IDX: usize = 21;+pub const SEC_ROUTE_STOP_SHAPE: usize = 22;+pub const SEC_FEED_MOTIS_PREFIX: usize = 23;+pub const SEC_STOP_GTFS_ID: usize = 24;++fn ru32(b: &[u8], off: usize) -> u32 {+ u32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])+}+fn ri32(b: &[u8], off: usize) -> i32 {+ i32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])+}+fn ru64(b: &[u8], off: usize) -> u64 {+ let mut a = [0u8; 8];+ a.copy_from_slice(&b[off..off + 8]);+ u64::from_le_bytes(a)+}++/// Read one unsigned LEB128 varint, advancing `pos`.+pub fn read_uvarint(b: &[u8], pos: &mut usize) -> u64 {+ let mut result = 0u64;+ let mut shift = 0;+ loop {+ let byte = b[*pos];+ *pos += 1;+ result |= ((byte & 0x7f) as u64) << shift;+ if byte & 0x80 == 0 {+ break;+ }+ shift += 7;+ }+ result+}++fn zigzag(u: u64) -> i64 {+ ((u >> 1) as i64) ^ -((u & 1) as i64)+}++/// One ROUTES entry (a 32-byte stride).+#[derive(Clone, Copy, Debug)]+pub struct RouteRec {+ pub name_off: u32,+ pub color: u32,+ pub route_type: u32,+ pub feed_idx: u32,+ pub n_stops: u32,+ pub first_route_stop: u32,+ pub n_trips: u32,+ /// Byte offset into ROUTE_TRIPS.+ pub trips_off: u32,+}++/// One trip decoded out of a route's ROUTE_TRIPS varint block.+#[derive(Clone, Copy, Debug)]+pub struct TripRec {+ pub start_time: u32,+ pub profile_id: u32,+ pub service_idx: u32,+ pub headsign_off: u32,+}++/// One SERVICES entry (a 12-byte stride).+#[derive(Clone, Copy, Debug)]+pub struct ServiceRec {+ /// Bit `b` set = runs on day `b`, Monday = 0.+ pub weekday_mask: u8,+ pub start_date: u32,+ pub end_date: u32,+}++/// The header's grid parameters.+#[derive(Clone, Copy, Debug)]+pub struct GridParams {+ pub lat0_e7: i32,+ pub lon0_e7: i32,+ pub cell_e7: u32,+ pub cols: u32,+ pub rows: u32,+}++pub struct Reader {+ buf: Vec<u8>,+ sec: Vec<(usize, usize)>,+}++impl Reader {+ /// Validate the header and index the section directory.+ pub fn new(buf: Vec<u8>) -> Result<Reader, String> {+ if buf.len() < HEADER_LEN {+ return Err(format!("pack is {} bytes, shorter than the header", buf.len()));+ }+ let magic = ru32(&buf, 0);+ if magic != MAGIC {+ return Err(format!("bad magic {magic:#x}"));+ }+ let version = ru32(&buf, 4);+ if !(VERSION_MIN..=VERSION).contains(&version) {+ return Err(format!("version {version} outside {VERSION_MIN}..={VERSION}"));+ }+ let count = ru32(&buf, 8) as usize;+ if count != SECTION_COUNT {+ return Err(format!("section_count {count}, expected {SECTION_COUNT}"));+ }+ if buf.len() < HEADER_LEN + count * 16 {+ return Err("pack is truncated inside the section directory".to_string());+ }+ let mut sec = Vec::with_capacity(count);+ for i in 0..count {+ let base = HEADER_LEN + i * 16;+ let off = ru64(&buf, base) as usize;+ let len = ru64(&buf, base + 8) as usize;+ match off.checked_add(len) {+ Some(end) if end <= buf.len() => {}+ _ => return Err(format!("section {i} at {off}+{len} runs past the file")),+ }+ sec.push((off, len));+ }+ Ok(Reader { buf, sec })+ }++ /// Read the pack at `path`.+ pub fn open(path: &std::path::Path) -> Result<Reader, String> {+ let buf = std::fs::read(path)+ .map_err(|e| format!("cannot read {}: {e}", path.display()))?;+ Reader::new(buf)+ }++ /// The `idx`-th u32 of the header.+ fn hdr(&self, idx: usize) -> u32 {+ ru32(&self.buf, idx * 4)+ }++ pub fn version(&self) -> u32 {+ self.hdr(1)+ }+ pub fn section_count(&self) -> u32 {+ self.hdr(2)+ }+ pub fn stop_count(&self) -> u32 {+ self.hdr(3)+ }+ pub fn route_count(&self) -> u32 {+ self.hdr(4)+ }+ pub fn trip_count(&self) -> u32 {+ self.hdr(5)+ }+ pub fn service_count(&self) -> u32 {+ self.hdr(6)+ }+ pub fn profile_count(&self) -> u32 {+ self.hdr(7)+ }+ pub fn feed_count(&self) -> u32 {+ self.hdr(8)+ }+ pub fn grid_cell_count(&self) -> u32 {+ self.hdr(9)+ }+ /// The pack name the device falls back to.+ pub fn pack_name(&self) -> String {+ self.read_str(self.hdr(10))+ }+ /// `(min_lat_e7, min_lon_e7, max_lat_e7, max_lon_e7)`.+ pub fn bbox(&self) -> (i32, i32, i32, i32) {+ (+ self.hdr(11) as i32,+ self.hdr(12) as i32,+ self.hdr(13) as i32,+ self.hdr(14) as i32,+ )+ }+ pub fn grid(&self) -> GridParams {+ GridParams {+ lat0_e7: self.hdr(15) as i32,+ lon0_e7: self.hdr(16) as i32,+ cell_e7: self.hdr(17),+ cols: self.hdr(18),+ rows: self.hdr(19),+ }+ }++ pub fn sec_bytes(&self, s: usize) -> &[u8] {+ let (o, l) = self.sec[s];+ &self.buf[o..o + l]+ }++ /// The NUL-terminated string at STRINGS offset `off`; "" for [`NONE`].+ pub fn read_str(&self, off: u32) -> String {+ if off == NONE {+ return String::new();+ }+ let s = self.sec_bytes(SEC_STRINGS);+ let start = off as usize;+ let mut n = 0;+ while start + n < s.len() && s[start + n] != 0 {+ n += 1;+ }+ String::from_utf8_lossy(&s[start..start + n]).into_owned()+ }++ /// `(lat, lon)` in degrees.+ pub fn stop_ll(&self, i: u32) -> (f64, f64) {+ let (lat, lon) = self.stop_ll_e7(i);+ (lat as f64 * 1e-7, lon as f64 * 1e-7)+ }++ pub fn stop_ll_e7(&self, i: u32) -> (i32, i32) {+ let s = self.sec_bytes(SEC_STOPS);+ let base = i as usize * 16;+ (ri32(s, base), ri32(s, base + 4))+ }++ pub fn stop_name(&self, i: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_STOPS), i as usize * 16 + 8))+ }++ pub fn stop_code(&self, i: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_STOPS), i as usize * 16 + 12))+ }++ /// The raw GTFS `stop_id` (v5 STOP_GTFS_ID).+ pub fn stop_gtfs_id(&self, i: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_STOP_GTFS_ID), i as usize * 4))+ }++ pub fn route(&self, r: u32) -> RouteRec {+ let s = self.sec_bytes(SEC_ROUTES);+ let base = r as usize * 32;+ RouteRec {+ name_off: ru32(s, base),+ color: ru32(s, base + 4),+ route_type: ru32(s, base + 8),+ feed_idx: ru32(s, base + 12),+ n_stops: ru32(s, base + 16),+ first_route_stop: ru32(s, base + 20),+ n_trips: ru32(s, base + 24),+ trips_off: ru32(s, base + 28),+ }+ }++ /// ROUTE_STOPS entry `i` (an absolute stop index).+ pub fn route_stop(&self, i: u32) -> u32 {+ ru32(self.sec_bytes(SEC_ROUTE_STOPS), i as usize * 4)+ }++ /// A route's stop pattern.+ pub fn route_stops(&self, rec: &RouteRec) -> Vec<u32> {+ (0..rec.n_stops).map(|p| self.route_stop(rec.first_route_stop + p)).collect()+ }++ /// Decode a route's varint trip block, in start-time order.+ pub fn route_trips(&self, rec: &RouteRec) -> Vec<TripRec> {+ let s = self.sec_bytes(SEC_ROUTE_TRIPS);+ let mut pos = rec.trips_off as usize;+ let mut prev = 0u32;+ let mut out = Vec::with_capacity(rec.n_trips as usize);+ for _ in 0..rec.n_trips {+ let start_time = prev + read_uvarint(s, &mut pos) as u32;+ out.push(TripRec {+ start_time,+ profile_id: read_uvarint(s, &mut pos) as u32,+ service_idx: read_uvarint(s, &mut pos) as u32,+ headsign_off: read_uvarint(s, &mut pos) as u32,+ });+ prev = start_time;+ }+ out+ }++ /// Profile `pid` as per-stop `(arr, dep)` offsets relative to a trip's+ /// `start_time`. `arr[0]` is negative by the first dwell.+ pub fn profile(&self, pid: u32) -> Vec<(i64, i64)> {+ let idx = self.sec_bytes(SEC_PROFILES_IDX);+ let off = ru32(idx, pid as usize * 4) as usize;+ let s = self.sec_bytes(SEC_PROFILES);+ let mut pos = off;+ let n = read_uvarint(s, &mut pos) as usize;+ let dwell0 = read_uvarint(s, &mut pos) as i64;+ let mut out = Vec::with_capacity(n);+ out.push((-dwell0, 0i64));+ let mut prev_dep = 0i64;+ for _ in 1..n {+ let hop = read_uvarint(s, &mut pos) as i64;+ let dwell = read_uvarint(s, &mut pos) as i64;+ let arr = prev_dep + hop;+ let dep = arr + dwell;+ out.push((arr, dep));+ prev_dep = dep;+ }+ out+ }++ pub fn feed_name(&self, feed_idx: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_FEEDS), feed_idx as usize * 4))+ }++ pub fn feed_tz(&self, feed_idx: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_FEED_TZ), feed_idx as usize * 4))+ }++ pub fn feed_motis_prefix(&self, feed_idx: u32) -> String {+ self.read_str(ru32(self.sec_bytes(SEC_FEED_MOTIS_PREFIX), feed_idx as usize * 4))+ }++ pub fn service(&self, service_idx: u32) -> ServiceRec {+ let s = self.sec_bytes(SEC_SERVICES);+ let base = service_idx as usize * 12;+ ServiceRec {+ weekday_mask: s[base],+ start_date: ru32(s, base + 4),+ end_date: ru32(s, base + 8),+ }+ }++ /// `(service_idx, date, added)` exceptions for one service, via the CSR.+ pub fn service_exceptions(&self, service_idx: u32) -> Vec<(u32, u32, u32)> {+ let idx = self.sec_bytes(SEC_EXCEPTIONS_IDX);+ let s = self.sec_bytes(SEC_EXCEPTIONS);+ let lo = ru32(idx, service_idx as usize * 4) as usize;+ let hi = ru32(idx, (service_idx as usize + 1) * 4) as usize;+ (lo..hi)+ .map(|i| (ru32(s, i * 12), ru32(s, i * 12 + 4), ru32(s, i * 12 + 8)))+ .collect()+ }++ /// `(route_idx, stop_pos)` pairs serving `stop`, via STOP_ROUTES(+_POS).+ pub fn stop_routes(&self, stop: u32) -> Vec<(u32, u32)> {+ let idx = self.sec_bytes(SEC_STOP_ROUTES_IDX);+ let routes = self.sec_bytes(SEC_STOP_ROUTES);+ let pos = self.sec_bytes(SEC_STOP_ROUTE_POS);+ let lo = ru32(idx, stop as usize * 4) as usize;+ let hi = ru32(idx, (stop as usize + 1) * 4) as usize;+ (lo..hi).map(|i| (ru32(routes, i * 4), ru32(pos, i * 4))).collect()+ }++ /// `(to_stop, secs)` footpaths from `stop`.+ pub fn transfers(&self, stop: u32) -> Vec<(u32, u32)> {+ let idx = self.sec_bytes(SEC_TRANSFERS_IDX);+ let s = self.sec_bytes(SEC_TRANSFERS);+ let lo = ru32(idx, stop as usize * 4) as usize;+ let hi = ru32(idx, (stop as usize + 1) * 4) as usize;+ (lo..hi).map(|i| (ru32(s, i * 8), ru32(s, i * 8 + 4))).collect()+ }++ /// Every non-empty grid cell as `(cell_id, stops)`, in file order (which the+ /// reader requires to be `cell_id`-ascending).+ pub fn grid_cells(&self) -> Vec<(u32, Vec<u32>)> {+ let ids = self.sec_bytes(SEC_GRID_CELL_IDS);+ let offs = self.sec_bytes(SEC_GRID_CELL_OFF);+ let stops = self.sec_bytes(SEC_GRID_STOPS);+ (0..self.grid_cell_count() as usize)+ .map(|c| {+ let lo = ru32(offs, c * 4) as usize;+ let hi = ru32(offs, (c + 1) * 4) as usize;+ (ru32(ids, c * 4), (lo..hi).map(|k| ru32(stops, k * 4)).collect())+ })+ .collect()+ }++ /// Byte offset of route `r`'s shape blob in SHAPE_COORDS, or `None`.+ pub fn route_shape_off(&self, r: u32) -> Option<usize> {+ let idx = self.sec_bytes(SEC_ROUTE_SHAPE_IDX);+ let route_count = self.route_count() as usize;+ assert_eq!(idx.len(), (route_count + 1) * 4, "ROUTE_SHAPE_IDX size");+ let off = ru32(idx, r as usize * 4);+ if off == NONE {+ return None;+ }+ let bound = ru32(idx, route_count * 4) as usize;+ assert_eq!(bound, self.sec_bytes(SEC_SHAPE_COORDS).len(), "ROUTE_SHAPE_IDX bound");+ assert!(off as usize + 4 <= bound, "shape offset {off} out of SHAPE_COORDS");+ Some(off as usize)+ }++ /// Decode the whole polyline at byte offset `off` in SHAPE_COORDS.+ pub fn shape_points(&self, off: usize) -> Vec<(i32, i32)> {+ let s = self.sec_bytes(SEC_SHAPE_COORDS);+ let n = ru32(s, off) as usize;+ let mut pos = off + 4;+ let (mut lat, mut lon) = (0i64, 0i64);+ (0..n)+ .map(|_| {+ lat += zigzag(read_uvarint(s, &mut pos));+ lon += zigzag(read_uvarint(s, &mut pos));+ (lat as i32, lon as i32)+ })+ .collect()+ }++ /// Vertex index for ROUTE_STOPS entry `i` ([`NONE`] when unshaped).+ pub fn route_stop_shape(&self, i: u32) -> u32 {+ ru32(self.sec_bytes(SEC_ROUTE_STOP_SHAPE), i as usize * 4)+ }++ /// Nearest stop to `(lat, lon)` via the sparse spatial grid, searching ±1+ /// cell exactly as the device does.+ pub fn nearest(&self, lat: f64, lon: f64) -> Option<u32> {+ let g = self.grid();+ let lat_e7 = (lat * 1e7) as i64;+ let lon_e7 = (lon * 1e7) as i64;+ let row0 = ((lat_e7 - g.lat0_e7 as i64) / g.cell_e7 as i64).max(0);+ let col0 = ((lon_e7 - g.lon0_e7 as i64) / g.cell_e7 as i64).max(0);+ let ids = self.sec_bytes(SEC_GRID_CELL_IDS);+ let offs = self.sec_bytes(SEC_GRID_CELL_OFF);+ let stops = self.sec_bytes(SEC_GRID_STOPS);+ let n_cells = ids.len() / 4;+ let find = |cid: u32| -> Option<usize> {+ let (mut lo, mut hi) = (0usize, n_cells);+ while lo < hi {+ let mid = (lo + hi) / 2;+ let v = ru32(ids, mid * 4);+ if v == cid {+ return Some(mid);+ } else if v < cid {+ lo = mid + 1;+ } else {+ hi = mid;+ }+ }+ None+ };+ let mut best = None;+ let mut bestd = f64::MAX;+ for dr in -1..=1 {+ for dc in -1..=1 {+ let r = row0 + dr;+ let c = col0 + dc;+ if r < 0 || c < 0 || c >= g.cols as i64 {+ continue;+ }+ if let Some(ci) = find((r * g.cols as i64 + c) as u32) {+ let s = ru32(offs, ci * 4) as usize;+ let e = ru32(offs, (ci + 1) * 4) as usize;+ for k in s..e {+ let sid = ru32(stops, k * 4);+ let (slat, slon) = self.stop_ll(sid);+ let d = (slat - lat).powi(2) + (slon - lon).powi(2);+ if d < bestd {+ bestd = d;+ best = Some(sid);+ }+ }+ }+ }+ }+ best+ }+}