Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / index_part6.rs
index_part6.rs197 lines · 8.2 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod shape_tests { |
| 3 | use super::*; |
| 4 | use super::tests::{agency, assert_shape_invariants, one_feed, shape_map, shaped_feed}; |
| 5 | use crate::gtfs::parse_csv; |
| 6 | use crate::reader::{Reader, SEC_SHAPE_COORDS}; |
| 7 | |
| 8 | #[test] |
| 9 | fn a_mismatched_shape_is_dropped_to_the_stop_to_stop_fallback() { |
| 10 | let t = shaped_feed(false); |
| 11 | let ag = agency("America/Los_Angeles"); |
| 12 | // The same shape_id, but 4000 km east. |
| 13 | let sh = shape_map(vec![( |
| 14 | "SH1", |
| 15 | vec![(40.700, -74.000), (40.710, -74.000), (40.720, -74.000)], |
| 16 | None, |
| 17 | )]); |
| 18 | let (blob, stats) = |
| 19 | build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build"); |
| 20 | let r = Reader::new(blob).expect("read back the pack"); |
| 21 | assert_eq!(stats.shaped_routes, 0); |
| 22 | assert_eq!(stats.dropped_shape_routes, 1); |
| 23 | assert!(r.route_shape_off(0).is_none(), "a bad shape must not be stored"); |
| 24 | assert_eq!(r.sec_bytes(SEC_SHAPE_COORDS).len(), 0); |
| 25 | assert_shape_invariants(&r); |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn a_reference_to_a_missing_shape_id_falls_back_silently() { |
| 30 | let t = shaped_feed(false); |
| 31 | let ag = agency("America/Los_Angeles"); |
| 32 | // shapes.txt exists but does not contain SH1. |
| 33 | let sh = shape_map(vec![("OTHER", vec![(37.700, -122.400), (37.710, -122.400)], None)]); |
| 34 | let (blob, stats) = |
| 35 | build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build"); |
| 36 | let r = Reader::new(blob).expect("read back the pack"); |
| 37 | assert_eq!(stats.shaped_routes, 0); |
| 38 | assert_eq!(stats.dropped_shape_routes, 0, "an absent shape is not a validation drop"); |
| 39 | assert!(r.route_shape_off(0).is_none()); |
| 40 | assert_shape_invariants(&r); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn the_modal_shape_id_wins_when_a_pattern_disagrees() { |
| 45 | let stops = parse_csv( |
| 46 | "stop_id,stop_name,stop_lat,stop_lon,stop_code\n\ |
| 47 | S1,Alpha,37.700,-122.400,A1\n\ |
| 48 | S2,Beta,37.710,-122.400,B2\n", |
| 49 | ); |
| 50 | let routes = parse_csv( |
| 51 | "route_id,route_short_name,route_long_name,route_type,route_color\n\ |
| 52 | R1,N,Judah,0,\n", |
| 53 | ); |
| 54 | // Two trips on SH_GOOD, one on SH_BAD: same pattern, so one RAPTOR route. |
| 55 | let trips = parse_csv( |
| 56 | "route_id,service_id,trip_id,trip_headsign,shape_id\n\ |
| 57 | R1,WK,T1,Downtown,SH_GOOD\n\ |
| 58 | R1,WK,T2,Downtown,SH_GOOD\n\ |
| 59 | R1,WK,T3,Downtown,SH_BAD\n", |
| 60 | ); |
| 61 | let stop_times = parse_csv( |
| 62 | "trip_id,stop_id,stop_sequence,arrival_time,departure_time\n\ |
| 63 | T1,S1,1,08:00:00,08:00:00\n\ |
| 64 | T1,S2,2,08:10:00,08:10:00\n\ |
| 65 | T2,S1,1,09:00:00,09:00:00\n\ |
| 66 | T2,S2,2,09:10:00,09:10:00\n\ |
| 67 | T3,S1,1,10:00:00,10:00:00\n\ |
| 68 | T3,S2,2,10:10:00,10:10:00\n", |
| 69 | ); |
| 70 | let calendar = parse_csv( |
| 71 | "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date\n\ |
| 72 | WK,1,1,1,1,1,0,0,20240101,20241231\n", |
| 73 | ); |
| 74 | let t = (stops, routes, trips, stop_times, calendar); |
| 75 | let ag = agency("America/Los_Angeles"); |
| 76 | let sh = shape_map(vec![ |
| 77 | // The modal shape detours west, so it is identifiable in the output. |
| 78 | ( |
| 79 | "SH_GOOD", |
| 80 | vec![(37.700, -122.400), (37.705, -122.410), (37.710, -122.400)], |
| 81 | None, |
| 82 | ), |
| 83 | ("SH_BAD", vec![(37.700, -122.400), (37.710, -122.400)], None), |
| 84 | ]); |
| 85 | let (blob, stats) = |
| 86 | build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build"); |
| 87 | let r = Reader::new(blob).expect("read back the pack"); |
| 88 | assert_eq!(stats.multi_shape_routes, 1, "the disagreement is reported"); |
| 89 | assert_eq!(stats.shaped_routes, 1); |
| 90 | let pts = r.shape_points(r.route_shape_off(0).expect("shaped")); |
| 91 | assert!( |
| 92 | pts.iter().any(|&(_, lon)| lon < -1_224_050_000), |
| 93 | "the modal SH_GOOD detour is missing: {pts:?}" |
| 94 | ); |
| 95 | assert_shape_invariants(&r); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn two_routes_sharing_a_shape_store_it_once() { |
| 100 | let stops = parse_csv( |
| 101 | "stop_id,stop_name,stop_lat,stop_lon,stop_code\n\ |
| 102 | S1,Alpha,37.700,-122.400,A1\n\ |
| 103 | S2,Beta,37.710,-122.400,B2\n", |
| 104 | ); |
| 105 | // Two GTFS routes over the same stop pattern -> two RAPTOR routes, one |
| 106 | // shape_id between them. |
| 107 | let routes = parse_csv( |
| 108 | "route_id,route_short_name,route_long_name,route_type,route_color\n\ |
| 109 | R1,N,Judah,0,\n\ |
| 110 | R2,NX,Judah Express,0,\n", |
| 111 | ); |
| 112 | let trips = parse_csv( |
| 113 | "route_id,service_id,trip_id,trip_headsign,shape_id\n\ |
| 114 | R1,WK,T1,Downtown,SH1\n\ |
| 115 | R2,WK,T2,Downtown,SH1\n", |
| 116 | ); |
| 117 | let stop_times = parse_csv( |
| 118 | "trip_id,stop_id,stop_sequence,arrival_time,departure_time\n\ |
| 119 | T1,S1,1,08:00:00,08:00:00\n\ |
| 120 | T1,S2,2,08:10:00,08:10:00\n\ |
| 121 | T2,S1,1,09:00:00,09:00:00\n\ |
| 122 | T2,S2,2,09:10:00,09:10:00\n", |
| 123 | ); |
| 124 | let calendar = parse_csv( |
| 125 | "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date\n\ |
| 126 | WK,1,1,1,1,1,0,0,20240101,20241231\n", |
| 127 | ); |
| 128 | let t = (stops, routes, trips, stop_times, calendar); |
| 129 | let ag = agency("America/Los_Angeles"); |
| 130 | let sh = shape_map(vec![( |
| 131 | "SH1", |
| 132 | vec![(37.700, -122.400), (37.705, -122.410), (37.710, -122.400)], |
| 133 | None, |
| 134 | )]); |
| 135 | let (blob, stats) = |
| 136 | build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build"); |
| 137 | let r = Reader::new(blob).expect("read back the pack"); |
| 138 | assert_eq!(r.route_count(), 2, "route_count"); |
| 139 | assert_eq!(stats.shaped_routes, 2); |
| 140 | let a = r.route_shape_off(0).expect("route 0 shaped"); |
| 141 | let b = r.route_shape_off(1).expect("route 1 shaped"); |
| 142 | assert_eq!(a, b, "identical geometry must be stored once"); |
| 143 | assert_eq!( |
| 144 | r.sec_bytes(SEC_SHAPE_COORDS).len(), |
| 145 | shapes::encode(&r.shape_points(a)).len(), |
| 146 | "SHAPE_COORDS holds exactly one blob" |
| 147 | ); |
| 148 | assert_shape_invariants(&r); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn a_shape_with_huge_gaps_roundtrips() { |
| 153 | // Consecutive points ~40 km apart: an i16 delta at 1e-7 degrees would |
| 154 | // overflow, which is why SHAPE_COORDS uses zigzag varints. |
| 155 | let stops = parse_csv( |
| 156 | "stop_id,stop_name,stop_lat,stop_lon,stop_code\n\ |
| 157 | S1,Alpha,37.700,-122.400,A1\n\ |
| 158 | S2,Beta,38.060,-122.400,B2\n", |
| 159 | ); |
| 160 | let routes = parse_csv( |
| 161 | "route_id,route_short_name,route_long_name,route_type,route_color\n\ |
| 162 | R1,X,Express,2,\n", |
| 163 | ); |
| 164 | let trips = parse_csv( |
| 165 | "route_id,service_id,trip_id,trip_headsign,shape_id\n\ |
| 166 | R1,WK,T1,North,SH1\n", |
| 167 | ); |
| 168 | let stop_times = parse_csv( |
| 169 | "trip_id,stop_id,stop_sequence,arrival_time,departure_time\n\ |
| 170 | T1,S1,1,08:00:00,08:00:00\n\ |
| 171 | T1,S2,2,08:40:00,08:40:00\n", |
| 172 | ); |
| 173 | let calendar = parse_csv( |
| 174 | "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date\n\ |
| 175 | WK,1,1,1,1,1,0,0,20240101,20241231\n", |
| 176 | ); |
| 177 | let t = (stops, routes, trips, stop_times, calendar); |
| 178 | let ag = agency("America/Los_Angeles"); |
| 179 | let sh = shape_map(vec![( |
| 180 | "SH1", |
| 181 | vec![(37.700, -122.400), (37.880, -122.300), (38.060, -122.400)], |
| 182 | None, |
| 183 | )]); |
| 184 | let (blob, stats) = |
| 185 | build_index("world", &one_feed(&t, &ag, Some(&sh))).expect("build"); |
| 186 | let r = Reader::new(blob).expect("read back the pack"); |
| 187 | assert_eq!(stats.shaped_routes, 1); |
| 188 | let pts = r.shape_points(r.route_shape_off(0).expect("shaped")); |
| 189 | assert_eq!(pts.first(), Some(&(377_000_000, -1_224_000_000))); |
| 190 | assert_eq!(pts.last(), Some(&(380_600_000, -1_224_000_000))); |
| 191 | assert!( |
| 192 | pts.iter().any(|&(lat, lon)| lat == 378_800_000 && lon == -1_223_000_000), |
| 193 | "the midpoint deviates far more than the tolerance and must survive: {pts:?}" |
| 194 | ); |
| 195 | assert_shape_invariants(&r); |
| 196 | } |
| 197 | } |