Feature layer change renderer

View on GitHubSample viewer app

Change the appearance of a feature layer with a renderer.

screenshot

Use case

A feature layer hosted on ArcGIS Online has a preset renderer and will display in your app with that renderer. However, for example, the color associated with the original renderer may be unsuitable for a company with staff or clients who are color blind, or for presentation with a different set of basemap and operational layers. In these cases, the renderer on the feature layer's data can be set to a more suitable color.

How to use the sample

Use the button to change the renderer on the feature layer. The original renderer displays orange circles, the diameters of which are proportional to carbon storage of each tree. When the blue renderer in this sample is applied, it displays the location of the trees simply as blue points.

How it works

  1. Create a ServiceFeatureTable from a URL.
  2. Create a FeatureLayer from the service feature table.
  3. Create a new renderer (in this case, a SimpleRenderer).
  4. Change the feature layer's renderer using FeatureLayer::setRenderer(SimpleRenderer).

Relevant API

  • FeatureLayer::setRenderer
  • ServiceFeatureTable
  • SimpleRenderer

About the data

This sample displays a feature layer in Warren Wilson College, North Carolina, showing carbon storage of trees on campus. The size of each circle is proportional to that particular tree's carbon storage total.

Tags

feature layer, renderer, visualization

Sample Code

FeatureLayerChangeRenderer.cppFeatureLayerChangeRenderer.cppFeatureLayerChangeRenderer.hFeatureLayerChangeRenderer.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
// [WriteFile Name=FeatureLayerChangeRenderer, Category=Features]
// [Legal]
// Copyright 2016 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 "FeatureLayerChangeRenderer.h"

// ArcGIS Maps SDK headers
#include "Basemap.h"
#include "Envelope.h"
#include "FeatureLayer.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "ServiceFeatureTable.h"
#include "SimpleLineSymbol.h"
#include "SimpleRenderer.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "Viewpoint.h"

// Qt headers
#include <QColor>
#include <QUrl>

using namespace Esri::ArcGISRuntime;

FeatureLayerChangeRenderer::FeatureLayerChangeRenderer(QQuickItem* parent) :
  QQuickItem(parent)
{
}

FeatureLayerChangeRenderer::~FeatureLayerChangeRenderer() = default;

void FeatureLayerChangeRenderer::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<FeatureLayerChangeRenderer>("Esri.Samples", 1, 0, "FeatureLayerChangeRendererSample");
}

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

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  // Create a map using the topographic basemap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);
  m_map->setInitialViewpoint(Viewpoint(Envelope(-13075816.4047166, 4014771.46954516, -13073005.6797177, 4016869.78617381, SpatialReference(102100))));

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

  // create the feature table
  ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/PoolPermits/FeatureServer/0"), this);
  // create the feature layer using the feature table
  m_featureLayer = new FeatureLayer(featureTable, this);

  connect(m_featureLayer, &FeatureLayer::loadStatusChanged, this, [this](LoadStatus loadStatus)
  {
    loadStatus == LoadStatus::Loaded ? m_initialized = true : m_initialized = false;
    emit layerInitializedChanged();
  });

  // add the feature layer to the map
  m_map->operationalLayers()->append(m_featureLayer);
}

bool FeatureLayerChangeRenderer::layerInitialized() const
{
  return m_initialized;
}

void FeatureLayerChangeRenderer::changeRenderer()
{
  if (m_stateChanged)
  {
    m_featureLayer->resetRenderer();
    m_stateChanged = false;
  }
  else
  {
    // create a line symbol
    SimpleLineSymbol* sls = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(0, 0, 255, 204), 2.0f, this);
    // create a renderer with the symbol above
    SimpleRenderer* simpleRenderer = new SimpleRenderer(sls, this);
    // change the renderer with the renderer created above
    m_featureLayer->setRenderer(simpleRenderer);
    m_stateChanged = true;
  }
}

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