trafiklab/gtfs-php-sdk

在您的PHP项目中使用GTFS和GTFS-realtime数据

dev-master 2023-08-18 17:50 UTC

This package is auto-updated.

Last update: 2024-09-18 19:59:31 UTC


README

Build status Latest Stable Version codecov License: MPL 2.0

此SDK使开发者更容易在PHP项目中使用GTFS数据。目前,仅支持静态文件。

安装

您可以通过composer安装此包

composer require trafiklab/gtfs-php-sdk

使用方法和示例

打开GTFS文件

您可以加载本地的GTFS zip文件,或者通过HTTP下载它

$gtfsArchive = GtfsArchive::createFromPath("gtfs.zip");
$gtfsArchive = GtfsArchive::createFromUrl("http://example.com/gtfs.zip");

可选地,您可以选择仅在文件自上次检索后已更改时下载GTFS zip文件。这在尝试自动化需要在数据库中存储的GTFS检索时非常有用,无需每次都重写相同的数据。以下Http Headers被使用

大多数机构都提供Last-Modified头信息,但如果它不存在,ETag是最佳选择。如果出于某种原因ETag也没有提供,它将正常继续,就像您使用原始的GtfsArchive::createFromUrl()方法一样。

 $gtfsArchive = GtfsArchive::createFromUrlIfModified(
    "http://example.com/gtfs.zip",
    "Wed, 10 Jun 2020 15:56:14 GMT",
    "99fa-5a7bce236c526"
 );

如果您一开始就没有ETag或Last-Modified值,只需简单地省略它们,方法将为您检索它们。

 if ($gtfsArchive = GtfsArchive::createFromUrlIfModified("http://example.com/gtfs.zip") {
    // Get Methods return null if GTFS Url does not contain the specified Header: ETag, Last-Modified.
    $lastModified = $gtfsArchive->getLastModified(); // Wed, 10 Jun 2020 15:56:14 GMT | null
    $eTag         = $gtfsArchive->getETag(); // "99fa-5a7bce236c526" | null
    
    // You can get the Last-Modified datetime PHP Object (Useful for storing in databases) by doing the following:
    $datetime = $gtfsArchive->getLastModifiedDateTime() // DateTime Object | null
    
    // Or Convert it back to a String using the standard Last-Modified HTTP header format.
    if ($datetime) {
      $lastModified = GtfsArchive::getLastModifiedFromDateTime($datetime); // Wed, 10 Jun 2020 15:56:14 GMT
    }  
 }
 

文件被提取到临时目录(/tmp/gtfs/)中,当GtfsArchive对象被销毁时将清理。您可以通过调用$gtfsArchive->deleteUncompressedFiles()来手动删除未压缩的文件。

读取文件

$agencyTxt = $gtfsArchive->getAgencyFile(); // read agency.txt
$calendarTxt = $gtfsArchive->getCalendarFile(); // read calendar.txt
$routesTxt = $gtfsArchive->getRoutesFile(); // read routes.txt
$stopTimesTxt = $gtfsArchive->getStopTimesFile(); // read stop_times.txt
...

所有文件都是延迟加载并缓存的。这意味着只有在调用如getStopTimesFile()之类的方法后才会加载数据。请注意,第一次读取数据可能需要一些时间。读取大型stop_times.txt文件可能需要长达一分钟。

读取文件数据

每个文件类都包含一个读取该文件中所有数据的方法。一些类还包含用于常用查询(如按id或外键过滤)的辅助方法。

对于每个(受支持的)文件都有一个PHP类,还有一个类用于该文件中的一行数据。每个getter函数的PHPDoc包含每个字段的定义,这样您就可以专注于编码,而不是在规范和代码之间切换。

$stopTimesTxt = $gtfsArchive->getStopTimesFile(); // The file is represented by a StopTimesFile object
$allStopTimes = $stopTimesTxt->getStopTimes(); // a method like this is always available
$stopTimesForStopX = $stopTimesTxt->getStopTimesForStop($stopId); // this is a helper method for foreign keys

$stopTime = $allStopTimes[0]; // Get the first row
$headsign = $stopTime->getStopHeadsign(); // One row of data is represented by a StopTime object

贡献

我们接受pull请求,但请首先创建一个issue来讨论添加或修复。如果您想看到添加新功能,也可以通过创建issue来创建功能请求。

帮助

如果您遇到问题,请通过Issue tracker寻求帮助。