From 4715d4999e2d568f9ce645ba6c6446b4c35598b6 Mon Sep 17 00:00:00 2001 From: Ilya Boyandin Date: Sun, 24 Mar 2019 21:23:55 +0100 Subject: [PATCH] Adding input location weight property support --- demo/worker.js | 1 + index.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/demo/worker.js b/demo/worker.js index 091c585..4727158 100644 --- a/demo/worker.js +++ b/demo/worker.js @@ -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, diff --git a/index.js b/index.js index 07db99d..4478498 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; @@ -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)); } } @@ -276,7 +279,7 @@ 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, @@ -284,6 +287,7 @@ function createCluster(x, y, id, numPoints, properties) { id, // encodes index of the first child of the cluster and its zoom level parentId: -1, // parent cluster id numPoints, + weight, properties }; } @@ -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