-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.js
593 lines (499 loc) · 14.3 KB
/
datasource.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
(function($) {
"use strict";
/* REQUIRES: jQuery, jBinary, jDataView */
// establish the root object (window in browsers))
var root = this;
// private properties
var load_status = false; // false before loading, deferred object during loading, true after loading
var trips, stations; // data
var distances, angles; // computed
function _int2(v) {
return Math.floor(v / 0x100000000);
}
// public functions
var DataSource = {
// [duration, gender, member, minute, year, month, weekday, hour, end station, start station].
FIELDS: {
stationStart: function(v) { return v & 0xff; },
stationEnd: function(v) { return v >> 8 & 0xff; },
startYear: function(v) { return 2011 + (v >> 28 & 7); },
startMonth: function(v) { return v >> 24 & 15; },
startWeekday: function(v) { return v >> 21 & 7; },
startHour: function(v) { return v >> 16 & 31; },
startMinute: function(v) { return 15 * ((v >> 31 & 1) + (2 * (_int2(v) & 1))); },
duration: function(v) { return _int2(v) >> 4 & 0xff; },
gender: function(v) { return _int2(v) >> 2 & 3; },
member: function(v) { return _int2(v) >> 1 & 1; }
},
AGGREGATORS: {
sum: {
ingest: function(v, u) {
return (u || 0) + (v || 0);
}
},
mean: {
ingest: function(v, u) {
if (Number.isNaN(u)) return v;
if (!v) {
return {count: 1, sum: u};
}
v.count++;
v.sum += u;
return v;
},
finalize: function(v) {
return v.sum / v.count;
}
},
min: {
ingest: function(v, u) {
return Math.min(v || Number.POSITIVE_INFINITY, u || Number.POSITIVE_INFINITY);
}
},
max: {
ingest: function(v, u) {
return Math.max(v || Number.NEGATIVE_INFINITY, u || Number.NEGATIVE_INFINITY);
}
}
},
COMPUTED: {
distance: {
initialize: function() {
if (!distances) {
_cacheDistances();
}
},
compute: function(trip) {
return distances[trip & 0xffff];
}
},
angle: {
initialize: function() {
if (!angles) {
_cacheAngles();
}
},
compute: function(trip) {
return angles[trip & 0xffff];
}
}
},
isLoaded: function() {
return true === load_status;
},
loadStations: function(src_stations) {
// already loaded
if (true === load_status) {
return $.Deferred().resolveWith(this).promise();
}
var dfd_stations = new $.Deferred(), that = this;
$.ajax({
dataType: "json",
url: src_stations
}).done(function (data) {
// notify progress
dfd_stations.notifyWith(that, ["downloaded-stations"]);
// confirm that it is indeed an array
if ($.isArray(data)) {
// store stations
if (!stations) {
stations = data;
}
// not really needed, but consistent with binary
dfd_stations.notifyWith(that, ["parsed-stations"]);
// mark as resolved
dfd_stations.resolveWith(that);
}
else {
// reject
dfd_stations.rejectWith(that, ["Expected array of stations."]);
}
}).fail(function (jqXHR, text, err) {
// reject
dfd_stations.rejectWith(that, [err, text]);
});
return dfd_stations.promise();
},
loadData: function(src_trips, src_stations) {
// already loaded
if (true === load_status) {
return $.Deferred().resolveWith(this).promise();
}
// is loading?
if (false !== load_status) {
return load_status;
}
// make a promise
var dfd_trips = new $.Deferred(), dfd_stations, that = this;
jBinary.loadData(src_trips, function(err, data) {
// ran into an error?
if (err) {
dfd_trips.rejectWith(that, [err]);
return;
}
// notify progress
dfd_trips.notifyWith(that, ["downloaded-trips"]);
// create data view
var bin = DataView ? new DataView(data) : new jDataView(data);
// parse
try {
_parseFromDataView(bin);
}
catch (e) {
dfd.rejectWith(that, [err]);
return;
}
// notify progress
dfd_trips.notifyWith(that, ["parsed-trips"]);
// set loaded and resolve
load_status = true;
dfd_trips.resolveWith(that);
});
// stations can be loaded separately
if (stations) {
dfd_stations = new $.Deferred();
dfd_stations.resolveWith(that);
}
else {
dfd_stations = this.loadStations(src_stations);
}
// store pending load status to avoid duplicate loads
return (load_status = $.when(dfd_trips, dfd_stations));
},
stations: function() {
var ret = [];
for (var i = 0; i < stations.length; ++i) {
ret.push($.extend({}, stations[i]));
}
return ret;
},
stationsByID: function() {
var ret = {};
for (var i = 0; i < stations.length; ++i) {
ret[stations[i].id] = $.extend({}, stations[i]);
}
return ret;
},
select: function(filters) {
// cached filter set? just return array
if ($.isArray(filters)) {
return filters.slice();
}
// received set of filters, evaluate all rows
var cbFilters = filters ? _compileFiltersToCb(filters) : null;
// return all raw data (could be expensive memory wise)
if (null === cbFilters) {
return trips.slice();
}
// array of matching entries
var ret = [];
for (var i = 0; i < trips.length; ++i) {
// append it
if (cbFilters(trips[i])) {
ret.push(trips[i]);
}
}
return ret;
},
query: function(filters, grouper, value, aggregator) {
var cbGrouper = grouper ? _compileGrouperToCb(grouper) : function() { return 0; };
var cbValue = value ? _compileValueToCb(value) : function() { return 1; };
var cbAggregator = _compileAggregatorToCb(aggregator || "sum");
// variables for iterating
var i;
var group, val, ret = {};
// allow passing in a set of filters
if ($.isArray(filters)) {
// received array of prefiltered rows, just evaluate those rows
var t;
for (i = 0; i < filters.length; ++i) {
t = filters[i];
// assemble group
group = cbGrouper(t);
// get value
val = cbValue(t);
// aggregate
ret[group] = cbAggregator.ingest(ret[group], val);
}
}
else {
// received set of filters, evaluate all rows
var cbFilters = filters ? _compileFiltersToCb(filters) : null;
for (i = 0; i < trips.length; ++i) {
// check filters
if (cbFilters && !cbFilters(trips[i])) {
continue;
}
// assemble group
group = cbGrouper(trips[i]);
// get value
val = cbValue(trips[i]);
// aggregate
ret[group] = cbAggregator.ingest(ret[group], val);
}
}
// finalize
if (cbAggregator.finalize) {
for (var prop in ret) {
if (ret.hasOwnProperty(prop)) {
ret[prop] = cbAggregator.finalize(ret[prop]);
}
}
}
// no grouper? return single value
if (!grouper) {
return ret[0];
}
return ret;
},
cacheFilter: function(filters, threshold) {
// compile filters to callback
var cbFilters = filters ? _compileFiltersToCb(filters) : null;
// include everything? nothing to cache
if (!cbFilters) {
return filters;
}
// default threshold
if (typeof threshold === "undefined") {
threshold = 0.25;
}
// when to check threshold
var check_threshold = Math.floor(trips.length / 4);
// return
var ret = [];
for (var i = 0; i < trips.length; ++i) {
// append it
if (cbFilters(trips[i])) {
ret.push(trips[i]);
}
// abort if not efficient
if (i === check_threshold) {
if (ret.length > (threshold * check_threshold)) {
return filters;
}
}
}
return ret;
}
};
root.DataSource = DataSource;
// private functions
function _parseFromDataView(bin) {
// get number of trips
var length = bin.getUint32(0, false);
// allocate array
trips = new Array(length);
// load trips
var first, cur, last = 0;
for (var i = 0, j = 4; i < length; ++i) {
first = bin.getUint8(j);
if (0 === (first & 128)) {
// one byte
cur = first;
j += 1;
}
else if (128 === (first & 192)) {
// two bytes
cur = ((first & 63) << 8) + bin.getUint8(j + 1);
j += 2;
}
else if (192 === (first & 224)) {
// three bytes
cur = ((first & 31) << 16) + (bin.getUint8(j + 1) << 8) + bin.getUint8(j + 2);
j += 3;
}
else if (224 === (first & 240)) {
// four bytes
cur = ((first & 15) << 24) + (bin.getUint8(j + 1) << 16) + (bin.getUint8(j + 2) << 8) + bin.getUint8(j + 3);
j += 4;
}
else if (240 === (first & 248)) {
// five bytes
cur = ((first & 7) * 0x100000000) + (bin.getUint8(j + 1) * 0x1000000) + (bin.getUint8(j + 2) << 16) + (bin.getUint8(j + 3) << 8) + bin.getUint8(j + 4);
j += 5;
}
else if (248 === (first & 252)) {
// six bytes
cur = ((first & 3) * 0x10000000000) + (bin.getUint8(j + 1) * 0x100000000) + (bin.getUint8(j + 2) * 0x1000000) + (bin.getUint8(j + 3) << 16) + (bin.getUint8(j + 4) << 8) + bin.getUint8(j + 5);
j += 6;
}
else if (252 === (first & 254)) {
// seven bytes
cur = ((first & 1) * 0x1000000000000) + (bin.getUint8(j + 1) * 0x10000000000) + (bin.getUint8(j + 2) * 0x100000000) + (bin.getUint8(j + 3) << 24) + (bin.getUint8(j + 4) << 16) + (bin.getUint8(j + 5) << 8) + bin.getUint8(j + 6);
j += 7;
}
else if (254 === first) {
// eight bytes
cur = (bin.getUint8(j + 1) * 0x1000000000000) + (bin.getUint8(j + 2) * 0x10000000000) + (bin.getUint8(j + 3) * 0x100000000) + (bin.getUint8(j + 4) * 0x1000000) + (bin.getUint8(j + 5) << 16) + (bin.getUint8(j + 6) << 8) + bin.getUint8(j + 7);
j += 8;
}
last += cur; // since difference encoded
trips[i] = last;
}
}
function _compileFiltersToCb(filters) {
switch (typeof filters) {
case "function": // allow direct callback
return filters;
case "object":
var filters_cb = [];
for (var prop in filters) {
if (filters.hasOwnProperty(prop)) {
if (null === filters[prop]) continue; // null? no filter needed
filters_cb.push(_compileFilterToCb(prop, filters[prop]));
}
}
// no filters
if (filters_cb.length === 0) {
return null;
}
// single filter
if (filters_cb.length === 1) {
return filters_cb[0];
}
return function(row) {
for (var j = 0; j < filters_cb.length; ++j) {
if (!filters_cb[j](row)) {
return false;
}
}
return true;
};
default:
throw "Unrecognized type for grouping: " + typeof filters;
}
}
function _compileFilterToCb(name, filter) {
// get value callback
if (!(name in root.DataSource.FIELDS)) {
throw "Unrecognized column for filtering: " + name;
}
var cb = DataSource.FIELDS[name];
// allow direct callbacks
if ("function" === typeof filter) {
return function(row) {
return filter(cb(row));
};
}
// array of possible values?
if ($.isArray(filter)) {
var hash = {};
for (var j = 0; j < filter.length; ++j) {
hash[filter[j]] = true;
}
return function(row) {
return true === hash[cb(row)];
};
}
// equal
return function(row) {
return filter === cb(row);
};
}
function _compileGrouperToCb(grouper) {
switch (typeof grouper) {
case "function": // allow direct callback
return grouper;
case "string":
if (grouper in DataSource.FIELDS) {
return DataSource.FIELDS[grouper];
}
throw "Unrecognized column for grouping: " + grouper;
default:
throw "Unrecognized type for grouping: " + typeof grouper;
}
}
function _compileValueToCb(value) {
switch (typeof value) {
case "function": // allow direct callback
return value;
case "string":
if (value in DataSource.FIELDS) {
return DataSource.FIELDS[value];
}
if (value in DataSource.COMPUTED) {
if (DataSource.COMPUTED[value].initialize) {
DataSource.COMPUTED[value].initialize();
}
return DataSource.COMPUTED[value].compute;
}
throw "Unrecognized column for value: " + value;
case "object":
if (value.initialize) {
value.initialize();
}
return value.compute;
default:
throw "Unrecognized type for value: " + typeof value;
}
}
function _compileAggregatorToCb(aggregator) {
switch (typeof aggregator) {
case "function": // allow direct callback
return {ingest: aggregator};
case "string":
if (aggregator in DataSource.AGGREGATORS) {
return DataSource.AGGREGATORS[aggregator];
}
throw "Unrecognized column for aggregator: " + aggregator;
case "object":
return aggregator;
default:
throw "Unrecognized type for aggregator: " + typeof aggregator;
}
}
// computed helpers
function _cacheDistances() {
var i, j, i_id, j_id, dist;
// allocate distances array
distances = new Array(256 * 256);
for (i = 0; i < stations.length; ++i) {
i_id = stations[i].id;
// set zero down diagonal
distances[i_id << 8 | i_id] = 0;
for (j = i + 1; j < stations.length; ++j) {
j_id = stations[j].id;
dist = _calculateDistance(stations[i].longitude, stations[i].latitude, stations[j].longitude, stations[j].latitude);
distances[i_id << 8 | j_id] = dist;
distances[j_id << 8 | i_id] = dist;
}
}
}
function _deg2rad(deg) {
return deg * Math.PI / 180;
}
function _calculateDistance(a_long, a_lat, b_long, b_lat) {
// radius of earth in m
var r = 6371 * 1000;
// convert to radians
var a_long_rad = _deg2rad(a_long);
var a_lat_rad = _deg2rad(a_lat);
var b_long_rad = _deg2rad(b_long);
var b_lat_rad = _deg2rad(b_lat);
var c = Math.cos(a_lat_rad) * Math.cos(b_lat_rad) * Math.cos(b_long_rad - a_long_rad) + Math.sin(a_lat_rad) * Math.sin(b_lat_rad);
return r * Math.acos(c);
}
function _cacheAngles() {
var i, j, i_id, j_id, ang;
var delta_x, delta_y;
// allocate angles array
angles = new Array(256 * 256);
for (i = 0; i < (stations.length - 1); ++i) {
i_id = stations[i].id;
// set NaN down diagonal
angles[i_id << 8 | i_id] = NaN;
for (j = i + 1; j < stations.length; ++j) {
j_id = stations[j].id;
delta_x = stations[i].longitude - stations[j].longitude;
delta_y = stations[i].latitude - stations[j].latitude;
ang = Math.atan2(delta_y, delta_x);
// positive
if (ang < 0) {
ang += 2 * Math.PI;
}
angles[i_id << 8 | j_id] = ang;
angles[j_id << 8 | i_id] = ang;
}
}
}
}).call(this, jQuery);