Skip to content

Commit

Permalink
minor code cleanup (#37)
Browse files Browse the repository at this point in the history
* refactor code
* update comments
  • Loading branch information
jingsam authored and mourner committed Feb 8, 2017
1 parent 6420199 commit 93253f3
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,24 @@ SuperCluster.prototype = {

this.points = points;

// generate a cluster object for each point
// generate a cluster object for each point and index input points into a KD-tree
var clusters = points.map(createPointCluster);
this.trees[this.options.maxZoom + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array);

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();

// index input points into a KD-tree
this.trees[z + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array);

clusters = this._cluster(clusters, z); // create a new set of clusters for the zoom
// create a new set of clusters for the zoom and index them with a KD-tree
clusters = this._cluster(clusters, z);
this.trees[z] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array);

if (log) console.log('z%d: %d clusters in %dms', z, clusters.length, +Date.now() - now);
}

// index top-level clusters
this.trees[this.options.minZoom] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array);

if (log) console.timeEnd('total time');

return this;
Expand All @@ -81,10 +79,10 @@ SuperCluster.prototype = {
getChildren: function (clusterId, clusterZoom) {
var origin = this.trees[clusterZoom + 1].points[clusterId];
var r = this.options.radius / (this.options.extent * Math.pow(2, clusterZoom));
var points = this.trees[clusterZoom + 1].within(origin.x, origin.y, r);
var ids = this.trees[clusterZoom + 1].within(origin.x, origin.y, r);
var children = [];
for (var i = 0; i < points.length; i++) {
var c = this.trees[clusterZoom + 1].points[points[i]];
for (var i = 0; i < ids.length; i++) {
var c = this.trees[clusterZoom + 1].points[ids[i]];
if (c.parentId === clusterId) {
children.push(c.numPoints ? getClusterJSON(c) : this.points[c.id]);
}
Expand Down Expand Up @@ -218,26 +216,27 @@ SuperCluster.prototype = {

for (var j = 0; j < neighborIds.length; j++) {
var b = tree.points[neighborIds[j]];
// filter out neighbors that are too far or already processed
if (zoom < b.zoom) {
var numPoints2 = b.numPoints || 1;
b.zoom = zoom; // save the zoom (so it doesn't get processed twice)
wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center
wy += b.y * numPoints2;
numPoints += numPoints2;
b.parentId = i;

if (this.options.reduce) {
this._accumulate(clusterProperties, b);
}
// filter out neighbors that are already processed
if (b.zoom <= zoom) continue;
b.zoom = zoom; // save the zoom (so it doesn't get processed twice)

var numPoints2 = b.numPoints || 1;
wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center
wy += b.y * numPoints2;

numPoints += numPoints2;
b.parentId = i;

if (this.options.reduce) {
this._accumulate(clusterProperties, b);
}
}

if (numPoints === 1) {
clusters.push(p);
} else {
p.parentId = i;
clusters.push(createCluster(wx / numPoints, wy / numPoints, numPoints, i, clusterProperties));
clusters.push(createCluster(wx / numPoints, wy / numPoints, i, numPoints, clusterProperties));
}
}

Expand All @@ -253,15 +252,15 @@ SuperCluster.prototype = {
}
};

function createCluster(x, y, numPoints, id, properties) {
function createCluster(x, y, id, numPoints, properties) {
return {
x: x, // weighted cluster center
y: y,
zoom: Infinity, // the last zoom the cluster was processed at
id: id, // index of the first child of the cluster in the zoom level tree
properties: properties,
parentId: -1, // parent cluster id
numPoints: numPoints
numPoints: numPoints,
properties: properties
};
}

Expand Down

0 comments on commit 93253f3

Please sign in to comment.