Search by

mapsight / pulp-gtfs

pjeweb

GTFS helpers for Pulp pipelines.

Package info

github.com/open-mapsight/pulp-gtfs

Homepage

pkg:composer/mapsight/pulp-gtfs

Statistics

Installs: 26

Dependents: 0

Suggesters: 0

Stars: 1

v1.1.1 2026-06-26 18:33 UTC

This package is auto-updated.

Last update: 2026-12-05 22:24:52 UTC


README

GTFS helpers for Pulp pipelines, currently focused on generating clean GeoJSON for stops and lines.

Features

  • Static GTFS input: Reads standard extracted GTFS .txt files from a directory.
  • Stops GeoJSON: Creates point features from stops.txt.
  • Lines GeoJSON: Creates representative route geometries from routes.txt, trips.txt, and shapes.txt, with an optional stop-based fallback.
  • Bounding box filtering: Limits output to stops inside a configured [minLon, minLat, maxLon, maxLat] bbox.
  • Line enrichment: Adds served lines and modes to stop features.
  • Accessibility enrichment: Maps wheelchair_boarding into normalized and legacy properties.
  • Stop direction enrichment (opt-in): Adds per-platform direction labels from headsigns and departure bearings from stop order or shapes.
  • Presentation-neutral output: Emits source/data properties only; applications can add icons, HTML descriptions, and other presentation fields afterwards.

Installation

composer require mapsight/pulp-gtfs

This package depends on mapsight/pulp.

Usage

Supported output types:

  • stops
  • lines
  • combined

Download, cache, and extract GTFS data before running the handler. The handler expects files like routes.txt, stops.txt, trips.txt, stop_times.txt, and shapes.txt to pass through the pipeline.

use OpenMapsight\Pulp;
use OpenMapsight\PulpGeoJSON;
use OpenMapsight\PulpGTFS;
use OpenMapsight\PulpJSON;

$gtfsDirectory = __DIR__ . '/cache/gtfs-extracted';

Pulp::start()
    ->pipe(Pulp::src('.*\.txt', $gtfsDirectory))
    ->pipe(Pulp::split(
        static fn(Pulp $p): Pulp => $p->pipe(PulpGTFS::geoJson(
            type: 'stops',
            sourceUrl: 'https://example.com/open-data-docs',
            bbox: [10.42, 52.18, 10.65, 52.36],
            sourceName: 'GTFS',
            documentationUrl: 'https://example.com/open-data-docs',
            publicSourceUrl: 'https://example.com/open-data-docs',
        )),
        static fn(Pulp $p): Pulp => $p->pipe(PulpGTFS::geoJson(
            type: 'lines',
            sourceUrl: 'https://example.com/open-data-docs',
            bbox: [10.42, 52.18, 10.65, 52.36],
            sourceName: 'GTFS',
            documentationUrl: 'https://example.com/open-data-docs',
            publicSourceUrl: 'https://example.com/open-data-docs',
        )),
    ))
    ->pipe(PulpGeoJSON::calculateBBoxes())
    ->pipe(PulpGeoJSON::addBuildInfo())
    ->pipe(PulpJSON::encodeJSON(JSON_PRETTY_PRINT))
    ->pipe(Pulp::dest(__DIR__ . '/result'))
    ->run();

If your extracted files use different names, pass a file map:

PulpGTFS::geoJson('combined', $sourceUrl, $bbox, options: [
    'files' => [
        'routes' => 'my-routes.csv',
        'stops' => 'my-stops.csv',
        'stopTimes' => 'my-stop-times.csv',
        'trips' => 'my-trips.csv',
        'shapes' => 'my-shapes.csv',
    ],
])

If a feed does not provide shapes, opt into simple stop-to-stop line strings:

PulpGTFS::geoJson('lines', $sourceUrl, $bbox, options: [
    'fallbackLineStringsFromStops' => true,
])

When this option is enabled, shapes.txt is no longer required for lines or combined output. If shapes are present, they are still preferred; the fallback is only used for routes without usable shape coordinates.

Handler Arguments

PulpGTFS::geoJson(...) accepts:

  • type: one of stops, lines, or combined.
  • sourceUrl: The public or internal source URL used for metadata.
  • bbox: [minLon, minLat, maxLon, maxLat].
  • departuresBaseUrl: Optional base URL for live departure data. When set, stop features get departuresUrl.
  • sourceName: Human-readable source name in feature properties and collection metadata.
  • documentationUrl: Public documentation URL in collection metadata.
  • publicSourceUrl: Public source URL written to feature properties and collection metadata. Use this when the actual sourceUrl contains a private token.
  • options: Optional handler settings. Use fallbackLineStringsFromStops to derive simple route geometries from ordered stops when shapes are unavailable, files to map non-standard GTFS file names, and stopDirections to enable the stop direction pass described below.

Stop Properties

Stop features include:

  • gtfsStopId
  • name
  • lines
  • routeIds
  • modes
  • modeLabels
  • wheelchairBoarding
  • wheelchairBoardingLabel
  • wheelchairAccessible
  • stopCode
  • parentStation
  • departuresUrl when configured
  • stopDirectionLabels, stopDirectionBearings, stopDirectionBearingSources and stopDirections when stopDirections is enabled

Stop Directions

Enable an explicit enrichment pass that keeps trip_headsign, stop_headsign and direction_id per platform and computes the direction vehicles leave each platform:

PulpGTFS::geoJson('stops', $sourceUrl, $bbox, options: [
    'stopDirections' => [
        'labelPrefix' => 'Towards ',
        'topHeadsigns' => 2,
    ],
])

true enables the pass with defaults. Without the option, output is unchanged.

Every stop feature then carries point-aligned arrays. A stop feature has one Point, so each array has exactly one entry; consumers that combine platforms into multi-point features can merge the detailed summaries with StopDirections::merge() and emit arrays in their own point order with StopDirections::pointAlignedProperties().

{
  "stopDirectionLabels": ["Towards Northgate / Harbour"],
  "stopDirectionBearings": [341],
  "stopDirectionBearingSources": ["next-stop"],
  "stopDirections": [{"stopIds": ["..."], "label": "...", "bearing": 341.4, "concentration": 1.0, "departures": 18, "terminating": 0, "headsigns": [], "branches": [], "vectors": {}, "sources": {}}]
}

How values are derived:

  • Headsigns: stop_headsign first, then trip_headsign, then the name of the trip's last stop. Only trips that depart from the platform count; trips that end there do not label it.
  • Ranking: more trips first; ties go to the lowest line in natural order (1 before 3 before 10), then to the headsign text. Input row order never changes the result.
  • Bearing per trip: initial bearing from the platform to the next stop at a different position (next-stop). With bearingMethod: 'shape', the shape tangent at the platform is used where the trip has a shape (shape); where a shape passes a platform twice (loops, out-and-back streets) the pass with the shortest run to the next stop wins.
  • Termini: a platform where trips only end gets the shape tangent if the shape runs on past it, e.g. into a turning loop (terminus-shape), else the incoming bearing + 180° (terminus-reverse). Platforms that also have departures ignore ending trips.
  • Mean: trip-weighted circular mean atan2(Σ sin θ, Σ cos θ), so 350° and 10° give 0°, not 180°. concentration is the mean resultant length; below minConcentration (default 0.5) the bearing is null with source ambiguous.
  • No data: platforms without usable trips get null label and bearing with source none; single-stop trips count as isolated, not as departing or ending. Values are never NaN.

Options: topHeadsigns (2), labelPrefix (''), labelSeparator (' / '), terminusLabel (null), minConcentration (0.5), bearingMethod (next-stop or shape), shapeLookaheadMeters (30), terminusShapeMinMeters (15), shapeMaxDistanceMeters (50), coincidentStopMeters (1), headsignNormalizer (callable or null), details (true; set false to omit stopDirections).

The pass streams trips.txt and stop_times.txt once more and keeps only the stop sequences of trips that serve the bbox. shapes.txt is optional for it: shapes are read only for terminating platforms, or for all departing trips with bearingMethod: 'shape'.

Line Properties

Line features include:

  • gtfsRouteId
  • gtfsShapeId
  • geometrySource: shapes for shape geometries, stops for fallback line strings
  • name
  • line
  • mode
  • modeLabel
  • routeType
  • routeShortName
  • routeLongName
  • routeUrl
  • stroke when route_color is present

Accessibility Mapping

GTFS wheelchair_boarding is mapped as:

  • 1: wheelchairBoarding = yes, wheelchairBoardingLabel = accessible
  • 2: wheelchairBoarding = no, wheelchairBoardingLabel = not accessible
  • 0 or missing: wheelchairBoarding = unknown, wheelchairBoardingLabel = unknown

If a stop has no explicit value, the parent station value is used when available.

Notes

  • The line output picks one representative shape per route based on the most common shape among trips serving stops in the bbox.
  • The handler streams large GTFS text files and avoids loading complete statewide trips.txt, stop_times.txt, or shapes.txt tables into memory.
  • Keep tokenized or licensed GTFS URLs out of generated public GeoJSON by passing a safe publicSourceUrl.
  • Add app-specific fields such as mapsightIconId, listInformation, or HTML description in the consuming project, not in this generic package.