Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / scripts / maps / gtfs_ingest / src / rail.rs

rail.rs120 lines · 4.9 KiBRaw
1//! Which GTFS routes and shapes the basemap's rail-line layer is drawn from.
2//!
3//! Shared by `transit_shapes`, which reads them out of raw feeds, and the pack builder, which
4//! stores exactly those shapes in `world.transit`'s RAIL_LINES section so the layer can be
5//! rebuilt from the pack alone. One definition, so the two cannot drift apart.
6
7use crate::gtfs::Csv;
8use std::collections::{BTreeMap, BTreeSet, HashMap};
9
10/// One rail route: `(mode, colour, is a fallback colour, display name, route_type)`.
11pub type RailRoute = (&'static str, u32, bool, String, u32);
12
13/// GTFS `route_type` → the basemap's mode vocabulary, or `None` for a mode this
14/// layer does not carry (bus, ferry, trolleybus, aerial lift, ...).
15///
16/// The extended ranges are here because European feeds use them almost
17/// exclusively: a feed publishing its metro as 402 rather than 1 would otherwise
18/// contribute nothing at all.
19pub fn mode_of(route_type: u32) -> Option<&'static str> {
20 Some(match route_type {
21 // GTFS 0 is "tram, streetcar, light rail" in one value. `light_rail`
22 // rather than `tram` because that is what North American agencies put
23 // there — Muni Metro, MAX, Link — and its zoom floor is the shallower of
24 // the two, so a street tram filed under 0 surfaces one zoom early rather
25 // than a light rail line vanishing until z13.
26 0 => "light_rail",
27 1 => "subway",
28 2 => "train",
29 // Cable tram and funicular: short, street-level, and the tram zoom floor.
30 5 | 7 => "tram",
31 12 => "monorail",
32 100..=117 => "train",
33 400..=404 => "subway",
34 405 => "monorail",
35 900..=906 => "tram",
36 _ => return None,
37 })
38}
39
40/// The fallback line colour per mode, as `0xRRGGBB`, for a route naming none.
41///
42/// **A matched pair with `mamaps_build`'s `schema::transit::MODES`**: the five
43/// names above and the five colours here are that table, duplicated because this
44/// crate is deliberately detached from the Android workspace and has no
45/// dependencies, so the two cannot share code. A change to either is a change to
46/// both.
47pub fn fallback_color(mode: &str) -> u32 {
48 match mode {
49 "subway" => 0xE4002B,
50 "light_rail" => 0x00985F,
51 "tram" => 0xFFD200,
52 "train" => 0x0057A8,
53 "monorail" => 0x9D9D9D,
54 _ => 0x666666,
55 }
56}
57
58/// A `route_color` cell as `0xRRGGBB`, or `None` to fall back per mode.
59///
60/// GTFS spells it as bare `RRGGBB`; a leading `#` is accepted because feeds put
61/// one there anyway. Zero is refused rather than emitted: it means "no colour" on
62/// the wire, so a route that really is black takes its mode's colour instead.
63pub fn route_color(raw: &str) -> Option<u32> {
64 let hex = raw.trim();
65 let hex = hex.strip_prefix('#').unwrap_or(hex);
66 if hex.len() != 6 || !hex.chars().all(|c| c.is_ascii_hexdigit()) {
67 return None;
68 }
69 let value = u32::from_str_radix(hex, 16).ok()?;
70 (value != 0).then_some(value)
71}
72
73/// `route_id` → [`RailRoute`] for every rail route in the feed. Everything else, bus
74/// included, is dropped here.
75pub fn rail_routes(routes: &Csv) -> HashMap<String, RailRoute> {
76 let mut out = HashMap::new();
77 for row in &routes.rows {
78 let id = routes.get(row, "route_id");
79 if id.is_empty() {
80 continue;
81 }
82 let Ok(route_type) = routes.get(row, "route_type").trim().parse::<u32>() else { continue };
83 let Some(mode) = mode_of(route_type) else { continue };
84 let (color, fallback) = match route_color(routes.get(row, "route_color")) {
85 Some(color) => (color, false),
86 None => (fallback_color(mode), true),
87 };
88 let short = routes.get(row, "route_short_name").trim();
89 let long = routes.get(row, "route_long_name").trim();
90 let name = if short.is_empty() { long } else { short };
91 out.insert(id.to_string(), (mode, color, fallback, name.to_string(), route_type));
92 }
93 out
94}
95
96/// The distinct `shape_id`s each rail route's trips reference.
97///
98/// One line per **distinct polyline**, not per trip and not per RAPTOR route
99/// group: a route's trips reference many shapes (one per direction, one per
100/// short-turn variant) and every trip carrying its own line would draw the same
101/// alignment hundreds of times. `BTreeMap`/`BTreeSet` rather than the hashed
102/// pair, because the output has to be byte-identical between runs.
103pub fn shape_ids_by_route(
104 trips: &Csv,
105 rail: &HashMap<String, RailRoute>,
106) -> BTreeMap<String, BTreeSet<String>> {
107 let mut out: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
108 for row in &trips.rows {
109 let route_id = trips.get(row, "route_id");
110 if !rail.contains_key(route_id) {
111 continue;
112 }
113 let shape_id = trips.get(row, "shape_id").trim();
114 if shape_id.is_empty() {
115 continue;
116 }
117 out.entry(route_id.to_string()).or_default().insert(shape_id.to_string());
118 }
119 out
120}