4

I have a MySQL database setup with GTFS data and I have php scripts that return stop times and I'm looking to add realtime data.

I know Google has instructions for how to use gtfs realtime with php (https://github.com/google/gtfs-realtime-bindings-php) but I can't seem to figure out how to get it working.

What I want to do is (using whatever informations is needed: trip_id, stop_id, etc.) return the trip_update delayed value so that I can update the stop time accordingly.

Does anyone know of a good tutorial? Or can someone help me figure out how to do this?

Chris Schlitt
  • 561
  • 1
  • 4
  • 20

2 Answers2

3

A more complete GTFS-realtime PHP example might look like the following:

foreach ($feed->getEntityList() as $entity) {
  if ($entity->hasTripUpdate()) {
    $trip = $entity->getTripUpdate();
    error_log("trip id: " . $trip->getTrip()->getTripId());
    foreach ($trip->getStopTimeUpdateList() as $stu) {
      if ($stu->hasArrival()) {
        $ste = $stu->getArrival();
        error_log("    arrival delay: " . $ste->getDelay());
        error_log("    arrival time: " . $ste->getTime());
      }
      if ($stu->hasDeparture()) {
        $ste = $stu->getDeparture();
        error_log("    departure delay: " . $ste->getDelay());
        error_log("    departure time: " . $ste->getTime());
      }
    }
  }
}

Notice how the method names correspond to the fields in the underlying GTFS-realtime schema:

https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto

You can see the PHP source that was generated from the schema at:

https://github.com/google/gtfs-realtime-bindings-php/blob/master/src/gtfs-realtime.php

Brian Ferris
  • 7,557
  • 5
  • 25
  • 27
2

To me, it seems that you do not need to deal directly with Protobuf files because you already have the data in your database. So someone else already take care of reading the information.

The data structure of GTFS-realtime is Protobuf Protocol and you need to know exactly how it works. Here is an outline of the typical process of reading Protobuf data. These instructions do not work for PHP but it give you the general idea of the process.

  1. First you need to install Protobuf compiler. If you are using Mac OS X, read this.
  2. Use protoc command to generate the protocol buffer API for your GTFS-realtime data. The command line only supports C++, Java, and Python.
  3. You can use this API to read your data.

As far as I understand the repo that you linked to, is only for GTFS-realtime data, so the protocol buffer API is already created. The only thing that you need to do is to use the code snippet that provided in the page:

require_once 'vendor/autoload.php';

use transit_realtime\FeedMessage;

$data = file_get_contents("URL OF YOUR GTFS-REALTIME SOURCE GOES HERE");
$feed = new FeedMessage();
$feed->parse($data);
foreach ($feed->getEntityList() as $entity) {
  if ($entity->hasTripUpdate()) {
    error_log("trip: " . $entity->getId());
  }
}

Conclusion:

GTFS-realtime is special kind of data in Protobuf protocol that handle transit information. You can create a Protobuf API to read the data from the Protobuf files. But if you already have them in your database, there is not much left to do.

Community
  • 1
  • 1
bman
  • 5,016
  • 4
  • 36
  • 69
  • I was able to get this far and the script works. What I am having trouble is figuring out how to extract the trip update data. I've tried getTripUpdate() and may others but I can't seem to get it to work – Chris Schlitt May 03 '15 at 23:31