-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrender.js
319 lines (254 loc) · 7.85 KB
/
render.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
var path = require('path');
var crypto = require('crypto');
var jquery = require('jquery');
var argv = require('optimist').argv;
var fs = require('fs');
var Gtfs = require(path.join(__dirname, ".", "parser", "loader"));
var Rainbow = require(path.join(__dirname, "lib", "rainbowvis"));
var rainbow = new Rainbow.Rainbow();
var shapes;
var trips;
var routes;
var segments = []
var sequences = []
var sequences_length = 0
var max;
var min;
var bbox;
var gtfs;
var large = true;
if (large) {
var render_area = {width: 3400, height: 3400};
var pathSuffix = "large";
} else {
var pathSuffix = "small";
var render_area = {width: 600, height: 600};
}
var requiredFile = "./gtfs/" + argv.gtfs + "/shapes.txt";
if (!fs.existsSync(requiredFile)) {
console.error("\nERROR: " + requiredFile + " DOES NOT EXIST!\nEXITING.\n");
process.exit(1);
}
debug("Loading GTFS files...");
Gtfs("./gtfs/" + argv.gtfs + "/", function(data) {
debug("GTFS files loaded.\n");
gtfs = data;
shapes = gtfs.getShapes();
trips = gtfs.getTrips();
prepareData();
createFile();
});
/* possible bug: can there be more route types for one shape? */
var route_types = [];
function getRouteTypeForShapeId(shape_id) {
return route_types[shape_id];
}
function prepareData() {
debug("Starting to prepare data...");
/* count the trips on a certain id */
var trips_on_a_shape = [];
for (var i in trips) {
var trip = trips[i];
if (trips_on_a_shape[ trip.shape_id ] == undefined)
trips_on_a_shape[ trip.shape_id ] = 1;
else
trips_on_a_shape[ trip.shape_id ]++;
var route_type = 1 * gtfs.getRouteById(trip.route_id).route_type;
if (route_types[trip.shape_id] == undefined)
route_types[trip.shape_id] = route_type;
}
/*
we have to break the shapes in chunks of predecessor/successor,
cause there might be overlapping segments of different shapes.
[
{ {x:.., y:..}, {x:.., y:..} }: {
trips: 3 // 3 trips along this segment
}
]
*/
/* ensure that the shape points are in the correct order */
for (var i in shapes) {
var shape = shapes[i];
if (sequences[shape.shape_id] == undefined)
sequences[shape.shape_id] = []
sequences[shape.shape_id][shape.shape_pt_sequence] = shape;
}
for (var i in sequences)
sequences_length++;
debug("Preparing data finished.");
debug("\nStarting to create shape segments array with trips per segment...");
for (var i in sequences) {
var A = undefined;
var B = undefined;
var last_undef = 0
for (var n in sequences[i]) {
var shape = sequences[i][n];
var shape_id = shape.shape_id
adjustBBox([new Number(shape.shape_pt_lat),
new Number(shape.shape_pt_lon)]);
if(last_undef == 1 && A == undefined)
console.log("shit")
/* was this the last point in a sequence? */
if (n == sequences[i].length-1 && A == undefined)
A = {"lat": shape.shape_pt_lat, "lng": shape.shape_pt_lon};
if (A == undefined) {
A = {"lat": shape.shape_pt_lat, "lng": shape.shape_pt_lon};
last_undef = 1;
continue;
} else {
last_undef = 0;
B = {"lat": shape.shape_pt_lat, "lng": shape.shape_pt_lon};
var segment_index = hash([A, B]);
// maybe shape from different direction, but on this segment
var segment_index2 = hash([B, A]);
if (segments[segment_index] == undefined) {
segments[segment_index] = {
"trips": 0
, "from": A
, "to": B
, "route_type": undefined
}
}
if (trips_on_a_shape[shape_id] == undefined) {
// maybe the shape exists, but
// there are no trips for it
//console.log("shit2")
trips_on_a_shape[shape_id] = 0;
//console.log(shape_id)
}
var route_type = getRouteTypeForShapeId(shape_id);
if (route_type != undefined) {
segments[segment_index].route_type = route_type;
} else {
//console.log("oh oh. undefined route_type for shape_id " + shape_id);
//return;
}
segments[segment_index].trips += trips_on_a_shape[shape_id];
/* check if {B, A} in arr */
if (segment_index != segment_index2 && segments[segment_index2] != undefined) {
segments[segment_index].trips = segments[segment_index].trips
}
if (segments[segment_index].trips > max || max == undefined)
max = segments[segment_index].trips;
if (segments[segment_index].trips < min || min == undefined)
min = segments[segment_index].trips;
A = B;
}
}
}
debug("Segments created.");
if (max == min && max > 0) min--;
if (max == min && max <= 0) max++;
debug("max trips per segment: " + max);
debug("min trips per segment: " + min);
rainbow.setNumberRange(min, max);
rainbow.setSpectrum('blue', 'green', 'yellow', 'red');
}
function coord2px(lat, lng) {
var coordX = bbox.width_f * (lng - bbox.left)
var coordY = bbox.height_f * (bbox.top - lat)
return {x: coordX, y: coordY};
}
function adjustBBox(coords) {
if (coords.length === 0) {
console.error("no coordinates could be parsed!");
console.error( JSON.stringify(coords) );
process.exit(1);
}
if (!bbox) {
bbox = {
left: coords[1]
, right: coords[1]
, top: coords[0]
, bottom: coords[0]
, width: 0
, height: 0
, shift_x: 0
, shift_y: 0
};
}
if (coords[1] < bbox.left)
bbox.left = coords[1];
if (coords[1] > bbox.right)
bbox.right = coords[1];
if (coords[0] > bbox.top)
bbox.top = coords[0];
if (coords[0] < bbox.bottom)
bbox.bottom = coords[0];
bbox.height = bbox.top - bbox.bottom;
bbox.width = bbox.right - bbox.left;
bbox.width_f = render_area.width / bbox.width;
bbox.height_f = render_area.height / bbox.height;
/* how much do we need to shift for the points to be in the visible area? */
var top_left = coord2px(bbox.left, bbox.top);
if (top_left.x < 0)
// so much, that the outermost point is on 0
bbox.shift_x = -1 * top_left.x;
else if (top_left.x > render_area.width)
bbox.shift_x = -1 * top_left.x;
if (top_left.y < 0)
bbox.shift_y = -1 * top_left.y;
else if (top_left.y > render_area.height)
bbox.shift_y = -1 * top_left.y;
}
function hash(val) {
var md5 = crypto.createHash('sha1');
md5.update(JSON.stringify(val), "ascii")
return md5.digest("hex")
}
function createFile() {
fs.truncateSync("./output/" + argv.gtfs + "/data_" + pathSuffix + ".lines", 0, "utf8");
var working = 0;
var one = 1;
debug("\nStarting to create file...");
for (var i in sequences) {
var A = undefined;
var B = undefined;
var last_px;
var last_shape;
var last_trips = 0;
var pts = []
for (var n in sequences[i]) {
var shape = sequences[i][n];
var px = coord2px(shape.shape_pt_lat, shape.shape_pt_lon);
pts.push({x: new Number(px.x), y: new Number(px.y)});
if (last_shape != undefined) {
A = {"lat": shape.shape_pt_lat, "lng": shape.shape_pt_lon};
B = {"lat": last_shape.shape_pt_lat, "lng": last_shape.shape_pt_lon};
var segment_index = hash([A, B]);
if (segments[segment_index] != undefined) {
/* do the trips vary from the - so far - concatenated segments? */
trips = segments[segment_index].trips;
if (trips != last_trips) {
var col = rainbow.colourAt(trips);
last_trips = trips;
var coords = "";
for (var un in pts) {
coords += pts[un].x + " " + pts[un].y + ","
}
var route_type = segments[segment_index].route_type;
var line = trips + "\t" + route_type + "\t" + coords + "\n";
fs.appendFileSync("./output/" + argv.gtfs + "/data_" +
pathSuffix + ".lines", line, "utf8", function(err) {
if (err) throw err;
});
}
}
}
last_px = px;
last_shape = shape;
}
last_trips = trips;
if ((sequences_length - working) % 5 == 0)
debug((sequences_length - working) + " left")
working += one;
}
fs.writeFileSync("./output/" + argv.gtfs + "/maxmin_" + pathSuffix + ".lines", max + "\n" + min, "utf8");
debug("Files written.");
}
function debug(msg) {
if (argv.verbose) {
var now = (new Date());
console.log(now.getHours() + "h" + now.getMinutes() + "m: " + msg);
}
}