我有一个带有GTFS数据的MySQL数据库设置,我有返回停止时间的php脚本,我希望添加实时数据。
我知道Google有关于如何在php (https://github.com/google/gtfs-realtime-bindings-php)中实时使用gtfs的说明,但我似乎不知道如何让它工作。
我想做的是(使用任何需要的信息: trip_id、stop_id等)返回trip_update延迟值,以便相应地更新停止时间。
有人知道一个好的教程吗?或者有人能帮我弄清楚该怎么做?
发布于 2015-05-04 04:37:18
一个更完整的GTFS-实时PHP示例可能如下所示:
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());
}
}
}
}请注意方法名称如何与底层GTFS-实时模式中的字段相对应:
https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto
您可以在以下位置看到从模式生成的PHP源代码:
https://github.com/google/gtfs-realtime-bindings-php/blob/master/src/gtfs-realtime.php
发布于 2015-05-03 04:50:35
在我看来,您似乎不需要直接处理Protobuf文件,因为您已经拥有数据库中的数据。所以其他人已经负责阅读信息了。
GTFS的数据结构是Protobuf协议,您需要确切地知道它是如何工作的。这里概述了读取Protobuf数据的典型过程。这些说明不适用于PHP,但它为您提供了流程的总体概念。
protoc命令为您的GTFS实时数据生成协议缓冲API。命令行只支持C++、Java和Python。据我所知,您的连系仅适用于GTFS--实时数据,因此已经创建了协议缓冲API。唯一需要做的就是使用页面中提供的代码片段:
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());
}
}结论:
实时数据是Protobuf协议中处理传输信息的特殊数据。您可以创建Protobuf来读取Protobuf文件中的数据。但是,如果数据库中已经有了它们,那么就没有什么可做的了。
https://stackoverflow.com/questions/30008632
复制相似问题