-
Notifications
You must be signed in to change notification settings - Fork 1
/
static-data.js
268 lines (226 loc) · 6.48 KB
/
static-data.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*jslint node: true, indent: 2, sloppy: true, white: true, vars: true */
var converter = require('./avl-gtfs-converter.js');
var csv = require('csv');
// cb(error, tripMap)
function createTripMap(builder, avlTrips, cb) {
var tripMap = null;
var blockMap = {};
var goodCount = 0;
var count = 0;
csv()
.from(avlTrips, {columns: false, trim: true})
.on('data', function (data, index) {
count += 1;
var startNode = data[3];
var endNode = data[2];
var endTime = data[1];
var avlTripId = data[0];
var blockId = data[4];
tripMap = builder(avlTripId, startNode, endNode, endTime);
if (tripMap[avlTripId] !== undefined) {
goodCount += 1;
}
var trips = blockMap[blockId];
if (trips === undefined) {
trips = [];
blockMap[blockId] = trips;
}
trips.push({
id: avlTripId,
endTime: endTime
});
})
.on('end', function (count) {
console.log('Successfully mapped ' + goodCount + ' trips out of ' + count);
cb(null, tripMap, blockMap);
})
.on('error', function (error) {
cb(error);
});
}
// cb(error, stopMap)
function createStopMap(builder, avlStop, cb) {
var stopMap = null;
var count = 0;
var badCount = 0;
csv()
.from(avlStop, {columns: false, trim: true})
.on('data', function (data, index) {
var avlId = data[0];
var stopName = data[1].toLocaleLowerCase();
stopMap = builder(stopName, avlId);
if (stopMap[avlId] === undefined) {
console.log('Error: did not find ' + stopName + ' in GTFS. AVL ID: ' + avlId);
badCount += 1;
}
count += 1;
})
.on('end', function (count) {
console.log('');
console.log('Processed ' + count + ' stops from AVL.');
console.log('Found ' + badCount + ' that did not match GTFS stop names.');
cb(null, stopMap);
})
.on('error', function (error) {
cb(error);
});
}
// cb(error, workBlockMap)
function createWorkTripMap(blockMap, avlWorkBlock, cb) {
// Map work piece IDs to a set of trips
// Each trip is an object containing an ID and an end time
var map = {};
var count = 0;
var badCount = 0;
csv()
.from(avlWorkBlock, {columns: false, trim: true})
.on('data', function (data, index) {
map[data[0]] = blockMap[data[1]];
count += 1;
if (map[data[0]] === undefined) {
badCount += 1;
}
})
.on('end', function (count) {
console.log('');
console.log('Processed ' + count + ' work piece IDs from AVL.');
console.log('Found ' + badCount + ' that did not match block IDs from the AVL trips table.');
cb(null, map);
})
.on('error', function (error) {
cb(error);
});
}
function StaticData() {
this.tripMap = null;
this.stopMap = null;
this.avlTimestamp = 0;
this.timestamps = {
trips: null,
stops: null,
blocks: null
};
}
//StaticData.prototype = new EventEmitter();
StaticData.prototype = {};
// Check if we've created all of the necessary data to understand the regular AVL updates
StaticData.prototype.hasCompleteData = function () {
// XXX
if (!this.avlTrips) {
console.log('Need static trip info.');
}
if (!this.startNodeMap) {
console.log('Need the GTFS start node map.');
}
if (!this.stopNameMap) {
console.log('Need the GTFS stop name map.');
}
if (!this.calendar) {
console.log('Need the GTFS calendar function.');
}
if (!this.avlBlocks) {
console.log('Need the map from work piece ID to block ID');
}
if (!this.avlStops) {
console.log('Need static stop info');
}
// XXX
return (this.avlStops !== null &&
this.avlTrips !== null &&
this.avlBlocks !== null &&
this.startNodeMap !== null &&
this.stopNameMap !== null &&
this.calendar !== null);
};
// create tripMap and stopMap
StaticData.prototype.createIdMaps = function(cb) {
var self = this;
var tripMapBuilder = converter.getTripMapBuilder(self.startNodeMap);
createTripMap(tripMapBuilder, self.avlTrips, function (error, tripMap, blockMap) {
if (error) {
console.log(error.message);
return;
}
self.tripMap = tripMap;
console.log('Built new map from AVL trips to GTFS trips.');
createWorkTripMap(blockMap, self.avlBlocks, function (error, map) {
// Reset the AVL static trip data
self.avlTrips = null;
// Reset the AVL work piece/block data
self.avlBlocks = null;
if (error) {
console.log(error.message);
} else {
self.workTripMap = map;
console.log('Built new map from AVL work pieces to AVL trips.');
}
});
});
var stopMapBuilder = converter.getStopMapBuilder(self.stopNameMap);
createStopMap(stopMapBuilder, self.avlStops, function (error, map) {
// Reset the AVL static stop data
self.avlStops = null;
if (error) {
console.log(error.message);
} else {
self.stopMap = map;
console.log('Built new map from AVL stops to GTFS stops.');
}
});
};
StaticData.prototype.setGtfsTables = function (tables) {
this.startNodeMap = tables.startNodeMap;
this.stopNameMap = tables.stopNameMap;
// Calendar is really a function, not a table.
this.calendar = tables.calendar;
if (this.hasCompleteData()) {
this.createIdMaps();
}
};
StaticData.prototype.setAvlTrips = function (avlTrips) {
this.avlTrips = avlTrips;
if (this.hasCompleteData()) {
this.createIdMaps();
}
// Update the timestamp
this.setTimestamp('trips', Date.now());
};
StaticData.prototype.setAvlStops = function (avlStops) {
this.avlStops = avlStops;
console.log('Got AVL static stop info.');
if (this.hasCompleteData()) {
this.createIdMaps();
}
// Update the timestamp
this.setTimestamp('stops', Date.now());
};
StaticData.prototype.setAvlBlocks = function (avlBlocks) {
this.avlBlocks = avlBlocks;
if (this.hasCompleteData()) {
this.createIdMaps();
}
// Update the timestamp
this.setTimestamp('blocks', Date.now());
};
StaticData.prototype.setTimestamp = function (name, ts) {
this.timestamps[name] = ts;
var tripsTs = this.timestamps.trips;
var stopsTs = this.timestamps.stops;
var blocksTs = this.timestamps.blocks;
if ((this.timestamps.trips !== null) &&
(this.timestamps.stops !== null) &&
(this.timestamps.blocks !== null)) {
this.avlTimestamp = ts;
this.timestamps.trips = null;
this.timestamps.stops = null;
this.timestamps.blocks = null;
}
};
StaticData.prototype.getAvlAge = function () {
return Date.now() - this.avlTimestamp;
};
module.exports = (function () {
return {
StaticData: StaticData
};
}());