What is @turf/clusters?
@turf/clusters is a module within the Turf.js library that provides functionality for clustering geographic points. It allows you to group points based on their proximity to each other, which is useful for various geospatial analyses such as identifying hotspots, aggregating data, and simplifying visualizations.
What are @turf/clusters's main functionalities?
Cluster K-Means
This feature allows you to perform K-Means clustering on a set of points. The code sample demonstrates how to cluster a set of points into 2 clusters using the K-Means algorithm.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 2]),
turf.point([3, 3]),
turf.point([8, 8]),
turf.point([8, 9]),
turf.point([7, 7])
]);
const clustered = turf.clustersKmeans(points, {numberOfClusters: 2});
console.log(clustered);
Cluster DBSCAN
This feature allows you to perform DBSCAN clustering on a set of points. The code sample demonstrates how to cluster a set of points using the DBSCAN algorithm with a maximum distance of 5 units and a minimum of 2 points per cluster.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 2]),
turf.point([3, 3]),
turf.point([8, 8]),
turf.point([8, 9]),
turf.point([7, 7])
]);
const clustered = turf.clustersDbscan(points, 5, 2);
console.log(clustered);
Cluster Hierarchical
This feature allows you to perform hierarchical clustering on a set of points. The code sample demonstrates how to cluster a set of points using hierarchical clustering with a minimum of 2 points per cluster and a maximum distance of 5 units.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 2]),
turf.point([3, 3]),
turf.point([8, 8]),
turf.point([8, 9]),
turf.point([7, 7])
]);
const clustered = turf.clustersHierarchical(points, {minPoints: 2, maxDistance: 5});
console.log(clustered);
Other packages similar to @turf/clusters
supercluster
Supercluster is a fast geospatial point clustering library for browsers and Node. It is designed for visualizing large datasets on maps by clustering points into groups. Compared to @turf/clusters, Supercluster is optimized for performance and is particularly useful for rendering clusters on interactive maps.
kmeans-js
kmeans-js is a simple implementation of the K-Means clustering algorithm in JavaScript. It is not specifically designed for geospatial data but can be used for general clustering tasks. Compared to @turf/clusters, kmeans-js is more general-purpose and lacks the geospatial-specific optimizations and features.
hdbscan
hdbscan is a high-performance implementation of the HDBSCAN clustering algorithm. It is designed for hierarchical density-based clustering and is suitable for large datasets. Compared to @turf/clusters, hdbscan offers more advanced clustering capabilities but requires more computational resources and is more complex to use.
@turf/clusters
getCluster
Get Cluster
Parameters
geojson
FeatureCollection GeoJSON Featuresfilter
any Filter used on GeoJSON properties to get Cluster
Examples
var geojson = turf.featureCollection([
turf.point([0, 0], {'marker-symbol': 'circle'}),
turf.point([2, 4], {'marker-symbol': 'star'}),
turf.point([3, 6], {'marker-symbol': 'star'}),
turf.point([5, 1], {'marker-symbol': 'square'}),
turf.point([4, 2], {'marker-symbol': 'circle'})
]);
var clustered = turf.clustersKmeans(geojson);
var cluster = turf.getCluster(clustered, {cluster: 0});
turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;
turf.getCluster(clustered, {'marker-symbol': 'square'}).length;
Returns FeatureCollection Single Cluster filtered by GeoJSON Properties
clusterEachCallback
Callback for clusterEach
Type: Function
Parameters
cluster
FeatureCollection? The current cluster being processed.clusterValue
any? Value used to create cluster being processed.currentIndex
number? The index of the current element being processed in the array.Starts at index 0
Returns void
clusterEach
clusterEach
Parameters
geojson
FeatureCollection GeoJSON Featuresproperty
(string | number) GeoJSON property key/value used to create clusterscallback
Function a method that takes (cluster, clusterValue, currentIndex)
Examples
var geojson = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 4]),
turf.point([3, 6]),
turf.point([5, 1]),
turf.point([4, 2])
]);
var clustered = turf.clustersKmeans(geojson);
turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {
})
var total = 0
turf.clusterEach(clustered, 'cluster', function () {
total++;
});
var values = []
turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {
values.push(clusterValue);
});
Returns void
clusterReduceCallback
Callback for clusterReduce
The first time the callback function is called, the values provided as arguments depend
on whether the reduce method has an initialValue argument.
If an initialValue is provided to the reduce method:
- The previousValue argument is initialValue.
- The currentValue argument is the value of the first element present in the array.
If an initialValue is not provided:
- The previousValue argument is the value of the first element present in the array.
- The currentValue argument is the value of the second element present in the array.
Type: Function
Parameters
previousValue
any? The accumulated value previously returned in the last invocation
of the callback, or initialValue, if supplied.cluster
FeatureCollection? The current cluster being processed.clusterValue
any? Value used to create cluster being processed.currentIndex
number? The index of the current element being processed in the
array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
clusterReduce
Reduce clusters in GeoJSON Features, similar to Array.reduce()
Parameters
geojson
FeatureCollection GeoJSON Featuresproperty
(string | number) GeoJSON property key/value used to create clusterscallback
Function a method that takes (previousValue, cluster, clusterValue, currentIndex)initialValue
any? Value to use as the first argument to the first call of the callback.
Examples
var geojson = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 4]),
turf.point([3, 6]),
turf.point([5, 1]),
turf.point([4, 2])
]);
var clustered = turf.clustersKmeans(geojson);
var initialValue = 0
turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {
return previousValue++;
}, initialValue);
var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {
return previousValue++;
}, 0);
var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {
return previousValue.concat(clusterValue);
}, []);
Returns any The value that results from the reduction.
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/clusters
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf