Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Display subtype feature layer

This code sample is available for these platforms:
View Sample on GitHub

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

Image of display subtype feature layer

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. Change the sublayer's visibiliy, renderer, and minimum scale using the on screen controls. Setting the minimum scale will change its value 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 IsVisible property.
  5. Change the sublayer's symbology using this sublayer's Renderer property.
  6. Update the sublayer's minimum scale value using the using the mapview's current scale.

Relevant API

  • LabelDefinition
  • ServiceFeatureTable
  • SubtypeFeatureLayer
  • SubtypeSublayer

About the data

The feature service layer in this sample represents an electric network in Naperville, Illinois, which contains a utility network with asset classification for different devices.

Tags

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

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.DisplaySubtypeFeatureLayer.DisplaySubtypeFeatureLayer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esri:MapView x:Name="MyMapView" />
        <Border
            x:Name="SublayerInfo"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <TextBlock
                    Grid.ColumnSpan="2"
                    Margin="5"
                    HorizontalAlignment="Center">
                    <Run Text="Current Map Scale: " />
                    <Run Text="1:" />
                    <Run Text="{Binding ElementName=MyMapView, Path=MapScale, Mode=OneWay}" /></TextBlock>
                <CheckBox
                    Grid.Row="2"
                    Margin="5"
                    VerticalContentAlignment="Center"
                    Content="Sublayer visible"
                    IsChecked="{Binding IsVisible, Mode=TwoWay}" />
                <Button
                    Grid.Row="2"
                    Grid.Column="1"
                    Margin="5"
                    HorizontalAlignment="Stretch"
                    Click="OnChangeRenderer"
                    Content="Change renderer" />
                <TextBlock
                    Grid.Row="3"
                    Margin="5"
                    VerticalAlignment="Center">
                    <Run Text="Sublayer min scale: 1:" />
                    <Run Text="{Binding MinScale, Mode=OneWay}" /></TextBlock>
                <Button
                    Grid.Row="3"
                    Grid.Column="1"
                    Margin="5"
                    HorizontalAlignment="Stretch"
                    Click="OnSetMinimumScale"
                    Content="Set min scale" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
// 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.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Drawing;
using Windows.UI.Popups;
using Windows.UI.Xaml;

namespace ArcGISRuntime.UWP.Samples.DisplaySubtypeFeatureLayer
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Display subtype feature layer",
        category: "Layers",
        description: "Displays a composite layer of all the subtype values in a feature class.",
        instructions: "The sample loads with the sublayer visible on the map. Change the sublayer's visibiliy, renderer, and minimum scale using the on screen controls. Setting the minimum scale will change its value to that of the current map scale. Zoom in and out to see the sublayer become visible based on its new scale range.",
        tags: new[] { "asset group", "feature layer", "labeling", "sublayer", "subtype", "symbology", "utility network", "visible scale range", "Featured" })]
    public partial class DisplaySubtypeFeatureLayer
    {
        // Reference to a sublayer.
        private SubtypeSublayer _sublayer;

        // JSON for labeling features from the sublayer.
        private const string _labelJSON = "{ \"labelExpression\":\"[nominalvoltage]\",\"labelPlacement\":\"esriServerPointLabelPlacementAboveRight\",\"useCodedValues\":true,\"symbol\":{\"angle\":0,\"backgroundColor\":[0,0,0,0],\"borderLineColor\":[0,0,0,0],\"borderLineSize\":0,\"color\":[0,0,255,255],\"font\":{\"decoration\":\"none\",\"size\":10.5,\"style\":\"normal\",\"weight\":\"normal\"},\"haloColor\":[255,255,255,255],\"haloSize\":2,\"horizontalAlignment\":\"center\",\"kerning\":false,\"type\":\"esriTS\",\"verticalAlignment\":\"middle\",\"xoffset\":0,\"yoffset\":0}}";

        // Renderers for the sublayer.
        private Renderer _defaultRenderer;
        private Renderer _customRenderer;

        public DisplaySubtypeFeatureLayer()
        {
            InitializeComponent();
            Initialize();
        }

        private async void Initialize()
        {
            try
            {
                // Starting viewpoint for the map view.
                Viewpoint _startingViewpoint = new Viewpoint(new Envelope(-9812691.11079696, 5128687.20710657, -9812377.9447607, 5128865.36767282, SpatialReferences.WebMercator));

                // Create the map.
                MyMapView.Map = new Map(Basemap.CreateStreetsNightVector()) { InitialViewpoint = _startingViewpoint };

                // NOTE: This layer supports any ArcGIS Feature Table that define subtypes.
                SubtypeFeatureLayer subtypeFeatureLayer = new SubtypeFeatureLayer(new ServiceFeatureTable(new Uri("https://sampleserver7.arcgisonline.com/arcgis/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer/100")));
                MyMapView.Map.OperationalLayers.Add(subtypeFeatureLayer);

                // Select sublayer to control.
                await subtypeFeatureLayer.LoadAsync();

                // Select the sublayer of street lights by name.
                _sublayer = subtypeFeatureLayer.GetSublayerBySubtypeName("Street Light");

                // Set the label definitions using the JSON.
                _sublayer.LabelDefinitions.Add(LabelDefinition.FromJson(_labelJSON));

                // Enable labels for the sub layer.
                _sublayer.LabelsEnabled = true;

                // Set the data context for data-binding in XAML.
                SublayerInfo.DataContext = _sublayer;

                // Get the default renderer for the sublayer.
                _defaultRenderer = _sublayer.Renderer.Clone();

                // Create a custom renderer for the sublayer.
                _customRenderer = new SimpleRenderer()
                {
                    Symbol = new SimpleMarkerSymbol()
                    {
                        Color = Color.Salmon,
                        Style = SimpleMarkerSymbolStyle.Diamond,
                        Size = 20,
                    }
                };

                // Set a default minimum scale.
                _sublayer.MinScale = 3000;
            }
            catch (Exception ex)
            {
                await new MessageDialog(ex.Message, ex.GetType().Name).ShowAsync();
            }
        }

        private void OnChangeRenderer(object sender, RoutedEventArgs e)
        {
            // Check if the current renderer is the custom renderer.
            if (_sublayer.Renderer == _customRenderer)
            {
                _sublayer.Renderer = _defaultRenderer;
            }
            else
            {
                _sublayer.Renderer = _customRenderer;
            }
        }

        private void OnSetMinimumScale(object sender, RoutedEventArgs e)
        {
            // Set the minimum scale of the sublayer.
            // NOTE: You may also update Sublayer.MaxScale
            _sublayer.MinScale = MyMapView.MapScale;
        }
    }
}