Clustering in ArcGIS JSAPI
Clustering
The ArcGIS API for JavaScript supports displaying large amounts of features. Points on points on points, for days. That’s cool and all, but depending on how dense the data is, it might not make much sense. You might not even be able to discern many patterns in large amounts of data just sprawled out on a map. But if you enable clustering on your data, sometimes you can make things pop. You could determine where most of the data is clustered, even what dominant attributes might exist in those clusters in a quick visual representation.
Probably the easiest way to get started is to enable clustering in the online MapViewer. But you’re not limited to that. You can enable clustering on features in your apps using featureReduction. Even if you bring in a hosted dataset with a defined visualization, you can still enable it. Maybe don’t use firefly symbols when you do though, just speaking from experience.
const layer = new FeatureLayer({
portalItem: {
id: itemId
},
featureReduction: {
type: 'cluster',
clusterRadius: 100
}
});
Make them useful
You’re not limited to just visually displaying clusters. Sometimes you want to interact with them, maybe find all the features that make up the clustered feature. You can use query.aggregateIds
. This is like an objectId
, but for clusters. When you use it with a query, it will return all the child features that comprise the clustered feature.
const ids = graphic.getObjectId();
query.aggregateIds = [ id ];
const { features } = await layerView.queryFeatures(query);
Once you have all the features, you can do something with them, display them or maybe run them through geometryEngine
. You can display a convexHull of the features, but first you need to union all the geometries.
const geometries = features.map((x) => x.geometry);
const geometry = union(geometries);
const hull = convexHull(geometry);
Now you can display the convexHull of the clustered features, use them for some sort of an analysis, the possibilities on endless. Maybe not endless, but you do what you gotta do.
For more details, check out the video below!