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

offset cluster ids, add index ids to non cluster points #121

Merged
merged 7 commits into from
Nov 5, 2019
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
42 changes: 30 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export default class Supercluster {
}

getChildren(clusterId) {
const originId = clusterId >> 5;
const originZoom = clusterId % 32;
const {originId, originZoom} = this._decodeClusterId(clusterId);
const errorMsg = 'No cluster with the specified id.';

const index = this.trees[originZoom];
Expand Down Expand Up @@ -151,14 +150,14 @@ export default class Supercluster {
}

getClusterExpansionZoom(clusterId) {
let clusterZoom = (clusterId % 32) - 1;
while (clusterZoom <= this.options.maxZoom) {
let expansionZoom = this._decodeClusterId(clusterId).originZoom - 1;
while (expansionZoom <= this.options.maxZoom) {
const children = this.getChildren(clusterId);
clusterZoom++;
expansionZoom++;
if (children.length !== 1) break;
clusterId = children[0].properties.cluster_id;
}
return clusterZoom;
return expansionZoom;
}

_appendLeaves(result, clusterId, limit, offset, skipped) {
Expand Down Expand Up @@ -192,18 +191,30 @@ export default class Supercluster {
_addTileFeatures(ids, points, x, y, z2, tile) {
for (const i of ids) {
const c = points[i];
const isCluster = c.numPoints;
const f = {
type: 1,
geometry: [[
Math.round(this.options.extent * (c.x * z2 - x)),
Math.round(this.options.extent * (c.y * z2 - y))
]],
tags: c.numPoints ? getClusterProperties(c) : this.points[c.index].properties
tags: isCluster ? getClusterProperties(c) : this.points[c.index].properties
};
const id = c.numPoints ? c.id : this.points[c.index].id;
if (id !== undefined) {
f.id = id;

// assign id
let id;
if (isCluster) {
id = c.id;
} else if (this.options.generateId) {
// optionally generate id
id = c.index;
} else if (this.points[c.index].id) {
// keep id if already assigned
id = this.points[c.index].id;
}

if (id !== undefined) f.id = id;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mourner ok i

  • put id assignment behind an undef check
  • put id generation behind the generateId option (not sure if that is directly avail from the mapbox gl options)
  • persisted already existing ids if the generateId option is false, otherwise we overwrite it -- could see that being reversed


tile.features.push(f);
}
}
Expand Down Expand Up @@ -234,8 +245,8 @@ export default class Supercluster {

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

// encode both zoom and point index on which the cluster originated
const id = (i << 5) + (zoom + 1);
// encode both zoom and point index on which the cluster originated -- offset by total length of features
const id = (i << 5) + (zoom + 1) + this.points.length;
Copy link
Member

Choose a reason for hiding this comment

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

If we're changing the meaning of cluster id, we should also change how it's decoded in methods like getLeaves, getChildren and getClusterExpansionZoom, otherwise those will break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok will change now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, updated decoding for getChildren and getClusterExpansionZoom -- I didn't see where it was decoded in the function series starting at getLeaves, am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

getLeaves decodes it through getChildren so should be good 👍


for (const neighborId of neighborIds) {
const b = tree.points[neighborId];
Expand Down Expand Up @@ -266,6 +277,13 @@ export default class Supercluster {
return clusters;
}

_decodeClusterId(clusterId) {
const decremented = clusterId - this.points.length;
const originId = decremented >> 5;
const originZoom = decremented % 32;
return {originZoom, originId};
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this the correct decoding @mourner

_map(point, clone) {
if (point.numPoints) {
return clone ? extend({}, point.properties) : point.properties;
Expand Down
Loading