-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (73 loc) · 2.2 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
const got = require('got');
const csv = require('csv');
const Jsonix = require('jsonix').Jsonix;
const stations = {
name: 'stations',
typeInfos: [{
localName: 'stations',
propertyInfos: [{
name: 'station',
collection: true,
typeInfo: '.station'
}]
}, {
localName: 'station',
propertyInfos: [{
type: 'attribute',
name: 'name',
typeInfo: 'String'
}, {
type: 'attribute',
name: 'eva',
typeInfo: 'String'
}, {
type: 'attribute',
name: 'ds100',
typeInfo: 'String'
}]
}],
elementInfos: [{
elementName: 'stations',
typeInfo: '.stations'
}]
};
var context = new Jsonix.Context([stations]);
var unmarshaller = context.createUnmarshaller();
const haltestellenUrl = "http://download-data.deutschebahn.com/static/datasets/haltestellen/D_Bahnhof_2016_01_alle.csv";
const stationsUrl = "https://iris.noncd.db.de/iris-tts/timetable/station/*";
got(haltestellenUrl).then(response => {
csv.parse(response.body, {columns: true, delimiter: ";"}, function(err, haltestellen) {
unmarshaller.unmarshalURL(stationsUrl, function(data) {
var stations = data.value.station;
processHaltestellenAndStations(haltestellen, stations);
});
});
});
const processHaltestellenAndStations = function(haltestellen, stations) {
var stationByDs100 = stations.reduce((stationByDs100, station) => Object.assign(stationByDs100, { [station.ds100]: station }), {});
var stationByEva = stations.reduce((stationByEva, station) => Object.assign(stationByEva, { [station.eva]: station }), {});
var stops = haltestellen.map(haltestelle => {
if (stationByDs100[haltestelle.DS100]){
return {
stop_id : haltestelle.EVA_NR,
stop_name : haltestelle.NAME,
stop_lon : Number(haltestelle.LAENGE),
stop_lat : Number(haltestelle.BREITE)
};
}
else if (stationByEva[haltestelle.EVA_NR]) {
var station = stationByEva[haltestelle.EVA_NR];
return {
stop_id : haltestelle.EVA_NR,
stop_name : haltestelle.NAME,
stop_lon : Number(haltestelle.LAENGE),
stop_lat : Number(haltestelle.BREITE)
};
} else {
return null;
}
}).filter(haltestelle => !!haltestelle);
csv.stringify(stops, {header: true, quotedString: true}, function(err, data){
process.stdout.write(data);
});
};