Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor code #37

Merged
merged 2 commits into from
Feb 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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