I recently came to learn that, in an effort toward open data project, New York city MTA (Metropolitan Transportation Authority) offers API feeds for quasi-real-time subway traffic data. Then, wouldn't it be nice if I can plot the train locations on Google map, and make a movie of screen shots taken at an interval? It should look something like a grand monitor screen in a control center. So I started this brief self-project.
Understanding API feeds, and basic analysis of the returned data were done with Python, but the real code was written in PHP and Javascript. More specifically, back-end task such as feed query / parsing / translation of station ID into GPS latitude + longitude pair were implemented with PHP + MySQL, and front-end Google Map plot with Javascript-based Google Maps API. The entire codes are available in my GitHub repository. The outcome looks like below. You can execute yourself here. Note that the page does not update by itself; you need to manually refresh the page.
The data are not truly real-time since MTA DB is not updated every seconds or more often. (Technical reasons will be spelled out below for those who are interested). So I do NOT guarantee precision of the markers. At this point this is a just-for-fun project. I learned basic components of web development using Python / PHP / MySQL / Javascript / xml / API (MTA, Google Maps) / Node.js despite not very deep. Which made me to re-realize how much failed trial and attempts a success demands. Hope you enjoy.
[Technical issues]
1. Statistical analysis of MTA feed revealed that the data are approximately 99 seconds old (with 28 seconds of standard deviation). Quasi but not truly real time.
2. The feed returns only the last station a train stopped at the moment of query, not a location in between. That's why all trains seem to be at a station, which must not be true at all. Also that is why it is very likely for you to see multiple trains at one stop. You might need to zoom all the way in to distinguish them.
3. MTA tells user only the stop sequence number, which has to be properly translated to unique station ID. This is a complicated job since the total number of stops depend on a variety of factors such as: (1) daytime or late night? (2) weekday or weekend? (3) express? (4) any temporary service change due to construction? I could extend the application so that it can incorporate all of these variation, but not yet.
4. Raw data from MTA shows a few trains at the terminal station. They are not shown on the map just for convenience.
No comments:
Post a Comment