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());
  });
}