mutable-supercluster
v1.1.0
Published
A library for fast and mutable geospatial point clustering.
Downloads
50
Maintainers
Readme
mutable-supercluster

This repository is a fork from mapbox/supercluster, further inspired by rorystephenson/supercluster_dart.
A Node.js library for fast and mutable geospatial point clustering.
const index = new Supercluster({
radius: 40,
maxZoom: 16,
getId: (point) => point.id,
});
index.load(points);
const clusters = index.getClusters([-180, -85, 180, 85], 2);Install
Install using NPM (npm install mutable-supercluster), then:
// import as a ES module in Node
import Supercluster from "mutable-supercluster";Methods
load(points)
Loads an array of GeoJSON Feature objects. Each feature's geometry must be a GeoJSON Point. Loading the points is destructive, so the provided list will be cleared.
getClusters(bbox, zoom)
For the given bbox array ([westLng, southLat, eastLng, northLat]) and integer zoom, returns an array of clusters and points as GeoJSON Feature objects.
getTile(z, x, y)
For a given zoom and x/y coordinates, returns a geojson-vt-compatible JSON tile object with cluster/point features.
getChildren(clusterId)
Returns the children of a cluster (on the next zoom level) given its id (cluster_id value from feature properties).
getLeaves(clusterId, limit = 10, offset = 0)
Returns all the points of a cluster (given its cluster_id), with pagination support:
limit is the number of points to return (set to Infinity for all points),
and offset is the amount of points to skip (for pagination).
getClusterExpansionZoom(clusterId)
Returns the zoom on which the cluster expands into several children (useful for "click to zoom" feature) given the cluster's cluster_id.
updatePointProperties(id, properties)
Updates the point in the cluster with the same id (according to getId), with the given properties. If the given id is not present in the cluster, this method will throw an error. The location of the point (point.geometry.coordinates) can not be updated through this method.
addPoint(point)
Adds the given point to the cluster. Just like load(), the given point must be a GeoJSON Feature with its geometry a GeoJSON Point.
removePoint(id)
Removes the point with the same id (according to getId) from the cluster.
Options
| Option | Default | Description | | ---------- | ------- | ------------------------------------------------------------------------ | | minZoom | 0 | Minimum zoom level at which clusters are generated. | | maxZoom | 16 | Maximum zoom level at which clusters are generated. | | minPoints | 2 | Minimum number of points to form a cluster. | | radius | 40 | Cluster radius, in pixels. | | extent | 512 | (Tiles) Tile extent. Radius is calculated relative to this value. | | zoomFactor | 2 | The factor with which the detail increases each zoom level. | | nodeSize | 9 | Size of the R-tree nodes. Affects performance. | | log | false | Whether timing info should be logged. | | generateId | false | Whether to generate ids for input features in vector tiles. | | getId | null | An function that accesses a unique id field in a point. Can not be null. |
Property map/reduce options
In addition to the options above, Supercluster supports property aggregation with the following two options:
map: a function that returns cluster properties corresponding to a single point.reduce: a reduce function that merges properties of two clusters into one.
Example of setting up a sum cluster property that accumulates the sum of myValue property values:
const index = new Supercluster({
map: (props) => ({ sum: props.myValue }),
reduce: (accumulated, props) => {
accumulated.sum += props.sum;
},
});The map/reduce options must satisfy these conditions to work correctly:
mapmust return a new object, not existingpropertiesof a point, otherwise it will get overwritten.reducemust not mutate the second argument (props).
Developing Supercluster
npm install # install dependencies
npm run build # generate dist/supercluster.js and dist/supercluster.min.js
npm test # run tests