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

Added support for multipoint features #151

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
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;
}