Skip to content

Commit

Permalink
added a couple of query routines on StopTimes which seem like they ma…
Browse files Browse the repository at this point in the history
…ke sense to add to gtfsdb
  • Loading branch information
fpurcell committed May 11, 2014
1 parent b06d609 commit de59a87
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions gtfsdb/model/stop_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StopTime(Base):

trip_id = Column(String(255), primary_key=True, index=True, nullable=False)
arrival_time = Column(String(8))
departure_time = Column(String(8))
departure_time = Column(String(8), index=True)
stop_id = Column(String(255), index=True, nullable=False)
stop_sequence = Column(Integer, primary_key=True, nullable=False)
stop_headsign = Column(String(255))
Expand All @@ -42,6 +42,14 @@ def __init__(self, *args, **kwargs):
self.timepoint = 'arrival_time' in kwargs


def get_headsign(self):
''' get the headsign at this stop ... rule is that if stop is empty, use trip headsign '''
ret_val = self.stop_headsign
if not ret_val:
ret_val = self.trip.trip_headsign
return ret_val


@classmethod
def post_process(cls, db, **kwargs):
''' delete all 'depature_time' values that appear for the last stop
Expand All @@ -66,6 +74,27 @@ def post_process(cls, db, **kwargs):
for r in q:
r.arrival_time = None


db.session.commit()

@classmethod
def get_stop_schedule(cls, session, stop_id, date, route_id=None):
''' helper routine which returns the stop schedule for a give date
'''
from gtfsdb.model.trip import Trip

# step 1: get stop times based on date
q = session.query(StopTime)
q = q.filter_by(stop_id=stop_id)
q = q.filter(StopTime.departure_time!=None)
q = q.filter(StopTime.trip.has(Trip.universal_calendar.any(date=date)))

# step 2: apply an optional route filter
if route_id:
q = q.filter(StopTime.trip.has(Trip.route_id == route_id))

# step 3: order the stop times
q = q.order_by(StopTime.departure_time)

ret_val = q.all()
return ret_val

0 comments on commit de59a87

Please sign in to comment.