Skip to content

Commit

Permalink
Fix a regression with cluster aggregation (#116)
Browse files Browse the repository at this point in the history
* Fix a regression with cluster aggregation

* relax the map safeguard
  • Loading branch information
mourner authored Jan 24, 2019
1 parent e07735a commit c07a99d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ const index = new Supercluster({
});
```

Note that `reduce` must not mutate the second argument (`props`).

## Developing Supercluster

```
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default class Supercluster {
let wx = p.x * numPoints;
let wy = p.y * numPoints;

const clusterProperties = reduce ? this._map(p) : null;
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);
Expand Down Expand Up @@ -266,8 +266,13 @@ export default class Supercluster {
return clusters;
}

_map(point) {
return point.numPoints ? point.properties : this.options.map(this.points[point.index].properties);
_map(point, clone) {
if (point.numPoints) {
return clone ? extend({}, point.properties) : point.properties;
}
const original = this.points[point.index].properties;
const result = this.options.map(original);
return clone && result === original ? extend({}, result) : result;
}
}

Expand Down
8 changes: 6 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ test('returns cluster expansion zoom for maxZoom', (t) => {
test('aggregates cluster properties with reduce', (t) => {
const index = new Supercluster({
map: props => ({sum: props.scalerank}),
reduce: (a, b) => { a.sum += b.sum; }
reduce: (a, b) => { a.sum += b.sum; },
radius: 100
}).load(places.features);

t.equal(index.getTile(0, 0, 0).features[0].tags.sum, 69);
t.same(index.getTile(1, 0, 0).features.map(f => f.tags.sum).filter(Boolean),
[146, 84, 63, 23, 34, 12, 19, 29, 8, 8, 80, 35]);
t.same(index.getTile(0, 0, 0).features.map(f => f.tags.sum).filter(Boolean),
[298, 122, 12, 36, 98, 7, 24, 8, 125, 98, 125, 12, 36, 8]);

t.end();
});
Expand Down

0 comments on commit c07a99d

Please sign in to comment.