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

Modern-Apps.git / scripts / maps / gtfs_ingest / src / bin / transit_shapes_intercity.rs

transit_shapes_intercity.rs74 lines · 2.9 KiBRaw
1//! Which rail lines are intercity, and the colour they draw in at country zoom.
2//!
3//! An intercity line is carried from z3 (`mamaps_build`'s `schema::transit`), where it is the
4//! only transit drawn, so it has to be both correctly picked out and readable on its own.
5
6use gtfs_ingest::shapes::distance_m;
7
8/// A rail shape at least this long, end to end along its path, is intercity whatever its
9/// `route_type`: Amtrak, VIA and Brightline all publish plain `2`. Commuter rail stays under it.
10pub(crate) const INTERCITY_LENGTH_M: f64 = 150_000.0;
11
12/// Relative luminance (0..1) above which an intercity line takes the train fallback colour.
13/// Amtrak publishes `#CAE4F1` (about 0.74), which vanishes on the light basemap at country
14/// zoom; VIA's `#FFCB06` (about 0.63) still reads and is kept.
15const MAX_LUMINANCE: f64 = 0.7;
16
17/// GTFS extended types that name long-distance rail outright: high speed, long distance,
18/// inter-regional and sleeper.
19pub(crate) fn is_long_distance_type(route_type: u32) -> bool {
20 matches!(route_type, 101 | 102 | 103 | 105)
21}
22
23pub(crate) fn is_intercity(long_distance_type: bool, points: &[(i32, i32)]) -> bool {
24 long_distance_type
25 || points.windows(2).map(|w| distance_m(w[0], w[1])).sum::<f64>() >= INTERCITY_LENGTH_M
26}
27
28/// The colour a line is written with: its own, unless it is intercity and too pale to read.
29pub(crate) fn drawn_color(color: u32, intercity: bool, fallback: u32) -> u32 {
30 if intercity && luminance(color) > MAX_LUMINANCE {
31 fallback
32 } else {
33 color
34 }
35}
36
37fn luminance(color: u32) -> f64 {
38 let channel = |shift: u32| {
39 let c = f64::from((color >> shift) & 0xFF) / 255.0;
40 if c <= 0.039_28 {
41 c / 12.92
42 } else {
43 ((c + 0.055) / 1.055).powf(2.4)
44 }
45 };
46 0.2126 * channel(16) + 0.7152 * channel(8) + 0.0722 * channel(0)
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn long_or_typed_rail_is_intercity() {
55 // ~1.1 degrees of latitude per step: 2 steps is ~222 km.
56 let long = [
57 (370_000_000, -1_220_000_000),
58 (380_000_000, -1_220_000_000),
59 (390_000_000, -1_220_000_000),
60 ];
61 let short = [(370_000_000, -1_220_000_000), (375_000_000, -1_220_000_000)];
62 assert!(is_intercity(false, &long), "222 km of plain type-2 rail");
63 assert!(!is_intercity(false, &short), "a 56 km commuter line");
64 assert!(is_intercity(is_long_distance_type(102), &short), "type 102 is long distance");
65 assert!(!is_long_distance_type(2) && !is_long_distance_type(109));
66 }
67
68 #[test]
69 fn pale_intercity_colours_take_the_fallback() {
70 assert_eq!(drawn_color(0xCAE4F1, true, 0x0057A8), 0x0057A8, "Amtrak's published colour");
71 assert_eq!(drawn_color(0xFFCB06, true, 0x0057A8), 0xFFCB06, "VIA yellow reads");
72 assert_eq!(drawn_color(0xCAE4F1, false, 0x0057A8), 0xCAE4F1, "only intercity lines change");
73 }
74}