GTFS Real Time Data: How to Convert Feed Message Print to Terminal to GTFS

时间:2017-04-10 00:57:17

标签: python gtfs

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!

1 个答案:

答案 0 :(得分:2)

具体答案取决于您要对Feed执行的操作。但总的来说,你有两种选择。

第一个选项是直接解析FeedMessage对象。一般的Python protobuf引用here对您有用,GTFS-rt的特定协议是here。例如,在解析消息后,您可以获得Feed中所有行程的车辆ID和行程ID,如下所示:

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)

但它完全取决于你想从Feed获得什么。如果您对延迟感兴趣,您可能会解析StopTimeUpdate对象(但请注意,这些对象可以包含过去/当前实际延迟,如果车辆已经停止到达/离开,或者未来预计的延迟以后停止路线)。

如果你更喜欢直接使用dicts而不是protobuf消息,我已经成功完成了项目protobuf-to-dict(参见here),可以通过pip安装。我一般认为在Python中使用dic比使用protobuf对象更自然。