April 2009


Creating a google transit feed for fun and profit

William Lachance, 23 April 2009

People frequently ask me how I manage to collect and input the data that is used by hbus.ca, given Metro Transit’s intransigence. The “bike and GPS” angle is well known by now, but what about the rest of the process? How do I get the data into a format that hbus.ca can consume?

The defacto standard for the interchange of transit information is Google Transit Feed (GTFS). This exceedingly simple comma seperated value format is now supported by a plethora of software, including Google Transit, graphserver, as well as my very own libroutez (used by hbus.ca). It was obvious to me right from the beginning that the first step to building hbus.ca would be to create one of these feeds.

Manipulating a GTFS by hand is probably not a great idea. It’s basically a dump of a relational database, and is pretty inscrutable from the point of view of a human being. What I really want to be able to do is be able to manipulate things on the level of stops, service periods, and routes– and let some kind of abstraction layer take care of the low-level details. Fortunately, the awesome engineers at google created a python library called Google Transit Data Feed, which can help with creating one of these things by providing abstractions of the key elements of a google transit feed (stops, service periods, etc.). You can then write a program which uses these abstractions to create and save a GTFS.

Of course, providing the library appropriate information is easier said than done. Metro Transit’s PDF schedules are not readily computer parsable (being designed to be printed out, after all). I needed some kind of semi-automated way of converting a Metro Transit schedule into GTFS, or this whole project was going nowhere fast.

As an initial step, it turns out that it’s quite possible to extract textual information from a PDF using the open source popplar library. From there, it’s possible to extract the stopping times for an individual bus route. Let’s give an example. For example, let’s take the case of adding the 60 (Portland Hill’s route), something I’m currently working on. All I had to do was download the PDF file from Metro Transit’s site and then run the following on the command line:

pdftotext -raw route60.pdf

The raw option basically makes sure the raw strings are dumped to disk, and that no attempt is made to preserve formatting. The result is a text file with content like this in it:

842a 847a 855a 858a 903a 906a 912a -
857a 902a 910a 913a 918a 921a - 925a
910a 915a 923a 926a 931a 934a 940a -
940a 945a 953a - 1000a 1003a 1009a -
...and every 30 minutes until
210p 215p 223p - 230p 233p 239p -

This type of format can be parsed easily enough. To create a proper transit feed though, schedule information isn’t enough: you also need to know the locations of the stops, names of routes, etc. After some deliberation, I came to the determination that I needed some kind of intermediate format to store the above schedule information and this additional information. It would be readable both by humans (to ease its creation) and machines.

The obvious markup for something like this is YAML (if you’re still using XML to store structured information, run, don’t walk, and look at YAML: you can thank me later). Simple, clean, effective. GTFS is still the better choice for using the information in another application as its representation is much more amenable to being stored in a graph. Here’s a few examples of my YAML format in action:

7 (Robie to Gottingen)
10 (Westphal)

Besides the scheduling information, the other main interesting component of a GTFS is the location of the stops. As anyone who’s used a Metro Transit schedule has noticed, only major timepoints are covered in the PDF schedules. What of all the stops in between? This is where the bike and GPS come in.

What I did was take a standard GPS from Mountain Equipment Co-op (The Garmin GPSMap 60x), get on my bike, take the readings of individual gotime numbers and positioning information, of the individual stops between the major timepoints. I then took this device back to my computer and, using a utility called GPSBabel, dumped out the stop information in a format called “comma seperated value”. It looks like this:

44.65825, -63.59252, 6785-21-31-33-34-35-3-7
44.65982, -63.59452, 6768-21-31-33-35-86-3-7
44.66113, -63.59659, 6782-21-31-33-34-35-3-7

The first two items are latitude and longitude, providing the positioning of the stop. The last item is a gotime number, followed by the set of buses which pass by the stop. Turning this into YAML is a matter of applying
the following regular expression to the input:

\([0-9]+.[0-9]+\), \(-63.[0-9]+\), \([0-9]+\)- -> - { name: xxx, stop_code: \3, lat: \1, lng: \2 }

To get an actual name for the stop (i.e.: “Gottingen and Young”), I wrote a simple script which finds the nearest intersection close to the stop in the GeoBase dataset. I then (at my discretion) corrected it based on my on-the-street knowledge of the layout of Halifax as well as adding certain details to help the user (e.g. bus stops on the way to the south end of Halifax are marked “south bound”).

With these two elements in place (a format for creating human-readable transit information and a library for creating GTFS), the only thing left to do is create a program which bridges the gap. Behold, the magic of
createfeed.py. With all of this in place, creating a google transit feed for Halifax is a simple matter of typing “make”.

Is this a ridiculous amount of work? I wouldn’t say so. The vast, vast majority of my work on hbus.ca has been in creating the pathfinding code and geocoding functionality. This is work that can be translated to many different municipalities, and can easily be extended and made more useful in a myriad of ways.

What does seem a little intimidating to me is completing what I started. Capturing bus stop information for the Halifax peninsula is one thing, but covering the outlying areas (Bayer’s Lake, Sackville, etc.) is quite
another. There’s a lot of biking involved there, more perhaps than what one person can reasonably be expected to do. It was my hope that the initial release of hbus would validate the model of community-developed transit software to Metro Transit and they would see the benefit of releasing their internal copy of this data to the public, but unfortunately that doesn’t seem to have happened.

Getting that problem solved seems to be more a political problem than a technical one, and it’s not my specialty. It really does make me wonder if I shouldn’t reconsider the option of crowd sourcing, which I had rejected earlier.

Complex distractions

Mark Côté, 5 April 2009

I had to deal with a piece of code today written by what I’d call an amateur cowboy: someone who had just enough knowledge to get them into trouble. Whoever originally wrote this class had probably just read an interesting C++ article or discovered an interesting implementation and was anxious to try it. I imagine a realization soon struck that it wasn’t quite as easy to implement the slick interface as it seemed. I further postulate that there was a lot of pressure put on this programmer to get it working as fast as possible. So rather than either abandoning the idea and returning to a simple, if perhaps not exciting, interface, or taking a mulligan and rewriting it with the failed experience serving to guide the new attempt, further complexities were bolted onto the already complex structure, resulting in a fragile system that did work under proper circumstances but had non obvious invocation requirements and the tendency to melt minds as soon as the hood was popped.

Some erratic behaviour was recently discovered in this class, and someone unfamiliar with the code made a heroic attempt to get to the bottom of it, only to emerge a broken person with no increase in his love for C++. Having worked with said code a bit (but never finding the excuse to rewrite it, as it did in fact appear to work even if being as mysterious and ugly as an undead wizard), I discovered the bug. It in fact was the classic mistake of not initializing a member variable.

This bug would probably have been found right away had the rest of the code been readable and well structured. Instead, the first thought that pops into the mind of someone experiencing a bug in this code is that it must be in the crazy tangled inner workings. Betting on problems occurring where the system is least understood seems entirely reasonable. However in this case it was a red herring which distracted a competent programmer, who was so mesmerized by the snarls of internal wiring that he missed the big hole on the surface.