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

Varying initial weights for points #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions demo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let index;
getJSON('../test/fixtures/places.json', (geojson) => {
console.log(`loaded ${ geojson.length } points JSON in ${ (Date.now() - now) / 1000 }s`);

geojson.features.forEach((d) => { d.properties.weight = Math.random() * 1000; });
index = new Supercluster({
log: true,
radius: 60,
Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ export default class Supercluster {
const tree = this.trees[zoom + 1];
const neighborIds = tree.within(p.x, p.y, r);

let weight = p.weight || 1;
let numPoints = p.numPoints || 1;
let wx = p.x * numPoints;
let wy = p.y * numPoints;
let wx = p.x * weight;
let wy = p.y * weight;

const clusterProperties = reduce ? this._map(p, true) : null;

Expand All @@ -243,10 +244,12 @@ export default class Supercluster {
if (b.zoom <= zoom) continue;
b.zoom = zoom; // save the zoom (so it doesn't get processed twice)

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

weight += weight2;
numPoints += numPoints2;
b.parentId = id;

Expand All @@ -259,7 +262,7 @@ export default class Supercluster {
clusters.push(p);
} else {
p.parentId = id;
clusters.push(createCluster(wx / numPoints, wy / numPoints, id, numPoints, clusterProperties));
clusters.push(createCluster(wx / weight, wy / weight, id, numPoints, weight, clusterProperties));
}
}

Expand All @@ -276,14 +279,15 @@ export default class Supercluster {
}
}

function createCluster(x, y, id, numPoints, properties) {
function createCluster(x, y, id, numPoints, weight, properties) {
return {
x, // weighted cluster center
y,
zoom: Infinity, // the last zoom the cluster was processed at
id, // encodes index of the first child of the cluster and its zoom level
parentId: -1, // parent cluster id
numPoints,
weight,
properties
};
}
Expand All @@ -293,6 +297,7 @@ function createPointCluster(p, id) {
return {
x: lngX(x), // projected point coordinates
y: latY(y),
weight: p.properties.weight || 1,
zoom: Infinity, // the last zoom the point was processed at
index: id, // index of the source feature in the original input array,
parentId: -1 // parent cluster id
Expand Down