Display points using clustering feature reduction

View on GitHubSample viewer app

Display a web map with a point feature layer that has feature reduction enabled to aggregate points into clusters.

screenshot

Use case

Feature clustering can be used to dynamically aggregate groups of points that are within proximity of each other in order to represent each group with a single symbol. Such grouping allows you to see patterns in the data that are difficult to visualize when a layer contains hundreds or thousands of points that overlap and cover each other.

How to use the sample

Pan and zoom the map to view how clustering is dynamically updated. Toggle clustering off to view the original point features that make up the clustered elements. When clustering is On, you can click on a clustered geoelement to view aggregated information and summary statistics for that cluster. When clustering is toggled off and you click on the original feature you get access to information about individual power plant features.

How it works

  1. Load a web map by constructing a Map with a PortalItem.
  2. Get the cluster enabled layer from the map's operational layers.
  3. Get the FeatureReduction from the feature layer and set the enabled property to enable or disable clustering on the feature layer.
  4. When the user clicks on the map, call identifyLayer on the feature layer and pass in the map click location and additional parameters. Calling identifyLayer will update the map view's identifyLayerResult property.
  5. When identifyLayer completes, get the Popup from the resulting IdentifyLayerResult and use it to construct a PopupManager.
  6. Get the feature's customHtmlDescription from the created PopupManager and use it to set the MapView's CalloutData detail and display the callout.

Relevant API

  • AggregateGeoElement
  • FeatureLayer
  • FeatureReduction
  • GeoElement
  • IdentifyLayerResult

About the data

This sample uses a web map that displays the Esri Global Power Plants feature layer with feature reduction enabled. When enabled, the aggregate features symbology shows the color of the most common power plant type, and a size relative to the average plant capacity of the cluster.

Tags

aggregate, bin, cluster, group, merge, normalize, reduce, summarize

Sample Code

DisplayPointsUsingClustering.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// [WriteFile Name=DisplayPointsUsingClustering, Category=DisplayInformation]
// [Legal]
// Copyright 2023 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick
import QtQuick.Controls
import Esri.ArcGISRuntime
import Esri.ArcGISRuntime.Toolkit

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set and keep the focus on MapView to enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            id: map
            item: PortalItem { itemId: "8916d50c44c746c1aafae001552bad23" }

            onLoadStatusChanged: {
                if (loadStatus !== Enums.LoadStatusLoaded)
                    return;

                busyIndicator.running = false;
            }
        }

        onMouseClicked: (mouseClick) => {
                            mapView.calloutData.visible = false;
                            if (!busyIndicator.running) {
                                busyIndicator.running = true;
                                const clickTolerance = 10;
                                const returnPopups = true;
                                mapView.identifyLayer(map.operationalLayers.get(0), mouseClick.x, mouseClick.y, clickTolerance, returnPopups);
                            }
                        }

        onIdentifyLayerStatusChanged: {
            if (identifyLayerStatus !== Enums.TaskStatusCompleted)
                return;

            busyIndicator.running = false;

            if (!mapView.identifyLayerResult)
                return;

            if (mapView.identifyLayerResult.error) {
                console.log("An error occured:", mapView.identifyLayerResult.error.message, mapView.identifyLayerResult.error.additionalMessage);
                return;
            }

            if (!mapView.identifyLayerResult.popups[0])
                return;

            const popup = mapView.identifyLayerResult.popups[0];
            const popupManager = ArcGISRuntimeEnvironment.createObject("PopupManager", {popup: popup});

            mapView.calloutData.visible = true;
            mapView.calloutData.location = popup.geoElement.geometry;
            contentText.text = popupManager.customHtmlDescription;
        }
    }

    Button {
        id: clusterToggleButton
        width: 250
        height: 50
        text: qsTr("Toggle feature clustering")
        anchors {
            top: parent.top
            right: parent.right
            margins: 15
        }
        enabled: !busyIndicator.running
        onClicked: {
            const isEnabled = map.operationalLayers.get(0).featureReduction.enabled;
            map.operationalLayers.get(0).featureReduction.enabled = !isEnabled;
            mapView.calloutData.visible = false;
        }
    }

    Callout {
        id: callout
        calloutData: mapView.calloutData
        implicitWidth: 150
        implicitHeight: contentText.implicitHeight + (contentText.implicitHeight * .05)
        contentItem: Label {
            id: contentText
            text: ""
            wrapMode: Text.WordWrap
            textFormat: Text.RichText
            horizontalAlignment: Qt.AlignHCenter
            verticalAlignment: Qt.AlignVCenter
        }
    }

    BusyIndicator {
        id: busyIndicator
        anchors.centerIn: parent
        running: true
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.