Home

Awesome

extract-gtfs-pathways

Command line tool to extract pathways as GeoJSON from a GTFS dataset.

npm version ISC-licensed minimum Node.js version support me via GitHub Sponsors chat with me on Twitter

Installation

npm install -g extract-gtfs-pathways

Usage

Usage:
    extract-gtfs-pathways <path-to-pathways-file> <path-to-stops-file> <output-directory>
Options:
    --quiet          -q  Don't log the written files.
    --pathway-props -f  A JS function to determine additional pathway properties.
                           Example: pw => ({isWalking: pw.pathway_mode === '1'})
                           Note: The argument will be eval-ed!
    --node-props    -F  A JS function to determine additional node properties.
                           Example: n => ({isFoo: n.stop_id === 'foo'})
                           Note: The argument will be eval-ed!
Examples:
    unzip -j gtfs.zip -d data/gtfs
    mkdir extracted-pathways
    extract-gtfs-pathways data/gtfs/pathways.txt data/gtfs/stops.txt extracted-pathways
Notes:
    This tool will read a reduced form of stops.txt into memory.

    stops.txt needs to be sorted by
    1. parent_station: lexically ascending, empty first
    2. location_type: numerically descending, empty first
    You can use Miller (https://miller.readthedocs.io/) and the
    Unix tool sponge to do this:
    mlr --csv sort -f parent_station -nr location_type \
      stops.txt | sponge stops.txt

with Docker

You can use the docker.io/derhuerst/extract-gtfs-pathways Docker image.

Remember to mount (using -v) the directory you're passing as <output-directory> (see above), otherwise the file will be created within the Docker container's virtual file system (and removed after the extraction has finished).

unzip -j gtfs.zip -d gtfs # directory with GTFS input
mkdir extracted-pathways # output directory
docker run -it --rm \
    -v $PWD/gtfs:/gtfs \
    -v $PWD/extracted-pathways:/extracted-pathways
    docker.io/derhuerst/extract-gtfs-pathways /gtfs/pathways.txt /gtfs/stops.txt /extracted-pathways

pro tip: customize color nodes & pathways

By default, extract-gtfs-pathways adds some style properties to the generated features, following the Mapbox GL JS naming scheme, e.g. line-color, line-width & circle-radius. If you open them with a tool that supports these style properties – e.g. view-geojson, it will be easier to tell the nodes & pathways apart.

You can use --pathway-props/-f and --node-props/-F to customize the pathways' and nodes' properties, respectively. As an example, let's define two functions that override some default properties:

const WALKWAY = '1'
const ESCALATOR = '4'
const ELEVATOR = '5'
const pwOpacities = {[WALKWAY]: .3, [ESCALATOR]: .8, [ELEVATOR]: 1}
const pathwayProps = (pw) => ({
	'line-opacity': pwOpacities[pw.pathway_mode] || .5,
	'line-width': 2,
})

const STOP = '0' // or empty
const ENTRANCE_EXIT = '2'
const BOARDING_AREA = '4'
const nodeColors = {
    [STOP]: '#ff0000', '': '#ff0000',
    [ENTRANCE_EXIT]: '#00ff00',
    [BOARDING_AREA]: '#0000ff',
}
const nodeProps = (n) => ({
	'circle-color': nodeColors[n.location_type] || '#444444',
})

We minify the functions and declare them as Bash variables:

todo: make these the default?

pw_props='pw => ({"line-opacity": {"1": .3, "4": .8, "5": 1}[pw.pathway_mode] || .5, "line-width": 2})'
node_props='n => ({"circle-color": {"0": "#ff0000", "": "#ff0000", "2": "#00ff00", "4": "#0000ff"}[n.location_type] || "#444444"})'

Then, we can use them:

extract-gtfs-pathways --pathway-props "$pw_props" --node-props "$node_props" gtfs/pathways.txt gtfs/stops.txt out

Alternatively, we can also use geojson.io's naming scheme:

pw_props='pw => ({"stroke-opacity": {"1": .3, "4": .8, "5": 1}[pw.pathway_mode] || .5, "stroke-width": 2})'
node_props='n => ({"marker-color": {"0": "#ff0000", "": "#ff0000", "2": "#00ff00", "4": "#0000ff"}[n.location_type] || "#444444"})'

Related

Contributing

If you have a question or need support using extract-gtfs-pathways, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use the issues page.