My First App — The Train Tracker

Last Friday, only 3 weeks into our classes at Viking Code School, we had our first hackathon.

My original idea was to build a Sinatra-based application that tracked freight trains. As a resident of the Chicago suburbs, one of my pain points is the unexpected wait time while a freight train lumbers across the tracks with hundreds of cars. It adds 5–15 minutes onto my travel time.

Thus far, as far as I can tell, GPS and Google Maps don’t track freight trains. This seemed like a good candidate for a problem to solve, and one that would be immediately applicable to my life.

I quickly discovered one reason this solution hasn’t yet been implemented — I couldn’t track down any freight train travel time data. Without a data source, my idea couldn’t be implemented.

Not to be thwarted, I switched gears and starting looking at Metra passenger train data — of which there was plenty. If I couldn’t make a web app that told you if a freight train was coming, maybe I could make a web app that told you if you were on time or late to your train!

I started by thinking, if I was potentially late for a train, what kind of app would I use?

The main point that jumped out at me is that the app would have to be fairly simple, I couldn’t depend on asking the user a bunch of questions — they wouldn’t have time for that. They would just want to visit the site, and immediately have the information they need to see if they could still catch their train.

To make this idea come to life, I really only needed 4 data points:

  1. Location of the user
  2. Location of the nearest train station
  3. Amount of time it would take the user to get to that train station
  4. Train arrival times at that station

Here’s the feature list I worked off of:

Getting the location of the user was somewhat trivial — I could just make an API request to FreeGeoIp to locate the user based on their IP address. Perhaps a bit big-brotherish, but ultimately worth the cost to complete the app.

I was also able to track down some helpful data sets from Metra’s site. I found a couple of helpful text files — one that gave the latitude and longitude of every train station in the system, and another that gave the train arrival times of every train in the system.

The final data point I needed was to calculate the amount of time it would take the user to get to the train station. I would implement this using Google Maps’ Directions API, which would return, among other pieces of data, the amount of time it would take to drive from the user’s current location to the nearest train station.

The clock ran out just as I began poking around Google’s Directions API, so I wasn’t able to close the loop within the time limits, but I hope to revisit this when I get a chance.

Here’s the UI of the final product!

And one for a “sad ending”

Overall I was pretty happy about taking the train tracker app from concept to (near) completion in about 8 hours. Of course, I’m always thinking of ways I can improve moving forward. Here are the features I’ll keep chipping away at when I have the time.

  1. Complete the driving time calculation using Google Maps’ Directions API
  2. Develop an orange UI for when the user has a chance of making it to the train on time.
  3. Develop logic to track either trains going into Chicago or trains leaving Chicago — right now the app grabs data for all trains
  4. Use a more accurate geolocation system, perhaps provided by Google. I’ve found that FreeGeoIP isn’t always 100% accurate
  5. Add additional information and options like alternate train stations, time of the next time arrival, maps, etc.
  6. Real-time tracking of trains that are late or early.

Overall, I had a blast seeing this project come to life. Looking forward to the next one!

Here’s the Github Repo for the interested.