Skip to content
Closed
Changes from 9 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
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ export default class Supercluster {
this.points = points;

// generate a cluster object for each point and index input points into a KD-tree
// dissemble multipoint features
let clusters = [];
for (let i = 0; i < points.length; i++) {
if (!points[i].geometry) continue;
clusters.push(createPointCluster(points[i], i));
if (!points[i].geometry) {
continue;
} else if (points[i].geometry.type === 'MultiPoint') {
const newPointFeatures = multiToSingles(points[i]);
for (let j = 0; j < newPointFeatures.length; j++) {
clusters.push(createPointCluster(newPointFeatures[j], i));
}
} else {
clusters.push(createPointCluster(points[i], i));
}
}
this.trees[maxZoom + 1] = new KDBush(clusters, getX, getY, nodeSize, Float32Array);

Expand Down Expand Up @@ -380,3 +389,23 @@ function getX(p) {
function getY(p) {
return p.y;
}

function multiToSingles(multiPointFeature) {
const featureTemplate = {
'type': 'Feature',
'properties': {
},
'geometry': {
}
};
const newFeatures = [];
for (let i = 0; i < multiPointFeature.geometry.coordinates.length; i++) {
const newCoordinates = multiPointFeature.geometry.coordinates[i];
const newProperties = multiPointFeature.properties[i];
featureTemplate.geometry.properties = newProperties;
featureTemplate.geometry.coordinates = newCoordinates;
featureTemplate.geometry.type = 'Point';
newFeatures.push(featureTemplate);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This won't work because JavaScript passes objects by references, so you'll end up with an array of features with exactly the same feature. Additionally, multiPointFeature.properties is an array so [i] shouldn't be there.

return newFeatures;
}