Created
September 17, 2014 15:00
-
-
Save paul91/d2aa0428bf3cf20ce094 to your computer and use it in GitHub Desktop.
Nodejs NYCT GTFS decoding example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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