Display content of utility network container

View inMAUIWPFWinUIView on GitHubSample viewer app

A utility network container allows a dense collection of features to be represented by a single feature, which can be used to reduce map clutter.

Image of display content of utility network container

Use case

Offering a container view for features aids in the review for valid structural attachment and containment relationships and helps determine if a dataset has an association role set. Container views often model a cluster of electrical devices on a pole top or inside a cabinet or vault.

How to use the sample

Tap on a container feature to show all features inside the container. The container is shown as a polygon graphic with the content features contained within. The viewpoint and scale of the map are also changed to the container's extent. Connectivity and attachment associations inside the container are shown as red and blue dotted lines respectively.

How it works

  1. Load a web map that includes ArcGIS Pro Subtype Group Layers with only container features visible (i.e. fuse bank, switch bank, transformer bank, hand hole and junction box).
  2. Add a GraphicsOverlay for displaying a container view.
  3. Create and load a UtilityNetwork with the same feature service URL as the layers in the Map.
  4. Add an event handler for the GeoViewTapped event of the MapView.
  5. Identify a feature and create a UtilityElement from it.
  6. Get the associations for this element using GetAssociationsAsync(UtilityElement, UtilityAssociationType.Containment).
  7. Turn-off the visibility of all OperationalLayers.
  8. Get the features for the UtilityElement(s) from the associations using GetFeaturesForElementsAsync(IEnumerable<UtilityElement>)
  9. Add a Graphic with the same geometry and symbol as these features.
  10. Add another Graphic that represents the combined extents of these features.
  11. Get associations for this extent using GetAssociationsAsync(Envelope)
  12. Add a Graphic to represent the association geometry between them using a symbol that distinguishes between Attachment and Connectivity association type.
  13. Zoom to the combined extents of the features in the container.
  14. To exit the container view, clear the Graphics and zoom out to the previous extent.

Relevant API

  • SubtypeFeatureLayer
  • UtilityAssociation
  • UtilityAssociationType
  • UtilityElement
  • UtilityNetwork
  • UtilityNetworkDefinition

About the data

The Naperville electric feature service contains a utility network layer used to find associations shown in this sample. The Naperville electric containers webmap uses the same feature service endpoint and displays only container features. Authentication is required and handled within the sample code.

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.

Tags

associations, connectivity association, containment association, structural attachment associations, utility network

Sample Code

DisplayUtilityNetworkContainer.xaml.csDisplayUtilityNetworkContainer.xaml.csDisplayUtilityNetworkContainer.xaml
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2022 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.Security;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UtilityNetworks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ArcGIS.WPF.Samples.DisplayUtilityNetworkContainer
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Display content of utility network container",
        category: "Utility network",
        description: "A utility network container allows a dense collection of features to be represented by a single feature, which can be used to reduce map clutter.",
        instructions: "Tap on a container feature to show all features inside the container. The container is shown as a polygon graphic with the content features contained within. The viewpoint and scale of the map are also changed to the container's extent. Connectivity and attachment associations inside the container are shown as red and blue dotted lines respectively.",
        tags: new[] { "associations", "connectivity association", "containment association", "structural attachment associations", "utility network" })]
    public partial class DisplayUtilityNetworkContainer
    {
        // Overlay to hold graphics for all of the associations.
        private GraphicsOverlay _associationsOverlay;

        // This viewpoint shows several associations clearly in the utility network.
        private readonly Viewpoint InitialViewpoint = new Viewpoint(41.801504, -88.163718, 4e3);

        // Utility network that will be created from the feature server.
        private UtilityNetwork _utilityNetwork;

        public DisplayUtilityNetworkContainer()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            // As of ArcGIS Enterprise 10.8.1, using utility network functionality requires a licensed user. The following login for the sample server is licensed to perform utility network operations.
            AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(async (info) =>
            {
                try
                {
                    // WARNING: Never hardcode login information in a production application. This is done solely for the sake of the sample.
                    string sampleServer7User = "viewer01";
                    string sampleServer7Pass = "I68VGU^nMurF";

                    return await AuthenticationManager.Current.GenerateCredentialAsync(info.ServiceUri, sampleServer7User, sampleServer7Pass);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return null;
                }
            });

            // Create a graphics overlay for associations.
            _associationsOverlay = new GraphicsOverlay();
            MyMapView.GraphicsOverlays.Add(_associationsOverlay);

            // Symbols for the associations.
            Symbol attachmentSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dot, Color.Blue, 5d);
            Symbol boundingBoxSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Color.Yellow, 5d);
            Symbol connectivitySymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dot, Color.Red, 5d);

            // Create a renderer for the associations.
            var attachmentValue = new UniqueValue("Attachment", string.Empty, attachmentSymbol, UtilityAssociationType.Attachment.ToString());
            var boundingBoxValue = new UniqueValue("Containment", string.Empty, boundingBoxSymbol, UtilityAssociationType.Containment.ToString());
            var connectivityValue = new UniqueValue("Connectivity", string.Empty, connectivitySymbol, UtilityAssociationType.Connectivity.ToString());
            _associationsOverlay.Renderer = new UniqueValueRenderer(new List<string> { "AssociationType" }, new List<UniqueValue> { attachmentValue, boundingBoxValue, connectivityValue }, string.Empty, null);

            // Populate the legend in the UI.
            var symbologyKey = new Dictionary<UtilityAssociationType, System.Windows.Media.ImageSource>();

            RuntimeImage attachmentSwatch = await attachmentSymbol.CreateSwatchAsync();
            symbologyKey[UtilityAssociationType.Attachment] = await attachmentSwatch?.ToImageSourceAsync();

            RuntimeImage boundingSwatch = await boundingBoxSymbol.CreateSwatchAsync();
            symbologyKey[UtilityAssociationType.Containment] = await boundingSwatch?.ToImageSourceAsync();

            RuntimeImage connectSwatch = await connectivitySymbol.CreateSwatchAsync();
            symbologyKey[UtilityAssociationType.Connectivity] = await connectSwatch?.ToImageSourceAsync();

            AssociationLegend.ItemsSource = symbologyKey;

            try
            {
                // Create a new map from the web map URL (includes ArcGIS Pro subtype group layers with only container features visible).
                MyMapView.Map = new Map(new Uri("https://sampleserver7.arcgisonline.com/portal/home/item.html?id=813eda749a9444e4a9d833a4db19e1c8"));

                // The feature service url contains a utility network used to find associations shown in this sample.
                Uri featureServiceURL = new Uri("https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer");

                _utilityNetwork = new UtilityNetwork(featureServiceURL);
                MyMapView.Map.UtilityNetworks.Add(_utilityNetwork);

                await _utilityNetwork.LoadAsync();

                // Set the starting viewpoint.
                await MyMapView.SetViewpointAsync(InitialViewpoint);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Message.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                LoadingBar.Visibility = Visibility.Collapsed;
            }
        }

        private void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
        {
            _ = IdentifyContainer(e.Position);
        }

        private async Task IdentifyContainer(System.Windows.Point position)
        {
            try
            {
                // Identify the feature to be used from the sublayer.
                IEnumerable<IdentifyLayerResult> identifyResult = await MyMapView.IdentifyLayersAsync(position, 10.0, false);
                ArcGISFeature feature = identifyResult?.FirstOrDefault()?.SublayerResults.FirstOrDefault()?.GeoElements?.FirstOrDefault() as ArcGISFeature;
                if (feature == null) { return; }

                // Create element from the identified feature.
                UtilityElement containerElement = _utilityNetwork.CreateElement(feature);

                // Get all of the containment associations for the selected container element.
                IEnumerable<UtilityAssociation> containmentAssociations = await _utilityNetwork.GetAssociationsAsync(containerElement, UtilityAssociationType.Containment);

                // Get all of the other elements associated with the selected element.
                IEnumerable<UtilityElement> contentElements = containmentAssociations.Select(association => association.FromElement.ObjectId == containerElement.ObjectId ? association.ToElement : association.FromElement);

                // Verify that there are elements in the associations.
                if (!contentElements.Any()) return;

                // Get features for these elements.
                IEnumerable<ArcGISFeature> contentFeatures = await _utilityNetwork.GetFeaturesForElementsAsync(contentElements);

                // Clear the graphics overlay.
                _associationsOverlay.Graphics.Clear();

                // Enable the close button.
                CloseButton.Visibility = Visibility.Visible;

                // Get the content features and give them each a symbol, and add them as a graphic to the graphics overlay.
                foreach (ArcGISFeature contentFeature in contentFeatures)
                {
                    // Get the symbol for each element from the feature table.
                    Symbol symbol = (contentFeature.FeatureTable as ArcGISFeatureTable).LayerInfo.DrawingInfo.Renderer.GetSymbol(contentFeature);
                    _associationsOverlay.Graphics.Add(new Graphic(contentFeature.Geometry, symbol));
                }

                // Create a bounding box for the container.
                Geometry boundingBox;

                // If there is only single element, create a bounding box using the container view scale.
                if (contentFeatures.Count() == 1 && contentFeatures.First().Geometry is MapPoint point)
                {
                    double containerViewScale = containerElement.AssetType.ContainerViewScale;
                    boundingBox = new Envelope(point, containerViewScale, containerViewScale);
                }
                else
                {
                    // Create a bounding box using the combined extents of the elements from associations.
                    Envelope combinedExtents = GeometryEngine.CombineExtents(contentFeatures.Select(f => f.Geometry));
                    boundingBox = GeometryEngine.Buffer(combinedExtents, 0.05);
                }

                // Add a graphic for the bounding box.
                Graphic boundingGraphic = new Graphic(boundingBox);
                boundingGraphic.Attributes["AssociationType"] = UtilityAssociationType.Containment.ToString();
                _associationsOverlay.Graphics.Add(boundingGraphic);

                // Add graphics for every association.
                IEnumerable<UtilityAssociation> allAssociations = await _utilityNetwork.GetAssociationsAsync(boundingBox.Extent);
                foreach (UtilityAssociation association in allAssociations)
                {
                    // Check if the graphics overlay already contains the association.
                    if (_associationsOverlay.Graphics.Any(g => g.Attributes.ContainsKey("GlobalId") && (Guid)g.Attributes["GlobalId"] == association.GlobalId) || association.Geometry == null)
                    {
                        continue;
                    }

                    // Add a graphic for the association.
                    Graphic graphic = new Graphic(association.Geometry);
                    graphic.Attributes["GlobalId"] = association.GlobalId;

                    // This association type will be used by the unique value renderer to determine the symbology of the graphic.
                    graphic.Attributes["AssociationType"] = association.AssociationType.ToString();
                    _associationsOverlay.Graphics.Add(graphic);
                }

                // Set the viewpoint to show the bounding box.
                await MyMapView.SetViewpointGeometryAsync(boundingBox, 25);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Message.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            CloseButton.Visibility = Visibility.Hidden;

            // Clear the graphics overlay.
            _associationsOverlay.Graphics.Clear();

            // Return to the initial viewpoint.
            MyMapView.SetViewpointAsync(InitialViewpoint);
        }
    }
}

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