Use a custom dictionary style created from a web style or local style file (.stylx) to symbolize features using a variety of attribute values.

Use case
When symbolizing geoelements in your map, you may need to convey several pieces of information with a single symbol. You could try to symbolize such data using a unique value renderer, but as the number of fields and values increases, that approach becomes impractical. With a dictionary renderer you can build each symbol on-the-fly, driven by one or more attribute values, and handle a nearly infinite number of unique combinations.
How to use the sample
Use the radio buttons to toggle between the dictionary symbols from the web style and style file. Pan and zoom around the map to see the symbology from the chosen dictionary symbol style. The web style and style file are slightly different to each other to give a visual indication of the switch between the two.
How it works
- Create a
PortalItem, referring to aPortaland the item ID of the web style. - Based on the style selected:
- If the web style toggle has been selected, create a new
DictionarySymbolStylefrom the portal item usingnew DictionarySymbolStyle(PortalItem *portalItem), and load it - If the file style toggle has been selected, create a new
DictionarySymbolStyleusingDictionarySymbolStyle::createFromFile(const QString &styleFilePath)
- If the web style toggle has been selected, create a new
- Create a new
DictionaryRenderer, providing the dictionary symbol style. - Apply the dictionary renderer to a feature layer using
FeatureLayer::setRenderer(Renderer *renderer). - Add the feature layer to the map’s operational layers using
Map::OperationalLayers::append(Layer* layer).
Relevant API
- DictionaryRenderer
- DictionarySymbolStyle
- Portal
- PortalItem
About the data
Data used in this sample are from a feature layer showing a subset of restaurants in Redlands, CA hosted as a feature service with attributes for rating, style, health score, and open hours.
The feature layer is symbolized using a dictionary renderer that displays a single symbol for all of these variables. The renderer uses symbols from a custom restaurant dictionary style created from a stylx file and a web style, available as items from ArcGIS Online, to show unique symbols based on several feature attributes. The symbols it contains were created using ArcGIS Pro. The logic used to apply the symbols comes from an Arcade script embedded in the stylx file (which is a SQLite database), along with a JSON string that defines expected attribute names and configuration properties.
Additional information
For information about creating your own custom dictionary style, see the open source dictionary renderer toolkit on GitHub.
Tags
dictionary, military, portal, portal item, renderer, style, stylx, unique value, visualization
Sample Code
// [WriteFile Name=CustomDictionaryStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 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 "CustomDictionaryStyle.h"
// ArcGIS Maps SDK headers#include "DictionaryRenderer.h"#include "DictionarySymbolStyle.h"#include "FeatureLayer.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "Point.h"#include "PortalItem.h"#include "ServiceFeatureTable.h"#include "SpatialReference.h"#include "Viewpoint.h"
// Qt headers#include <QStandardPaths>#include <QtCore/qglobal.h>
using namespace Esri::ArcGISRuntime;
// helper method to get cross platform data pathnamespace{QString defaultDataPath(){ QString dataPath;
#ifdef Q_OS_IOS dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);#else dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);#endif
return dataPath;}} // namespace
CustomDictionaryStyle::CustomDictionaryStyle(QObject* parent /* = nullptr */): QObject(parent), m_map(new Map(BasemapStyle::ArcGISTopographic, this)){ // Set an initial viewpoint Viewpoint viewpoint(Point(-13'046'305, 4'036'698, SpatialReference(3857)), 5000); m_map->setInitialViewpoint(viewpoint);
// Create a feature layer from a feature service and it to the map ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/Redlands_Restaurants/FeatureServer/0"), this); m_featureLayer = new FeatureLayer(featureTable, this); m_map->operationalLayers()->append(m_featureLayer);
// Create a DictionaryRenderer using the local .stylx file DictionarySymbolStyle* localDictionaryStyle = DictionarySymbolStyle::createFromFile(defaultDataPath() + "/ArcGIS/Runtime/Data/styles/arcade_style/Restaurant.stylx", this); m_localDictionaryRenderer = new DictionaryRenderer(localDictionaryStyle, this);
// Set initial FeatureLayer renderer to the local DictionaryRenderer m_featureLayer->setRenderer(m_localDictionaryRenderer);
// Create a DictionarySymbolStyle from a portal item, using the default arcgis.com path PortalItem* portalItem = new PortalItem("adee951477014ec68d7cf0ea0579c800", this); DictionarySymbolStyle* dictSymbStyleFromPortal = new DictionarySymbolStyle(portalItem, this);
// The source feature layer fields do not match those of the the DictionarySymbolStyle so we create a fieldMap to correct this QMap<QString, QString> fieldMap; // With the following override, the feature layer's "inspection" field will be mapped to the dictionary symbol style's "healthgrade" field fieldMap["healthgrade"] = "inspection"; m_webDictionaryRenderer = new DictionaryRenderer(dictSymbStyleFromPortal, fieldMap, fieldMap, this);}
void CustomDictionaryStyle::changeDictionarySymbolStyleSource(){ m_featureLayer->setRenderer(m_featureLayer->renderer() == m_webDictionaryRenderer ? m_localDictionaryRenderer : m_webDictionaryRenderer);}
CustomDictionaryStyle::~CustomDictionaryStyle() = default;
void CustomDictionaryStyle::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<CustomDictionaryStyle>("Esri.Samples", 1, 0, "CustomDictionaryStyleSample");}
MapQuickView* CustomDictionaryStyle::mapView() const{ return m_mapView;}
// Set the view (created in QML)void CustomDictionaryStyle::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) return;
m_mapView = mapView; m_mapView->setMap(m_map);
emit mapViewChanged();}// [WriteFile Name=CustomDictionaryStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 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 CUSTOMDICTIONARYSTYLE_H#define CUSTOMDICTIONARYSTYLE_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{class DictionaryRenderer;class FeatureLayer;class Map;class MapQuickView;}
Q_MOC_INCLUDE("MapQuickView.h")
class CustomDictionaryStyle : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
public: explicit CustomDictionaryStyle(QObject* parent = nullptr); ~CustomDictionaryStyle();
static void init();
Q_INVOKABLE void changeDictionarySymbolStyleSource();
signals: void mapViewChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::DictionaryRenderer* m_localDictionaryRenderer = nullptr; Esri::ArcGISRuntime::DictionaryRenderer* m_webDictionaryRenderer = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_featureLayer = nullptr;};
#endif // CUSTOMDICTIONARYSTYLE_H// [WriteFile Name=CustomDictionaryStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 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 Esri.Samples
Item {
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the map etc. and supply the view CustomDictionaryStyleSample { id: model mapView: view }
Rectangle { id: rectangle anchors { left: parent.left top: parent.top margins: 5 } width: radioColumn.width height: radioColumn.height color: "white" border { color: "black" width: 1 } opacity: 0.9
Column { id: radioColumn padding: 5 spacing: 5
Text { text: "Custom Dictionary Symbol Style Source" }
RadioButton { text: "Local .stylx file" checked: true }
RadioButton { text: "Web style" onCheckedChanged: { model.changeDictionarySymbolStyleSource(); } } } }}// [Legal]// Copyright 2019 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 "CustomDictionaryStyle.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("CustomDictionaryStyle"));
// 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 CustomDictionaryStyle::init();
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/DisplayInformation/CustomDictionaryStyle/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
CustomDictionaryStyle { anchors.fill: parent }}