-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (83 loc) · 2.95 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
'use strict'
const got = require('got');
const csv = require('csv');
const leftPad = require('left-pad');
const ENDPOINT_BASE_URL_TEMPLATE = "http://efa.vrr.de/vrr/XSLT_COORD_REQUEST?&jsonp=&boundingBox=&boundingBoxLU={minx}%3A{miny}%3AWGS84%5BDD.DDDDD%5D&boundingBoxRL={maxx}%3A{maxy}%3AWGS84%5BDD.DDDDD%5D&coordOutputFormat=WGS84%5BGGZHTXX%5D&type_1=STOP&outputFormat=json&inclFilter=1";
const EPSILON = Math.pow(2, -2);
const MAX_STOPS = 1000;
const RS = [
"05966",
"05970"
].reduce((rs, r) => Object.assign(rs, { [r]: true }), {});
const queryStops = function(minx, miny, maxx, maxy, callback) {
const url = ENDPOINT_BASE_URL_TEMPLATE
.replace("{minx}", minx)
.replace("{miny}", miny)
.replace("{maxx}", maxx)
.replace("{maxy}", maxy);
const requery = function() {
const midx = (minx + maxx) / 2;
const midy = (miny + maxy) / 2;
queryStops(minx, miny, midx, midy, callback);
queryStops(midx, miny, maxx, midy, callback);
queryStops(minx, midy, midx, maxy, callback);
queryStops(midx, midy, maxx, maxy, callback);
};
// console.log("Querying " + url + ".");
got(url).then(response => {
const result = JSON.parse(response.body);
// console.log("Got " + result.pins.length + " results from " + url + ".");
if (result.pins.length > 0 && result.pins.length <= MAX_STOPS) {
result.pins.forEach(callback);
}
else if (maxx - minx > EPSILON)
{
requery();
}
}).catch(error =>{
requery();
});
};
const exportStops = function(stops) {
csv.stringify(stops, {header: true, quotedString: true, columns: ["stop_id", "stop_name", "stop_lon", "stop_lat", "stop_code"]}, function(err, data){
process.stdout.write(data);
});
}
const retrieveStops = function(minx, miny, maxx, maxy) {
const stops = [];
const stopsById = {};
const callback = s => {
if (!s.attrs) { s.attrs = []; }
var attributes = s.attrs.reduce((attibutes, attribute) => Object.assign(attibutes, { [attribute.name]: attribute.value }), {});
var stopId = s.stateless;
var stop = {};
stop.stop_id = stopId;
stop.stop_name = s.locality + ", " + s.desc;
var lonLat = s.coords.split(",");
stop.stop_lon = Number(lonLat[0]/100000);
stop.stop_lat = Number(lonLat[1]/100000);
stop.stop_code = attributes.STOP_GLOBAL_ID || "";
var rs = leftPad((stop.stop_code.match(/^de:(\d+):/)||['', ''])[1], 5, '0');
if (RS[rs] && stop.stop_lon && stop.stop_lat) {
const existingStop = stopsById[stopId]
if (existingStop) {
if (stop.stop_id !== existingStop.stop_id ||
stop.stop_name !== existingStop.stop_name ||
stop.stop_lon !== existingStop.stop_lon ||
stop.stop_lat !== existingStop.stop_lat)
{
// console.log("Duplicate but different stop.");
// console.log("Existing stop:", existingStop);
// console.log("Duplicate stop:", stop);
}
}
else {
stopsById[stopId] = stop;
stops.push(stop);
}
}
};
queryStops(minx, miny, maxx, maxy, callback);
setTimeout(function () { exportStops(stops); }, 60000);
};
retrieveStops(5, 47, 15, 56);