Skip to content

Commit

Permalink
offset cluster ids, add index ids to non cluster points (#121)
Browse files Browse the repository at this point in the history
* offset cluster ids by features.length, provide features index ids

* check for undef, put id gen behind option

* update clusterId decoding

* update var

* fix linting

* fix tests

* remove yarn.lock
  • Loading branch information
Clifton Campbell authored and mourner committed Nov 5, 2019
1 parent ef3f6a6 commit 1f984c3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 71 deletions.
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;

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

let clusterProperties = reduce && numPoints > 1 ? 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;

for (const neighborId of neighborIds) {
const b = tree.points[neighborId];
Expand Down Expand Up @@ -267,6 +278,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};
}

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

0 comments on commit 1f984c3

Please sign in to comment.