Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# mutable-supercluster [![Test](https://github.com/SegmentationFaults0/mutable_supercluster/actions/workflows/test.yml/badge.svg)](https://github.com/SegmentationFaults0/mutable_supercluster/actions/workflows/test.yml) ![NPM Version](https://img.shields.io/npm/v/mutable-supercluster)

_This repository is a fork from [mapbox/supercluster](https://github.com/mapbox/supercluster)._
_This repository is a fork from [mapbox/supercluster](https://github.com/mapbox/supercluster), further inspired by [rorystephenson/supercluster_dart](https://github.com/rorystephenson/supercluster_dart.git)._

A Node.js library for fast and mutable geospatial point clustering.

```js
const index = new Supercluster({ radius: 40, maxZoom: 16 });
const index = new Supercluster({
radius: 40,
maxZoom: 16,
getId: (point) => point.id,
});
index.load(points);

const clusters = index.getClusters([-180, -85, 180, 85], 2);
Expand Down Expand Up @@ -58,6 +62,14 @@ Returns the zoom on which the cluster expands into several children (useful for

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](https://tools.ietf.org/html/rfc7946#section-3.2) with its `geometry` a [GeoJSON Point](https://tools.ietf.org/html/rfc7946#section-3.1.2).

#### `removePoint(id)`

Removes the point with the same `id` (according to `getId`) from the cluster.

## Options

| Option | Default | Description |
Expand Down
4 changes: 3 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
html,
body,
#map {
height: 100%;
height: 95%;
margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<button onclick="addPoint();">add new point</button>
<button onclick="removePoint();">remove point</button>
<script src="index.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ markers.on("click", (e) => {
});
}
});

function addPoint() {
worker.postMessage({ addPoint: true });
}

function removePoint() {
worker.postMessage({ removePoint: true });
}
28 changes: 21 additions & 7 deletions demo/worker.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/*global importScripts Supercluster */

importScripts("../dist/supercluster.js");
importScripts("../dist/mutable-supercluster.js");

const now = Date.now();
let geojson;

let index;
let addCounter = 10;
let removeCounter = 0;

getJSON("../test/fixtures/places.json", (geojson) => {
getJSON("../test/fixtures/places.json", () => {
console.log(
`loaded ${geojson.features.length} points JSON in ${(Date.now() - now) / 1000}s`,
);

index = new Supercluster({
log: true,
radius: 60,
extent: 256,
maxZoom: 17,
}).load(geojson.features);
getId: (point) => point.pointId,
}).load(geojson.features.slice(0, 10));

console.log(index.getTile(0, 0, 0));

Expand All @@ -31,6 +32,18 @@ self.onmessage = function (e) {
),
center: e.data.center,
});
} else if (e.data.addPoint) {
if (addCounter < geojson.features.length) {
index.addPoint(geojson.features[addCounter]);
addCounter++;
}
postMessage({ ready: true });
} else if (e.data.removePoint) {
if (removeCounter < addCounter) {
index.removePoint(removeCounter);
removeCounter++;
}
postMessage({ ready: true });
} else if (e.data) {
postMessage(index.getClusters(e.data.bbox, e.data.zoom));
}
Expand All @@ -48,7 +61,8 @@ function getJSON(url, callback) {
xhr.status < 300 &&
xhr.response
) {
callback(xhr.response);
geojson = xhr.response;
callback();
}
};
xhr.send();
Expand Down
Loading