1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};

use anyhow::Result;

use abstutil::Timer;
use geom::{Distance, Duration, FindClosest, HashablePt2D, Time};

use crate::make::match_points_to_lanes;
use crate::raw::{RawBusRoute, RawBusStop};
use crate::{
    BusRoute, BusRouteID, BusStop, BusStopID, LaneID, LaneType, Map, PathConstraints, Position,
};

/// Construct the final model of bus/train stops and routes. This is quite broken currently, so not
/// going to describe how it works.
pub fn make_stops_and_routes(map: &mut Map, raw_routes: &[RawBusRoute], timer: &mut Timer) {
    timer.start("make transit stops and routes");
    let matcher = Matcher::new(raw_routes, map, timer);

    // TODO I'm assuming the vehicle_pos <-> driving_pos relation is one-to-one...
    let mut pt_to_stop: BTreeMap<(Position, Position), BusStopID> = BTreeMap::new();
    for r in raw_routes {
        if let Err(err) = make_route(map, r, &mut pt_to_stop, &matcher) {
            warn!("Skipping route {} ({}): {}", r.full_name, r.osm_rel_id, err);
        }
    }

    // Remove orphaned bus stops. This messes up the BusStopID indexing.
    for id in map
        .bus_stops
        .keys()
        .filter(|id| map.get_routes_serving_stop(**id).is_empty())
        .cloned()
        .collect::<Vec<_>>()
    {
        map.bus_stops.remove(&id);
        map.lanes
            .get_mut(&id.sidewalk)
            .unwrap()
            .bus_stops
            .remove(&id);
    }

    timer.stop("make transit stops and routes");
}

fn make_route(
    map: &mut Map,
    r: &RawBusRoute,
    pt_to_stop: &mut BTreeMap<(Position, Position), BusStopID>,
    matcher: &Matcher,
) -> Result<()> {
    let route_type = if r.is_bus {
        PathConstraints::Bus
    } else {
        PathConstraints::Train
    };

    let mut stops = Vec::new();
    for stop in &r.stops {
        match matcher.lookup(route_type, stop, map) {
            Ok((sidewalk_pos, driving_pos)) => {
                // Create a new bus stop if needed.
                let stop_id = if let Some(id) = pt_to_stop.get(&(sidewalk_pos, driving_pos)) {
                    *id
                } else {
                    let id = BusStopID {
                        sidewalk: sidewalk_pos.lane(),
                        idx: map.get_l(sidewalk_pos.lane()).bus_stops.len(),
                    };
                    pt_to_stop.insert((sidewalk_pos, driving_pos), id);
                    map.lanes
                        .get_mut(&sidewalk_pos.lane())
                        .unwrap()
                        .bus_stops
                        .insert(id);
                    map.bus_stops.insert(
                        id,
                        BusStop {
                            id,
                            name: stop.name.clone(),
                            driving_pos,
                            sidewalk_pos,
                            is_train_stop: !r.is_bus,
                        },
                    );
                    id
                };
                stops.push(stop_id);
            }
            Err(err) => {
                bail!("couldn't match stop {}: {}", stop.name, err);
            }
        }
    }

    // Start or end at a border?
    let mut end_border = None;
    let start = if let Some(i) = r.border_start {
        let i = map.get_i(map.find_i_by_osm_id(i).unwrap());
        if !i.is_border() {
            panic!("Route starts at {}, but isn't a border?", i.orig_id);
        }
        if let Some(l) = i.get_outgoing_lanes(map, route_type).get(0) {
            *l
        } else {
            bail!(
                "Route {} starts at {} ({}), but no starting lane for a {:?}?",
                r.osm_rel_id,
                i.id,
                i.orig_id,
                route_type
            );
        }
    } else {
        // Not starting at a border. Find a lane at or before the first stop that's at least 13m.
        pick_start_lane(map.get_bs(stops[0]).driving_pos, route_type, map)?
    };
    if let Some(i) = r.border_end {
        let i = map.get_i(map.find_i_by_osm_id(i).unwrap());
        if !i.is_border() {
            panic!("Route ends at {}, but isn't a border?", i.orig_id);
        }
        // If the last stop is on a lane leading to the border, don't try to lane-change last
        // minute
        let last_stop_l = map.get_bs(*stops.last().unwrap()).driving_pos.lane();
        if map.get_l(last_stop_l).dst_i == i.id {
            end_border = Some(last_stop_l);
        } else if let Some(l) = i.get_incoming_lanes(map, route_type).get(0) {
            end_border = Some(*l);
        } else {
            // TODO Should panic
            println!(
                "Route {} ends at {} ({}), but no ending lane for a {:?}?",
                r.osm_rel_id, i.id, i.orig_id, route_type
            );
        }
    }

    let route = BusRoute {
        id: BusRouteID(map.bus_routes.len()),
        full_name: r.full_name.clone(),
        short_name: r.short_name.clone(),
        osm_rel_id: r.osm_rel_id,
        gtfs_trip_marker: r.gtfs_trip_marker.clone(),
        stops,
        route_type,
        start,
        end_border,
        spawn_times: default_spawn_times(),
        orig_spawn_times: default_spawn_times(),
    };

    let mut debug_route = "All parts of the route:".to_string();
    debug_route = format!("{}\nStart at {}", debug_route, route.start);
    for (idx, bs) in route.stops.iter().enumerate() {
        let stop = map.get_bs(*bs);
        debug_route = format!(
            "{}\nStop {} ({}): {}",
            debug_route,
            idx + 1,
            stop.name,
            stop.driving_pos
        );
    }
    if let Some(l) = route.end_border {
        debug_route = format!("{}\nEnd at {}", debug_route, l);
    }

    // Make sure the route is connected
    for req in route.all_steps(map) {
        if req.start.lane() == req.end.lane() && req.start.dist_along() > req.end.dist_along() {
            bail!(
                "Two stops seemingly out of order somewhere on {}",
                map.get_parent(req.start.lane()).orig_id
            );
        }

        if let Err(err) = map.pathfind(req.clone()) {
            bail!(
                "No path between stop on {} and {}: {}. {}",
                map.get_parent(req.start.lane()).orig_id,
                map.get_parent(req.end.lane()).orig_id,
                err,
                debug_route
            );
        }
    }

    map.bus_routes.push(route);
    Ok(())
}

struct Matcher {
    // TODO Eventually, maybe also map to a station building too
    sidewalk_pts: HashMap<HashablePt2D, Position>,
    light_rail_pts: HashMap<HashablePt2D, Position>,
}

impl Matcher {
    fn new(routes: &[RawBusRoute], map: &Map, timer: &mut Timer) -> Matcher {
        // Match all of the points to an exact position along a lane.
        let mut lookup_sidewalk_pts = HashSet::new();
        let mut lookup_light_rail_pts = HashSet::new();
        for r in routes {
            for stop in &r.stops {
                if !r.is_bus {
                    lookup_light_rail_pts.insert(stop.vehicle_pos.1.to_hashable());
                }
                if let Some(pt) = stop.ped_pos {
                    lookup_sidewalk_pts.insert(pt.to_hashable());
                }
            }
        }
        let sidewalk_pts = match_points_to_lanes(
            map.get_bounds(),
            lookup_sidewalk_pts,
            map.all_lanes(),
            |l| l.is_walkable(),
            Distance::ZERO,
            // TODO Generous for cap hill light rail platform
            Distance::meters(50.0),
            timer,
        );
        let light_rail_pts = match_points_to_lanes(
            map.get_bounds(),
            lookup_light_rail_pts,
            map.all_lanes(),
            |l| l.lane_type == LaneType::LightRail,
            Distance::ZERO,
            Distance::meters(10.0),
            timer,
        );

        Matcher {
            sidewalk_pts,
            light_rail_pts,
        }
    }

    // returns (sidewalk, driving)
    fn lookup(
        &self,
        route_type: PathConstraints,
        stop: &RawBusStop,
        map: &Map,
    ) -> Result<(Position, Position)> {
        if route_type == PathConstraints::Train {
            // Light rail needs explicit platforms.
            let sidewalk_pt = stop
                .ped_pos
                .ok_or_else(|| anyhow!("light rail missing platform"))?;
            let sidewalk_pos = *self
                .sidewalk_pts
                .get(&sidewalk_pt.to_hashable())
                .ok_or_else(|| anyhow!("sidewalk for light rail didnt match: {}", sidewalk_pt))?;
            let driving_pos = *self
                .light_rail_pts
                .get(&stop.vehicle_pos.1.to_hashable())
                .ok_or_else(|| {
                    anyhow!("vehicle for light rail didnt match: {}", stop.vehicle_pos.0)
                })?;
            return Ok((sidewalk_pos, driving_pos));
        }

        // We already figured out what side of the road we're on
        let (r, dir) = stop.matched_road.unwrap();
        let r = map.get_r(map.find_r_by_osm_id(r)?);
        // Prefer the rightmost match. DON'T use find_closest_lane here; we only want one side of
        // the road.
        let l = map.get_l(
            r.children(dir)
                .iter()
                .rev()
                .find(|(l, _)| route_type.can_use(map.get_l(*l), map))
                .ok_or_else(|| anyhow!("{} {}, doesn't have a bus or driving lane", r.id, dir))?
                .0,
        );

        // Where exactly along this lane?
        // TODO This should just be a method in PolyLine
        let mut closest: FindClosest<()> = FindClosest::new(map.get_bounds());
        closest.add((), l.lane_center_pts.points());
        let (_, pt) = closest
            .closest_pt(stop.vehicle_pos.1, Distance::meters(10.0))
            .ok_or_else(|| anyhow!("{} isn't near {}", stop.vehicle_pos.0, l.id))?;
        let mut driving_pos = Position::new(l.id, l.dist_along_of_point(pt).unwrap());

        let sidewalk_pos = if let Some(pt) = stop.ped_pos {
            *self
                .sidewalk_pts
                .get(&pt.to_hashable())
                .ok_or_else(|| anyhow!("sidewalk didnt match"))?
        } else {
            let sidewalk = map
                .get_parent(driving_pos.lane())
                .find_closest_lane(
                    driving_pos.lane(),
                    |l| PathConstraints::Pedestrian.can_use(l, map),
                    map,
                )
                .ok_or_else(|| anyhow!("driving {} to sidewalk failed", driving_pos.lane()))?;
            driving_pos.equiv_pos(sidewalk, map)
        };

        // If we're a stop right at an incoming border, make sure to be at least past where the bus
        // will spawn from the border. pick_start_lane() can't do anything for borders.
        if map
            .get_i(map.get_l(driving_pos.lane()).src_i)
            .is_incoming_border()
        {
            if let Some(pos) = driving_pos.min_dist(Distance::meters(1.0), map) {
                driving_pos = pos;
            } else {
                bail!("too close to start of a border {}", driving_pos.lane());
            }
        }
        Ok((sidewalk_pos, driving_pos))
    }
}

fn pick_start_lane(
    first_stop: Position,
    constraints: PathConstraints,
    map: &Map,
) -> Result<LaneID> {
    let min_len = Distance::meters(13.0);
    if first_stop.dist_along() >= min_len {
        return Ok(first_stop.lane());
    }

    // Flood backwards until we find a long enough lane
    let mut queue = VecDeque::new();
    queue.push_back(first_stop.lane());
    while !queue.is_empty() {
        let current = queue.pop_front().unwrap();
        if current != first_stop.lane() && map.get_l(current).length() >= min_len {
            return Ok(current);
        }
        for t in map.get_turns_to_lane(current) {
            if constraints.can_use(map.get_l(t.id.src), map) {
                queue.push_back(t.id.src);
            }
        }
    }
    bail!(
        "couldn't find any lanes leading to {} that're long enough for a bus to spawn",
        first_stop.lane()
    )
}

fn default_spawn_times() -> Vec<Time> {
    // Hourly spawning from midnight to 7, then every 30 minutes till 7, then hourly again
    let mut times = Vec::new();
    for i in 0..24 {
        let hour = Time::START_OF_DAY + Duration::hours(i);
        times.push(hour);
        if (7..=19).contains(&i) {
            times.push(hour + Duration::minutes(30));
        }
    }
    times
}