Ever come across a GTFS feed without the optional shapes.txt file? bus-router creates a shapes.txt file for stop_times.txt using OSRM. The routing isn't always perfect, but it gets the dirty parts out of the way.
🎈🎂NEW!!🎈🎂 bus-router now creates valid GeoJSON from your shapes.txt file so that you can play with/view the routes in geojson.io, gist, GitHub, etc.
Need Python 2.7 and pip to install geojson package.
This script is probably most relevant for those routes that have wide stop spacing (for example, express bus routes), where you have stretches of uncharted territory between stops. It still works for local bus routes, but the value added is less.
Final word: you can adjust the "smoothness" of the final shapes, by changing the second argument for the simplify()
call. Right now it's at .0002
, make it larger for more jagged-y shapes, smaller for smoother.
-
Clone the repo.
git clone git@github.com:atlregional/bus-router.git
-
Get packages for geojson libraries (use
sudo
if you experience permissions errors)pip install geojson
pip install gpolyencode
-
Grab your GTFS data.
Copy
stop_times.txt
,trips.txt
, andstops.txt
intobus-router/data/gtfs
. -
Change file name
env.json.tmp
toenv.json
and replace"INSERT KEY HERE"
your Google API key. -
Run the script
cd bus-router
python bus_router.py
optional arguments:
-h, --help show this help message and exit
-d osrm, --dir osrm specify the directions provider, either "goog" or
"osrm"
-s, --shapes create shapes.txt from GeoJSON
-l, --lines process polylines if directions calls have already been
made
-t, --trips modify trips file with new shape_ids (creates new file)
-g, --geojson create GeoJSON from shapes.txt
It should be noted that using the Google Directions API to create these routes is questionable at best from a licensing perspective (Google ToS). Ironically enough, this tool could very well be used by public transport agencies to feed data to Google Transit...
UPDATE: It is now pretty explicitly against the Google Maps Terms of Service to use the API to create these routes under the new derivative works section 10.5 b.
The default option for creating bus routes here is the Open Source Routing Machine (OSRM). As such, any routes derived from this tool is subject to the ODbL.
Be sure that any use of this tool is respectful of OSRM's API Usage Policy.
Big thanks to OSRM for creating a routing engine for the entire globe!
There are a few finicky things about this script at the moment.
shape_dist_traveled
column must be empty (or not exist). bus-router does not create shapes with the corresponding column, so sometimes validators yell at you if these two don't match up.
For example, if we take route_id
MARTA_110, bus-router only creates new route shapes for each of the trip_headsign
values associated with this route. So if there are two trip headsigns but actually 4 different trip patterns, bus-router will only generate 2 shapes.
This is to cut down on the number of Google Maps API requests and because I didn't implement a database here...