Turf.jsMeta

turf.getCluster

What is turf.getCluster?

turf.getCluster extracts a subset of a FeatureCollection by matching a filter against each Feature's properties. It is the canonical way to pull out one cluster from the output of turf.clustersKmeans or turf.clustersDbscan.

JavaScript
turf.getCluster(geojson, filter) → FeatureCollection

The filter may be a property name (string), an array of property names, a property object, or a predicate function — any shape accepted by the underlying filtering routine.

When would you use turf.getCluster?

Use turf.getCluster after running a clustering algorithm when you need to work with one cluster at a time — computing the convex hull of cluster 3, rendering cluster A in a different colour, or exporting a single cluster as its own file.

Because the filter accepts objects and functions, getCluster doubles as a general-purpose property filter — it is a convenient shortcut when you do not want to hand-roll a .filter(f => f.properties.x === y) over a FeatureCollection.

JavaScript
1const clustered = turf.clustersKmeans(points, { numberOfClusters: 5 });
2const cluster2 = turf.getCluster(clustered, { cluster: 2 });

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/clusters and import import { getCluster } from '@turf/clusters', or use turf.getCluster from the @turf/turf bundle.

What does the filter argument accept?

Strings (presence of a property), arrays of strings (presence of all), objects (property equality), or predicate functions (props, i) => boolean.

Does getCluster preserve order?

Yes — the returned FeatureCollection keeps the original iteration order of the matched Features.

When should I use clusterEach instead?

Use clusterEach when you want to iterate over every cluster one at a time. Use getCluster when you need only one cluster out of many (or a property-filtered subset).