import time
import gtfs_realtime_pb2
feed = gtfs_realtime_pb2.FeedMessage()
feed_header = feed.FeedHeader()
#feed_header = gtfs_realtime_pb2.FeedHeader()
feed_header.gtfs_realtime_version = str(2.0)
feed_header.timestamp = int(time.time())
#feed_header.Incrementality = 1
feed_header = gtfs_realtime_pb2.FeedHeader()feed_header = feed.FeedHeader() Hi, pls note, this works on python2 and not on python3.We need python3 compatibility soon as python2 is being phased out. I found i could install the package as advised using pip3, but the same import statement didnt work. And on searching i came across postings of problems with python3 (but couldnt find how they managed to import it to be able to have those errors!)
The gtfs bindings python page has the snippet about reading, but doesnt say anything about creating .pb files. If i find out the proper python commands, i'll post them here and post issue/PR on github too to update the documentation.
I'd also like to know if someone has tried already, which coding language other than python is making it easier.
Nikhil VJ
Pune, India
First things first: install the gtfs-realtime-bindings package. And make sure you're on a virtual environment of python3. I had got some problems trying to run it in my main system, because there were multiple versions of python3 and the package got installed in one and not the other. Always best to work in virtual environment in python, esp when you're on a linux system.
Next, clear some concepts: you don't need to do protocol buffer work, simply start reading / writing .pb files. Please read this earlier post: https://groups.google.com/d/msg/gtfs-realtime/JNOj60WA1OY/9f1ElL2yBQAJ
Here are two sample programs I've made in python. I worked with a VehiclePositions .pb file from MBTA.
Reading a vehicle positions file:
https://nbviewer.jupyter.org/url/nikhilvj.co.in/files/gtfsrt/locations.ipynb
Writing a vehicle positions file:
https://nbviewer.jupyter.org/url/nikhilvj.co.in/files/gtfsrt/write1.ipynb
I had later validated the output of the latter and it was a valid .pb file
Hope this helps.
Nikhil VJ, Pune, India