Run valve isolation trace

View on GitHub

Run a filtered trace to locate operable features that will isolate an area from the flow of network resources.

Image of run valve isolation trace with category comparison Image of run valve isolation trace with filter barriers

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 utility category. Toggle "Isolated Features" to update trace configuration. Tap "Trace" to run a subnetwork-based isolation trace. Tap "Reset" to clear filter barriers and trace results.

How it works

  1. Create a MapView instance.

  2. Create and load a ServiceGeodatabase with a feature service URL and get tables with their layer IDs.

  3. Create a Map object that contains FeatureLayer(s) created from the service geodatabase's tables.

  4. Create and load a UtilityNetwork with the same feature service URL and map. Use the onSingleTapGesture modifier to listen for tap events on the map view.

  5. Create UtilityTraceParameters with isolation trace type and a default starting location from a given asset type and global ID.

  6. Get a default UtilityTraceConfiguration from a given tier in a domain network. Set its filter property with an UtilityTraceFilter object.

  7. Add a GraphicsOverlay for showing starting location and filter barriers.

  8. Populate the choice list for the filter barriers from the categories property of UtilityNetworkDefinition.

  9. When the map view is tapped, identify which feature is at the tap location, and add a Graphic to represent a filter barrier.

  10. Create a UtilityElement for the identified feature and add this element to the trace parameters' filterBarriers property.

    • 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 it is an edge, set its fractionAlongEdge property using GeometryEngine.polyline(_:fractionalLengthClosestTo:tolerance:) method.
  11. If "Trace" is tapped without filter barriers:

    • Create a new UtilityCategoryComparison with the selected category and UtilityCategoryComparison.Operator.exists.
    • Assign this condition to UtilityTraceFilter.barriers from the default configuration from step 6.
    • Update the configuration's includesIsolatedFeatures property.
    • Set this configuration to the parameters' traceConfiguration property.
    • Run UtilityNetwork.trace(parameters:) with the specified parameters.

    If "Trace" is tapped with filter barriers:

    • Update includesIsolatedFeatures property of the default configuration from step 6.
    • Run UtilityNetwork.trace(parameters:) with the specified parameters.
  12. For every FeatureLayer in this map with trace result elements, select features by converting UtilityElement(s) to ArcGISFeature(s) using UtilityNetwork.features(for:).

Relevant API

  • GeometryEngine.polyline(_:fractionalLengthClosestTo:tolerance:)
  • ServiceGeodatabase
  • UtilityCategory
  • UtilityCategoryComparison
  • UtilityCategoryComparison.Operator
  • UtilityDomainNetwork
  • UtilityElement
  • UtilityElementTraceResult
  • UtilityNetwork
  • UtilityNetworkDefinition
  • UtilityTerminal
  • UtilityTier
  • UtilityTraceFilter
  • UtilityTraceParameters
  • UtilityTraceParameters.TraceType
  • UtilityTraceResult

About the data

The Naperville gas network feature service, hosted on ArcGIS Online, contains a utility network used to run the isolation trace shown in this sample.

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

RunValveIsolationTraceView.swiftRunValveIsolationTraceView.swiftRunValveIsolationTraceView.Model.swift
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
// Copyright 2023 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
//
//   https://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 ArcGIS
import SwiftUI

struct RunValveIsolationTraceView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// The last locations in the screen and map where a tap occurred.
    @State private var lastSingleTap: (screenPoint: CGPoint, mapPoint: Point)?

    /// A Boolean value indicating if the configuration sheet is presented.
    @State private var isConfigurationPresented = false

    /// A Boolean value indicating whether to include isolated features in the
    /// trace results when used in conjunction with an isolation trace.
    @State private var includeIsolatedFeatures = true

    var body: some View {
        MapViewReader { mapViewProxy in
            MapView(
                map: model.map,
                graphicsOverlays: [model.parametersOverlay]
            )
            .onSingleTapGesture { screenPoint, mapPoint in
                lastSingleTap = (screenPoint, mapPoint)
            }
            .overlay(alignment: .top) {
                Text(model.statusText)
                    .padding(10)
                    .frame(maxWidth: .infinity, alignment: .center)
                    .background(.ultraThinMaterial, ignoresSafeAreaEdges: .horizontal)
                    .multilineTextAlignment(.center)
            }
            .task {
                await model.setup()
                if let point = model.startingLocationPoint {
                    await mapViewProxy.setViewpointCenter(point, scale: 3_000)
                }
            }
            .task(id: lastSingleTap?.mapPoint) {
                guard let lastSingleTap else {
                    return
                }
                if let feature = try? await mapViewProxy.identifyLayers(
                    screenPoint: lastSingleTap.screenPoint,
                    tolerance: 10
                ).first?.geoElements.first as? ArcGISFeature {
                    model.addFilterBarrier(for: feature, at: lastSingleTap.mapPoint)
                }
            }
            .toolbar {
                ToolbarItemGroup(placement: .bottomBar) {
                    Button("Configuration") {
                        isConfigurationPresented.toggle()
                    }
                    .disabled(model.tracingActivity == .runningTrace ||
                              model.tracingActivity == .loadingServiceGeodatabase ||
                              model.tracingActivity == .loadingNetwork)
                    Spacer()
                    Button("Trace") {
                        Task { await model.trace(includeIsolatedFeatures: includeIsolatedFeatures) }
                    }
                    .disabled(!model.traceEnabled)
                    Spacer()
                    Button("Reset") {
                        model.reset()
                        if let point = model.startingLocationPoint {
                            Task { await mapViewProxy.setViewpointCenter(point, scale: 3_000) }
                        }
                    }
                    .disabled(!model.resetEnabled)
                }
            }
            .sheet(isPresented: $isConfigurationPresented) {
                NavigationStack {
                    configurationView
                }
            }
            .overlay(alignment: .center) {
                if let tracingActivity = model.tracingActivity {
                    VStack {
                        Text(tracingActivity.label)
                        ProgressView()
                            .progressViewStyle(.circular)
                    }
                    .padding()
                    .background(.thinMaterial)
                    .cornerRadius(10)
                }
            }
            .alert(
                "Select Terminal",
                isPresented: $model.terminalSelectorIsOpen,
                actions: { terminalPickerButtons }
            )
        }
    }

    /// Buttons for each the available terminals on the last added utility element.
    @ViewBuilder var terminalPickerButtons: some View {
        if let lastAddedElement = model.lastAddedElement,
           let terminalConfiguration = lastAddedElement.assetType.terminalConfiguration {
            ForEach(terminalConfiguration.terminals) { terminal in
                Button(terminal.name) {
                    lastAddedElement.terminal = terminal
                    model.terminalSelectorIsOpen = false
                    model.addTerminal(to: lastSingleTap!.mapPoint)
                }
            }
        }
    }

    @ViewBuilder var configurationView: some View {
        Form {
            Section {
                List(model.filterBarrierCategories, id: \.name) { category in
                    HStack {
                        Text(category.name)
                        Spacer()
                        if category === model.selectedCategory {
                            Image(systemName: "checkmark")
                                .foregroundStyle(Color.accentColor)
                        }
                    }
                    // Allows the whole row to be tapped. Without this only the text is
                    // tappable.
                    .contentShape(Rectangle())
                    .onTapGesture {
                        if category.name == model.selectedCategory?.name {
                            model.unselectCategory(category)
                        } else {
                            model.selectCategory(category)
                        }
                    }
                }
            } header: {
                Text("Category")
            } footer: {
                Text("Choose a category to run the valve isolation trace. The selected utility category defines constraints and conditions based upon specific characteristics of asset types in the utility network.")
            }
            Section {
                Toggle(isOn: $includeIsolatedFeatures) {
                    Text("Include Isolated Features")
                }
            } header: {
                Text("Other Options")
            } footer: {
                Text("Choose whether or not the trace should include isolated features. This means that isolated features are included in the trace results when used in conjunction with an isolation trace.")
            }
            .toggleStyle(.switch)
        }
        .navigationTitle("Configuration")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .confirmationAction) {
                Button("Done") { isConfigurationPresented = false }
            }
        }
    }
}

extension RunValveIsolationTraceView.Model.TracingActivity {
    /// A human-readable label for the tracing activity.
    var label: String {
        switch self {
        case .loadingServiceGeodatabase: return "Loading service geodatabase…"
        case .loadingNetwork: return "Loading utility network…"
        case .startingLocation: return "Getting starting location feature…"
        case .runningTrace: return "Running isolation trace…"
        }
    }
}

#Preview {
    NavigationStack {
        RunValveIsolationTraceView()
    }
}

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