Display subtype feature layer

View inC++QMLView on GitHubSample viewer app

Displays a composite layer of all the subtype values in a feature class.

screenshot

Use case

This is useful for controlling labeling, visibility and symbology of a given subtype as though they are distinct layers on the map.

How to use the sample

The sample loads with the sublayer visible on the map. Toggle its visibility with the "Show sublayer" checkbox. Change the sublayer's renderer with the radio buttons, using "Show original renderer" or "Show alternative renderer", and set its minimum scale using the "Set sublayer minimum scale" button. This will set the sublayer's minimum scale to that of the current map scale. Zoom in and out to see the sublayer become visible based on its new scale range.

How it works

  1. Create a SubtypeFeatureLayer from a ServiceFeatureTable that defines a subtype, and add it to the Map.
  2. Get a SubtypeSublayer from the subtype feature using its name.
  3. Enable the sublayer's labels and define them with LabelDefinitions.
  4. Set the visibility status using this sublayer's setVisible property.
  5. Change the sublayer's symbology with SubtypeSublayer::setRenderer().
  6. Update the sublayer's minimum scale value with SubtypeSublayer::setMinScale().

Relevant API

  • LabelDefinition
  • ServiceFeatureTable
  • SimpleLabelExpression
  • SubtypeFeatureLayer
  • SubtypeSublayer
  • TextSymbol

About the data

The Naperville electrical network feature service, hosted on ArcGIS Online (authentication required: this is handled within the sample code), contains a utility network with asset classification for different devices.

Additional information

Using utility network on ArcGIS Enterprise 10.8 requires an ArcGIS Enterprise member account licensed with the Utility Network user type extension. Please refer to the utility network services documentation.

Credentials:

  • Username: viewer01
  • Password: I68VGU^nMurF

Tags

asset group, feature layer, labeling, sublayer, subtype, symbology, utility network, visible scale range

Sample Code

DisplaySubtypeFeatureLayer.cppDisplaySubtypeFeatureLayer.cppDisplaySubtypeFeatureLayer.hDisplaySubtypeFeatureLayer.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// [WriteFile Name=DisplaySubtypeFeatureLayer, Category=Layers]
// [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

#include "DisplaySubtypeFeatureLayer.h"

#include "FeatureLayer.h"
#include "LabelDefinition.h"
#include "Map.h"
#include "MapQuickView.h"
#include "ServiceFeatureTable.h"
#include "SimpleLabelExpression.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleRenderer.h"
#include "SubtypeFeatureLayer.h"
#include "SubtypeSublayer.h"
#include "SubtypeSublayerListModel.h"
#include "TextSymbol.h"

using namespace Esri::ArcGISRuntime;

DisplaySubtypeFeatureLayer::DisplaySubtypeFeatureLayer(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISStreetsNight, this)),
  m_cred(new Credential("viewer01", "I68VGU^nMurF", this)),
  m_alternateRenderer(new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor(Qt::magenta), 20, this), this))
{
  m_busy = true;

  // create the feature table
  ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer/0"), m_cred, this);
  // create the feature layer using the feature table
  m_subtypeFeatureLayer = new SubtypeFeatureLayer(featureTable, this);

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

  // set the viewpoint to Naperville, Illinois
  m_map->setInitialViewpoint(Viewpoint(Envelope(-9812691.11079696, 5128687.20710657, -9812377.9447607, 5128865.36767282, SpatialReference::webMercator())));

  // when subtype feature layer is loaded get the subtype sublayer street lights and define its labels
  connect(m_subtypeFeatureLayer, &SubtypeFeatureLayer::doneLoading, this, &DisplaySubtypeFeatureLayer::getSubtypeSublayerAndDefineLabels);
}

DisplaySubtypeFeatureLayer::~DisplaySubtypeFeatureLayer() = default;

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

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

// Set the view (created in QML)
void DisplaySubtypeFeatureLayer::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

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

  connect(m_mapView, &MapQuickView::mapScaleChanged, this, [this]()
  {
    m_mapScale = m_mapView->mapScale();
    emit mapScaleChanged();
  });

  emit mapViewChanged();
}

void DisplaySubtypeFeatureLayer::getSubtypeSublayerAndDefineLabels(const Error& e)
{
  if (!e.isEmpty())
    return;

  m_busy = false;
  emit busyChanged();

  if (!m_subtypeFeatureLayer)
    return;

  // get the Street Light sublayer and define its labels
  m_subtypeSublayer = m_subtypeFeatureLayer->sublayerWithSubtypeName("Street Light", this);
  m_labelDefinition = createLabelDefinition();

  if (!m_labelDefinition || !m_subtypeSublayer)
    return;

  m_subtypeSublayer->labelDefinitions()->append(m_labelDefinition);
  m_subtypeSublayer->setLabelsEnabled(true);

  // get the original renderer of the sublayer
  m_originalRenderer = m_subtypeSublayer->renderer();

  // Set a default minimum scale.
  m_subtypeSublayer->setMinScale(3000.0);
  m_sublayerMinScale = m_subtypeSublayer->minScale();
  emit sublayerMinScaleChanged();
}

void DisplaySubtypeFeatureLayer::switchSublayerVisibility()
{
  if (m_subtypeSublayer)
    m_subtypeSublayer->setVisible(!m_subtypeSublayer->isVisible());
}

void DisplaySubtypeFeatureLayer::setOringalRenderer()
{
  if (m_subtypeSublayer)
    m_subtypeSublayer->setRenderer(m_originalRenderer);
}

void DisplaySubtypeFeatureLayer::setAlternativeRenderer()
{
  if (m_subtypeSublayer)
    m_subtypeSublayer->setRenderer(m_alternateRenderer);
}

void DisplaySubtypeFeatureLayer::setSublayerMinScale()
{
  if (!m_subtypeSublayer)
    return;

  const double currentScale = m_mapView->mapScale();
  m_subtypeSublayer->setMinScale(currentScale);
  m_sublayerMinScale = currentScale;
  emit sublayerMinScaleChanged();
}

LabelDefinition* DisplaySubtypeFeatureLayer::createLabelDefinition()
{
  SimpleLabelExpression* labelExpression = new SimpleLabelExpression("[nominalvoltage]", this);

  TextSymbol* textSymbol = new TextSymbol(this);
  textSymbol->setSize(14);
  textSymbol->setColor(Qt::blue);
  textSymbol->setHaloColor(Qt::white);
  textSymbol->setHaloWidth(3);
  textSymbol->setHorizontalAlignment(HorizontalAlignment::Center);
  textSymbol->setVerticalAlignment(VerticalAlignment::Middle);

  LabelDefinition* labelDefinition = new LabelDefinition(labelExpression, textSymbol, this);
  labelDefinition->setPlacement(LabelingPlacement::PointAboveRight);
  labelDefinition->setUseCodedValues(true);
  return labelDefinition;
}

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