gtfs-centos7
TypeScript icon, indicating that this package has built-in type declarations

3.3.4 • Public • Published

➡️ Installation | Quick Start | TypeScript Support | Configuration | Query Methods ⬅️

node-GTFS

CircleCI

Import and Export GTFS transit data into SQLite. Query or change routes, stops, times, fares and more.

NPM


node-GTFS loads transit data in GTFS format into a SQLite database and provides some methods to query for agencies, routes, stops, times, fares, calendars and other GTFS data. It also offers spatial queries to find nearby stops, routes and agencies and can convert stops and shapes to geoJSON format.

Additionally, this library can export data from the SQLite database back into GTFS (csv) format.

You can use it as a command-line tool or as a node.js module.

This library has three parts: the GTFS import script, the query methods and the GTFS export script

Installation

If you would like to use this library as a command-line utility, you can install it globally directly from npm:

npm install gtfs -g

If you are using this as a node module as part of an application, you can include it in your project's package.json file.

Quick Start

Command-line examples

gtfs-import --gtfsUrl http://www.bart.gov/dev/schedules/google_transit.zip

or

gtfs-import --gtfsPath /path/to/your/gtfs.zip

or

gtfs-import --gtfsPath /path/to/your/unzipped/gtfs

or

gtfs-import --configPath /path/to/your/custom-config.json

gtfs-export --configPath /path/to/your/custom-config.json

Code example

import { importGtfs } from 'gtfs';
import { readFile } from 'fs/promises';
const config = JSON.parse(
  await readFile(new URL('./config.json', import.meta.url))
);

importGtfs(config)
  .then(() => {
    console.log('Import Successful');
  })
  .catch((err) => {
    console.error(err);
  });

Example Applications

GTFS-to-HTML GTFS-to-HTML uses `node-gtfs` for downloading, importing and querying GTFS data. It provides a good example of how to use this library and is used by over a dozen transit agencies to generate the timetables on their websites.
GTFS-to-geojson GTFS-to-geojson creates geoJSON files for transit routes for use in mapping. It uses `node-gtfs` for downloading, importing and querying GTFS data. It provides a good example of how to use this library.
GTFS-to-Chart GTFS-to-chart generates a stringline chart in D3 for all trips for a specific route using data from an agency's GTFS. It uses `node-gtfs` for downloading, importing and querying GTFS data.
GTFS-TTS GTFS-Text-to-Speech app tests GTFS stop name pronunciation for text-to-speech. It uses `node-gtfs` for loading stop names from GTFS data.

Command Line Usage

The gtfs-import command-line utility will import GTFS into SQLite3.

The gtfs-export command-line utility will create GTFS from data previously imported into SQLite3.

gtfs-import Command-line options

configPath

Allows specifying a path to a configuration json file. By default, node-gtfs will look for a config.json file in the directory it is being run from. Using a config.json file allows you specify more options than CLI arguments alone - see below.

gtfs-import --configPath /path/to/your/custom-config.json

gtfsPath

Specify a local path to GTFS, either zipped or unzipped.

gtfs-import --gtfsPath /path/to/your/gtfs.zip

or

gtfs-import --gtfsPath /path/to/your/unzipped/gtfs

gtfsUrl

Specify a URL to a zipped GTFS file.

gtfs-import --gtfsUrl http://www.bart.gov/dev/schedules/google_transit.zip

TypeScript Support

Basic TypeScript typings are included with this library. Please open an issue if you find any inconsistencies between the declared types and underlying code.

Configuration

Copy config-sample.json to config.json and then add your projects configuration to config.json.

cp config-sample.json config.json
option type description
agencies array An array of GTFS files to be imported.
csvOptions object Options passed to csv-parse for parsing GTFS CSV files. Optional.
exportPath string A path to a directory to put exported GTFS files. Optional, defaults to gtfs-export/<agency_name>.
sqlitePath string A path to an SQLite database. Optional, defaults to using an in-memory database.
verbose boolean Whether or not to print output to the console. Optional, defaults to true.

agencies

{Array} Specify the GTFS files to be imported in an agencies array. GTFS files can be imported via a url or a local path.

For GTFS files that contain more than one agency, you only need to list each GTFS file once in the agencies array, not once per agency that it contains.

To find an agency's GTFS file, visit transitfeeds.com. You can use the URL from the agency's website or you can use a URL generated from the transitfeeds.com API along with your API token.

  • Specify a download URL:
{
  "agencies": [
    {
      "url": "http://countyconnection.com/GTFS/google_transit.zip"
    }
  ]
}
  • Specify a download URL with custom headers:
{
  "agencies": [
    {
      "url": "http://countyconnection.com/GTFS/google_transit.zip",
      "headers": {
        "Content-Type": "application/json",
        "Authorization": "bearer 1234567890"
      }
    }
  ]
}
  • Specify a path to a zipped GTFS file:
{
  "agencies": [
    {
      "path": "/path/to/the/gtfs.zip"
    }
  ]
}
  • Specify a path to an unzipped GTFS file:
{
  "agencies": [
    {
      "path": "/path/to/the/unzipped/gtfs/"
    }
  ]
}
  • Exclude files - if you don't want all GTFS files to be imported, you can specify an array of files to exclude.
{
  "agencies": [
    {
      "path": "/path/to/the/unzipped/gtfs/",
      "exclude": ["shapes", "stops"]
    }
  ]
}
  • Specify multiple agencies to be imported into the same database
{
  "agencies": [
    {
      "path": "/path/to/the/gtfs.zip"
    },
    {
      "path": "/path/to/the/othergtfs.zip"
    }
  ]
}

csvOptions

{Object} Add options to be passed to csv-parse with the key csvOptions. This is an optional parameter.

For instance, if you wanted to skip importing invalid lines in the GTFS file:

    "csvOptions": {
      "skip_lines_with_error": true
    }

See full list of options.

exportPath

{String} A path to a directory to put exported GTFS files. If the directory does not exist, it will be created. Used when running gtfs-export script or exportGtfs(). Optional, defaults to gtfs-export/<agency_name> where <agency_name> is a sanitized, snake-cased version of the first agency_name in agency.txt.

sqlitePath

{String} A path to an SQLite database. Optional, defaults to using an in-memory database with a value of :memory:.

    "sqlitePath": "/dev/sqlite/gtfs"

verbose

{Boolean} If you don't want the import script to print any output to the console, you can set verbose to false. Defaults to true.

{
  "agencies": [
    {
      "path": "/path/to/the/unzipped/gtfs/"
    }
  ],
  "verbose": false
}

If you want to route logs to a custom function, you can pass a function that takes a single text argument as logFunction. This can't be defined in config.json but instead passed in a config object to importGtfs(). For example:

import { importGtfs } from 'gtfs';

const config = {
  agencies: [
    {
      url: 'http://countyconnection.com/GTFS/google_transit.zip',
      exclude: ['shapes'],
    },
  ],
  logFunction: function (text) {
    // Do something with the logs here, like save it or send it somewhere
    console.log(text);
  },
};

importGtfs(config);

gtfs-import Script

The gtfs-import script reads from a JSON configuration file and imports the GTFS files specified to a SQLite database. Read more on setting up your configuration file.

Run the gtfs-import script from command-line

gtfs-import

By default, it will look for a config.json file in the project root. To specify a different path for the configuration file:

gtfs-import --configPath /path/to/your/custom-config.json

Use importGtfs script in code

Use importGtfs() in your code to run an import of a GTFS file specified in a config.json file.

import { importGtfs } from 'gtfs';
import { readFile } from 'fs/promises';

const config = JSON.parse(
  await readFile(new URL('./config.json', import.meta.url))
);

importGtfs(config)
  .then(() => {
    console.log('Import Successful');
  })
  .catch((err) => {
    console.error(err);
  });

Configuration can be a JSON object in your code

import { importGtfs } from 'gtfs';

const config = {
  sqlitePath: '/dev/sqlite/gtfs',
  agencies: [
    {
      url: 'http://countyconnection.com/GTFS/google_transit.zip',
      exclude: ['shapes'],
    },
  ],
};

importGtfs(config)
  .then(() => {
    console.log('Import Successful');
  })
  .catch((err) => {
    console.error(err);
  });

gtfs-export Script

The gtfs-export script reads from a JSON configuration file and exports data in GTFS format from a SQLite database. Read more on setting up your configuration file.

This could be used to export a GTFS file from SQLite after changes have been made to the data in the database manually.

Make sure to import GTFS data into SQLite first

Nothing will be exported if there is no data to export. See the GTFS import script.

Run the gtfs-export script from Command-line

gtfs-export

By default, it will look for a config.json file in the project root. To specify a different path for the configuration file:

gtfs-export --configPath /path/to/your/custom-config.json

Command Line options

Specify path to config JSON file

You can specify the path to a config file to be used by the export script.

gtfs-export --configPath /path/to/your/custom-config.json

Show help

Show all command line options

gtfs-export --help

Use exportGtfs script in code

Use exportGtfs() in your code to run an export of a GTFS file specified in a config.json file.

import { exportGtfs } from 'gtfs';

const config = {
  sqlitePath: '/dev/sqlite/gtfs',
  agencies: [
    {
      url: 'http://countyconnection.com/GTFS/google_transit.zip',
      exclude: ['shapes'],
    },
  ],
};

exportGtfs(config)
  .then(() => {
    console.log('Export Successful');
  })
  .catch((err) => {
    console.error(err);
  });

Query Methods

This library includes many methods you can use in your project to query GTFS data. These methods return promises.

Most methods accept three optional arguments: query, fields and sortBy.

Query

For example, to get a list of all routes with just route_id, route_short_name and route_color sorted by route_short_name:

import { openDb, getRoutes } from 'gtfs';
import { readFile } from 'fs/promises';
const config = JSON.parse(
  await readFile(new URL('./config.json', import.meta.url))
);

const db = await openDb(config);
const routes = await getRoutes(
  {},
  ['route_id', 'route_short_name', 'route_color'],
  [['route_short_name', 'ASC']]
);

To get a list of all trip_ids for a specific route:

import { openDb, getTrips } from 'gtfs';
import { readFile } from 'fs/promises';
const config = JSON.parse(
  await readFile(new URL('./config.json', import.meta.url))
);

const db = await openDb(config);
const trips = await getTrips(
  {
    route_id: '123',
  },
  ['trip_id']
);

To get a few stops by specific stop_ids:

import { openDb, getStops } from 'gtfs';
import { readFile } from 'fs/promises';
const config = JSON.parse(await readFile(new URL('./config.json', import.meta.url)));

const db = await openDb(config);
const stops = await getStops(
  {
    stop_id: [
      '123',
      '234'
      '345'
    ]
  }
);

Setup

Include this library.

import { openDb } from 'gtfs';

Open database before making any queries

const db = await openDb(config);

getAgencies(query, fields, sortBy)

Queries agencies and returns a promise. The result of the promise is an array of agencies.

import { getAgencies } from 'gtfs';

// Get all agencies
getAgencies();

// Get a specific agency
getAgencies({
  agency_id: 'caltrain',
});

getAttributions(query, fields, sortBy)

Queries attributions and returns a promise. The result of the promise is an array of attributions.

import { getAttributions } from 'gtfs';

// Get all attributions
getAttributions();

// Get a specific attribution
getAttributions({
  attribution_id: '123',
});

getRoutes(query, fields, sortBy)

Queries routes and returns a promise. The result of the promise is an array of routes.

import { getRoutes } from 'gtfs';

// Get all routes, sorted by route_short_name
getRoutes({}, [], [['route_short_name', 'ASC']]);

// Get a specific route
getRoutes({
  route_id: 'Lo-16APR',
});

getRoutes allows passing a stop_id in the query and it will query stoptimes and trips to find all routes that serve that stop_id.

import { getRoutes } from 'gtfs';

// Get routes that serve a specific stop, sorted by `stop_name`.
getRoutes(
  {
    stop_id: '70011',
  },
  [],
  [['stop_name', 'ASC']]
);

getStops(query, fields, sortBy)

Queries stops and returns a promise. The result of the promise is an array of stops.

import { getStops } from 'gtfs';

// Get all stops
getStops();

// Get a specific stop by stop_id
getStops({
  stop_id: '70011',
});

getStops allows passing a route_id in the query and it will query trips and stoptimes to find all stops served by that route_id.

import { getStops } from 'gtfs';

// Get all stops for a specific route
getStops({
  route_id: 'Lo-16APR',
});

getStops allows passing a trip_id in the query and it will query stoptimes to find all stops on that trip_id.

import { getStops } from 'gtfs';

// Get all stops for a specific trip
getStops({
  trip_id: '37a',
});

getStops allows passing a shape_id in the query and it will query trips and stoptimes to find all stops that use that shape_id.

import { getStops } from 'gtfs';

// Get all stops for a specific trip
getStops({
  shape_id: 'cal_sf_tam',
});

getStopsAsGeoJSON(query)

Queries stops and returns a promise. The result of the promise is an geoJSON object of stops. All valid queries for getStops() work for getStopsAsGeoJSON().

import { getStopsAsGeoJSON } from 'gtfs';

// Get all stops for an agency as geoJSON
getStopsAsGeoJSON();

// Get all stops for a specific route as geoJSON
getStopsAsGeoJSON({
  route_id: 'Lo-16APR',
});

getStoptimes(query, fields, sortBy)

Queries stop_times and returns a promise. The result of the promise is an array of stop_times.

import { getStoptimes } from 'gtfs';

// Get all stoptimes
getStoptimes();

// Get all stoptimes for a specific stop
getStoptimes({
  stop_id: '70011',
});

// Get all stoptimes for a specific trip, sorted by stop_sequence
getStoptimes(
  {
    trip_id: '37a',
  },
  [],
  [['stop_sequence', 'ASC']]
);

// Get all stoptimes for a specific stop and service_id
getStoptimes({
  stop_id: '70011',
  service_id: 'CT-16APR-Caltrain-Weekday-01',
});

getTrips(query, fields, sortBy)

Queries trips and returns a promise. The result of the promise is an array of trips.

import { getTrips } from 'gtfs';

// Get all trips
getTrips();

// Get trips for a specific route and direction
getTrips({
  route_id: 'Lo-16APR',
  direction_id: 0
});

// Get trips for direction '' or null
getTrips({
  route_id: 'Lo-16APR',
  direction_id: null
});

// Get trips for a specific route and direction limited by a service_id
getTrips({
  route_id: 'Lo-16APR',
  direction_id: 0,
  service_id: '
});

getShapes(query, fields, sortBy)

Queries shapes and returns a promise. The result of the promise is an array of shapes.

import { getShapes } from 'gtfs';

// Get all shapes for an agency
getShapes();

getShapes allows passing a route_id in the query and it will query trips to find all shapes served by that route_id.

import { getShapes } from 'gtfs';

// Get all shapes for a specific route and direction
getShapes({
  route_id: 'Lo-16APR',
});

getShapes allows passing a trip_id in the query and it will query trips to find all shapes served by that trip_id.

import { getShapes } from 'gtfs';

// Get all shapes for a specific trip_id
getShapes({
  trip_id: '37a',
});

getShapes allows passing a service_id in the query and it will query trips to find all shapes served by that service_id.

import { getShapes } from 'gtfs';

// Get all shapes for a specific service_id
.etShapes({
  service_id: 'CT-16APR-Caltrain-Sunday-02'
});

getShapesAsGeoJSON(query)

Queries shapes and returns a promise. The result of the promise is an geoJSON object of shapes. All valid queries for getShapes() work for getShapesAsGeoJSON().

Returns geoJSON of shapes.

import { getShapesAsGeoJSON } from 'gtfs';

// Get geoJSON of all routes in an agency
getShapesAsGeoJSON();

// Get geoJSON of shapes for a specific route
getShapesAsGeoJSON({
  route_id: 'Lo-16APR',
});

// Get geoJSON of shapes for a specific trip
getShapesAsGeoJSON({
  trip_id: '37a',
});

// Get geoJSON of shapes for a specific `service_id`
getShapesAsGeoJSON({
  service_id: 'CT-16APR-Caltrain-Sunday-02',
});

// Get geoJSON of shapes for a specific `shape_id`
getShapesAsGeoJSON({
  shape_id: 'cal_sf_tam',
});

getCalendars(query, fields, sortBy)

Queries calendars and returns a promise. The result of the promise is an array of calendars.

import { getCalendars } from 'gtfs';

// Get all calendars for an agency
getCalendars();

// Get calendars for a specific `service_id`
getCalendars({
  service_id: 'CT-16APR-Caltrain-Sunday-02',
});

getCalendarDates(query, fields, sortBy)

Queries calendar_dates and returns a promise. The result of the promise is an array of calendar_dates.

import { getCalendarDates } from 'gtfs';

// Get all calendar_dates for an agency
getCalendarDates();

// Get calendar_dates for a specific `service_id`
getCalendarDates({
  service_id: 'CT-16APR-Caltrain-Sunday-02',
});

getFareAttributes(query, fields, sortBy)

Queries fare_attributes and returns a promise. The result of the promise is an array of fare_attributes.

import { getFareAttributes } from 'gtfs';

// Get all `fare_attributes` for an agency
getFareAttributes();

// Get `fare_attributes` for a specific `fare_id`
getFareAttributes({
  fare_id: '123',
});

getFareRules(query, fields, sortBy)

Queries fare_rules and returns a promise. The result of the promise is an array of fare_rules.

import { getFareRules } from 'gtfs';

// Get all `fare_rules` for an agency
getFareRules();

// Get fare_rules for a specific route
getFareRules({
  route_id: 'Lo-16APR',
});

getFeedInfo(query, fields, sortBy)

Queries feed_info and returns a promise. The result of the promise is an array of feed_infos.

import { getFeedInfo } from 'gtfs';

// Get feed_info
getFeedInfo();

getFrequencies(query, fields, sortBy)

Queries frequencies and returns a promise. The result of the promise is an array of frequencies.

import { getFrequencies } from 'gtfs';

// Get all frequencies
getFrequencies();

// Get frequencies for a specific trip
getFrequencies({
  trip_id: '1234',
});

getLevels(query, fields, sortBy)

Queries levels and returns a promise. The result of the promise is an array of levels.

import { getLevels } from 'gtfs';

// Get levels
getLevels();

getPathways(query, fields, sortBy)

Queries pathways and returns a promise. The result of the promise is an array of pathways.

import { getPathways } from 'gtfs';

// Get pathways
getPathways();

getTransfers(query, fields, sortBy)

Queries transfers and returns a promise. The result of the promise is an array of transfers.

import { getTransfers } from 'gtfs';

// Get all transfers
getTransfers();

// Get transfers for a specific stop
getTransfers({
  from_stop_id: '1234',
});

getTranslations(query, fields, sortBy)

Queries translations and returns a promise. The result of the promise is an array of translations.

import { getTranslations } from 'gtfs';

// Get translations
getTranslations();

getDirections(query, fields, sortBy)

Queries directions and returns a promise. The result of the promise is an array of directions. These are from the non-standard directions.txt file. See documentation and examples of this file.

import { getDirections } from 'gtfs';

// Get all directions
getDirections();

// Get directions for a specific route
getDirections({
  route_id: '1234',
});

// Get directions for a specific route and direction
getDirections({
  route_id: '1234',
  direction_id: 1,
});

getStopAttributes(query, fields, sortBy)

Queries stop_attributes and returns a promise. The result of the promise is an array of stop_attributes. These are from the non-standard stop_attributes.txt file. See documentation and examples of this file.

import { getStopAttributes } from 'gtfs';

// Get all stop attributes
getStopAttributes();

// Get stop attributes for specific stop
getStopAttributes({
  stop_id: '1234',
});

getTimetables(query, fields, sortBy)

Queries timetables and returns a promise. The result of the promise is an array of timetables. These are from the non-standard timetables.txt file. See [documentation and examples of this file](https://gtfstohtml.com/docs/timetables.

import { getTimetables } from 'gtfs';

// Get all timetables for an agency
getTimetables();

// Get a specific timetable
getTimetables({
  timetable_id: '1',
});

getTimetableStopOrders(query, fields, sortBy)

Queries timetable_stop_orders and returns a promise. The result of the promise is an array of timetable_stop_orders. These are from the non-standard timetable_stop_order.txt file. See documentation and examples of this file.

import { getTimetableStopOrders } from 'gtfs';

// Get all timetable_stop_orders
getTimetableStopOrders();

// Get timetable_stop_orders for a specific timetable
getTimetableStopOrders({
  timetable_id: '1',
});

getTimetablePages(query, fields, sortBy)

Queries timetable_pages and returns a promise. The result of the promise is an array of timetable_pages. These are from the non-standard timetable_pages.txt file. See documentation and examples of this file.

import { getTimetablePages } from 'gtfs';

// Get all timetable_pages for an agency
getTimetablePages();

// Get a specific timetable_page
getTimetablePages({
  timetable_page_id: '2',
});

getTimetableNotes(query, fields, sortBy)

Queries timetable_notes and returns a promise. The result of the promise is an array of timetable_notes. These are from the non-standard timetable_notes.txt file. See documentation and examples of this file.

import { getTimetableNotes } from 'gtfs';

// Get all timetable_notes for an agency
getTimetableNotes();

// Get a specific timetable_note
getTimetableNotes({
  note_id: '1',
});

getTimetableNotesReferences(query, fields, sortBy)

Queries timetable_notes_references and returns a promise. The result of the promise is an array of timetable_notes_references. These are from the non-standard timetable_notes_references.txt file. See documentation and examples of this file.

import { getTimetableNotesReferences } from 'gtfs';

// Get all timetable_notes_references for an agency
getTimetableNotesReferences();

// Get all timetable_notes_references for a specific timetable
getTimetableNotesReferences({
  timetable_id: '4',
});

Contributing

Pull requests are welcome, as is feedback and reporting issues.

Tests

To run tests:

npm test

To run a specific test:

NODE_ENV=test mocha ./test/mocha/gtfs.get-stoptimes.js

Linting

npm run lint

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 3.3.4
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 3.3.4
    1
  • 3.3.3
    0
  • 3.3.2
    1
  • 3.3.1
    1

Package Sidebar

Install

npm i gtfs-centos7

Weekly Downloads

1

Version

3.3.4

License

MIT

Unpacked Size

279 kB

Total Files

117

Last publish

Collaborators

  • mr-tm