我正在试着看纽约市地铁实时GTFS馈送。在阅读了很多之后,我了解了协议缓冲器并安装了protoc编译器。
纽约市公共交通有文件nyct-subway.proto.txt,第一行说,纽约地铁扩展了GTFS-实时协议。这应该与gtfs-实时-proto相结合吗?我分别编译了两个协议缓冲区,并得到了警告:
[libprotobuf WARNING google/protobuf/compiler/parser.cc:471]
No syntax specified for the proto file.
Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)在Python中,写了一行来调用protoc创建的库:
import gtfs_realtime_pb2, nyct_subway_pb2尽管我以前做过安装工作,但是Python对import google.protobuf一无所知,所以它做了sudo pip install protobuf。
此时,我仍然没有读取任何数据--我可以使用http://datamine.mta.info/mta_esi.php?key=<key>&feed_id=1获得一个gtfs文件,这是不可读的。
如何结合起来从GTFS文件读取数据?
发布于 2015-02-25 07:20:25
为了进一步澄清Jamie的评论,您应该可以这样做:
import urllib2
import gtfs_realtime_pb2, nyct_subway_pb2
...
// initialize the feed parser
feed = gtfs_realtime_pb2.FeedMessage()
// fetch the raw gtfs-realtime feed
gtfs_raw = urllib2.urlopen("http://datamine.mta.info/mta_esi.php?key=<key>&feed_id=1").read()
// parse the raw feed
feed.ParseFromString(gtfs_raw)
// access the data structure as needed
print feed.header.timestamp
print feed.header.gtfs_realtime_version
for entity in feed.entity:
// etc.交替方法(命令行+ JSON)
就我个人而言,我认为协议缓冲区和gtfs-实时可能是一个痛苦。为了跳过这项工作,我编写了一个独立的工具来将GTFS-实时转换为JSON:
只需下载(没有安装),并运行:gtfs_realtime_json <feed_url>
这是一个示例JSON输出。
发布于 2016-10-26 02:14:15
您可以使用protobuf包将两者结合在一起。下载两个.proto文件并将其放入docs/gtfs_proto中,为输出创建一个gtfs_proto文件夹,然后运行:
export SRC_DIR=docs/gtfs_proto
export DST_DIR=gtfs_proto
protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/nyct-subway.protohttps://stackoverflow.com/questions/27531172
复制相似问题