Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / graph_build_part3.rs
graph_build_part3.rs194 lines · 7.7 KiBRaw
| 1 | /// Hook every transit stop that is *not* attached to a real road network onto the |
| 2 | /// nearest node that is. Without this a matched GTFS stop that OSM never attached |
| 3 | /// to a road is unreachable and the transit planner cannot get the user to it. |
| 4 | /// |
| 5 | /// # Why component *size* and not the largest component |
| 6 | /// |
| 7 | /// The question worth asking is "is this stop attached to a road network?", not |
| 8 | /// "is it attached to the biggest one?". On a region those coincide — the largest |
| 9 | /// component is 98.2% of California — so the pass used to find the largest |
| 10 | /// component and reconnect everything outside it. On a planet they do not: |
| 11 | /// continents are not road-connected, so the largest component is |
| 12 | /// Eurasia-plus-Africa at 57.7% and every stop in the Americas, Australia, Japan, |
| 13 | /// the UK and Indonesia was classified as isolated. The code then tried to bridge |
| 14 | /// an ocean to reach a road in France, failed 1,173,347 times — a quarter of all |
| 15 | /// stops — and paid for a widening search out to a million-node index window |
| 16 | /// before each failure. |
| 17 | /// |
| 18 | /// So a stop needs reconnecting only when its own component is smaller than |
| 19 | /// [`MIN_ROUTABLE_COMPONENT`], and the target is the nearest node in *any* |
| 20 | /// component at or above that threshold. A stop alone on an untagged node is a |
| 21 | /// component of size 1 and is still connected; a stop on a small island's road |
| 22 | /// network is left alone, because it is already as reachable as that island gets; |
| 23 | /// a stop in North America connects to North America. That also removes the |
| 24 | /// pathology rather than its symptom: a qualifying node is now metres away, so the |
| 25 | /// widening search almost always succeeds on its first window. |
| 26 | /// |
| 27 | /// The threshold is capped at the largest component that exists, because a graph |
| 28 | /// with no component of 1000 nodes still has a road network — it is just a small |
| 29 | /// one — and requiring a size nothing reaches would reconnect nothing at all. |
| 30 | /// |
| 31 | /// One caveat on the word "component": the scan follows out-edges only, because |
| 32 | /// that is the index the build has — a reverse CSR would be another 4.3 GB of |
| 33 | /// `targets` at planet scale. So the partition is by out-reachability, and a node |
| 34 | /// fed only by incoming one-ways can be labelled separately from the network that |
| 35 | /// feeds it, undercounting that network's size. The error is in the safe direction: |
| 36 | /// such a node looks *less* connected than it is, so at worst it gains a connector |
| 37 | /// it did not need. The same traversal is what measured California's largest |
| 38 | /// component at 98.24%, which bounds how far off it can be in practice. |
| 39 | /// |
| 40 | /// `coords` and `stops` are both in final (Morton) id space. |
| 41 | fn reconnect_isolated_stops(coords: &[geom::Pt], stops: &Bitset, csr: &Csr) -> Reconnected { |
| 42 | let n = coords.len(); |
| 43 | if n == 0 { |
| 44 | return Reconnected { |
| 45 | lcc_size: 0, |
| 46 | synth: Vec::new(), |
| 47 | already_connected: 0, |
| 48 | unreachable: 0, |
| 49 | }; |
| 50 | } |
| 51 | println!("Identifying connected components..."); |
| 52 | |
| 53 | let mut component = vec![u32::MAX; n]; |
| 54 | // One size per component id. Planet has ~291 K components, so 1.2 MB. |
| 55 | let mut sizes: Vec<u32> = Vec::new(); |
| 56 | let mut queue: Vec<u32> = Vec::with_capacity(n); |
| 57 | let mut prog = crate::progress::Progress::new("Connected components", n as u64); |
| 58 | for start in 0..n { |
| 59 | prog.inc(); |
| 60 | if component[start] != u32::MAX { |
| 61 | continue; |
| 62 | } |
| 63 | let id = sizes.len() as u32; |
| 64 | let base = queue.len(); |
| 65 | queue.push(start as u32); |
| 66 | component[start] = id; |
| 67 | let mut head = base; |
| 68 | while head < queue.len() { |
| 69 | let u = queue[head]; |
| 70 | head += 1; |
| 71 | for v in csr.out(u) { |
| 72 | if component[*v as usize] == u32::MAX { |
| 73 | component[*v as usize] = id; |
| 74 | queue.push(*v); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | sizes.push((queue.len() - base) as u32); |
| 79 | } |
| 80 | prog.finish(); |
| 81 | drop(queue); |
| 82 | |
| 83 | let lcc_size = sizes.iter().copied().max().unwrap_or(0); |
| 84 | let threshold = MIN_ROUTABLE_COMPONENT.min(lcc_size); |
| 85 | println!( |
| 86 | "{} component(s), largest {lcc_size} / {n} node(s) ({:.1}%); a component of \ |
| 87 | {threshold}+ node(s) counts as routable", |
| 88 | sizes.len(), |
| 89 | f64::from(lcc_size) * 100.0 / n as f64 |
| 90 | ); |
| 91 | |
| 92 | let routable = |node: u32| sizes[component[node as usize] as usize] >= threshold; |
| 93 | let mut stop_count = 0usize; |
| 94 | let mut isolated: Vec<u32> = Vec::new(); |
| 95 | for i in 0..n as u32 { |
| 96 | if stops.get(u64::from(i)) { |
| 97 | stop_count += 1; |
| 98 | if !routable(i) { |
| 99 | isolated.push(i); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | let already_connected = stop_count - isolated.len(); |
| 104 | if isolated.is_empty() { |
| 105 | println!("{stop_count} stop(s), all already on a routable component"); |
| 106 | return Reconnected { |
| 107 | lcc_size: u64::from(lcc_size), |
| 108 | synth: Vec::new(), |
| 109 | already_connected, |
| 110 | unreachable: 0, |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | // The nodes are Morton-sorted, so index distance approximates spatial |
| 115 | // distance: widen an index window around the stop until it contains a node in |
| 116 | // a routable component, then take the closest one in that window. |
| 117 | let found = par::map_chunks(&isolated, 256, |_, chunk| { |
| 118 | let mut out: Vec<Synth> = Vec::new(); |
| 119 | let mut failed = 0usize; |
| 120 | for &i in chunk { |
| 121 | let stop = coords[i as usize]; |
| 122 | let mut best: Option<(u32, u32)> = None; |
| 123 | let mut radius = 1000usize; |
| 124 | while radius <= 1_000_000 { |
| 125 | let lo = (i as usize).saturating_sub(radius); |
| 126 | let hi = (i as usize + radius).min(n); |
| 127 | for (j, coord) in coords.iter().enumerate().take(hi).skip(lo) { |
| 128 | if !routable(j as u32) { |
| 129 | continue; |
| 130 | } |
| 131 | let d = accurate_dist_mm(stop.0, stop.1, coord.0, coord.1); |
| 132 | if best.is_none_or(|(bd, _)| d < bd) { |
| 133 | best = Some((d, j as u32)); |
| 134 | } |
| 135 | } |
| 136 | if best.is_some() { |
| 137 | break; |
| 138 | } |
| 139 | radius *= 10; |
| 140 | } |
| 141 | if let Some((dist_mm, target)) = best { |
| 142 | out.push(Synth { |
| 143 | source: i, |
| 144 | target, |
| 145 | dist_mm, |
| 146 | }); |
| 147 | out.push(Synth { |
| 148 | source: target, |
| 149 | target: i, |
| 150 | dist_mm, |
| 151 | }); |
| 152 | } else { |
| 153 | failed += 1; |
| 154 | } |
| 155 | } |
| 156 | (out, failed) |
| 157 | }); |
| 158 | |
| 159 | let unreachable: usize = found.iter().map(|(_, f)| f).sum(); |
| 160 | let mut synth: Vec<Synth> = found.into_iter().flat_map(|(e, _)| e).collect(); |
| 161 | println!( |
| 162 | "{stop_count} stop(s): {already_connected} already connected, {} reconnected, \ |
| 163 | {unreachable} found nothing", |
| 164 | synth.len() / 2 |
| 165 | ); |
| 166 | if unreachable > 0 { |
| 167 | // The index window caps at 1e6 nodes, so a stop with no routable road |
| 168 | // within that window stays unreachable. Say so rather than dropping it |
| 169 | // silently: an unreachable stop is exactly what this pass exists to |
| 170 | // prevent. A large count here means MIN_ROUTABLE_COMPONENT is too high. |
| 171 | eprintln!("WARN: {unreachable} isolated stop(s) had no routable road nearby"); |
| 172 | } |
| 173 | synth.sort_by_key(|s| (s.source, s.target)); |
| 174 | Reconnected { |
| 175 | lcc_size: u64::from(lcc_size), |
| 176 | synth, |
| 177 | already_connected, |
| 178 | unreachable, |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /// What `write_graph` produced, for [`Stats`]. |
| 183 | struct Written { |
| 184 | edge_count: u64, |
| 185 | geometry_edges: u64, |
| 186 | reversed_edges: u64, |
| 187 | intermediate_bytes: u64, |
| 188 | edges_bytes: u64, |
| 189 | escape_count: u64, |
| 190 | named_edges: u64, |
| 191 | census: Census, |
| 192 | } |
| 193 | |
| 194 | include!("graph_build_part3_s1.rs"); |