/// Hook every transit stop that is *not* attached to a real road network onto the /// nearest node that is. Without this a matched GTFS stop that OSM never attached /// to a road is unreachable and the transit planner cannot get the user to it. /// /// # Why component *size* and not the largest component /// /// The question worth asking is "is this stop attached to a road network?", not /// "is it attached to the biggest one?". On a region those coincide — the largest /// component is 98.2% of California — so the pass used to find the largest /// component and reconnect everything outside it. On a planet they do not: /// continents are not road-connected, so the largest component is /// Eurasia-plus-Africa at 57.7% and every stop in the Americas, Australia, Japan, /// the UK and Indonesia was classified as isolated. The code then tried to bridge /// an ocean to reach a road in France, failed 1,173,347 times — a quarter of all /// stops — and paid for a widening search out to a million-node index window /// before each failure. /// /// So a stop needs reconnecting only when its own component is smaller than /// [`MIN_ROUTABLE_COMPONENT`], and the target is the nearest node in *any* /// component at or above that threshold. A stop alone on an untagged node is a /// component of size 1 and is still connected; a stop on a small island's road /// network is left alone, because it is already as reachable as that island gets; /// a stop in North America connects to North America. That also removes the /// pathology rather than its symptom: a qualifying node is now metres away, so the /// widening search almost always succeeds on its first window. /// /// The threshold is capped at the largest component that exists, because a graph /// with no component of 1000 nodes still has a road network — it is just a small /// one — and requiring a size nothing reaches would reconnect nothing at all. /// /// One caveat on the word "component": the scan follows out-edges only, because /// that is the index the build has — a reverse CSR would be another 4.3 GB of /// `targets` at planet scale. So the partition is by out-reachability, and a node /// fed only by incoming one-ways can be labelled separately from the network that /// feeds it, undercounting that network's size. The error is in the safe direction: /// such a node looks *less* connected than it is, so at worst it gains a connector /// it did not need. The same traversal is what measured California's largest /// component at 98.24%, which bounds how far off it can be in practice. /// /// `coords` and `stops` are both in final (Morton) id space. fn reconnect_isolated_stops(coords: &[geom::Pt], stops: &Bitset, csr: &Csr) -> Reconnected { let n = coords.len(); if n == 0 { return Reconnected { lcc_size: 0, synth: Vec::new(), already_connected: 0, unreachable: 0, }; } println!("Identifying connected components..."); let mut component = vec![u32::MAX; n]; // One size per component id. Planet has ~291 K components, so 1.2 MB. let mut sizes: Vec = Vec::new(); let mut queue: Vec = Vec::with_capacity(n); let mut prog = crate::progress::Progress::new("Connected components", n as u64); for start in 0..n { prog.inc(); if component[start] != u32::MAX { continue; } let id = sizes.len() as u32; let base = queue.len(); queue.push(start as u32); component[start] = id; let mut head = base; while head < queue.len() { let u = queue[head]; head += 1; for v in csr.out(u) { if component[*v as usize] == u32::MAX { component[*v as usize] = id; queue.push(*v); } } } sizes.push((queue.len() - base) as u32); } prog.finish(); drop(queue); let lcc_size = sizes.iter().copied().max().unwrap_or(0); let threshold = MIN_ROUTABLE_COMPONENT.min(lcc_size); println!( "{} component(s), largest {lcc_size} / {n} node(s) ({:.1}%); a component of \ {threshold}+ node(s) counts as routable", sizes.len(), f64::from(lcc_size) * 100.0 / n as f64 ); let routable = |node: u32| sizes[component[node as usize] as usize] >= threshold; let mut stop_count = 0usize; let mut isolated: Vec = Vec::new(); for i in 0..n as u32 { if stops.get(u64::from(i)) { stop_count += 1; if !routable(i) { isolated.push(i); } } } let already_connected = stop_count - isolated.len(); if isolated.is_empty() { println!("{stop_count} stop(s), all already on a routable component"); return Reconnected { lcc_size: u64::from(lcc_size), synth: Vec::new(), already_connected, unreachable: 0, }; } // The nodes are Morton-sorted, so index distance approximates spatial // distance: widen an index window around the stop until it contains a node in // a routable component, then take the closest one in that window. let found = par::map_chunks(&isolated, 256, |_, chunk| { let mut out: Vec = Vec::new(); let mut failed = 0usize; for &i in chunk { let stop = coords[i as usize]; let mut best: Option<(u32, u32)> = None; let mut radius = 1000usize; while radius <= 1_000_000 { let lo = (i as usize).saturating_sub(radius); let hi = (i as usize + radius).min(n); for (j, coord) in coords.iter().enumerate().take(hi).skip(lo) { if !routable(j as u32) { continue; } let d = accurate_dist_mm(stop.0, stop.1, coord.0, coord.1); if best.is_none_or(|(bd, _)| d < bd) { best = Some((d, j as u32)); } } if best.is_some() { break; } radius *= 10; } if let Some((dist_mm, target)) = best { out.push(Synth { source: i, target, dist_mm, }); out.push(Synth { source: target, target: i, dist_mm, }); } else { failed += 1; } } (out, failed) }); let unreachable: usize = found.iter().map(|(_, f)| f).sum(); let mut synth: Vec = found.into_iter().flat_map(|(e, _)| e).collect(); println!( "{stop_count} stop(s): {already_connected} already connected, {} reconnected, \ {unreachable} found nothing", synth.len() / 2 ); if unreachable > 0 { // The index window caps at 1e6 nodes, so a stop with no routable road // within that window stays unreachable. Say so rather than dropping it // silently: an unreachable stop is exactly what this pass exists to // prevent. A large count here means MIN_ROUTABLE_COMPONENT is too high. eprintln!("WARN: {unreachable} isolated stop(s) had no routable road nearby"); } synth.sort_by_key(|s| (s.source, s.target)); Reconnected { lcc_size: u64::from(lcc_size), synth, already_connected, unreachable, } } /// What `write_graph` produced, for [`Stats`]. struct Written { edge_count: u64, geometry_edges: u64, reversed_edges: u64, intermediate_bytes: u64, edges_bytes: u64, escape_count: u64, named_edges: u64, census: Census, } include!("graph_build_part3_s1.rs");