示例#1
0
def db_connect_tester():
    ''' simple routine to connect to an existing database and list a few stops
        bin/connect-tester --database_url sqlite:///gtfs.db _no_gtfs_zip_needed_
    '''
    from gtfsdb import Database, Stop, Route, StopTime
    args, kwargs = get_args()
    db = Database(**kwargs)
    for s in db.session.query(Stop).limit(2):
        print s.stop_name
    for r in db.session.query(Route).limit(2):
        print r.route_name
    #import pdb; pdb.set_trace()
    stop_times = StopTime.get_departure_schedule(db.session, stop_id='11411')
    for st in stop_times:
        print st.get_direction_name()
        break
示例#2
0
def db_connect_tester():
    """ simple routine to connect to an existing database and list a few stops
        bin/connect-tester --database_url sqlite:///gtfs.db _no_gtfs_zip_needed_
    """
    from gtfsdb import Database, Stop, Route, StopTime
    args, kwargs = get_args()
    db = Database(**kwargs)
    for s in db.session.query(Stop).limit(2):
        print s.stop_name
    for r in db.session.query(Route).limit(2):
        print r.route_name
    #import pdb; pdb.set_trace()
    stop_times = StopTime.get_departure_schedule(db.session, stop_id='11411')
    for st in stop_times:
        print st.get_direction_name()
        break
    def get_stop_schedule(cls, session, stop_id, date=None, route_id=None, agency="TODO", detailed=False, show_alerts=False):
        """ factory returns full-on schedule DAO for this stop, on this date.  detailed flag gets all meta-data, whereas
            show_alerts reduces the queries down to just alerts for this stop (and routes hitting the stop).
        """
        ret_val = None

        headsigns = {}
        schedule = []
        alerts = []

        # step 1: figure out date and time
        now = datetime.datetime.now()
        if date is None:
            date = now

        # step 2: get the stop
        stop = StopDao.from_stop_id(session=session, stop_id=stop_id, agency=agency, detailed=detailed, show_alerts=show_alerts, date=date)

        # step 3: get the stop schedule if the first query with route_id doesn't return anything, lets try again w/out a route
        stop_times = []
        if stop:
            if route_id and stop.find_route(route_id):
                # step 3a: filter the schedule by a valid route_id (e.g., if route_id is scheduled for this stop at some point)
                stop_times = StopTime.get_departure_schedule(session, stop_id, date, route_id)
            else:
                stop_times = StopTime.get_departure_schedule(session, stop_id, date)

        # step 4: loop through our queried stop times
        for i, st in enumerate(stop_times):
            if st.is_boarding_stop(): # IMPORTANT todo: is this where we're broken on some stops ???

                # 4b: only once, capture the route's different headsigns shown at this stop
                #     (e.g., a given route can have multiple headsignss show at this stop)
                id = StopHeadsignDao.unique_id(st)
                if not headsigns.has_key(id):
                    try:
                        # make a new headsign
                        h = StopHeadsignDao(st)
                        h.sort_order += len(headsigns)
                        h.id = id
                        headsigns[id] = h

                        # check to see if we have an alert for this headsign
                        r = stop.find_route(h.route_id)
                        if r and r.alerts and len(r.alerts) > 0:
                            h.has_alerts = True

                            # add the route to an array
                            if h.route_id not in alerts:
                                alerts.append(h.route_id)
                    except:
                        log.info("get_stop_schedule: we saw some strange headsing stuff")

                # 4c: add new stoptime to headsign cache
                if id in headsigns:
                    time = cls.make_stop_time(st, id, now, i+1)
                    schedule.append(time)
                    headsigns[id].last_time = st.departure_time
                    headsigns[id].num_trips += 1

        # step 5: build the DAO object (assuming there was a valid stop / schedule based on the query)
        ret_val = StopScheduleDao(stop, schedule, headsigns, alerts, route_id)

        return ret_val