定制 trafiklab/gtfs-php-sdk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

trafiklab/gtfs-php-sdk

Composer 安装命令:

composer require trafiklab/gtfs-php-sdk

包简介

Use GTFS and GTFS-realtime data in your PHP projects

README 文档

README

Build status Latest Stable Version codecov License: MPL 2.0

This SDK makes it easier for developers to use GTFS data in their PHP projects. At this moment, only static files are supported.

Installation

You can install this package through composer

composer require trafiklab/gtfs-php-sdk

Usage and examples

Opening a GTFS file

You can either load a local GTFS zip file, or you can download it over HTTP

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

Optionally, you can choose to download a GTFS zip file only if it has changed since the last retrieval. This is useful when trying to automate GTFS retrievals that need to be stored within a database, without constantly rewriting the same data each time. The following Http Headers are used:

Most agencies provide a Last-Modified header, but if it's not present, ETag is the best route to go. If for some reason ETag is also not provided, it will just continue normally as if you were to use the original GtfsArchive::createFromUrl() method.

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

If you don't have an ETag or Last-Modified value to begin with, simply leave them out and the method will retrieve them for you.

 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
    }  
 }
 

Files are extracted to a temporary directory (/tmp/gtfs/), and cleaned up when the GtfsArchive object is destructed. You can call $gtfsArchive->deleteUncompressedFiles() to manually remove the uncompressed files.

Reading a file

$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
...

All files are lazy loaded and cached. This means that data is only loaded after calling a method such as getStopTimesFile(). Keep in mind that in can take a while to read the data for the first time. It can take up to a minute to read a large stop_times.txt file.

Reading file data

Every file class contains a method to read all data in that file. Some classes contain additional helper methods for frequently used queries such as filtering by an id or foreign key.

There is one PHP class for every (supported) file, and another class for the data contained in one row of that file. The definition of each field is contained in the PHPDoc for each getter function, allowing you to focus on coding, and less on alt-tabbing between specification and code.

$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

Contributing

We accept pull requests, but please create an issue first in order to discuss the addition or fix. If you would like to see a new feature added, you can also create a feature request by creating an issue.

Help

If you're stuck with a question, feel free to ask help through the Issue tracker.

统计信息

  • 总下载量: 5.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 6
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MPL-2.0
  • 更新时间: 2019-05-21