What is @turf/clusters-dbscan?
@turf/clusters-dbscan is a module from the Turf.js library that provides functionality for clustering geographical points using the DBSCAN (Density-Based Spatial Clustering of Applications with Noise) algorithm. This is useful for identifying clusters of points in spatial data, which can be applied in various fields such as geographic information systems (GIS), urban planning, and environmental monitoring.
What are @turf/clusters-dbscan's main functionalities?
Cluster Points
This feature allows you to cluster geographical points based on their proximity to each other. The code sample demonstrates how to create a collection of points and then apply the DBSCAN algorithm to identify clusters within a specified distance (0.3 units) and a minimum number of points (2) to form a cluster.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([0.1, 0.1]),
turf.point([0.2, 0.2]),
turf.point([10, 10]),
turf.point([10.1, 10.1])
]);
const clustered = turf.clustersDbscan(points, 0.3, { minPoints: 2 });
console.log(clustered);
Identify Noise Points
This feature helps in identifying noise points, which are points that do not belong to any cluster. The code sample shows how to filter out noise points from the clustered result.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([0.1, 0.1]),
turf.point([0.2, 0.2]),
turf.point([10, 10]),
turf.point([10.1, 10.1])
]);
const clustered = turf.clustersDbscan(points, 0.3, { minPoints: 2 });
const noisePoints = clustered.features.filter(feature => feature.properties.cluster === 'noise');
console.log(noisePoints);
Other packages similar to @turf/clusters-dbscan
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 clusters of varying sizes. Unlike @turf/clusters-dbscan, which uses the DBSCAN algorithm, Supercluster uses a hierarchical clustering approach optimized for map rendering.
kdbush
KDBush is a very fast static spatial index for 2D points based on a flat KD-tree. It is used for indexing and querying points in 2D space. While it does not perform clustering like @turf/clusters-dbscan, it can be used in conjunction with clustering algorithms to efficiently query and manage spatial data.
geocluster
Geocluster is a simple clustering library for geographical points. It uses a different clustering approach compared to DBSCAN and is designed to be easy to use for basic clustering needs. It is less feature-rich compared to @turf/clusters-dbscan but can be a good choice for simpler use cases.
@turf/clusters-dbscan
clustersDbscan
Takes a set of points and partition them into clusters according to https://en.wikipedia.org/wiki/DBSCAN data clustering algorithm.
Parameters
-
points
FeatureCollection<Point> to be clustered
-
maxDistance
number Maximum Distance between any point of the cluster to generate the clusters (kilometers by default, see options)
-
options
Object Optional parameters (optional, default {}
)
options.units
string in which maxDistance
is expressed, can be degrees, radians, miles, or kilometers (optional, default "kilometers"
)options.mutate
boolean Allows GeoJSON input to be mutated (optional, default false
)options.minPoints
number Minimum number of points to generate a single cluster,
points which do not meet this requirement will be classified as an 'edge' or 'noise'. (optional, default 3
)
Examples
var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});
var maxDistance = 100;
var clustered = turf.clustersDbscan(points, maxDistance);
var addToMap = [clustered];
Returns FeatureCollection<Point> Clustered Points with an additional two properties associated to each Feature:* {number} cluster - the associated clusterId
- {string} dbscan - type of point it has been classified as ('core'|'edge'|'noise')
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-dbscan
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf