def get_route_from_stop_time( stop_time ): """Query the route that contains a given stop_time value. This does a stop_times JOIN trips JOIN routes query. This is ill-constrained, since it can provide lots of routes all over the transit system. :param stop_time: a :class:`Stop_Time` instance. :return: a :class:`Route` instance. """ Route_Definition.set_db( settings.db ) return list( Route_Definition.view( 'service/stop_time', key_start= stop_time ) )
def get_route( id=None ): """Returns route or list of routes. If ``id`` is None, it returns all route definitions. Otherwise, it returns the requested route definition. :param id: Optional route id. If no route id, all routes are returned. :return: route definition or list of route definitions. """ Route_Definition.set_db( settings.db ) if id: return list( Route_Definition.view( 'service/route', key=id ) ) else: return list(Route_Definition.view( 'service/route' ))
def get_route_stops( id, dir=None, date=None ): """Returns a route with a list of stops on that route. The route ID is required. Either a direction or a date must be given to filter the stops along the route. If direction is given, All stops in a particular direction along the route. The direction is more-or-less inoound or outbound, and is actually a foreign key to a direction table. If date is given, All stops along the route filtered by services available on the given date. Day of week is generally sufficient, but there are calendar overrides, so full date is required. :param id: route id. :param dir: optional direction, codes are '1' and '0'. :param date: a datetime.date object; the services available on this date are used to filter the results. :returns: a :class:`Route_Definition` instance. """ Route_Definition.set_db( settings.db ) route= list( Route_Definition.view('service/route',key=id) )[0] if date: services= set(get_services_today( date )) for s in route.trips: if s not in services: del route.trips[s] if dir: for svc in route.trips: for trip in route.trips[svc]: if route.trips[svc][trip]['direction_id'] != dir: del route.trips[svc][trip] return route
def _load_routes(self, rdr): """Fetch the routes table, creating :class:`Route_Definition` objects.""" Route_Definition.set_db( settings.db ) for row in rdr: for r in Route_Definition.view( 'service/route', key=row['route_id'] ): r.delete() route= Route_Definition(**row) route.save()
def get_next_stop_time( stop, time, services ): """Given a :class:`Stop_Definition` object, a time after midnight, and specific classes of service, locate the remaining sequence of stops. :param stop: A :class:`Stop_Definition` object :param time: A seconds-after-midnight arrival time to use as a filter :param services: The iterable sequence of services applicable :returns: sequence of dictionaries with stop time information. """ for s in services: if s in stop.trips: for trip in stop.trips[s]: route_view= Route_Definition.view('service/route',key=stop.trips[s][trip]['route']) for route in route_view: for trip in route.trips[s]: for t in route.trips[s][trip]['stops']: if t['arrival_time'] >= time: yield t
def _load_stop_times(self, rdr): """Fetch the stop_times table, :class:`Route_Definition` objects with times. Must be done last. Requires the self.trip and self.trip_by_route information. """ for row in rdr: try: row['arrival_time']= self.time_parse(row['arrival_time']) row['departure_time']= self.time_parse(row['departure_time']) except ValueError: raise GTFException( "Bad Time {0!r}".format(row) ) trip= self.trip[row['trip_id']] docs= list( Route_Definition.view( 'service/route', key=trip['route_id'] ) ) route= docs[0] if trip['service_id'] not in route.trips: route.trips[ trip['service_id'] ]= {} if trip['trip_id'] not in route.trips[trip['service_id']]: route.trips[ trip['service_id'] ][trip['trip_id']] = dict( direction_id= trip['direction_id'], block_id= trip['block_id'], stops= [], ) route_trip= route.trips[ trip['service_id'] ][trip['trip_id']] route_trip['stops'].append( row ) route.save() docs = list( Stop_Definition.view( 'service/stop', key=row['stop_id'])) stop= docs[0] if trip['service_id'] not in stop.trips: stop.trips[ trip['service_id'] ]= {} if trip['trip_id'] not in stop.trips[trip['service_id']]: stop.trips[ trip['service_id'] ][trip['trip_id']] = dict( direction_id= trip['direction_id'], block_id= trip['block_id'], route= trip['route_id'], times= [], ) stop_trip= stop.trips[ trip['service_id'] ][trip['trip_id']] stop_trip['times'].append( row ) stop.save()