Google transitfeed Library Quickstart

Documentation for the useful Google transitfeed library is light on the ground. This post will take you from 0 to GTFS feed in no time flat.

The instructions below will work under Python 2.7. The library has not been ported to Python 3 as yet.

First import the transitfeed library:

import transitfeed

Next add a Schedule. This object manages the agencies, routes, stops, and service period information required to create a GTFS file.

schedule = transitfeed.Schedule()

From here we add an Agency. The information in this object will appear in your final agency.txt file.

schedule.AddAgency(name="Agency Name", url="http://agency.url", timezone="America/Toronto", agency_id="my_agency")

Note: time zones must be valid Time Zone Database time zones.

Next create a ServicePeriod object to house service information for the calendar.txt file

# Creates a default ServicePeriod when none has yet been specified
service_period = schedule.GetDefaultServicePeriod()
# Sets the service ID
service_period.SetServiceId("my_service_period")
# Handy helpers
service_period.SetWeekdayService(True)
service_period.SetWeekendService(False)
service_period.SetDayOfWeekHasService(5) # Saturday. 0-6 = Monday-Sunday
# Start and end dates for service
service_period.SetStartDate("20180603") # YYYYMMDD
service_period.SetEndDate("20181231")

Our next step is to create Route and Stop objects that contain information for the routes.txt and stops.txt files respectively. Add as many as needed to describe your transit system.

route = schedule.AddRoute(short_name="Route", long_name="Long Name", route_type="Bus", route_id="my_route")
stop1 = schedule.AddStop(lat=43.6414417, lng=-79.3915419, name="Stop Name 1", stop_id="stop1")
stop2 = schedule.AddStop(lat=43.6421313, lng=-79.3900129, name="Stop Name 2", stop_id="stop2")

After creating Routes and Stops we can now create Trip objects with information for the trips.txt file. Trip objects are associated with Route objects.

trip = route.AddTrip(schedule=schedule, trip_id="my_trip")

After creating a Trip we need to list the Stop’s that make up that Trip and the times at which those Stop’s are visited.

trip.AddStopTime(stop=stop1, stop_time='05:30:00')
trip.AddStopTime(stop=stop2, stop_time='05:35:00')

Finally we add Shape objects to describe the path taken by a trip. Shape objects are made up of ShapePoint objects. This information will be housed in the shapes.txt file.

shape = transitfeed.Shape(shape_id="my_shape")
shape.AddPoint(lat=43.6414417, lon=-79.3915419)
shape.AddPoint(lat=43.6421313, lon=-79.3900129)
# Add Shape to Schedule
schedule.AddShapeObject(shape)
# Associate Shape with Trip
trip.shape_id = "my_shape"

Now we have all the information to create a GTFS feed. We can use the transitfeed built-in feed validation tools and then write our GTFS feed to disk.

schedule.Validate()
schedule.WriteGoogleTransitFeed("gtfs.zip")

At this point you should have a working, albeit simple GTFS feed. Happy GTFS’ing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: