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

Modern-Apps.git / maps / src / main / rust / src / transit_part12.rs

transit_part12.rs280 lines · 10.3 KiBRaw
1 // --- Corridor-bundled rail lines + trip itineraries (`transit_part11`) ---
2
3 #[test]
4 fn rail_lines_serve_a_shaped_route_with_its_colour() {
5 let idx = shaped_route_pack().index();
6 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
7 assert_eq!(lines.len(), 1, "the one shaped route serves the bbox");
8 let l = &lines[0];
9 assert_eq!(l.name, "N");
10 assert_eq!(l.color, 0x0000FF);
11 assert_eq!(l.feed, "sfmuni");
12 // The fitted shape detours east: 5 vertices = 10 doubles, and the
13 // second point must swing off the stops' shared meridian.
14 assert_eq!(l.coords.len(), 10);
15 assert!(l.coords[2] > -122.400, "on the eastward detour");
16 }
17
18 #[test]
19 fn rail_lines_skip_a_bbox_no_stop_serves() {
20 let idx = shaped_route_pack().index();
21 let lines = rail_lines(&idx, 40.0, -75.0, 41.0, -74.0);
22 assert!(lines.is_empty(), "far from the SF stops: nothing to draw");
23 }
24
25 #[test]
26 fn trip_itinerary_lists_every_stop_with_times() {
27 let idx = one_route_pack().index();
28 // Vehicle id for route 0, trip 0, today: the packing active_vehicles uses.
29 let id = 0;
30 let itin =
31 trip_itinerary(&idx, id, sched(wednesday())).expect("trip 0 runs Wednesday");
32 assert_eq!(itin.route_name, "N");
33 assert_eq!(itin.headsign, "Downtown");
34 assert!(!itin.cancelled);
35 assert_eq!(itin.stops.len(), 3);
36 assert_eq!(itin.stops[0].name, "Alpha");
37 assert_eq!(itin.stops[2].name, "Gamma");
38 assert_eq!(
39 (itin.stops[0].dep_secs, itin.stops[2].arr_secs),
40 (28_800, 29_400)
41 );
42 // Dwell at Beta: arrives 29_100, departs 29_160.
43 assert_eq!(
44 (itin.stops[1].arr_secs, itin.stops[1].dep_secs),
45 (29_100, 29_160)
46 );
47 }
48
49 #[test]
50 fn trip_itinerary_refuses_an_unknown_trip() {
51 let idx = one_route_pack().index();
52 // Route 9 does not exist; trip 99 neither.
53 assert!(
54 trip_itinerary(&idx, 9i64 << 32, sched(wednesday())).is_none()
55 );
56 assert!(
57 trip_itinerary(&idx, 99i64 << 1, sched(wednesday())).is_none()
58 );
59 }
60
61 /// Two routes over one track, in different agency colours, fan into
62 /// adjacent lanes rather than stacking.
63 fn two_colour_pack() -> Pack {
64 let mut pack = one_route_pack();
65 pack.routes.push(Route {
66 name: "M",
67 color: 0xFF0000,
68 route_type: 0,
69 feed: 0,
70 pattern: vec![0, 1, 2],
71 trips: vec![Trip {
72 start: 28_800,
73 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
74 service: 0,
75 headsign: "Downtown",
76 }],
77 // Same alignment, surveyed a metre east: one corridor, not two.
78 shape: Some(Shape {
79 points: vec![
80 (37.700, -122.39999),
81 (37.705, -122.38999),
82 (37.710, -122.39999),
83 (37.715, -122.38999),
84 (37.720, -122.39999),
85 ],
86 stop_vertices: vec![0, 2, 4],
87 }),
88 });
89 pack.routes[0].shape = Some(Shape {
90 points: vec![
91 (37.700, -122.400),
92 (37.705, -122.390),
93 (37.710, -122.400),
94 (37.715, -122.390),
95 (37.720, -122.400),
96 ],
97 stop_vertices: vec![0, 2, 4],
98 });
99 pack
100 }
101
102 #[test]
103 fn shared_track_fans_into_adjacent_lanes() {
104 let idx = two_colour_pack().index();
105 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
106 assert!(!lines.is_empty(), "both routes serve the bbox");
107 // Every span of the two colours shares one corridor: same lane
108 // count, complementary ordinals.
109 let mut by_color: std::collections::HashMap<u32, Vec<(u8, u8)>> =
110 std::collections::HashMap::new();
111 for l in &lines {
112 by_color.entry(l.color).or_default().push((l.ordinal, l.lanes));
113 }
114 assert_eq!(by_color.len(), 2, "both agency colours draw: {by_color:?}");
115 for (color, spans) in &by_color {
116 assert!(
117 spans.iter().any(|&(_, lanes)| lanes == 2),
118 "colour {color:06X} fans as one of two: {spans:?}"
119 );
120 }
121 let ordinals: std::collections::HashSet<u8> =
122 lines.iter().filter(|l| l.lanes == 2).map(|l| l.ordinal).collect();
123 assert_eq!(ordinals, std::collections::HashSet::from([0, 1]), "{ordinals:?}");
124 }
125
126 #[test]
127 fn a_byte_identical_republication_draws_once() {
128 let shape_points = vec![
129 (37.700, -122.400),
130 (37.705, -122.390),
131 (37.710, -122.400),
132 (37.715, -122.390),
133 (37.720, -122.400),
134 ];
135 let mut pack = one_route_pack();
136 pack.routes[0].shape = Some(Shape {
137 points: shape_points.clone(),
138 stop_vertices: vec![0, 2, 4],
139 });
140 // Same geometry, same colour, new name: a republished feed.
141 pack.routes.push(Route {
142 name: "N-dup",
143 color: 0x0000FF,
144 route_type: 0,
145 feed: 0,
146 pattern: vec![0, 1, 2],
147 trips: vec![Trip {
148 start: 28_800,
149 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
150 service: 0,
151 headsign: "Downtown",
152 }],
153 shape: Some(Shape { points: shape_points, stop_vertices: vec![0, 2, 4] }),
154 });
155 let idx = pack.index();
156 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
157 let blue: Vec<_> = lines.iter().filter(|l| l.color == 0x0000FF).collect();
158 assert_eq!(blue.len(), 1, "one blue line, not two: {lines:?}");
159 }
160
161 #[test]
162 fn same_route_same_direction_patterns_draw_once() {
163 // A second pattern of the same route in the same direction, with a
164 // wider eastward detour (a per-pattern shape variant): the same
165 // route-direction, so only one geometry may draw.
166 let mut pack = shaped_route_pack();
167 pack.routes.push(Route {
168 name: "N",
169 color: 0x0000FF,
170 route_type: 0,
171 feed: 0,
172 pattern: vec![0, 1, 2],
173 trips: vec![Trip {
174 start: 36_000,
175 stoptimes: vec![(36_000, 36_000), (36_300, 36_360), (36_600, 36_600)],
176 service: 0,
177 headsign: "Downtown",
178 }],
179 shape: Some(Shape {
180 points: vec![
181 (37.700, -122.400),
182 (37.705, -122.380),
183 (37.710, -122.400),
184 (37.715, -122.380),
185 (37.720, -122.400),
186 ],
187 stop_vertices: vec![0, 2, 4],
188 }),
189 });
190 // And the reverse direction on its own alignment 200 m north: a
191 // different direction, so it keeps its own path.
192 pack.routes.push(Route {
193 name: "N",
194 color: 0x0000FF,
195 route_type: 0,
196 feed: 0,
197 pattern: vec![2, 1, 0],
198 trips: vec![Trip {
199 start: 39_600,
200 stoptimes: vec![(39_600, 39_600), (39_900, 39_960), (40_200, 40_200)],
201 service: 0,
202 headsign: "Outbound",
203 }],
204 shape: Some(Shape {
205 points: vec![
206 (37.722, -122.400),
207 (37.717, -122.390),
208 (37.712, -122.400),
209 (37.707, -122.390),
210 (37.702, -122.400),
211 ],
212 stop_vertices: vec![0, 2, 4],
213 }),
214 });
215 let idx = pack.index();
216 let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39);
217 let blue: Vec<_> = lines.iter().filter(|l| l.color == 0x0000FF).collect();
218 assert_eq!(
219 blue.len(),
220 2,
221 "one path per route+direction, not per pattern: {:?}",
222 lines.iter().map(|l| (l.name.as_str(), l.coords.len())).collect::<Vec<_>>()
223 );
224 }
225
226 #[test]
227 fn stop_lines_name_only_serving_routes_and_keep_buses() {
228 // The shaped tram plus a crosstown line through the same stops and a
229 // bus serving the middle stop: tapping that stop draws all three
230 // (buses included), tapping far away draws nothing.
231 let mut pack = shaped_route_pack();
232 pack.routes.push(Route {
233 name: "Crosstown",
234 color: 0x00FF00,
235 route_type: 1,
236 feed: 0,
237 pattern: vec![0, 1, 2],
238 trips: vec![Trip {
239 start: 28_800,
240 stoptimes: vec![(28_800, 28_800), (29_100, 29_160), (29_400, 29_400)],
241 service: 0,
242 headsign: "East",
243 }],
244 shape: None,
245 });
246 pack.routes.push(Route {
247 name: "38",
248 color: 0xFF8800,
249 route_type: 3,
250 feed: 0,
251 pattern: vec![1, 2],
252 trips: vec![Trip {
253 start: 28_800,
254 stoptimes: vec![(28_800, 28_800), (29_400, 29_400)],
255 service: 0,
256 headsign: "Downtown",
257 }],
258 shape: None,
259 });
260 let idx = pack.index();
261 // Beta (37.710) sits on all three patterns (shared platforms). Assert
262 // on the route-name set, not the span count: corridor fanning may cut
263 // one route into several spans.
264 let lines = routes_for_stop(&idx, 37.710, -122.400);
265 let names: std::collections::HashSet<&str> =
266 lines.iter().map(|l| l.name.as_str()).collect();
267 assert_eq!(
268 names,
269 std::collections::HashSet::from(["N", "Crosstown", "38"]),
270 "every route serving Beta draws: {lines:?}"
271 );
272 assert!(
273 lines.iter().any(|l| l.name == "38"),
274 "the selected-stop case keeps buses: {lines:?}"
275 );
276 // Far from every stop: nothing to draw.
277 let far = routes_for_stop(&idx, 40.0, -75.0);
278 assert!(far.is_empty(), "no stop near: nothing to draw");
279 }
280