-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeo-time.js
136 lines (130 loc) · 4.67 KB
/
geo-time.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
if(!input || !output || !finestZoom || !minZoom || !maxZoom) {
throw "You didn't define the arguments";
}
if (minZoom > maxZoom) {
var temp = minZoom; minZoom = maxZoom; maxZoom = temp;
}
if (finestZoom < maxZoom) {
throw "finestZoom must be >= maxZoom";
}
var map = function() {
function getDataZoom(zoom) { return Math.min(zoom + 4, finestZoom); }
function pad(n) {return n < 10 ? '0' + n : n}
function getDayDate(d) { d = new Date(d); return [pad(d.getUTCMonth()+1), pad(d.getUTCDate())].join("-"); }
function getHourDate(d) { d = new Date(d); return [pad(d.getUTCMonth()+1), pad(d.getUTCDate()), pad(d.getUTCHours())].join("-"); }
function latLongToGoogleTile(a, f, b) {
var g = Math.PI * 12756274 / 256, c = Math.PI * 6378137;
a = [f * c / 180, Math.log(Math.tan((90 + a) * Math.PI / 360)) / (Math.PI / 180) * (c / 180)];
a = (function (a, b, d) {d = g / Math.pow(2, d);a = [(a + c) / d, (b + c) / d];return [Math.ceil(a[0] / 256) - 1, Math.ceil(a[1] / 256) - 1];})(a[0], a[1], b);
return [a[0], Math.pow(2, b) - 1 - a[1]];
}
function getCoordinates() {
if (this.geo && this.geo.coordinates) {
return this.geo.coordinates;
}
else if (this && this.place && this.place.bounding_box && this.place.bounding_box.coordinates) {
var c = this.place.bounding_box.coordinates[0];
var x = Geo.distance(c[0], c[1]);
var y = Geo.distance(c[2], c[1])
if (x * y < .000055) {
// Find the middle of bounding box.
return [
Math.abs((c[0][1] + c[1][1] + c[2][1] + c[3][1]) / 4),
Math.abs((c[0][0] + c[1][0] + c[2][0] + c[3][0]) / 4),
];
}
}
return false;
}
var tiles = [];
var c = getCoordinates.apply(this);
if (c===false) {
return;
}
// Precompute all the tile values to avoid redundant computation
for (var zoom = getDataZoom(maxZoom); zoom >= minZoom; --zoom) {
tiles[zoom] = latLongToGoogleTile(c[0], c[1], zoom);
}
for (var zoom = getDataZoom(maxZoom); zoom >= minZoom; --zoom) {
var dataZoom = getDataZoom(zoom);
var tile = tiles[zoom];
var dataTile = tiles[dataZoom];
var dataTileRowSize = Math.pow(2, dataZoom - zoom);
var subIndex = (dataTile[0] % dataTileRowSize) + dataTileRowSize * (dataTile[1] % dataTileRowSize);
var baseKey = "" + zoom + "/" + tile.join(",") + "/";
counts = {};
counts[subIndex] = 1;
var emittedValue = {
total: 1,
counts: counts,
res: (dataTileRowSize * dataTileRowSize),
zoom: zoom,
};
var key = baseKey + "all";
//print("Emitting " + key);
emittedValue.timeRes = "all";
emit(key, emittedValue);
key = baseKey + getDayDate(this.created_at);
//print("Emitting " + key);
emittedValue.timeRes = "day";
emit(key, emittedValue);
key = baseKey + getHourDate(this.created_at);
//print("Emitting " + key);
emittedValue.timeRes = "hour";
emit(key, emittedValue);
}
};
var reduce = function(key, values) {
if (values.length == 1) {
return values[0];
}
var result = {
total: 0,
counts: {},
res: values[0].res,
zoom: values[0].zoom,
timeRes: values[0].timeRes,
};
for (var i = 0; i < values.length; ++i) {
var value = values[i];
result.total += value.total;
for (var j in value.counts) {
result.counts[j] = (result.counts[j] || 0) + value.counts[j];
}
}
return result;
};
/*
var finalize = function(key, value) {
var maxCount = 0;
var counts = value.counts;
var numCounts = 0;
for (var i in counts) {
maxCount = Math.max(maxCount, counts[i]);
++numCounts;
}
value.maxCount = maxCount;
value.numCounts = numCounts;
return value;
}
*/
if (db[input].count() == 0) {
print("Input collection (" + db[input] + ") is empty.");
}
else {
var startTime = new Date();
print("Input collection (" + db[input] + ") pre-count: " + db[input].count());
print("Output collection (" + db[output] + ") pre-count: " + db[output].count());
db[input].mapReduce(map, reduce, {
out: output,
scope: {
minZoom: minZoom,
maxZoom: maxZoom,
finestZoom: finestZoom,
},
//finalize: finalize,
});
print("Run time: " + ((new Date() - startTime)/1000) + " seconds");
print("Output collection (" + output + ") post-count: " + db[output].count());
print("Input collection (" + input + ") post-count: " + db[input].count());
}