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

getChildren method for getting children of a cluster on the next zoom #31

Merged
merged 1 commit into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
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
36 changes: 30 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,25 @@ SuperCluster.prototype = {
var clusters = [];
for (var i = 0; i < ids.length; i++) {
var c = tree.points[ids[i]];
clusters.push(c.id !== -1 ? this.points[c.id] : getClusterJSON(c));
clusters.push(c.numPoints === 1 ? this.points[c.id] : getClusterJSON(c));
}
return clusters;
},

getChildren: function (clusterId, zoom) {
var origin = this.trees[zoom + 1].points[clusterId];
var r = this.options.radius / (this.options.extent * Math.pow(2, zoom));
var points = this.trees[zoom + 1].within(origin.x, origin.y, r);
var children = [];
for (var i = 0; i < points.length; i++) {
var c = this.trees[zoom + 1].points[points[i]];
if (c.parentId === clusterId) {
children.push(c.numPoints === 1 ? this.points[c.id] : getClusterJSON(c));
}
}
return children;
},

getTile: function (z, x, y) {
var tree = this.trees[this._limitZoom(z)];
var z2 = Math.pow(2, z);
Expand Down Expand Up @@ -109,7 +123,7 @@ SuperCluster.prototype = {
Math.round(this.options.extent * (c.x * z2 - x)),
Math.round(this.options.extent * (c.y * z2 - y))
]],
tags: c.id !== -1 ? this.points[c.id].properties : getClusterProperties(c)
tags: c.numPoints === 1 ? this.points[c.id].properties : getClusterProperties(c)
});
}
},
Expand All @@ -133,7 +147,6 @@ SuperCluster.prototype = {
var tree = this.trees[zoom + 1];
var neighborIds = tree.within(p.x, p.y, r);

var foundNeighbors = false;
var numPoints = p.numPoints;
var wx = p.x * numPoints;
var wy = p.y * numPoints;
Expand All @@ -142,15 +155,20 @@ SuperCluster.prototype = {
var b = tree.points[neighborIds[j]];
// filter out neighbors that are too far or already processed
if (zoom < b.zoom) {
foundNeighbors = true;
b.zoom = zoom; // save the zoom (so it doesn't get processed twice)
wx += b.x * b.numPoints; // accumulate coordinates for calculating weighted center
wy += b.y * b.numPoints;
numPoints += b.numPoints;
b.parentId = i;
}
}

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

return clusters;
Expand All @@ -162,7 +180,12 @@ function createCluster(x, y, numPoints, id) {
x: x, // weighted cluster center
y: y,
zoom: Infinity, // the last zoom the cluster was processed at
id: id, // index of the source feature in the original input array

// point id: index of the source feature in the original input array
// cluster id: index of the first child of the cluster in the zoom level tree
id: id,

parentId: -1, // parent cluster id
numPoints: numPoints
};
}
Expand All @@ -189,6 +212,7 @@ function getClusterProperties(cluster) {
count >= 1000 ? (Math.round(count / 100) / 10) + 'k' : count;
return {
cluster: true,
cluster_id: cluster.id,
point_count: count,
point_count_abbreviated: abbrev
};
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/places-z0-0-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 0,
"point_count": 16,
"point_count_abbreviated": 16
}
Expand All @@ -18,6 +19,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 1,
"point_count": 18,
"point_count_abbreviated": 18
}
Expand All @@ -29,6 +31,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 2,
"point_count": 13,
"point_count_abbreviated": 13
}
Expand All @@ -40,6 +43,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 3,
"point_count": 8,
"point_count_abbreviated": 8
}
Expand All @@ -51,6 +55,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 4,
"point_count": 15,
"point_count_abbreviated": 15
}
Expand All @@ -62,6 +67,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 5,
"point_count": 4,
"point_count_abbreviated": 4
}
Expand All @@ -73,6 +79,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 8,
"point_count": 6,
"point_count_abbreviated": 6
}
Expand Down Expand Up @@ -101,6 +108,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 11,
"point_count": 3,
"point_count_abbreviated": 3
}
Expand All @@ -112,6 +120,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 13,
"point_count": 4,
"point_count_abbreviated": 4
}
Expand All @@ -123,6 +132,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 15,
"point_count": 6,
"point_count_abbreviated": 6
}
Expand Down Expand Up @@ -202,6 +212,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 21,
"point_count": 3,
"point_count_abbreviated": 3
}
Expand All @@ -213,6 +224,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 23,
"point_count": 6,
"point_count_abbreviated": 6
}
Expand Down Expand Up @@ -258,6 +270,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 26,
"point_count": 2,
"point_count_abbreviated": 2
}
Expand All @@ -269,6 +282,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 30,
"point_count": 13,
"point_count_abbreviated": 13
}
Expand All @@ -280,6 +294,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 32,
"point_count": 5,
"point_count_abbreviated": 5
}
Expand All @@ -291,6 +306,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 35,
"point_count": 2,
"point_count_abbreviated": 2
}
Expand Down Expand Up @@ -319,6 +335,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 38,
"point_count": 13,
"point_count_abbreviated": 13
}
Expand All @@ -330,6 +347,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 40,
"point_count": 4,
"point_count_abbreviated": 4
}
Expand Down Expand Up @@ -358,6 +376,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 47,
"point_count": 7,
"point_count_abbreviated": 7
}
Expand Down Expand Up @@ -420,6 +439,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 58,
"point_count": 2,
"point_count_abbreviated": 2
}
Expand All @@ -431,6 +451,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 38,
"point_count": 13,
"point_count_abbreviated": 13
}
Expand All @@ -442,6 +463,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 40,
"point_count": 4,
"point_count_abbreviated": 4
}
Expand Down Expand Up @@ -470,6 +492,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 47,
"point_count": 7,
"point_count_abbreviated": 7
}
Expand Down Expand Up @@ -498,6 +521,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 23,
"point_count": 6,
"point_count_abbreviated": 6
}
Expand All @@ -509,6 +533,7 @@
],
"tags": {
"cluster": true,
"cluster_id": 26,
"point_count": 2,
"point_count_abbreviated": 2
}
Expand Down