Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / index_part3_s1.rs
index_part3_s1.rs169 lines · 7.2 KiBRaw
| 1 | /// Footpath transfers, services/exceptions and the spatial grid for |
| 2 | /// [`IndexBuilder::finish_to`]: pure section builders extracted verbatim |
| 3 | /// from that method (same statement order, same debug assertions). |
| 4 | fn build_transfers(stop_lat: &[i32], stop_lon: &[i32]) -> (Vec<u8>, Vec<u8>, usize) { |
| 5 | let stop_count = stop_lat.len(); |
| 6 | // --- Footpath transfers via a coarse spatial grid (cross-feed for free) --- |
| 7 | // A flat `(cell, stop)` vector sorted once, not a `HashMap` of buckets: on a |
| 8 | // world pack the map is millions of cells each holding a tiny `Vec`, whose |
| 9 | // headers and allocations cost far more than the stop ids in them. Sorting |
| 10 | // by `(cell, stop)` keeps each cell's stops ascending, which is the order the |
| 11 | // old bucket pushes produced and which the distance sort below tie-breaks on. |
| 12 | let xcell = |lat_e7: i32, lon_e7: i32| -> (i32, i32) { |
| 13 | ( |
| 14 | (lat_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, |
| 15 | (lon_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, |
| 16 | ) |
| 17 | }; |
| 18 | let mut xgrid: Vec<((i32, i32), u32)> = (0..stop_count) |
| 19 | .map(|i| (xcell(stop_lat[i], stop_lon[i]), i as u32)) |
| 20 | .collect(); |
| 21 | xgrid.sort_unstable(); |
| 22 | let xbucket = |key: (i32, i32)| -> &[((i32, i32), u32)] { |
| 23 | let lo = xgrid.partition_point(|&(k, _)| k < key); |
| 24 | let hi = xgrid.partition_point(|&(k, _)| k <= key); |
| 25 | &xgrid[lo..hi] |
| 26 | }; |
| 27 | let mut transfer_total = 0usize; |
| 28 | let mut sec_transfers = Vec::new(); |
| 29 | let mut sec_transfers_idx = Vec::new(); |
| 30 | let mut tacc: u32 = 0; |
| 31 | for i in 0..stop_count { |
| 32 | append_u32(&mut sec_transfers_idx, tacc); |
| 33 | let (cx, cy) = xcell(stop_lat[i], stop_lon[i]); |
| 34 | let mut cand: Vec<(u32, f64)> = Vec::new(); |
| 35 | for dx in -1..=1 { |
| 36 | for dy in -1..=1 { |
| 37 | for &(_, j) in xbucket((cx + dx, cy + dy)) { |
| 38 | if j as usize == i { |
| 39 | continue; |
| 40 | } |
| 41 | let d = |
| 42 | dist_m(stop_lat[i], stop_lon[i], stop_lat[j as usize], stop_lon[j as usize]); |
| 43 | if d <= MAX_TRANSFER_M { |
| 44 | cand.push((j, d)); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | // Stable, so equidistant candidates keep the dx/dy/stop order above and |
| 50 | // `truncate` drops a deterministic tail. |
| 51 | cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)); |
| 52 | cand.truncate(MAX_TRANSFERS_PER_STOP); |
| 53 | for (j, d) in cand { |
| 54 | let secs = (d / WALK_SPEED_M_S).ceil() as u32; |
| 55 | append_u32(&mut sec_transfers, j); |
| 56 | append_u32(&mut sec_transfers, secs); |
| 57 | tacc += 1; |
| 58 | transfer_total += 1; |
| 59 | } |
| 60 | } |
| 61 | append_u32(&mut sec_transfers_idx, tacc); |
| 62 | (sec_transfers, sec_transfers_idx, transfer_total) |
| 63 | } |
| 64 | |
| 65 | fn build_services_and_exceptions( |
| 66 | svc_mask: &[u8], |
| 67 | svc_start: &[u32], |
| 68 | svc_end: &[u32], |
| 69 | exceptions: Vec<(u32, u32, u32)>, |
| 70 | ) -> (Vec<u8>, Vec<u8>, Vec<u8>) { |
| 71 | // --- Services + exceptions --- |
| 72 | let mut sec_services = Vec::new(); |
| 73 | for i in 0..svc_mask.len() { |
| 74 | sec_services.push(svc_mask[i]); |
| 75 | sec_services.extend_from_slice(&[0u8, 0, 0]); |
| 76 | append_u32(&mut sec_services, svc_start[i]); |
| 77 | append_u32(&mut sec_services, svc_end[i]); |
| 78 | } |
| 79 | // --- Exceptions: sorted by (service_idx, date), one row per pair, with a |
| 80 | // CSR index so the device does a range lookup + binary search instead of a |
| 81 | // full scan per trip. `add_feed` already stable-sorted each feed's slice |
| 82 | // and a feed's services are a contiguous index range, so the whole array |
| 83 | // is sorted; for duplicate (service, date) rows the last in CSV order |
| 84 | // wins — matching the old reader, which scanned every exception without |
| 85 | // breaking out. --- |
| 86 | debug_assert!( |
| 87 | exceptions.windows(2).all(|w| (w[0].0, w[0].1) <= (w[1].0, w[1].1)), |
| 88 | "per-feed exception slices did not concatenate into a sorted array" |
| 89 | ); |
| 90 | let mut exc_unique: Vec<(u32, u32, u32)> = Vec::with_capacity(exceptions.len()); |
| 91 | for e in exceptions { |
| 92 | match exc_unique.last_mut() { |
| 93 | Some(last) if last.0 == e.0 && last.1 == e.1 => *last = e, |
| 94 | _ => exc_unique.push(e), |
| 95 | } |
| 96 | } |
| 97 | let mut sec_exceptions = Vec::new(); |
| 98 | let mut sec_exceptions_idx = Vec::new(); |
| 99 | let mut eacc: u32 = 0; |
| 100 | let mut ei = 0usize; |
| 101 | for s in 0..svc_mask.len() as u32 { |
| 102 | append_u32(&mut sec_exceptions_idx, eacc); |
| 103 | while let Some(&(sidx, date, added)) = exc_unique.get(ei) { |
| 104 | if sidx != s { |
| 105 | break; |
| 106 | } |
| 107 | append_u32(&mut sec_exceptions, sidx); |
| 108 | append_u32(&mut sec_exceptions, date); |
| 109 | append_u32(&mut sec_exceptions, added); |
| 110 | eacc += 1; |
| 111 | ei += 1; |
| 112 | } |
| 113 | } |
| 114 | append_u32(&mut sec_exceptions_idx, eacc); |
| 115 | (sec_services, sec_exceptions, sec_exceptions_idx) |
| 116 | } |
| 117 | |
| 118 | fn build_spatial_grid( |
| 119 | stop_lat: &[i32], |
| 120 | stop_lon: &[i32], |
| 121 | grid_lat0: i32, |
| 122 | grid_lon0: i32, |
| 123 | max_lat: i32, |
| 124 | max_lon: i32, |
| 125 | ) -> (Vec<u8>, Vec<u8>, Vec<u8>, u32, u32, u32, usize) { |
| 126 | let stop_count = stop_lat.len(); |
| 127 | let grid_cell_e7 = (GRID_CELL_DEG * 1e7) as u32; |
| 128 | let cell_col = |lon_e7: i32| -> i64 { |
| 129 | ((lon_e7 as i64 - grid_lon0 as i64) / grid_cell_e7 as i64).max(0) |
| 130 | }; |
| 131 | let cell_row = |lat_e7: i32| -> i64 { |
| 132 | ((lat_e7 as i64 - grid_lat0 as i64) / grid_cell_e7 as i64).max(0) |
| 133 | }; |
| 134 | let grid_cols = (cell_col(max_lon) + 1) as u32; |
| 135 | let grid_rows = (cell_row(max_lat) + 1) as u32; |
| 136 | // Sorted by `(cell_id, stop)`, which is exactly the two orderings the device |
| 137 | // requires: GRID_CELL_IDS ascending, because the reader binary-searches it, |
| 138 | // and each cell's GRID_STOPS run stop-ascending. |
| 139 | let mut grid: Vec<(u32, u32)> = (0..stop_count) |
| 140 | .map(|i| { |
| 141 | let row = cell_row(stop_lat[i]) as u32; |
| 142 | let col = cell_col(stop_lon[i]) as u32; |
| 143 | (row * grid_cols + col, i as u32) |
| 144 | }) |
| 145 | .collect(); |
| 146 | grid.sort_unstable(); |
| 147 | let mut sec_grid_cell_ids = Vec::new(); |
| 148 | let mut sec_grid_cell_off = Vec::new(); |
| 149 | let mut sec_grid_stops = Vec::with_capacity(stop_count * 4); |
| 150 | let mut grid_cell_count = 0usize; |
| 151 | for (k, &(cid, stop)) in grid.iter().enumerate() { |
| 152 | if k == 0 || cid != grid[k - 1].0 { |
| 153 | append_u32(&mut sec_grid_cell_ids, cid); |
| 154 | append_u32(&mut sec_grid_cell_off, k as u32); |
| 155 | grid_cell_count += 1; |
| 156 | } |
| 157 | append_u32(&mut sec_grid_stops, stop); |
| 158 | } |
| 159 | append_u32(&mut sec_grid_cell_off, grid.len() as u32); |
| 160 | ( |
| 161 | sec_grid_cell_ids, |
| 162 | sec_grid_cell_off, |
| 163 | sec_grid_stops, |
| 164 | grid_cols, |
| 165 | grid_rows, |
| 166 | grid_cell_e7, |
| 167 | grid_cell_count, |
| 168 | ) |
| 169 | } |