Home > Perl-GTFS

Perl-GTFS

Perl-GTFS is a project mainly written in Perl, it's free.

Perl modules for handling GTFS (transit) data

Perl-GTFS

A set of perl modules for dealing with GTFS data

See Google's GTFS spec for the details on what a GTFS Feed looks like

INSTALLATION:

  1. Obtain a GTFS feed from your favorite transit agency, unzip it to ./input/$(agency)_gtfs/

From here on, I'll just refer to "agency_gtfs"

  1. Run the preprocessor command

perl -i~ preprocessor.pl input/agency_gtfs/*.txt

This will strip off offending byte order markers, remove blank lines in the file, and remove empty strings that actually have whitespace between the commas.

  1. Edit "load-gtfs.sql" to update the database name for your agency. For example, change the database name to wmata_gtfs for wmata.

  2. Create a database in your MySQL database, and the appropriate tables. You'll need CREATE DATABASE permissions on your SQL server to do this.

mysql -u root -p < load-gtfs.sql

optionally, create a user that has at least table permissions in your database.

  1. Create the SQL importing script, edit loadfromfile.pl to include your username and password, and the name of your agency database (also the name for your agency files directory)

loadfromfile.pl

  1. Use the created SQL importing script to load your data

mysql -u user -p -D agency_gtfs < input/agency_gtfs/load-data.sql

  1. (optional) Next, use the script to create patterns and write the pattern tables:

USAGE:

Create a database handle $dbh with one of the DBI modules.

For example, using CSV, with the files stored in $mydir:

use DBI; $dbh = DBI->connect("DBI:CSV:f_dir=$mydir;f_ext=.txt;csv_sep_char=,;csv_quote_char="") or die; $gtfs_data = GTFS->new($dbh,[$options]);

or using a mySQL database:

use DBI; $dbh = DBI->connect("DBI:mysql:database=$database",$username,$password) or die; $gtfs_data = GTFS->new($dbh,[$options]);

You can use other DBI connection handles, I've tested CSV and MySQL, which is what I'll use for testing.

If you have a database handle that's suitable for writing, you can write an existing GTFS stops, routes or trips hash to the table (overwrites all data -- be careful!)

$gtfs_data->write[Stops|Trips|Routes|All]toDB($dbh);

$gtfs_data is a reference to a hash with the following key->value pairs:

$->{database} is the stored reference to the database handle $dbh $->{options} is a currently unsupported hashref that does nothing at this time $->{routes} is a reference to a hash of Route objects (see Route.pm), keyed on the route_id attribute $->{trips} is a similar has for Trip objects (Trip.pm) keyed on trip_id $->{stops} is the same with Stop.pm and stop_id

TO DO: write similar methods for writing each object type to a database handle extend the methods for Route and Trip to fill in the array of trips and stops, respectively write a GTFS method for initializing the Pattern objects (done) and storing them in the database (done) write methods to find average speed, travel time and stops per mile for each Trip or Pattern object write a KML output module