//! The `run` pipeline for `transit_shapes`, moved out of `transit_shapes.rs` //! to keep the bin under the file-size limit. Behaviour-identical. use super::*; use rayon::prelude::*; use std::time::Instant; /// Where the lines come from: raw GTFS feeds, or the `world.transit` pack built from them. #[derive(Clone, Copy)] pub(crate) enum Inputs<'a> { Feeds(&'a [FeedSpec]), Pack(&'a Path), } /// Untimed entry, kept for the unit tests in `transit_shapes_extra`. #[cfg(test)] pub(crate) fn run(out_path: &Path, specs: &[FeedSpec]) -> Result<(), String> { run_with_timer(out_path, Inputs::Feeds(specs), None) } /// Timed pipeline entry: ticks `parse-feeds` over the feed loop below, the /// corridor stages inside [`expand_corridor_spans`], then `write-output` over /// the sort and file write, and emits the `TIMING` line before the human /// summary. `None` skips every tick, which is what the tests drive via [`run`]. pub(crate) fn run_with_timer( out_path: &Path, inputs: Inputs<'_>, mut timer: Option, ) -> Result<(), String> { let mut seen: HashSet<(u64, u32)> = HashSet::new(); let mut lines: Vec = Vec::new(); // Track already drawn, per colour and per mode. Two of one colour must never draw as // parallel lines — nothing distinguishes them, so the second is pure over-draw — and what // stops that is the slot, which is assigned per colour so they land on top of each other. // This drops the ones that are wholly redundant as well, so the layer does not carry a // route's two directions and every other feed's copy of them. // // The per-mode cover is for a line wearing its mode's fallback colour, which means the // feed gave it none: if that track is already drawn in *someone's* colour, a line with // no colour of its own should not be drawn over it in a guessed one. let mut by_color: BTreeMap<(&'static str, u32), bundle::Covered> = BTreeMap::new(); let mut by_mode: BTreeMap<&'static str, bundle::Covered> = BTreeMap::new(); let (mut kept, mut without_shape, mut deduped, mut fell_back) = (0usize, 0usize, 0usize, 0usize); let (mut shapes_read, mut merged) = (0usize, 0usize); let mut crowded = 0usize; // Feeds are parsed in parallel and folded in sequentially. // // Parsing is pure per feed and was most of the wall clock: a world-scale set spent ~40 of // its 54 minutes here, single-threaded, on machines with dozens of idle cores. The fold // below cannot move — `seen`, `by_color` and `by_mode` are order-dependent, and feed order // is what decides which duplicate of a shared alignment survives — so only the reads are // spread out, and their results are put back into spec order before any of them is folded. // The output is therefore bit-identical to the sequential version. // // Chunked rather than read-all-then-fold: a chunk of twice the pool keeps every core busy // while capping how many parsed feeds are alive at once, so peak memory follows the pool // size and not the number of feeds. // // ONE subphase for the whole feed loop below: thousands of feeds must not // produce thousands of entries. The sequential fold rides along inside it. if let Some(t) = timer.as_mut() { t.switch("parse-feeds"); } let parse_start = Instant::now(); let threads = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1); let chunk_size = threads.saturating_mul(2).max(1); let total = match inputs { Inputs::Feeds(specs) => specs.len(), Inputs::Pack(_) => 1, }; let chunks: Vec<&[FeedSpec]> = match inputs { Inputs::Feeds(specs) => specs.chunks(chunk_size).collect(), // One pass over the whole pack: it is already one merged, ordered set. Inputs::Pack(_) => vec![&[]], }; let mut processed = 0usize; for chunk in chunks { let parsed = match inputs { Inputs::Pack(path) => vec![(0, super::pack::pack_lines(path))], Inputs::Feeds(_) => parse_chunk(chunk, threads), }; // Feeds unwrapped in spec order and their lines gathered, so the dedup key of every // line can be computed wide before the sequential fold. Stats accumulate here so an // unreadable feed still surfaces its error in feed order, exactly as before. let mut chunk_lines: Vec = Vec::new(); for (_, feed) in parsed { let feed = feed?; kept += feed.routes_kept; without_shape += feed.routes_without_shape; shapes_read += feed.shapes_read; chunk_lines.extend(feed.lines); processed += 1; print_feed_meter(processed, total, &parse_start); } // The dedup key is a content hash of the geometry, so it is order-independent and every // line's can be computed in parallel. Only the `seen.insert` below stays sequential — // it is what decides, in feed order, which duplicate of a shared alignment survives. let keys: Vec<(u64, u32)> = chunk_lines .par_iter() .map(|line| (polyline_hash(&line.points), line.color)) .collect(); for (line, key) in chunk_lines.into_iter().zip(keys) { // Across feeds as well as within one, so an alignment published by both a city // feed and the regional feed that merges it draws once. // // Keyed on the colour too, which the hash alone was not: two services running // the same track are routinely published against one `shape_id`, and dropping // one of them because its geometry had been seen is how a corridor loses a // line. This is only the fast path for the subtraction below, which would reach // the same answer the slow way. if !seen.insert(key) { deduped += 1; continue; } // Walked once here: `covered_fraction`, `crowd_reaches` and the two `add`s // below each used to re-walk `line.points` and allocate a fresh sample vector. // The samples depend only on the geometry, so one walk feeds all four. let walked = bundle::walk(&line.points); let mode_cover = by_mode.entry(line.mode).or_default(); let redundant = if line.fallback { mode_cover.covered_fraction_walked(&walked) >= MOSTLY_DRAWN } else { by_color .entry((line.mode, line.color)) .or_default() .covered_fraction_walked(&walked) >= MOSTLY_DRAWN }; if redundant { merged += 1; continue; } // The gates above are colour-scoped, deliberately: two services sharing a track // are two real services and both should draw. On a planet that stops being true. // One alignment is republished by a city feed, the regional feed containing it and // a national feed on top, each with its own `route_color` and often its own // `route_type`, so each reads as a distinct service and claims its own lane. That // is what makes one railway render as fifteen jagged parallel lines. // // A ceiling rather than a ban, so the two-services-on-one-track case the tests // pin still works. Above it the track is already saying everything it can. // // Intercity lines are exempt: at country zoom they are the only transit drawn, so // losing one to a crowded shared corridor (the Northeast Corridor carries a dozen // commuter services) would cut a gap in the national network. if !line.intercity && mode_cover.crowd_reaches_walked(&walked, MAX_SERVICES_PER_TRACK) { crowded += 1; continue; } mode_cover.add_tagged_walked(&walked, line.color); by_color.entry((line.mode, line.color)).or_default().add_walked(&walked); if line.fallback { fell_back += 1; } lines.push(line); } } eprintln!(); drop(by_color); drop(by_mode); // Which routes share a corridor, which lane each takes in it, and where each line has // to be cut for that lane to change. Once, over the whole set: neither the tiler nor // the renderer sees more than one tile, so deciding it there is what puts a jog in every // route at every tile seam. let bundled = expand_corridor_spans(&mut lines, timer.as_mut()); if let Some(t) = timer.as_mut() { t.switch("write-output"); } // Deterministic output, so a rebuild produces a byte-identical layer and the // tile diff is empty when nothing changed. Feed order already fixes which // duplicate survives; this fixes the order they are written in. lines.par_sort_by(|a, b| { a.points .cmp(&b.points) .then_with(|| a.color.cmp(&b.color)) .then_with(|| a.mode.cmp(b.mode)) .then_with(|| (a.ordinal, a.lanes, a.taper).cmp(&(b.ordinal, b.lanes, b.taper))) .then_with(|| a.route.cmp(&b.route)) }); let file = std::fs::File::create(out_path) .map_err(|e| format!("cannot write {}: {e}", out_path.display()))?; let mut out = BufWriter::new(file); let mut buf: Vec = Vec::new(); for line in &lines { buf.clear(); buf.extend_from_slice( b"{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[", ); for (i, (lat, lon)) in line.points.iter().enumerate() { if i > 0 { buf.push(b','); } write!(buf, "[{:.7},{:.7}]", *lon as f64 * 1e-7, *lat as f64 * 1e-7) .map_err(io_err)?; } write!( buf, "]}},\"properties\":{{\"color\":\"{:06X}\",\"mode\":\"{}\",\"ordinal\":{},\ \"lanes\":{},\"taper\":{},{}\"route\":\"", intercity::drawn_color(line.color, line.intercity, fallback_color(line.mode)), line.mode, line.ordinal, line.lanes, line.taper, // Only when set, so every other line is written exactly as before. if line.intercity { "\"intercity\":1," } else { "" }, ) .map_err(io_err)?; json_escape(line.route.as_bytes(), &mut buf); buf.extend_from_slice(b"\"}}\n"); out.write_all(&buf).map_err(io_err)?; } out.flush().map_err(io_err)?; // TIMING before the human summary: stdout carries facts + TIMING only. if let Some(t) = timer { t.finish(); } println!( "transit_shapes: wrote {} ({} feed(s), {kept} rail route(s) kept, \ {without_shape} with no usable shape, {shapes_read} shape(s) read, \ {deduped} republished byte-for-byte, {merged} already drawn in their colour, \ {crowded} over track already carrying {MAX_SERVICES_PER_TRACK} services, \ {} line(s), {} intercity, {bundled} in a shared corridor, \ {fell_back} on a per-mode fallback colour)", out_path.display(), total, lines.len(), lines.iter().filter(|l| l.intercity).count(), ); Ok(()) } /// Parse one chunk of feeds on `threads` workers, back in spec order, so the fold sees exactly /// the sequence a sequential read would give it. fn parse_chunk(chunk: &[FeedSpec], threads: usize) -> Vec<(usize, Result)> { let next = AtomicUsize::new(0); let parsed: Mutex)>> = Mutex::new(Vec::with_capacity(chunk.len())); std::thread::scope(|scope| { for _ in 0..threads.min(chunk.len()) { scope.spawn(|| loop { let i = next.fetch_add(1, Ordering::Relaxed); if i >= chunk.len() { break; } let read = read_feed(&chunk[i]); parsed.lock().expect("feed reader pool").push((i, read)); }); } }); let mut parsed = parsed.into_inner().expect("feed reader pool"); parsed.sort_unstable_by_key(|(i, _)| *i); parsed } /// One stderr redraw of the feed-parse meter in the standard shape: /// `\r{label:<28} [{pct:>3}%] {done}/{total} {elapsed} elapsed, ETA {eta}`. fn print_feed_meter(done: usize, total: usize, start: &Instant) { let elapsed = start.elapsed().as_secs_f64(); let pct = if total == 0 { 100 } else { (done * 100 / total.max(1)).min(100) }; let eta = if done == 0 { "--".to_string() } else { fmt_dur(elapsed / done as f64 * (total - done) as f64) }; eprint!( "\r{:<28} [{:>3}%] {done}/{total} {} elapsed, ETA {eta}", "Parse feeds", pct, fmt_dur(elapsed), ); let _ = std::io::stderr().flush(); } /// Short stable durations for the meter: `12s`, `2m05s`, `1h02m`. fn fmt_dur(secs: f64) -> String { let s = secs.max(0.0).round() as u64; if s < 60 { format!("{s}s") } else if s < 3600 { format!("{}m{:02}s", s / 60, s % 60) } else { format!("{}h{:02}m", s / 3600, (s % 3600) / 60) } }