Taming MTA New York City Transit's bus GTFS feeds
If you go to the MTA’s developer resources page, you'll find that while there is one GTFS feed to download for the subway (and Staten Island Railway), there are five feeds for bus data—one per borough. Your first reaction might be one of annoyance—after all, the agency almost certainly keeps data for all five boroughs in the same system internally, so why not release the data in the same structure?
However, if you look at the files more closely, you'll soon see why they're structured the way they are: they are, simply put, massive. The problem is in the
stop_times.txt file; the largest, for Brooklyn, is nearly 700 megabytes. Concatenate them together, and you get a 2 gigabyte file containing more than 30 million records. (This is a result of how the feeds are constructed, as dissected in this thread on the developer mailing list)
Most tools designed for working with GTFS feeds simply can't handle anything that large (or they choke badly). Yet, at the same time, many tools also assume that there will be a single feed per agency, so the per-borough feeds (which have some degree of overlap) can be something of a pain to work with.
This leads to a conundrum: you can work with the feeds one borough at a time (although even then, with some difficulty, as even the individual borough feeds are rather large), but there's no good way to see the whole city's bus service at once.
It turns out that with some ingenuity, this problem can be solved, although doing so takes some time and CPU resources. The basic strategy is to first naively merge the feeds together, and then refactor the merged feed, to reduce the number of stop times. The refactoring is described in this post by Brian Ferris.
Actually merging the feeds together isn't that hard; the
shapes.txt files are identical across the five feeds. The
stops.txt file has to be merged and then deduplicated, but this can be done with simple command-line tools. For the
stop_times.txt files, there's no other option than to concatenate them together. This does result in a massive
stop_times.txt file, but it's only temporary.
After producing the naively concatenated feed, apply the previously-mentioned OneBusAway GTFS transformer (described in more detail here) to the feed.
The transformer will need about 8 GB of memory to run (so launch the JVM with
-Xmx10G, or thereabouts), and on an EC2 large instance, it'll take about 10 minutes. When it's done, you'll have a stop_times.txt file which contains around 6 million records, which isn't quite so bad (considering that the entire merged and refactored feed for the five boroughs ends up being about the same size as the unmodified feed for Brooklyn alone, it's actually almost good).
As an aside, here's how I constructed the merged feed; I'm always a fan of solutions which make use of basic Unix tools.
(Finally, a disclaimer: I haven't extensively tested the feed which is the result of the process described in this post. It's possible that this process has unintended consequences which could affect its integrity or usefulness for certain applications.)