Run a filtered trace to locate operable features that will isolate an area from the flow of network resources.
Use case
Determine the set of operable features required to stop a network's resource, effectively isolating an area of the network. For example, you can choose to return only accessible and operable valves: ones that are not paved over or rusted shut.
How to use the sample
Tap on one or more features to use as filter barriers or create and set the configuration's filter barriers by selecting a category. Check or uncheck 'Include Isolated Features'. Click 'Trace' to run a subnetwork-based isolation trace. Click 'Reset' to clear filter barriers.
How it works
- Create a
MapView
and subscribe to itsGeoViewTapped
event. - Create and load a
ServiceGeodatabase
with a feature service URL and get tables by their layer IDs. - Create a
Map
that containsFeatureLayer
(s) created from theServiceGeodatabase
's tables. - Create and load a
UtilityNetwork
with the same feature service URL and thisMap
. - Create
UtilityTraceParameters
withUtilityTraceType.Isolation
and a starting location from a given asset type and global ID. - Get a default
UtilityTraceConfiguration
from a given tier in a domain network to setUtilityTraceParameters.TraceConfiguration
. - Add a
GraphicsOverlay
with aGraphic
that represents this starting location; and anotherGraphicsOverlay
for filter barriers. - Populate the choice list for the 'Filter Barrier: Category exists' from
UtilityNetworkDefinition.Categories
. - When the MapView is tapped, identify which features are at the tap location and add a
Graphic
that represents a filter barrier. - Create a
UtilityElement
for the identified feature and add thisUtilityElement
to a collection of filter barriers.- If the element is a junction with more than one terminal, display a terminal picker. Then set the junction's
Terminal
property with the selected terminal. - If an edge, set its
FractionAlongLine
property usingGeometryEngine.FractionAlong
.
- If the element is a junction with more than one terminal, display a terminal picker. Then set the junction's
- If 'Trace' is clicked without filter barriers:
- Create a new
UtilityCategoryComparison
with the selected category andUtilityCategoryComparisonOperator.Exists
. - Create a new
UtilityTraceFilter
with this condition asBarriers
to setFilter
and updateIncludeIsolatedFeatures
properties of the default configuration from step 5. - Run a
UtilityNetwork.TraceAsync
.
- Update
IncludeIsolatedFeatures
property of the default configuration from step 5. - Run a
UtilityNetwork.TraceAsync
.
- Create a new
- For every
FeatureLayer
in the map, select the features returned withGetFeaturesForElementsAsync
from the elements matching theirNetworkSource.FeatureTable
with the layer'sFeatureTable
.
Relevant API
- FractionAlong
- ServiceGeodatabase
- UtilityCategory
- UtilityCategoryComparison
- UtilityCategoryComparisonOperator
- UtilityDomainNetwork
- UtilityElement
- UtilityElementTraceResult
- UtilityNetwork
- UtilityNetworkDefinition
- UtilityTerminal
- UtilityTier
- UtilityTraceFilter
- UtilityTraceParameters
- UtilityTraceResult
- UtilityTraceType
About the data
The Naperville gas network feature service contains a utility network used to run the isolation trace shown in this sample. 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
category comparison, condition barriers, filter barriers, isolated features, network analysis, subnetwork trace, trace configuration, trace filter, utility network
Sample Code
// Copyright 2021 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.UI.Controls;
using Esri.ArcGISRuntime.UtilityNetworks;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ArcGIS.WinUI.Samples.PerformValveIsolationTrace
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Perform valve isolation trace",
category: "Utility network",
description: "Run a filtered trace to locate operable features that will isolate an area from the flow of network resources.",
instructions: "Tap on one or more features to use as filter barriers or create and set the configuration's filter barriers by selecting a category. Check or uncheck 'Include Isolated Features'. Click 'Trace' to run a subnetwork-based isolation trace. Click 'Reset' to clear filter barriers.",
tags: new[] { "category comparison", "condition barriers", "filter barriers", "isolated features", "network analysis", "subnetwork trace", "trace configuration", "trace filter", "utility network" })]
public partial class PerformValveIsolationTrace
{
// Feature service for an electric utility network in Naperville, Illinois.
private const string FeatureServiceUrl = "https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleGas/FeatureServer";
private const int LineLayerId = 3;
private const int DeviceLayerId = 0;
private UtilityNetwork _utilityNetwork;
// For creating the default trace configuration.
private const string DomainNetworkName = "Pipeline";
private const string TierName = "Pipe Distribution System";
private UtilityTraceConfiguration _configuration;
// For creating the default starting location.
private const string NetworkSourceName = "Gas Device";
private const string AssetGroupName = "Meter";
private const string AssetTypeName = "Customer";
private const string GlobalId = "{98A06E95-70BE-43E7-91B7-E34C9D3CB9FF}";
private UtilityElement _startingLocation;
private UtilityTraceParameters _parameters;
private GraphicsOverlay _barrierOverlay;
// Task completion source for the user selected terminal.
private TaskCompletionSource<UtilityTerminal> _terminalCompletionSource = null;
public PerformValveIsolationTrace()
{
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)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
});
try
{
// Disable the UI.
FilterOptions.Visibility = Visibility.Collapsed;
// Create and load a service geodatabase that matches utility network.
ServiceGeodatabase serviceGeodatabase = await ServiceGeodatabase.CreateAsync(new Uri(FeatureServiceUrl));
// Create a map with layers in this utility network.
MyMapView.Map = new Map(BasemapStyle.ArcGISStreetsNight);
MyMapView.Map.OperationalLayers.Add(new FeatureLayer(serviceGeodatabase.GetTable(LineLayerId)));
MyMapView.Map.OperationalLayers.Add(new FeatureLayer(serviceGeodatabase.GetTable(DeviceLayerId)));
// Create and load the utility network.
_utilityNetwork = await UtilityNetwork.CreateAsync(new Uri(FeatureServiceUrl), MyMapView.Map);
// Get a trace configuration from a tier.
UtilityDomainNetwork domainNetwork = _utilityNetwork.Definition.GetDomainNetwork(DomainNetworkName) ?? throw new ArgumentException(DomainNetworkName);
UtilityTier tier = domainNetwork.GetTier(TierName) ?? throw new ArgumentException(TierName);
_configuration = tier.GetDefaultTraceConfiguration();
// Create a trace filter.
_configuration.Filter = new UtilityTraceFilter();
// Get a default starting location.
UtilityNetworkSource networkSource = _utilityNetwork.Definition.GetNetworkSource(NetworkSourceName) ?? throw new ArgumentException(NetworkSourceName);
UtilityAssetGroup assetGroup = networkSource.GetAssetGroup(AssetGroupName) ?? throw new ArgumentException(AssetGroupName);
UtilityAssetType assetType = assetGroup.GetAssetType(AssetTypeName) ?? throw new ArgumentException(AssetTypeName);
Guid globalId = Guid.Parse(GlobalId);
_startingLocation = _utilityNetwork.CreateElement(assetType, globalId);
// Create a graphics overlay.
GraphicsOverlay overlay = new GraphicsOverlay();
MyMapView.GraphicsOverlays.Add(overlay);
// Display starting location.
IEnumerable<ArcGISFeature> elementFeatures = await _utilityNetwork.GetFeaturesForElementsAsync(new List<UtilityElement> { _startingLocation });
MapPoint startingLocationGeometry = elementFeatures.FirstOrDefault().Geometry as MapPoint;
Symbol symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Cross, System.Drawing.Color.LimeGreen, 25d);
Graphic graphic = new Graphic(startingLocationGeometry, symbol);
overlay.Graphics.Add(graphic);
// Create a graphics overlay for barriers.
_barrierOverlay = new GraphicsOverlay();
MyMapView.GraphicsOverlays.Add(_barrierOverlay);
// Create the utility trace parameters.
_parameters = new UtilityTraceParameters(UtilityTraceType.Isolation, new[] { _startingLocation });
// Set the starting viewpoint.
await MyMapView.SetViewpointAsync(new Viewpoint(startingLocationGeometry, 3000));
// Build the choice list for categories populated with the `Name` property of each `UtilityCategory` in the `UtilityNetworkDefinition`.
Categories.ItemsSource = _utilityNetwork.Definition.Categories;
Categories.SelectedItem = _utilityNetwork.Definition.Categories.First();
// Enable the UI.
FilterOptions.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, ex.GetType().Name).ShowAsync();
}
finally
{
LoadingBar.Visibility = Visibility.Collapsed;
}
}
private async void OnTrace(object sender, RoutedEventArgs e)
{
try
{
LoadingBar.Visibility = Visibility.Visible;
// Clear previous selection from the layers.
MyMapView.Map.OperationalLayers.OfType<FeatureLayer>().ToList().ForEach(layer => layer.ClearSelection());
if (Categories.SelectedItem is UtilityCategory category)
{
// NOTE: UtilityNetworkAttributeComparison or UtilityCategoryComparison with Operator.DoesNotExists
// can also be used. These conditions can be joined with either UtilityTraceOrCondition or UtilityTraceAndCondition.
UtilityCategoryComparison categoryComparison = new UtilityCategoryComparison(category, UtilityCategoryComparisonOperator.Exists);
// Add the filter barrier.
_configuration.Filter = new UtilityTraceFilter() { Barriers = categoryComparison };
}
// Set the include isolated features property.
_configuration.IncludeIsolatedFeatures = IncludeIsolatedFeatures.IsChecked == true;
// Build parameters for isolation trace.
_parameters.TraceConfiguration = _configuration;
// Get the trace result from trace.
IEnumerable<UtilityTraceResult> traceResult = await _utilityNetwork.TraceAsync(_parameters);
UtilityElementTraceResult elementTraceResult = traceResult?.FirstOrDefault() as UtilityElementTraceResult;
// Select all the features from the result.
if (elementTraceResult?.Elements?.Count > 0)
{
foreach (FeatureLayer layer in MyMapView.Map.OperationalLayers.OfType<FeatureLayer>())
{
IEnumerable<UtilityElement> elements = elementTraceResult.Elements.Where(element => element.NetworkSource.Name == layer.FeatureTable.TableName);
IEnumerable<Feature> features = await _utilityNetwork.GetFeaturesForElementsAsync(elements);
layer.SelectFeatures(features);
}
}
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, ex.GetType().Name).ShowAsync();
}
finally
{
LoadingBar.Visibility = Visibility.Collapsed;
}
}
private void OnReset(object sender, RoutedEventArgs e)
{
_parameters.Barriers.Clear();
_barrierOverlay.Graphics.Clear();
foreach (FeatureLayer layer in MyMapView.Map.OperationalLayers.OfType<FeatureLayer>())
{
layer.ClearSelection();
}
}
private async Task<UtilityTerminal> GetTerminalAsync(IEnumerable<UtilityTerminal> terminals)
{
try
{
MyMapView.GeoViewTapped -= OnGeoViewTapped;
TerminalPicker.Visibility = Visibility.Visible;
MainUI.Visibility = Visibility.Collapsed;
Picker.ItemsSource = terminals;
Picker.SelectedIndex = 1;
// Waits for user to select a terminal.
_terminalCompletionSource = new TaskCompletionSource<UtilityTerminal>();
return await _terminalCompletionSource.Task;
}
finally
{
TerminalPicker.Visibility = Visibility.Collapsed;
MainUI.Visibility = Visibility.Visible;
MyMapView.GeoViewTapped += OnGeoViewTapped;
}
}
private void OnTerminalSelected(object sender, RoutedEventArgs e)
{
_terminalCompletionSource.TrySetResult(Picker.SelectedItem as UtilityTerminal);
}
private async void OnGeoViewTapped(object sender, GeoViewInputEventArgs e)
{
try
{
LoadingBar.Visibility = Visibility.Visible;
// Identify the feature to be used.
IEnumerable<IdentifyLayerResult> identifyResult = await MyMapView.IdentifyLayersAsync(e.Position, 10.0, false);
ArcGISFeature feature = identifyResult?.FirstOrDefault()?.GeoElements?.FirstOrDefault() as ArcGISFeature;
if (feature == null) { return; }
// Create element from the identified feature.
UtilityElement element = _utilityNetwork.CreateElement(feature);
if (element.NetworkSource.SourceType == UtilityNetworkSourceType.Junction)
{
// Select terminal for junction feature.
IEnumerable<UtilityTerminal> terminals = element.AssetType.TerminalConfiguration?.Terminals;
if (terminals?.Count() > 1)
{
element.Terminal = await GetTerminalAsync(terminals);
}
}
else if (element.NetworkSource.SourceType == UtilityNetworkSourceType.Edge)
{
// Compute how far tapped location is along the edge feature.
if (feature.Geometry is Polyline line)
{
// Remove elevation data, FractionAlong only supports 2D lines.
line = line.RemoveZ() as Polyline;
double fraction = line.FractionAlong(e.Location, -1);
// Check for rare edge case where the fraction is invalid.
if (double.IsNaN(fraction)) { return; }
// Set the fraction of the utility element.
element.FractionAlongEdge = fraction;
}
}
// Check whether starting location or barrier is added to update the right collection and symbology.
_parameters.Barriers.Add(element);
Symbol symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, System.Drawing.Color.OrangeRed, 25d);
// Add a graphic for the new utility element.
Graphic traceLocationGraphic = new Graphic(feature.Geometry as MapPoint ?? e.Location, symbol);
_barrierOverlay.Graphics.Add(traceLocationGraphic);
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, ex.GetType().Name).ShowAsync();
}
finally
{
LoadingBar.Visibility = Visibility.Collapsed;
}
}
}
}