Display a web map with a point feature layer that has feature reduction enabled to aggregate points into clusters.
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
- Create a map from a web map
PortalItem
. - Get the cluster enabled layer from the map's operational layers.
- Get the
FeatureReduction
from the feature layer and callsetEnabled(bool enabled)
to enable or disable clustering on the feature layer. - When the user clicks on the map, call
identifyFeatureLayerAsync
on the feature layer and pass in the map click location. - Get the
Popup
from the resultingIdentifyLayerResult
and use it to construct aPopupManager
. - Get the feature's
customHtmlDescription
from the createdPopupManager
and use it to set the MapView'sCalloutData
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
// [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;
}