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

Feature: get zoom level where a point gets unclustered #205

Open
wants to merge 5 commits 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ and `offset` is the amount of points to skip (for pagination).

Returns the zoom on which the cluster expands into several children (useful for "click to zoom" feature) given the cluster's `cluster_id`.

#### `getPointUnclusterZoom(point)`

Returns the zoom on which a point appears unclustered. The point must be provided as array with `[Lng, Lat]`.

## Options

| Option | Default | Description |
Expand Down
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ export default class Supercluster {
return expansionZoom;
}

getPointUnclusterZoom(point) {
const [lng, lat] = point;
const pointX = fround(lngX(lng));
const pointY = fround(latY(lat));

let expansionZoom = this.options.minZoom;
while (expansionZoom < this.options.maxZoom) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since points are more likely to get clustered on higher zooms, it would likely be faster to iterate starting from maxZoom and up until there are no points in the location or are all clusters.

const tree = this.trees[expansionZoom];

const pointIdxs = tree.within(pointX, pointY, 0);

const unclustered = pointIdxs.some(
idx => tree.data[idx].parentId !== -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong — tree.data layout is different now as I mentioned, it's a flat array of numbers. It should look something like this:

Suggested change
idx => tree.data[idx].parentId !== -1
idx => tree.data[this.stride * idx + OFFSET_PARENT] !== -1

But even that, this doesn't look right, because parentId will not be -1 for ALL points that eventually end up in a cluster regardless of zoom level, so this logic doesn't work here. Instead I'd suggest checking whether number of points equals 1 (tree.data[this.stride * idx + OFFSET_NUM] === 1).

);
if (unclustered) return expansionZoom;

expansionZoom++;
}

return expansionZoom;
}

_appendLeaves(result, clusterId, limit, offset, skipped) {
const children = this.getChildren(clusterId);

Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,17 @@ test('makes sure unclustered point coords are not rounded', () => {

assert.deepEqual(index.getTile(20, 1028744, 656754).features[0].geometry[0], [421, 281]);
});

test('returns point uncluster zoom level', () => {
const points = [
{type: 'Feature', geometry: {type: 'Point', coordinates: [173, 53]}},
{type: 'Feature', geometry: {type: 'Point', coordinates: [174, 54]}}
];
const index = new Supercluster({maxZoom: 19}).load(points);
const cluster = index.getClusters([172, 52, 175, 55], 1)[0];

const clusterExpansionZoom = index.getClusterExpansionZoom(cluster.id);
const pointUnclusterZoom = index.getPointUnclusterZoom(points[0].geometry.coordinates);

assert.deepEqual(clusterExpansionZoom, pointUnclusterZoom);
});