Pages

Thursday 31 January 2013

How to read MTA GTFS real time subway data?

I am reserving this blog for my code to unlock MTA real time data in Google map format. It takes me 2 nights to solve. But you can have it in half an hour. Will post the detail here shortly. Check back later!

0. Register a key from MTA. Pretty fast and easy!
1. Download the google file named protobuf-2.5.0rc1 2 and import to your eclipse workspace src folder


import com.google.protobuf.ByteString;
import com.google.transit.realtime.GtfsRealtime;
import com.google.transit.realtime.GtfsRealtime.FeedMessage;


2. Add following code to get each entity where the times is in it.


String url = "http://datamine.mta.info/mta_esi.php?key=xxx";
new DownloadGtfs().execute(url);

...

FeedMessage gtfs;

gtfs = GtfsRealtime.FeedMessage.parseFrom(content);
for (GtfsRealtime.FeedEntity entity: gtfs.getEntityList())
{
Log.d("Gtfs EntityId:", entity.getId());
}


3. Here you go! Live subway time.. With the above code, you can figure out in 30 minutes!




No comments:

Post a Comment