Parsing GTFS format transit data in real time with Python

Get yourself an API key

Install Google’s GTFS Python library

pip install --upgrade gtfs-realtime-bindings
python -m google.transit.gtfs_realtime_pb2

Install requests if you haven’t already

pip install --upgrade requests
python
>>> import requests
>>> requests.__version__
'2.21.0'

Building a barebones data feed

1. Initialize an instance of FeedMessage

feed = gtfs_realtime_pb2.FeedMessage()

2. Get the response from the API

response = requests.get(<URL OF YOUR GTFS SOURCE>, allow_redirects=True)

3. Pass the response to the parser

feed.ParseFromString(response.content)
>>> len(feed.entity)
373
>>> feed.entity[0]
id: "000001"
trip_update {
trip {
trip_id: "057150_1..N03R"
start_date: "20190422"
route_id: "1"
}
stop_time_update {
arrival {
time: 1555943380
}
stop_id: "101N"
}
}

Use trip_update

for entity in feed.entity:
if entity.HasField('trip_update'):
# Do something
>>> len(feed.entity)
373
>>> sum([1 for ent in feed.entity if ent.HasField('trip_update')])
218
>>> sum([1 for ent in feed.entity if not ent.HasField('trip_update')])
155

Put it all together

Read more

Top writer in Technology | Backend Web Developer | bennettgarner.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store