This PHP-Javascript code is to emulate a control center main display, a big screen showing (quasi-)real-time locations of all trains. It pulls train data from NYC MTA (Metropolitan Transportation Authority) subway feeds and shows on Google map like this.
- line_station_coordinates.csv has station ID vs. latitude-longitude pair rows necessary for conversion of ID into coordinates. Rows are grouped by route, where route encodes details such as subway line, direction, regular / express, etc. This file needs to be uploaded to your MySQL database. The raw data published by MTA is available here.
- MySQL authentication / DB info are stored in phpsqlajax_dbinfo.php.
- MTA_query.php pulls MTA data through feed links available in this page. The route ID, station coordinates and direction for each train is saved to an xml file (train_locations.xml).
- Javascript code embedded in NYC_subway_locations.html reads the xml file and plot each train on the Google map using Google Maps API.
- marker_???.png are symbols for each line. You can find the official color codes here. Note that I am using lighter blue for A/C/E as the original color is too dark to overlay route label. I am uploading them in a separate folder here but they need to be in the same folder as the above files for execution.
- MTA_query_test.py is a short python code I used for quick test & analysis of the data from MTA.
- Shuttle line and Staten Island line are not included.
- Multiple trains apparently at the terminal stations were excluded for simplicity.
- random noise were added to latitude and longitude to offset coincident markers (thanks to @brianshim in this thread). See lines 66-70 in NYC_subway_locations.html.
- Add the following require clause to composer.json:
"require" : {
"google/gtfs-realtime-bindings": "0.0.2"
}
This is a just-for-fun project at this point. The mapped locations are NOT guaranteed to be precise for a few reasons:
- Train location data provided by MTA are about 100 seconds old on the average, with 28 seconds of standard deviation. Thus quasi but not true real-time.
- The GTFS data by MTA indicate only the last station the train stopped in the trip at the moment of data request, rather than GPS locations of the train.
- The station information is given through stop sequence and station ID, which needs some translation. The stop sequence is tricky to interprete due to the complexity of subway operation; the total number of stops for a trip depends on time of a day (late night service), day of a week (weekend schedules are different), rush hour condition (look at 5 / 6 / B / D in Bronx), trip direction (F / N train stops are not symmetrical between uptown and downtown trip in Brooklyn), temporary change due to construction, etc. These are only partially reflected in the current version. I am currently examining the interpretation I employed, for further improvement.