Create symbol styles from web styles

View inQMLC++
View on GitHub
Sample viewer app

Create symbol styles from a style file hosted on a portal.

screenshot

Use case

Style files hosted on an ArcGIS Online or Enterprise portal are known as web styles. They can be used to style symbols on a feature layer or graphic overlay. Since styles are published from ArcGIS Pro, you can author and design your own beautiful multilayer vector symbols. These vector symbols look good at any resolution and scale well. Runtime users can now access these styles from their native application, and make use of the vector symbols within them to enhance features and graphics in the map.

How to use the sample

The sample displays a map with a set of symbols that represent the categories of the features within the dataset. Pan and zoom on the map and view the legend to explore the appearance and names of the different symbols from the selected symbol style.

How it works

  1. Create a FeatureLayer and add it to the map.
  2. Create a UniqueValueRenderer and set it to the feature layer.
  3. Create a SymbolStyle from a portal by passing in the web style name and portal URL. * Note: passing null as the portal will default to ArcGIS.com.
  4. Create a SymbolStylesSearchParameters class and set its keys parameter to the names of the symbols in the web style that you want to use.
  5. Call searchSymbols(SymbolStylesSearchParameters) to create a SymbolStyleSearchResultListModel.
  6. For each SymbolStyleSearchResult in the returned list model, create a SymbolStyleSearchResultSymbolFetcher by calling fetchSymbol().
  7. When fetchSymbol has completed, create UniqueValue objects using the returned Symbol and the values of the features to apply it to.
  8. Add each UniqueValue to the UniqueValueRenderer.

Relevant API

  • FeatureLayer
  • Symbol
  • SymbolStyle
  • UniqueValue
  • UniqueValueRenderer

About the data

The sample uses the 'Esri2DPointSymbolsStyle' Web Style.

The map shows features from the LA County Points of Interest service hosted on ArcGIS Online.

Additional information

2D web styles, dictionary web styles, and 3D web styles can all be hosted on an ArcGIS Online or Enterprise portal.

Tags

renderer, symbol, symbology, web style

Sample Code

CreateSymbolStylesFromWebStyles.cppCreateSymbolStylesFromWebStyles.cppCreateSymbolStylesFromWebStyles.hCreateSymbolStylesFromWebStyles.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
// [WriteFile Name=CreateSymbolStylesFromWebStyles, Category=DisplayInformation]
// [Legal]
// Copyright 2021 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 "CreateSymbolStylesFromWebStyles.h"

#include "FeatureLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "ServiceFeatureTable.h"
#include "SimpleMarkerSymbol.h"
#include "SymbolStyle.h"
#include "SymbolStyleSearchResultSymbolFetcher.h"
#include "UniqueValueRenderer.h"

using namespace Esri::ArcGISRuntime;

CreateSymbolStylesFromWebStyles::CreateSymbolStylesFromWebStyles(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISNavigation, this))
{
  const QUrl webStyleLayerUrl = QUrl("https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/LA_County_Points_of_Interest/FeatureServer/0");
  m_webStyleLayer = new FeatureLayer(new ServiceFeatureTable(webStyleLayerUrl, this), this);

  m_uniqueValueRenderer = new UniqueValueRenderer(this);

  // The UniqueValueRenderer will affect specific features based on the values of the specified FieldName(s)
  m_uniqueValueRenderer->setFieldNames({"cat2"});

  // The UniqueValueRenderer defines how features of a FeatureLayer are stylized
  // Without an overriding UniqueValueRenderer, features from m_webStyleLayer will use the web layer's default gray circle style
  m_webStyleLayer->setRenderer(m_uniqueValueRenderer);

  m_map->operationalLayers()->append(m_webStyleLayer);

  // Set the scale at which feature symbols and text will appear at their default size
  m_map->setReferenceScale(100'000);

  createSymbolStyles();
}

void CreateSymbolStylesFromWebStyles::createSymbolStyles()
{
  QMap<QString,QStringList> categoriesMap = createCategoriesMap();
  SymbolStyle* symbolStyle = new SymbolStyle("Esri2DPointSymbolsStyle", {}, this);
  SymbolStyleSearchParameters searchParams;
  searchParams.setKeysStrictlyMatch(true);

  connect(symbolStyle, &SymbolStyle::searchSymbolsCompleted, this, [this, symbolStyle, categoriesMap](QUuid /* taskId */, SymbolStyleSearchResultListModel* searchResults)
  {
    m_legendInfoListModel = searchResults;
    emit legendInfoListModelChanged();

    const auto results = searchResults->searchResults();
    for (const SymbolStyleSearchResult& symbolStyleSearchResult : results)
    {
      const QString symbolLabel = symbolStyleSearchResult.key();

      // We pass symbolStyle as the QObject parent for fetchSymbol() because we don't need access to the resulting class outside the lifetime of this SymbolStyle
      SymbolStyleSearchResultSymbolFetcher* symbolFetcher = symbolStyleSearchResult.fetchSymbol(symbolStyle);

      connect(symbolFetcher, &SymbolStyleSearchResultSymbolFetcher::fetchSymbolCompleted, this, [this, symbolLabel, categoriesMap](QUuid /* taskId */, Symbol* symbol)
      {
        // If multiple field names are set, we can pass multiple values from each field,
        // However, even though we are using the same symbol, we must create a UniqueValue for each value from the same field
        // When the FeatureLayer is rendered, all features with a matching value in the specified FieldNames will appear with the defined UniqueValue
        for (const QString &category : categoriesMap[symbolLabel])
        {
          m_uniqueValueRenderer->uniqueValues()->append(new UniqueValue(symbolLabel, "", {category}, symbol, this));
        }
      });
    }
  });

  searchParams.setKeys(categoriesMap.keys());
  symbolStyle->searchSymbols(searchParams);
}

CreateSymbolStylesFromWebStyles::~CreateSymbolStylesFromWebStyles() = default;

void CreateSymbolStylesFromWebStyles::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<CreateSymbolStylesFromWebStyles>("Esri.Samples", 1, 0, "CreateSymbolStylesFromWebStylesSample");
  qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");
}

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

void CreateSymbolStylesFromWebStyles::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

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

  constexpr double x_longitude = -118.44186;
  constexpr double y_latitude = 34.28301;
  constexpr double scale = 7000;
  const Point centerPt(x_longitude, y_latitude, SpatialReference::wgs84());

  m_mapView->setViewpointCenter(centerPt, scale);

  connect(m_mapView, &MapQuickView::mapScaleChanged, this, [this]()
  {
    // Set scale symbols to true when we zoom in so the symbols don't take up the entire view
    m_webStyleLayer->setScaleSymbols(m_mapView->mapScale() >= 80'000);
  });

  emit mapViewChanged();
}

QMap<QString,QStringList> CreateSymbolStylesFromWebStyles::createCategoriesMap()
{
  QMap<QString,QStringList> categories;
  categories["atm"] << "Banking and Finance";
  categories["beach"] << "Beaches and Marinas";
  categories["campground"] << "Campgrounds";
  categories["city-hall"] << "City Halls" << "Government Offices";
  categories["hospital"] << "Hospitals and Medical Centers" << "Health Screening and Testing" << "Health Centers" << "Mental Health Centers";
  categories["library"] << "Libraries";
  categories["park"] << "Parks and Gardens";
  categories["place-of-worship"] << "Churches";
  categories["police-station"] << "Sheriff and Police Stations";
  categories["post-office"] << "DHL Locations" << "Federal Express Locations";
  categories["school"] << "Public High Schools" << "Public Elementary Schools" << "Private and Charter Schools";
  categories["trail"] << "Trails";
  return categories;
}

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