supercluster
supercluster copied to clipboard
Handle invalid input more gracefully
Some of my zoom levels are returning an empty list cluster, while other zoom levels are properly returning a list of clusters.
const index = new Supercluster({
radius: 200,
maxZoom: 16,
});
index.load(markers);
const clusterResponse = {};
const ZOOM_LEVELS = [9, 10, 11, 12, 13, 14, 15];
ZOOM_LEVELS.forEach((zoomLevel) => {
const clusters = index.getClusters([-180, -85, 180, 85], zoomLevel);
clusterResponse[zoomLevel.toString()] = clusters
.map(cluster => ({
id: cluster.id || cluster.properties.id,
count: cluster.properties.cluster ? cluster.properties.point_count : 1,
longitude: cluster.geometry.coordinates[0],
latitude: cluster.geometry.coordinates[1],
}));
});
Can you please provide a full reproducible minimal test case (including data)? There's isn't enough information to diagnose the issue.
It turns out the problem is that if you have a list of markers and one of the points has a null latitude or longitude. All the clusters will fail to be created. Even if all the other markers have valid latitude or longitudes.
You can recreate it yourself if you have a list of markers with lat long values and set one of the points lat or long values to null.
I think this is a bug, currently we are working around it ourselves by filtering out points with lat or long. But I think the expected behavior would be:
-
index. load()
function to filter out invalid markers. -
A warning could be raised to let user know
Warning: 5 markers were dropped due to invalid lat or long values
-
Create a function
index.getWarningMessages()
to let the user know that certain points had to be filtered out due to invalid lat or long values and let the user know what points those were.
Here is the current workaround we are doing
const filteredMarkers = markers.map(marker => marker.latitude && marker.longitude)
@mourner curious to hear your thoughts?