Apply a unique value with alternate symbols at different scales.

Use case
When a layer is symbolized with unique value symbology, you can specify the visible scale range for each unique value. This is an effective strategy to limit the amount of detailed data at smaller scales without having to make multiple versions of the layer, each with a unique definition query.
Once scale ranges are applied to unique values, you can further refine the appearance of features within those scale ranges by establishing alternate symbols to different parts of the symbol class scale range.
How to use the sample
Zoom in and out of the map to see alternate symbols at each scale. The symbology changes according to the following scale ranges: 0-5000, 5000-10000, 10000-20000. To go back to the initial viewpoint, press “Reset Viewpoint”.
How it works
-
Create a
featureLayerusing the service url and add it to the map’s list of operational layers. -
Create two alternate symbols (a blue square and a yellow diamond) to be used as alternate symbols. To create an alternate symbol:
a. Create a symbol using
SimpleMarkerSymbol.b. Convert the simple marker symbol to a
MultilayerSymbolusingSimpleMarkerSymbol::toMultilayerSymbol.c. Set the valid scale range through reference properties on the multilayer point symbols blue square and yellow diamond by calling
MultilayerSymbol::setReferenceProperties(new SymbolReferenceProperties(double minScale, double maxScale, QObject *parent = nullptr));. -
Create a third multilayer symbol to be used to create a
UniqueValueclass. -
Create a unique value using the red triangle from step 3 and the list of alternate symbols from step 2.
-
Create a
UniqueValueRendererand add the unique value from step 4 to it. -
Create a purple diamond simple marker and convert it to a multilayer symbol to be used as the default symbol.
-
Set the default symbol on the unique value renderer to the purple diamond from step 6 using
setDefaultSymbol. -
Set the
fieldNameson the unique value renderer to “req_type”. -
Apply this unique value renderer to the renderer on feature layer.
Relevant API
- MultilayerSymbol
- SimpleMarkerSymbol
- SymbolReferenceProperties
- UniqueValue
- UniqueValueRenderer
About the data
The San Francisco 311 incidents layer in this sample displays point features related to crime incidents such as grafitti and tree damage that have been reported by city residents.
Tags
alternate symbols, multilayer symbol, scale based rendering, simple marker symbol, symbol reference properties, unique value, unique value renderer
Sample Code
// [WriteFile Name=ApplyUniqueValuesWithAlternateSymbols, Category=Layers]// [Legal]// Copyright 2022 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
// sample headers#include "ApplyUniqueValuesWithAlternateSymbols.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "FeatureLayer.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MultilayerPointSymbol.h"#include "Point.h"#include "ServiceFeatureTable.h"#include "SimpleMarkerSymbol.h"#include "SpatialReference.h"#include "SymbolReferenceProperties.h"#include "SymbolTypes.h"#include "UniqueValue.h"#include "UniqueValueListModel.h"#include "UniqueValueRenderer.h"#include "Viewpoint.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
ApplyUniqueValuesWithAlternateSymbols::ApplyUniqueValuesWithAlternateSymbols(QObject* parent /* = nullptr */) : QObject(parent), m_map(new Map(BasemapStyle::ArcGISTopographic, this)){ // Create the feature table ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0"), this);
// Create the feature layer using the feature table m_featureLayer = new FeatureLayer(featureTable, this);
// Add the feature layer to the map m_map->operationalLayers()->append(m_featureLayer);
// This function creates a unique value renderer using // one unique value "req_type" that includes a list of // alternate symbols for various scales createUniqueValueRenderer();}
ApplyUniqueValuesWithAlternateSymbols::~ApplyUniqueValuesWithAlternateSymbols() = default;
void ApplyUniqueValuesWithAlternateSymbols::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<ApplyUniqueValuesWithAlternateSymbols>("Esri.Samples", 1, 0, "ApplyUniqueValuesWithAlternateSymbolsSample");}
MapQuickView* ApplyUniqueValuesWithAlternateSymbols::mapView() const{ return m_mapView;}
// Set the view (created in QML)void ApplyUniqueValuesWithAlternateSymbols::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) { return; }
m_mapView = mapView; m_mapView->setMap(m_map);
// Set the initial view point upon opening the sample to focus on an area with a lot of features Viewpoint vpCenter = Viewpoint(Point(-13631205.660131, 4546829.846004, SpatialReference::webMercator()), 25000); m_mapView->setViewpointAsync(vpCenter);
queryCurrentScale(); emit mapViewChanged();}
void ApplyUniqueValuesWithAlternateSymbols::createUniqueValueRenderer(){ QList<Symbol*> alternateSymbols = createAlternateSymbols();
double minScale = 5000; double maxScale = 0; SimpleMarkerSymbol* symbol1 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Triangle, QColor("red"), 30, this); MultilayerPointSymbol* multilayerSymbol1 = symbol1->toMultilayerSymbol(); multilayerSymbol1->setReferenceProperties(new SymbolReferenceProperties(minScale, maxScale, this));
// Create a unique value with alternate symbols UniqueValue* uniqueValue = new UniqueValue("unique value", "unique values based on request type", QVariantList{"Damaged Property"}, multilayerSymbol1, alternateSymbols, this);
// Create a unique value renderer m_uniqueValueRenderer = new UniqueValueRenderer(this);
// Create and append unique value m_uniqueValueRenderer->uniqueValues()->append(uniqueValue);
m_uniqueValueRenderer->setFieldNames({"req_type"});
SimpleMarkerSymbol* defaultSym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor("purple"), 15, this); m_uniqueValueRenderer->setDefaultSymbol(defaultSym->toMultilayerSymbol());
// Set the unique value renderer on the feature layer m_featureLayer->setRenderer(m_uniqueValueRenderer);}
QList<Symbol*> ApplyUniqueValuesWithAlternateSymbols::createAlternateSymbols(){ double minScale1 = 10000; double maxScale1 = 5000; SimpleMarkerSymbol* alternateSymbol1 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor("blue"), 45, this); MultilayerPointSymbol* alternateSymbolMultilayer1 = alternateSymbol1->toMultilayerSymbol(); alternateSymbolMultilayer1->setReferenceProperties(new SymbolReferenceProperties(minScale1, maxScale1, this));
double minScale2 = 20000; double maxScale2 = 10000; SimpleMarkerSymbol* alternateSymbol2 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor("yellow"), 30, this); MultilayerPointSymbol* alternateSymbolMultilayer2 = alternateSymbol2->toMultilayerSymbol(); alternateSymbolMultilayer2->setReferenceProperties(new SymbolReferenceProperties(minScale2, maxScale2, this));
return {alternateSymbolMultilayer1, alternateSymbolMultilayer2};}
void ApplyUniqueValuesWithAlternateSymbols::resetViewpoint(){ if (m_mapView) { Viewpoint vpCenter = Viewpoint(Point(-13631205.660131, 4546829.846004, SpatialReference::webMercator()), 25000); m_mapView->setViewpointAsync(vpCenter, 5); }}
double ApplyUniqueValuesWithAlternateSymbols::currentScale() const{ return m_currentScale;}
void ApplyUniqueValuesWithAlternateSymbols::queryCurrentScale(){ connect(m_mapView, &MapQuickView::viewpointChanged, this, [this]() { m_currentScale = m_mapView->mapScale(); emit currentScaleChanged(); });}// [WriteFile Name=ApplyUniqueValuesWithAlternateSymbols, Category=Layers]// [Legal]// Copyright 2022 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]
#ifndef APPLYUNIQUEVALUESWITHALTERNATESYMBOLS_H#define APPLYUNIQUEVALUESWITHALTERNATESYMBOLS_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{ class FeatureLayer; class Map; class MapQuickView; class Renderer; class ServiceFeatureTable; class Symbol; class UniqueValueRenderer;} // namespace Esri::ArcGISRuntime
Q_MOC_INCLUDE("MapQuickView.h")
class ApplyUniqueValuesWithAlternateSymbols : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(double currentScale READ currentScale NOTIFY currentScaleChanged)
public: explicit ApplyUniqueValuesWithAlternateSymbols(QObject* parent = nullptr); ~ApplyUniqueValuesWithAlternateSymbols();
static void init(); Q_INVOKABLE void resetViewpoint();
signals: void mapViewChanged(); void currentScaleChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); void createUniqueValueRenderer(); QList<Esri::ArcGISRuntime::Symbol*> createAlternateSymbols(); void queryCurrentScale(); double currentScale() const;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_featureLayer = nullptr; Esri::ArcGISRuntime::UniqueValueRenderer* m_uniqueValueRenderer = nullptr; double m_currentScale;};
#endif // APPLYUNIQUEVALUESWITHALTERNATESYMBOLS_H// [WriteFile Name=ApplyUniqueValuesWithAlternateSymbols, Category=Layers]// [Legal]// Copyright 2022 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 QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samples
Item { // add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set and keep the focus on MapView to enable keyboard navigation forceActiveFocus(); } } Rectangle { anchors { margins: 5 left: parent.left top: parent.top } width: 200 height: childrenRect.height color: palette.base radius: 5
MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: mouse => mouse.accepted = true onDoubleClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
ColumnLayout { width: parent.width Label { text: qsTr("Current scale: 1:" + Math.round(model.currentScale)) Layout.fillWidth: true Layout.margins: 3 font { weight: Font.DemiBold } } Button { text: qsTr("Reset Viewpoint") font { weight: Font.DemiBold } Layout.margins: 3 Layout.fillWidth: true onClicked: model.resetViewpoint(); } } } // Declare the C++ instance which creates the map etc. and supply the view ApplyUniqueValuesWithAlternateSymbolsSample { id: model mapView: view }}// [WriteFile Name=ApplyUniqueValuesWithAlternateSymbols, Category=Layers]// [Legal]// Copyright 2022 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]
// sample headers#include "ApplyUniqueValuesWithAlternateSymbols.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
int main(int argc, char* argv[]){ QGuiApplication app(argc, argv); app.setApplicationName(QString("ApplyUniqueValuesWithAlternateSymbols"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample ApplyUniqueValuesWithAlternateSymbols::init();
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
#ifdef ARCGIS_RUNTIME_IMPORT_PATH_2 engine.addImportPath(ARCGIS_RUNTIME_IMPORT_PATH_2);#endif
// Set the source engine.load(QUrl("qrc:/Samples/Layers/ApplyUniqueValuesWithAlternateSymbols/main.qml"));
return app.exec();}// Copyright 2022 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.
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
ApplyUniqueValuesWithAlternateSymbols { anchors.fill: parent }}