Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read GTFS protocol buffer in PHP?

I have a GTFS protocol buffer message (VehiclePosition.pb), and the corresponding protocol format (gtfs-realtime.proto), I would like to read the message in PHP alone (is that even possible?).

I looked at Google's python tutorial https://developers.google.com/protocol-buffers/docs/pythontutorial and encoding documentation https://developers.google.com/protocol-buffers/docs/encoding and https://github.com/maxious/ACTBus-ui/tree/master/lib/Protobuf-PHP, but I am having a really hard time conceptualizing what is going on. I think I understand that gtfs-realtime.php is a compiled instruction set of the encoding defined in gtfs-realtime.proto (please correct me if I am wrong), but I have no clue how to get it to decode VehiclePosition.pb. Also, what are the dependencies of gtfs-realtime.php (or the python equivalent for that matter)? Is there anything else I have to compile myself or anything that is not a simple php script if all I want to do is read VehiclePosition.pb?

Thanks.

like image 668
SkyNT Avatar asked Sep 14 '12 21:09

SkyNT


2 Answers

edmonscommerce and Julian are on the right track.

However, I've gone down the same path and I've found that the PHP implementation of Protocol Buffers is cumbersome (especially in the case of NYCT's MTA feed).


Alternative Method (Command Line + JSON):

If you're comfortable with command line tools and JSON, I wrote a standalone tool that converts GTFS-realtime into simple JSON: https://github.com/harrytruong/gtfs_realtime_json

Just download (no install), and run: gtfs_realtime_json <feed_url>

Here's a sample JSON output.

To use this in PHP, just put gtfs_realtime_json in the same directory as your scripts, and run the following:

<?php 

$json = exec('./gtfs_realtime_json "http://developer.mbta.com/lib/GTRTFS/Alerts/VehiclePositions.pb"');
$feed = json_decode($json, TRUE);

var_dump($feed);
like image 148
Harry Truong Avatar answered Oct 26 '22 13:10

Harry Truong


You can use the official tool: https://developers.google.com/transit/gtfs-realtime/code-samples#php

It was released very recently. I've been using it for a few days and works like a charm.

like image 36
Julian Avatar answered Oct 26 '22 13:10

Julian