Change sublayer renderer

View on GitHubSample viewer app

Apply a renderer to a sublayer.

screenshot

Use case

A layer showing animal populations contains sublayers for different species. A renderer could be applied which gives each sublayer a different color, so that populations of each species can be compared visually.

How to use the sample

Wait for the map image layer to load. Click the 'Change Sublayer Renderer' button to apply a unique value renderer to see different population ranges in the counties sub-layer data.

How it works

  1. Create an ArcGISMapImageLayer from its URL.
  2. After it is done loading, get its ArcGISSublayerListModel with imageLayer::mapImageSublayers().
  3. Cast the sublayer you want to change to the appropriate type: dynamic_cast<ArcGISMapImageSublayer>(ArcGISSublayer).
  4. Create a ClassBreaksRenderer with a collection of ClassBreaks for different population ranges.
  5. Set the renderer of the sublayer with sublayer::setRenderer(renderer).

Relevant API

  • ArcGISMapImageLayer
  • ArcGISMapImageSubLayer
  • ClassBreaksRenderer
  • ClassBreaksRenderer::classBreak

About the data

This application displays census data from an ArcGIS Server map service. It contains various population statistics, including total population for each county in 2007.

Additional information

The service hosting the layer must support dynamic layers to be able to change the rendering of sublayers.

Tags

class breaks, dynamic layer, dynamic rendering, renderer, sublayer, symbology, visualization

Sample Code

ChangeSublayerRenderer.cppChangeSublayerRenderer.cppChangeSublayerRenderer.hChangeSublayerRenderer.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
// [WriteFile Name=ChangeSublayerRenderer, Category=Layers]
// [Legal]
// Copyright 2018 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 "ChangeSublayerRenderer.h"

// ArcGIS Maps SDK headers
#include "ArcGISMapImageLayer.h"
#include "ArcGISMapImageSublayer.h"
#include "ArcGISSublayerListModel.h"
#include "ClassBreaksRenderer.h"
#include "Envelope.h"
#include "Error.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "Viewpoint.h"

using namespace Esri::ArcGISRuntime;

ChangeSublayerRenderer::ChangeSublayerRenderer(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

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

void ChangeSublayerRenderer::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a map using the streets basemap
  m_map = new Map(BasemapStyle::ArcGISStreets, this);

  // Add the map image layer
  ArcGISMapImageLayer* mapImageLayer = new ArcGISMapImageLayer(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer"), this);

  // Get the sublayer
  connect(mapImageLayer, &ArcGISMapImageLayer::doneLoading, this, [this, mapImageLayer](const Error& e)
  {
    if (!e.isEmpty())
      return;

    if (mapImageLayer->mapImageSublayers()->size() < 3)
      return;

    m_sublayer = dynamic_cast<ArcGISMapImageSublayer*>(mapImageLayer->mapImageSublayers()->at(2));

    // get the sublayer's original renderer
    connect(m_sublayer, &ArcGISMapImageSublayer::doneLoading, this, [this](const Error& e)
    {
      if (!e.isEmpty())
        return;

      m_originalRenderer = m_sublayer->renderer()->clone(this);

      emit sublayerLoaded();
    });

    // load the sublayer
    m_sublayer->load();
  });

  // add the layer to the map
  m_map->operationalLayers()->append(mapImageLayer);

  // set initial viewpoint
  Envelope env(-13834661.666904, 331181.323482, -8255704.998713, 9118038.075882, SpatialReference(3857));
  Viewpoint initialViewpoint(env);
  m_map->setInitialViewpoint(initialViewpoint);

  // create the class breaks renderer
  createClassBreaksRenderer();

  // Set map to map view
  m_mapView->setMap(m_map);
}

void ChangeSublayerRenderer::createClassBreaksRenderer()
{
  // create class breaks renderer
  m_classBreaksRenderer = new ClassBreaksRenderer(this);
  m_classBreaksRenderer->setFieldName(QStringLiteral("POP2007"));

  // create and append class breaks
  m_classBreaksRenderer->classBreaks()->append(createClassBreak(QColor(227, 235, 207), -99, 8560));
  m_classBreaksRenderer->classBreaks()->append(createClassBreak(QColor(150, 194, 191), 8560, 18109));
  m_classBreaksRenderer->classBreaks()->append(createClassBreak(QColor(97, 166, 181), 18109, 35501));
  m_classBreaksRenderer->classBreaks()->append(createClassBreak(QColor(69, 125, 150), 35501, 86100));
  m_classBreaksRenderer->classBreaks()->append(createClassBreak(QColor(41, 84, 120), 86100, 10110975));
}

// helper function to create class breaks for the renderer
ClassBreak* ChangeSublayerRenderer::createClassBreak(const QColor &color, double min, double max)
{
  SimpleLineSymbol* outline = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(153, 153, 153), 1.0f /*width*/, this);
  SimpleFillSymbol* sfs1 = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, color, outline, this);
  const QString description = QString("> %1 to %2").arg(QString::number(min), QString::number(max));
  const QString label = description;
  return new ClassBreak(description, label, min, max, sfs1);
}

// apply the class breaks renderer
void ChangeSublayerRenderer::applyRenderer()
{
  if (!m_sublayer || !m_classBreaksRenderer)
    return;

  m_sublayer->setRenderer(m_classBreaksRenderer);
}

// reset to the original renderer
void ChangeSublayerRenderer::resetRenderer()
{
  if (!m_sublayer || !m_originalRenderer)
    return;

  m_sublayer->setRenderer(m_originalRenderer);
}

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