-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
91 lines (81 loc) · 3.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var express = require('express');
var router = express.Router();
var rest = require('restler');
//-----------------------------------------------------------------------------
// CONSTANTS AND HELPERS
//-----------------------------------------------------------------------------
var API_KEY_PLACEHOLDER = process.env.WMATA_API_KEY || '2cc419c974f94b5486dd79d698f7d3f6';
var BEERME = process.env.BEERME || false;
var RAINBOW = process.env.RAINBOW || false;
console.log("using WMATA API Key - " + API_KEY_PLACEHOLDER);
if (BEERME == 'true') { console.log("Beer Me! "); }
if (RAINBOW == 'true') { console.log("Rainbows! "); }
// var DCBEER = {
// type: "FeatureCollection",
// features: [{
// type: "Feature",
// geometry: {
// type: "Point",
// coordinates: [-77.009003, 38.889931]
// },
// properties: {
// "name": "BEER",
// "marker-color": "#000000",
// "marker-symbol": "beer",
// }
// }]
// };
//-----------------------------------------------------------------------------
// ROUTES
//-----------------------------------------------------------------------------
function wmataJsonToGeoJson(jsonData) {
var dataOut = { type: "FeatureCollection", features: [] };
var markerSym = "bus";
var markerColor = "#000000";
if (BEERME == 'true') { markerSym = "beer"; }
if (jsonData.BusPositions) {
jsonData.BusPositions.forEach(function(item, index) {
if (RAINBOW == 'true') { markerColor = '#'+(Math.random()*0xFFFFFF<<0).toString(16); }
dataOut.features.push({type:"Feature",
geometry: {
type: "Point",
coordinates: [item['Lon'], item['Lat']]
},
properties: {
title: "Bus #" + item['VehicleID'],
description: item['TripHeadsign'],
'marker-size': "small",
"marker-color": markerColor,
"marker-symbol": markerSym,
}
});
});
}
return dataOut;
}
//-----------------------------------------------------------------------------
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('dcmetro', { title: 'D.C. Metro Stations', BEERME: BEERME });
});
//-----------------------------------------------------------------------------
/* GET busses - this is called by our dcmetro map index periodically */
router.get('/busses.json', function(req, res, next) {
// center on DC and 20 miles = ~32K meters
//var WMATA_URL='https://api.wmata.com/Bus.svc/json/jBusPositions?Lat=38.889931&Lon=-77.009003&Radius=32186.9';
var WMATA_URL='https://api.wmata.com/Bus.svc/json/jBusPositions';
//var WMATA_URL='https://api.wmata.com/NextBusService.svc/json/jBusPositions';
// rest call to get
rest.get(WMATA_URL, {
query : { Lat: 38.889931, Lon: -77.009003, Radius: 32186.9 },
headers : { api_key: API_KEY_PLACEHOLDER }
}).on('complete', function(data) {
//console.log("GET on " + WMATA_URL + " data out:");
//console.log(data); // auto convert to object
// TODO error handling of "statusCode: 401"
// TODO error handling for valid geo JSON
res.send(wmataJsonToGeoJson(data));
//res.send(DCBEER); // uncomment to test with static JSON data - a beer icon center on DC
});
});
module.exports = router;