forked from mapbox/supercluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
242 lines (205 loc) · 7.28 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
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
'use strict';
var rbush = require('rbush');
module.exports = supercluster;
function supercluster(options) {
return new SuperCluster(options);
}
function SuperCluster(options) {
this.options = extend(Object.create(this.options), options);
this._initTrees();
}
SuperCluster.prototype = {
options: {
minZoom: 0, // min zoom to generate clusters on
maxZoom: 16, // max zoom level to cluster the points on
radius: 40, // cluster radius in pixels
extent: 512, // tile extent (radius is calculated relative to it)
nodeSize: 16, // size of the R-tree leaf node, affects performance
log: false // whether to log timing info
},
load: function (points) {
var log = this.options.log;
if (log) console.time('total time');
var timerId = 'prepare ' + points.length + ' points';
if (log) console.time(timerId);
// generate a cluster object for each point
var clusters = points.map(createPointCluster);
if (log) console.timeEnd(timerId);
// cluster points on max zoom, then cluster the results on previous zoom, etc.;
// results in a cluster hierarchy across zoom levels
for (var z = this.options.maxZoom; z >= this.options.minZoom; z--) {
var now = +Date.now();
this.trees[z + 1].load(clusters); // index input points into an R-tree
clusters = this._cluster(clusters, z); // create a new set of clusters for the zoom
if (log) console.log('z%d: %d clusters in %dms', z, clusters.length, +Date.now() - now);
}
this.trees[this.options.minZoom].load(clusters); // index top-level clusters
if (log) console.timeEnd('total time');
return this;
},
getClusters: function (bbox, zoom) {
var projBBox = [lngX(bbox[0]), latY(bbox[3]), lngX(bbox[2]), latY(bbox[1])];
var clusters = this.trees[this._limitZoom(zoom)].search(projBBox);
return clusters.map(getClusterJSON);
},
getTile: function (z, x, y) {
var z2 = Math.pow(2, z);
var extent = this.options.extent;
var p = this.options.radius / extent;
var clusters = this.trees[this._limitZoom(z)].search([
(x - p) / z2,
(y - p) / z2,
(x + 1 + p) / z2,
(y + 1 + p) / z2
]);
if (!clusters.length) return null;
var tile = {
features: []
};
for (var i = 0; i < clusters.length; i++) {
var c = clusters[i];
var feature = {
type: 1,
geometry: [[
Math.round(extent * (c.wx * z2 - x)),
Math.round(extent * (c.wy * z2 - y))
]],
tags: c.point ? c.point.properties : getClusterProperties(c)
};
tile.features.push(feature);
}
return tile;
},
_limitZoom: function (z) {
return Math.max(this.options.minZoom, Math.min(z, this.options.maxZoom + 1));
},
_initTrees: function () {
this.trees = [];
// make an R-Tree index for each zoom level
for (var z = 0; z <= this.options.maxZoom + 1; z++) {
this.trees[z] = rbush(this.options.nodeSize);
this.trees[z].toBBox = toBBox;
this.trees[z].compareMinX = compareMinX;
this.trees[z].compareMinY = compareMinY;
}
},
_cluster: function (points, zoom) {
var clusters = [];
var r = this.options.radius / (this.options.extent * Math.pow(2, zoom));
var bbox = [0, 0, 0, 0];
// loop through each point
for (var i = 0; i < points.length; i++) {
var p = points[i];
// if we've already visited the point at this zoom level, skip it
if (p.zoom <= zoom) continue;
p.zoom = zoom;
// find all nearby points with a bbox search
bbox[0] = p.wx - r;
bbox[1] = p.wy - r;
bbox[2] = p.wx + r;
bbox[3] = p.wy + r;
var bboxNeighbors = this.trees[zoom + 1].search(bbox);
var foundNeighbors = false;
var numPoints = p.numPoints;
var wx = p.wx * numPoints;
var wy = p.wy * numPoints;
for (var j = 0; j < bboxNeighbors.length; j++) {
var b = bboxNeighbors[j];
// filter out neighbors that are too far or already processed
if (zoom < b.zoom && distSq(p, b) <= r * r) {
foundNeighbors = true;
b.zoom = zoom; // save the zoom (so it doesn't get processed twice)
wx += b.wx * b.numPoints; // accumulate coordinates for calculating weighted center
wy += b.wy * b.numPoints;
numPoints += b.numPoints;
}
}
if (!foundNeighbors) {
clusters.push(p); // no neighbors, add a single point as cluster
continue;
}
// form a cluster with neighbors
var cluster = createCluster(p.x, p.y);
cluster.numPoints = numPoints;
// save weighted cluster center for display
cluster.wx = wx / numPoints;
cluster.wy = wy / numPoints;
clusters.push(cluster);
}
return clusters;
}
};
function toBBox(p) {
return [p.x, p.y, p.x, p.y];
}
function compareMinX(a, b) {
return a.x - b.x;
}
function compareMinY(a, b) {
return a.y - b.y;
}
function createCluster(x, y) {
return {
x: x, // cluster center
y: y,
wx: x, // weighted cluster center
wy: y,
zoom: Infinity, // the last zoom the cluster was processed at
point: null,
numPoints: 1
};
}
function createPointCluster(p) {
var coords = p.geometry.coordinates;
var cluster = createCluster(lngX(coords[0]), latY(coords[1]));
cluster.point = p;
return cluster;
}
function getClusterJSON(cluster) {
return cluster.point ? cluster.point : {
type: 'Feature',
properties: getClusterProperties(cluster),
geometry: {
type: 'Point',
coordinates: [xLng(cluster.wx), yLat(cluster.wy)]
}
};
}
function getClusterProperties(cluster) {
var count = cluster.numPoints;
var abbrev = count >= 10000 ? Math.round(count / 1000) + 'k' :
count >= 1000 ? (Math.round(count / 100) / 10) + 'k' : count;
return {
cluster: true,
point_count: count,
point_count_abbreviated: abbrev
};
}
// longitude/latitude to spherical mercator in [0..1] range
function lngX(lng) {
return lng / 360 + 0.5;
}
function latY(lat) {
var sin = Math.sin(lat * Math.PI / 180),
y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI);
return y < 0 ? 0 :
y > 1 ? 1 : y;
}
// spherical mercator to longitude/latitude
function xLng(x) {
return (x - 0.5) * 360;
}
function yLat(y) {
var y2 = (180 - y * 360) * Math.PI / 180;
return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
}
// squared distance between two points
function distSq(a, b) {
var dx = a.wx - b.wx;
var dy = a.wy - b.wy;
return dx * dx + dy * dy;
}
function extend(dest, src) {
for (var id in src) dest[id] = src[id];
return dest;
}