supercluster icon indicating copy to clipboard operation
supercluster copied to clipboard

Clustering with option minPoints 1

Open michaelfarrelly opened this issue 4 years ago • 4 comments

With option minPoints: 1, my assumption would be that a cluster could exist with 1 point in the cluster area, but since minPoints is 1, it would have zero neighbors.

Including another if (!clusterProperties) clusterProperties = this._map(p, true); after the for loop solves this by setting the clusterProperties when no neighbors are found.

Fork with change: https://github.com/michaelfarrelly/supercluster/commit/3dbbc52fc0c7bb8a20cc52ac338f74b3c4ddfca0

If this against the purpose of this property, then I can revoke this request. My projects goal was to have clustering only (no points) when zoomed out.

https://github.com/mapbox/supercluster/blob/main/index.js#L266-L282

for (const neighborId of neighborIds) {
	const b = tree.points[neighborId];

	if (b.zoom <= zoom) continue;
	b.zoom = zoom; // save the zoom (so it doesn't get processed twice)

	const numPoints2 = b.numPoints || 1;
	wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center
	wy += b.y * numPoints2;

	b.parentId = id;

	if (reduce) {
		if (!clusterProperties) clusterProperties = this._map(p, true);
		reduce(clusterProperties, this._map(b));
	}
}

=>

for (const neighborId of neighborIds) {
	const b = tree.points[neighborId];

	if (b.zoom <= zoom) continue;
	b.zoom = zoom; // save the zoom (so it doesn't get processed twice)

	const numPoints2 = b.numPoints || 1;
	wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center
	wy += b.y * numPoints2;

	b.parentId = id;

	if (reduce) {
		if (!clusterProperties) clusterProperties = this._map(p, true);
		reduce(clusterProperties, this._map(b));
	}
}

if (!clusterProperties) clusterProperties = this._map(p, true);

michaelfarrelly avatar Nov 23 '20 22:11 michaelfarrelly

Hi!

We encounter the same situation on my project. It would be nice if we you can support this case 🙏

ghoullier avatar May 28 '21 14:05 ghoullier

Hi. We encountered the same situation too. Would be great to be able to set minPoints with 1 so that everything shown on the map is a cluster. The markers will be shown when the user zooms in, based on the maxZoom option which will disable clustering.

nekkon avatar Nov 11 '21 17:11 nekkon

Encountered same situation BUT : My opinion is that the minPointspurpose is different from the clusterOnly purpose where the getClustersmethod should only return clusters. The goal of the minPoints options is to define the minimum of points to be able to create a Cluster. Whereas a clusterOnly options would force the algorithm to return clusters.

IMO Both parameters could be used, for ex to have clusters with a minPoints = 3 and clustersOnly = truewhere it would form normal clusters of 3 points minimum, and the other points would be clusterized in clusters of 1 point.

shide1989 avatar Nov 26 '21 14:11 shide1989

Same for me. clusterOnly can be a good property to have a proper display with clusters only.

FR073N avatar Feb 15 '22 16:02 FR073N

Any reference/example on how this be achieved?

spaceod avatar Dec 22 '22 13:12 spaceod

@spaceod something like this ?

const supercluster = new Supercluster({
      minPoints: 1,
      minZoom: 1,
      radius: 150,
    }).load();

...
supercluster
    .getClusters([-180, -85, 180, 85], zoomLevel)
    .filter((c) => c.properties.cluster === true);

shide1989 avatar Dec 22 '22 13:12 shide1989

Encountered the same situation. Intuitively used minPoints: 1 but it didn't work

maxflex avatar Aug 21 '23 16:08 maxflex