GTFS RT Newbie needs help with python and protobuf

2,571 views
Skip to first unread message

Mike Bonkowski

unread,
May 17, 2018, 4:06:36 AM5/17/18
to GTFS-realtime
I try to generate a GTFS RT feed with the original .proto File from Google

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

At first I tried to do 

feed_header = gtfs_realtime_pb2.FeedHeader()

but if i try to serilized it to file it will raise an error FeedMeesage has no Header.

So I try this one

feed_header = feed.FeedHeader()

but I get an error "FeedMessage has no attribute "FeedHeader"

BTW: Incrementality is read only, but in .proto definitions it is optional?

Can somebody help me?

Regards Mike


Jacob Whitbeck

unread,
May 29, 2018, 6:11:17 PM5/29/18
to GTFS-realtime
Here is a script that allows me to read a .pb file:
from google.transit import gtfs_realtime_pb2
import urllib

feed = gtfs_realtime_pb2.FeedMessage()
response = urllib.request.urlopen('URLGOESHERE')
feed.ParseFromString(response.read())
for entity in feed.entity:
if entity.HasField('trip_update'):
print(entity.trip_update)
Message has been deleted

Stefan de Konink

unread,
Jul 13, 2018, 6:16:56 PM7/13/18
to gtfs-r...@googlegroups.com
On vrijdag 13 juli 2018 17:20:23 CEST, Nikhil VJ wrote:
> I'd also like to know if someone has tried already, which
> coding language other than python is making it easier.

I can recommend doing it in Java. If you search on this list, I posted an
example how to generate it from the Java bindings.

--
Stefan

Sean Barbeau

unread,
Jul 19, 2018, 10:12:49 AM7/19/18
to GTFS-realtime
There are a number of example projects producing GTFS-realtime feeds listed here:
...and converters from other sources to GTFS-realtime here:

On Friday, July 13, 2018 at 11:20:23 AM UTC-4, Nikhil VJ wrote:
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

Nikhil VJ

unread,
Oct 25, 2018, 10:42:55 PM10/25/18
to GTFS-realtime
Hi,
I had earlier posted on this thread about python2 / 3. That was my mistake; apologies. I later found out how I should work in a virtual environment of python instead of trying to install in my main system.

I worked out how to read, and write .pb files in python. Sharing two sample programs here.

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


Reply all
Reply to author
Forward
0 new messages