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

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
- Create a
SubtypeFeatureLayerfrom aServiceFeatureTablethat defines a subtype, and add it to theMap. - Get a
SubtypeSublayerfrom the subtype feature using its name. - Enable the sublayer’s labels and define them with
LabelDefinitions. - Set the visibility status using this sublayer’s
setVisibleproperty. - Change the sublayer’s symbology with
SubtypeSublayer::setRenderer(). - 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
// [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
// sample headers#include "DisplaySubtypeFeatureLayer.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"#include "Authentication/AuthenticationManager.h"#include "Authentication/ArcGISAuthenticationChallenge.h"#include "Authentication/TokenCredential.h"#include "Envelope.h"#include "Error.h"#include "ErrorException.h"#include "FeatureLayer.h"#include "LabelDefinition.h"#include "LabelDefinitionListModel.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "ServiceFeatureTable.h"#include "ServiceTypes.h"#include "SimpleLabelExpression.h"#include "SimpleMarkerSymbol.h"#include "SimpleRenderer.h"#include "SpatialReference.h"#include "SubtypeFeatureLayer.h"#include "SubtypeSublayer.h"#include "SymbolTypes.h"#include "TextSymbol.h"#include "Viewpoint.h"
#include <QFuture>
using namespace Esri::ArcGISRuntime;using namespace Esri::ArcGISRuntime::Authentication;
DisplaySubtypeFeatureLayer::DisplaySubtypeFeatureLayer(QObject* parent /* = nullptr */): ArcGISAuthenticationChallengeHandler(parent), m_map(new Map(BasemapStyle::ArcGISStreetsNight, this)), m_alternateRenderer(new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor(Qt::magenta), 20, this), this)){ ArcGISRuntimeEnvironment::authenticationManager()->setArcGISAuthenticationChallengeHandler(this);
m_busy = true;
// create the feature table ServiceFeatureTable* featureTable = new ServiceFeatureTable(QUrl("https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer/0"), 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;}
void DisplaySubtypeFeatureLayer::handleArcGISAuthenticationChallenge(ArcGISAuthenticationChallenge* challenge){ TokenCredential::createWithChallengeAsync(challenge, "viewer01", "I68VGU^nMurF", {}, this).then(this, [challenge](TokenCredential* tokenCredential) { challenge->continueWithCredential(tokenCredential); }).onFailed(this, [challenge](const ErrorException& e) { challenge->continueWithError(e.error()); });}// [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]
#ifndef DISPLAYSUBTYPEFEATURELAYER_H#define DISPLAYSUBTYPEFEATURELAYER_H
// ArcGIS Maps SDK headers#include "SubtypeSublayer.h"#include "Authentication/ArcGISAuthenticationChallengeHandler.h"
namespace Esri::ArcGISRuntime{class FeatureLayer;class LabelDefinition;class Map;class MapQuickView;class Renderer;class SimpleRenderer;class SubtypeFeatureLayer;class SubtypeSublayer;}
namespace Esri::ArcGISRuntime::Authentication{ class ArcGISAuthenticationChallenge;}
Q_MOC_INCLUDE("MapQuickView.h")
class DisplaySubtypeFeatureLayer : public Esri::ArcGISRuntime::Authentication::ArcGISAuthenticationChallengeHandler{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(double mapScale MEMBER m_mapScale NOTIFY mapScaleChanged) Q_PROPERTY(double sublayerMinScale MEMBER m_sublayerMinScale NOTIFY sublayerMinScaleChanged) Q_PROPERTY(bool busy MEMBER m_busy NOTIFY busyChanged)
public: explicit DisplaySubtypeFeatureLayer(QObject* parent = nullptr); ~DisplaySubtypeFeatureLayer();
Q_INVOKABLE void switchSublayerVisibility(); Q_INVOKABLE void setOringalRenderer(); Q_INVOKABLE void setAlternativeRenderer(); Q_INVOKABLE void setSublayerMinScale();
static void init();
signals: void mapViewChanged(); void mapScaleChanged(); void sublayerMinScaleChanged(); void busyChanged();
private slots: void getSubtypeSublayerAndDefineLabels(const Esri::ArcGISRuntime::Error& e);
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); Esri::ArcGISRuntime::LabelDefinition* createLabelDefinition();
void handleArcGISAuthenticationChallenge(Esri::ArcGISRuntime::Authentication::ArcGISAuthenticationChallenge* challenge) override;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::SubtypeFeatureLayer* m_subtypeFeatureLayer = nullptr; Esri::ArcGISRuntime::SubtypeSublayer* m_subtypeSublayer = nullptr; Esri::ArcGISRuntime::LabelDefinition* m_labelDefinition = nullptr; Esri::ArcGISRuntime::Renderer* m_originalRenderer = nullptr; Esri::ArcGISRuntime::SimpleRenderer* m_alternateRenderer = nullptr;
double m_mapScale = 0.0; double m_sublayerMinScale = 0.0; bool m_busy = false;};
#endif // DISPLAYSUBTYPEFEATURELAYER_H// [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]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport QtQuick.Shapesimport 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(); }
Rectangle { id: controlsBox anchors { left: parent.left top: parent.top margins: 3 } width: childrenRect.width height: childrenRect.height color: "lightgrey" opacity: 0.8 radius: 5
// catch mouse signals from propagating to parent MouseArea { anchors.fill: parent onClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
ColumnLayout { id: controlItemsLayout
CheckBox { text: qsTr("Show sublayer") Layout.margins: 2 Layout.alignment: Qt.AlignLeft checked: true enabled: !busy.visible onCheckedChanged: model.switchSublayerVisibility(); }
RadioButton { text: qsTr("Show original renderer") Layout.margins: 2 Layout.alignment: Qt.AlignLeft checked: true enabled: !busy.visible onCheckedChanged: { if (checked) model.setOringalRenderer(); } }
RadioButton { text: qsTr("Show alternative renderer") Layout.margins: 2 Layout.alignment: Qt.AlignLeft enabled: !busy.visible onCheckedChanged: { if (checked) model.setAlternativeRenderer(); } }
Shape { id: pageBreak height: 2 ShapePath { strokeWidth: 1 strokeColor: "black" strokeStyle: ShapePath.SolidLine startX: 2; startY: 0 PathLine { x: controlItemsLayout.width - 2 ; y: 0 } } }
Text { text: qsTr("Current map scale: 1:%1".arg(Math.round(model.mapScale))) Layout.margins: 2 Layout.alignment: Qt.AlignLeft }
Text { text: qsTr("Sublayer min scale: 1:%1".arg(model.sublayerMinScale > 0.0 ? Math.round(model.sublayerMinScale) : "not set")) Layout.margins: 2 Layout.alignment: Qt.AlignLeft }
Button { text: qsTr("Set sublayer minimum scale") Layout.margins: 2 Layout.alignment: Qt.AlignLeft enabled: !busy.visible onClicked: model.setSublayerMinScale(); } } }
BusyIndicator { id: busy anchors.centerIn: parent visible: model.busy } }
// Declare the C++ instance which creates the map etc. and supply the view DisplaySubtypeFeatureLayerSample { id: model mapView: view }}// [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 "DisplaySubtypeFeatureLayer.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
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("DisplaySubtypeFeatureLayer"));
// 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 DisplaySubtypeFeatureLayer::init();
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
#ifdef ARCGIS_RUNTIME_IMPORT_PATH_2 engine.addImportPath(ARCGIS_RUNTIME_IMPORT_PATH_2);#endif
// Set the source engine.load(QUrl("qrc:/Samples/Layers/DisplaySubtypeFeatureLayer/main.qml"));
return app.exec();}// 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.
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
DisplaySubtypeFeatureLayer { anchors.fill: parent }}