This tutorial will teach how you to convert any transit agency’s General Transit Feed Specification (GTFS) data into ESRI ArcGIS-compatible shapefiles (.shp), KML, or XML. This is simple to do because GTFS data is essentially a collection of CSV (comma separated values) text files (really, really large text files).
Note: I don’t know how to do the reverse, converting shapefiles or other geodata into GTFS data. I’m not sure if this is possible and I’m still investigating it. If you have tips, let me know.
Converting GTFS to GIS shapefiles
Instructions require the use of ArcGIS (Windows only) and a free plugin called ET GeoWizards GIS for any version of ArcGIS. I do not have instructions for Mac users at this time.
I wrote these instructions while converting the Chicago Transit Authority’s GTFS files into shapefiles based on a reader’s request. “Field names” are quoted and layer names are italicized.
- Download the GTFS data you want. Find data from agencies around the world (although not many from Europe) on GTFS Data Exchange.
- Import into ArcGIS the shapes.txt file using Tools>Add XY Data. Specify Y=lat and X=lon
- Using ET GeoWizards GIS tools, in the Convert tab, convert the points shapefile to polyline.
- Select the shapes layer in the wizard, then create a destination file. Click Next.
- Select the “shape_id” field
- Click the checkbox next to Order and select the field “shape_pt_sequence” and click Finish.
- Depending on the number of records (the CTA has 466,000 shapes), it may take a while.
- The new shapefile will be added to your Table of Contents and appear in your map.
- Import the trips.txt and routes.txt files. Inspect them for any NULL values in the “route_id” field. You will be using this field to join the routes and trips table. It may be a case that ArcGIS imported them incorrectly; the text files will show the correct data. If NULL values appear, follow steps 10 and 11 and continue. If not, follow steps 10 and 12 and continue. This happens because ArcGIS inspected some of the data and determined they were integers and ignored text. However, this is not the case.
- Export the text files as DBF files so that ArcGIS operates on them better. Then remove the text files from the Table of Contents.
- (Only if NULL values appear) Go into editing mode and fix the NULL values you noticed in step 9. You may have to make a new column with a more forgiving data type (string) and then copy the “route_id” column into the new column. Then continue to step 12.
- Join routes and trips based on the field “route_id” – export as trips_routes.dbf
- Add a new column to shapes.shp called “shape_id2”, with data type double 18, 11. This is so we can perform step 14. Use the field calculator to copy the values from “shape_id” (also known as ET_ID) to “shape_id2”
- Join routes_trips with shapes into routes_poly based on the field “shape_id” (and “shape_id2”)
- Dissolve routes_poly on “route_id.” Make sure all selections are cleared.Â Use statistics/summary fields: “route_long,” “route_url.” Save as routes_diss.shp
- Inspect the new shapefile to ensure it was created correctly. You may notice that some bus routes don’t have names. Since these routes are well documented on the CTA website, I’m not going to fill in their names.
Click on the screenshot to see various steps in the tutorials.
Converting GTFS to KML
After you have it in shapefile form, converting to KML is easy – follow these instructions for using QGIS. Or if you want to skip the shapefile-creation process (quite involved!), you can use KMLWriter, a Python script. Also, I think the latest version of ArcGIS has built-in KML exporting.
Converting GTFS to XML
If you want to convert the GTFS data (which are essentially comma-separated value – CSV – files) to XML, that’s easier and you can avoid using GIS programs.
- First try Mr. Data Converter (very user friendly).
- If that doesn’t work, try this websiteÂ form on Creativyst. I tested it by converting the CTA’s smallest GTFS table, frequencies.txt, and it worked properly. However, it has a data size limit. (User friendly.)
- Next try csv2xml, a command line tool. (Not user friendly.)
- You can also use Microsoft Excel, but read these tips and caveats first. (I haven’t found a Microsoft application I like or think is user friendly.)