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 1 commit
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
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ export default class Supercluster {
return expansionZoom;
}

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

let expansionZoom = 0;
while (expansionZoom <= this.options.maxZoom) {
const tree = this.trees[expansionZoom];

const unclustered = tree.points.some(
treePoint => !treePoint.id && treePoint.x === pointX && treePoint.y === pointY
);
Copy link
Member

Choose a reason for hiding this comment

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

This essentially loops through all clusters across all zooms until it reaches the unclustered point, in many instances iterating over nearly the whole dataset. I think we could implement this much more efficiently, utilizing the R-tree indices — e.g. only look at clusters within radius of the point.

Also, what if the point does have id? This filter doesn't look right...

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your review and advises!

I improved the code and used the within function of KDBush to filter all points that don't have the same coordinate as the provided point, by setting radius to 0.

I mistakenly assumed that only clusters have an id, so I wanted to filter them first. The value of the parentId should be better suited for this.

Additionally the expansionZoom now starts at the minZoom level, instead of 0, to avoid getting undefined trees entries.

If you have some other advises for improvements, please let me know.


if (unclustered) return expansionZoom;

expansionZoom++;
}

return expansionZoom;
}

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

Expand Down