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

Fix small-radius clusters not always forming on higher zooms #173

Merged
merged 1 commit into from
Jan 19, 2021
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
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const defaultOptions = {
map: props => props // props => ({sum: props.my_value})
};

const fround = Math.fround || (tmp => (x => (tmp[0] = +x)))(new Float32Array(1));

export default class Supercluster {
constructor(options) {
this.options = extend(Object.create(defaultOptions), options);
Expand Down Expand Up @@ -323,8 +325,8 @@ export default class Supercluster {

function createCluster(x, y, id, numPoints, properties) {
return {
x, // weighted cluster center
y,
x: fround(x), // weighted cluster center; round for consistency with Float32Array index
y: fround(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
Expand All @@ -336,8 +338,8 @@ function createCluster(x, y, id, numPoints, properties) {
function createPointCluster(p, id) {
const [x, y] = p.geometry.coordinates;
return {
x: lngX(x), // projected point coordinates
y: latY(y),
x: fround(lngX(x)), // projected point coordinates
y: fround(latY(y)),
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
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,18 @@ test('does not crash on weird bbox values', (t) => {
t.end();
});

test('makes sure same-location points are clustered', (t) => {
const index = new Supercluster({
maxZoom: 20,
extent: 8192,
radius: 16,
log: true
}).load([
{type: 'Feature', geometry: {type: 'Point', coordinates: [-1.426798, 53.943034]}},
{type: 'Feature', geometry: {type: 'Point', coordinates: [-1.426798, 53.943034]}}
]);

t.equal(index.trees[20].ids.length, 1);

t.end();
});