1

I have followed the example from this link

Parsing NYC Transit/MTA historical GTFS data (not realtime)

which grabs a historical GTFS real time data feed and then use python language bindings to output a gtfs_rt.FeedMessage to the terminal.

After I printed it out to the terminal, I am quite clueless to what to do next. I have been trying to find a way to read the terminal output and then, hopefully convert the feed message to JSON format? Ideally, I would want it to be comparable to GTFS data format of the static data so that I can calculate delay times etc.

Thanks!

Community
  • 1
  • 1
Zarni
  • 109
  • 6

1 Answers1

3

The specific answer depends on what you want to do with the feed. But in general, you have two options.

The first option is to parse the FeedMessage object directly. The general Python protobuf reference here will be useful to you, and the specific protocol for GTFS-rt is here. For example, after parsing the message, you could get the vehicle IDs and trip IDs of all trips in the feed like so:

for feed_entity in msg.entity:
    if feed_entity.hasField('trip_update'):
        tu = feed_entity.trip_update
        vid = tu.vehicle.vehicle.id
        tid = tu.trip.trip_id
        print "vehicle {} is covering trip {}".format(vid, tid)

But it will depend entirely on what you want to get from the feed. If you are interested in delays, you will probably be parsing the StopTimeUpdate objects (but note that these objects can contain both past/current actual delays, if the vehicle has already arrived/departed at a stop, or future projected delays for later stops on the route).

If you prefer to work with dicts rather than the protobuf messages directly, I have had success with the project protobuf-to-dict (see here), which can be installed through pip. I generally find working with dicts much more natural in Python than protobuf objects.

abeboparebop
  • 7,396
  • 6
  • 37
  • 46