Search in sources :

Example 1 with Bounds

use of com.mapbox.mapboxsdk.plugins.cluster.geometry.Bounds in project mapbox-plugins-android by mapbox.

the class NonHierarchicalDistanceBasedAlgorithm method getClusters.

@Override
public Set<? extends Cluster<T>> getClusters(double zoom) {
    final int discreteZoom = (int) zoom;
    final double zoomSpecificSpan = mMaxDistance / Math.pow(2, discreteZoom) / 256;
    final Set<QuadItem<T>> visitedCandidates = new HashSet<QuadItem<T>>();
    final Set<Cluster<T>> results = new HashSet<Cluster<T>>();
    final Map<QuadItem<T>, Double> distanceToCluster = new HashMap<QuadItem<T>, Double>();
    final Map<QuadItem<T>, StaticCluster<T>> itemToCluster = new HashMap<QuadItem<T>, StaticCluster<T>>();
    synchronized (mQuadTree) {
        for (QuadItem<T> candidate : mItems) {
            if (visitedCandidates.contains(candidate)) {
                // Candidate is already part of another cluster.
                continue;
            }
            Bounds searchBounds = createBoundsFromSpan(candidate.getPoint(), zoomSpecificSpan);
            Collection<QuadItem<T>> clusterItems;
            clusterItems = mQuadTree.search(searchBounds);
            if (clusterItems.size() == 1) {
                // Only the current marker is in range. Just add the single item to the results.
                results.add(candidate);
                visitedCandidates.add(candidate);
                distanceToCluster.put(candidate, 0d);
                continue;
            }
            StaticCluster<T> cluster = new StaticCluster<T>(candidate.mClusterItem.getPosition());
            results.add(cluster);
            for (QuadItem<T> clusterItem : clusterItems) {
                Double existingDistance = distanceToCluster.get(clusterItem);
                double distance = distanceSquared(clusterItem.getPoint(), candidate.getPoint());
                if (existingDistance != null) {
                    // Item already belongs to another cluster. Check if it's closer to this cluster.
                    if (existingDistance < distance) {
                        continue;
                    }
                    // Move item to the closer cluster.
                    itemToCluster.get(clusterItem).remove(clusterItem.mClusterItem);
                }
                distanceToCluster.put(clusterItem, distance);
                cluster.add(clusterItem.mClusterItem);
                itemToCluster.put(clusterItem, cluster);
            }
            visitedCandidates.addAll(clusterItems);
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) Bounds(com.mapbox.mapboxsdk.plugins.cluster.geometry.Bounds) Cluster(com.mapbox.mapboxsdk.plugins.cluster.clustering.Cluster) Point(com.mapbox.mapboxsdk.plugins.cluster.geometry.Point) HashSet(java.util.HashSet)

Aggregations

Cluster (com.mapbox.mapboxsdk.plugins.cluster.clustering.Cluster)1 Bounds (com.mapbox.mapboxsdk.plugins.cluster.geometry.Bounds)1 Point (com.mapbox.mapboxsdk.plugins.cluster.geometry.Point)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1