Display clusters

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. Create a map from a web map PortalItem.
  2. Get the cluster enabled layer from the map's operational layers.
  3. Get the FeatureReduction from the feature layer and call setEnabled(bool enabled) to enable or disable clustering on the feature layer.
  4. When the user clicks on the map, call identifyFeatureLayerAsync on the feature layer and pass in the map click location.
  5. 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

DisplayClusters.cppDisplayClusters.cppDisplayClusters.hDisplayClusters.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// [WriteFile Name=DisplayClusters, 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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "DisplayClusters.h"

#include "AggregateGeoElement.h"
#include "CalloutData.h"
#include "Error.h"
#include "FeatureLayer.h"
#include "FeatureReduction.h"
#include "FeatureTable.h"
#include "GeoElement.h"
#include "IdentifyLayerResult.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "Popup.h"
#include "PopupManager.h"
#include "PortalItem.h"

#include <QFuture>

using namespace Esri::ArcGISRuntime;

DisplayClusters::DisplayClusters(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(new PortalItem("8916d50c44c746c1aafae001552bad23", this), this))
{
  connect(m_map, &Map::doneLoading, this, [this](const Error& e)
  {
    if (!e.isEmpty())
    {
      qWarning() << e.message() << e.additionalMessage();
      return;
    }

    // Get the power plants feature layer for querying
    m_powerPlantsLayer = static_cast<FeatureLayer*>(m_map->operationalLayers()->first());
    m_taskRunning = false;
    emit taskRunningChanged();
  });
}

DisplayClusters::~DisplayClusters() = default;

void DisplayClusters::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<DisplayClusters>("Esri.Samples", 1, 0, "DisplayClustersSample");
}

MapQuickView* DisplayClusters::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void DisplayClusters::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  connect(m_mapView, &MapQuickView::mouseClicked, this, &DisplayClusters::onMouseClicked);

  emit mapViewChanged();
}

void DisplayClusters::onMouseClicked(const QMouseEvent &mouseClick)
{
  if (m_taskRunning)
    return;

  m_taskRunning = true;
  emit taskRunningChanged();

  m_mapView->calloutData()->setVisible(false);

  // clear cluster selection
  if (m_aggregateGeoElement)
    m_aggregateGeoElement->setSelected(false);

  // Clean up any children objects associated with this parent
  m_resultParent.reset(new QObject(this));
  m_aggregateGeoElement = nullptr;

  m_mapView->identifyLayerAsync(m_powerPlantsLayer, mouseClick.position(), 3, false, m_resultParent.get())
      .then(this, [this](IdentifyLayerResult* identifyResult)
  {
    m_taskRunning = false;
    emit taskRunningChanged();

    // Invalid identify result
    if (!identifyResult)
      return;

    if (!identifyResult->error().isEmpty())
    {
      qDebug() << "Identify error occurred:" << identifyResult->error().message() << identifyResult->error().additionalMessage();
      return;
    }

    if (identifyResult->popups().isEmpty())
      return;


    Popup* popup = identifyResult->popups().constFirst();

    // if the identified object is a cluster, select it
    m_aggregateGeoElement = dynamic_cast<AggregateGeoElement*>(popup->geoElement());
    if (m_aggregateGeoElement)
      m_aggregateGeoElement->setSelected(true);

    // Create a PopupManager with the IdentifyLayerResult's parent so it will get cleaned up as well.
    PopupManager* popupManager = new PopupManager(popup, identifyResult->parent());

    // Use the custom HTML description in the PopupManager to popuplate a Callout and display it.
    m_calloutText = popupManager->customHtmlDescription();
    m_mapView->calloutData()->setLocation(Point(popup->geoElement()->geometry()));
    m_mapView->calloutData()->setVisible(true);

    emit calloutTextChanged();
  });
}

void DisplayClusters::toggleClustering()
{
  if (m_map->loadStatus() != LoadStatus::Loaded)
    return;

  if (!m_powerPlantsLayer)
  {
    m_powerPlantsLayer = static_cast<FeatureLayer*>(m_map->operationalLayers()->first());

    // Check if the cast was successful
    if (!m_powerPlantsLayer)
      return;
  }

  m_powerPlantsLayer->featureReduction()->setEnabled(!m_powerPlantsLayer->featureReduction()->isEnabled());

  m_mapView->calloutData()->setVisible(false);
}

QString DisplayClusters::calloutText() const
{
  return m_calloutText;
}

bool DisplayClusters::taskRunning() const
{
  return m_taskRunning;
}

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