Skip to content

Instantly share code, notes, and snippets.

@paul91
Created September 17, 2014 15:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul91/d2aa0428bf3cf20ce094 to your computer and use it in GitHub Desktop.
Save paul91/d2aa0428bf3cf20ce094 to your computer and use it in GitHub Desktop.
Nodejs NYCT GTFS decoding example
// All thanks goes to: http://stackoverflow.com/a/25733922
var ProtoBuf = require("protobufjs");
var http = require('http');
// MTA Realtime endpoint http://datamine.mta.info/mta_esi.php?key=xxxx
var feedUrl = "...";
// Initialize from .proto file
// Requires nyct-subway.proto and gtfs-realtime.proto
var transit = ProtoBuf.loadProtoFile("nyct-subway.proto").build("transit_realtime");
// HTTP GET the binary feed
http.get(feedUrl, parse);
// process the feed
function parse(res) {
// gather the data chunks into a list
var data = [];
res.on("data", function(chunk) {
data.push(chunk);
});
res.on("end", function() {
// merge the data to one buffer, since it's in a list
data = Buffer.concat(data);
// create a FeedMessage object by decooding the data with the protobuf object
var msg = transit.FeedMessage.decode(data);
// do whatever with the object
console.log(msg);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment