Perform valve isolation trace

View on GitHub
Sample viewer app

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

Image of Perform valve isolation trace

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

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.

How it works

  1. Create an ArcGISMap and add it to a MapView.
  2. Using the URL to a utility network's feature service, create FeatureLayers that contain the utility network's features, and add them to the operational layers of the map.
  3. Create and load a UtilityNetwork with the same feature service URL and map.
  4. Get a default UtilityTraceConfiguration from a given tier in a domain network. Set its filter with a new UtilityTraceFilter.
  5. Create a UtilityElement to represent a default starting location from the UtilityNetwork, using a given asset type and global ID.
  6. Use utilityNetwork.fetchFeaturesForElementsAsync() to obtain the Geometry of this element, and create a Graphic with that geometry.
  7. Add a GraphicsOverlay with the graphic that represents the starting location.
  8. Populate the combo box for choosing the filter barrier category from UtilityNetworkDefinition.getCategories().
  9. When "Trace" is clicked,

    • Create a new UtilityCategoryComparison with the selected category and UtilityCategoryComparisonOperator.EXISTS.
    • Assign this condition to utilityTraceFilter.setBarriers() from the default configuration from step 7. Update this configuration's utilityTraceConfiguration.isIncludeIsolatedFeatures() property.
    • Create a UtilityTraceParameters with UtilityTraceType.ISOLATION and default starting location from step 4.
    • Set its utility trace configuration with this configuration and then, run a utilityNetwork.traceAsync().
  10. Get the list of UtilityElements from the first trace result.
  11. For every feature layer in the map, select all the features for which the layer's FeatureTable.getTableName() matches the NetworkSource.getName() of one of the utility elements.

Relevant API

  • UtilityCategory
  • UtilityCategoryComparison
  • UtilityCategoryComparisonOperator
  • UtilityDomainNetwork
  • UtilityElement
  • UtilityElementTraceResult
  • UtilityNetwork
  • UtilityNetworkDefinition
  • UtilityTier
  • UtilityTraceFilter
  • UtilityTraceParameters
  • UtilityTraceResult
  • UtilityTraceType

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.

Tags

category comparison, condition barriers, isolated features, network analysis, subnetwork trace, trace configuration, trace filter, utility network

Sample Code

PerformValveIsolationTraceController.javaPerformValveIsolationTraceSample.javaUtilityCategoryListCell.javamain.fxml
                                                                                                                                                                                                                                                                                                                               
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright 2020 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.
 */

package com.esri.samples.perform_valve_isolation_trace;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.paint.Color;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.ArcGISFeature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.security.UserCredential;
import com.esri.arcgisruntime.symbology.ColorUtil;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.esri.arcgisruntime.utilitynetworks.UtilityAssetGroup;
import com.esri.arcgisruntime.utilitynetworks.UtilityAssetType;
import com.esri.arcgisruntime.utilitynetworks.UtilityCategory;
import com.esri.arcgisruntime.utilitynetworks.UtilityCategoryComparison;
import com.esri.arcgisruntime.utilitynetworks.UtilityCategoryComparisonOperator;
import com.esri.arcgisruntime.utilitynetworks.UtilityDomainNetwork;
import com.esri.arcgisruntime.utilitynetworks.UtilityElement;
import com.esri.arcgisruntime.utilitynetworks.UtilityElementTraceResult;
import com.esri.arcgisruntime.utilitynetworks.UtilityNetwork;
import com.esri.arcgisruntime.utilitynetworks.UtilityNetworkDefinition;
import com.esri.arcgisruntime.utilitynetworks.UtilityNetworkSource;
import com.esri.arcgisruntime.utilitynetworks.UtilityTier;
import com.esri.arcgisruntime.utilitynetworks.UtilityTraceConfiguration;
import com.esri.arcgisruntime.utilitynetworks.UtilityTraceFilter;
import com.esri.arcgisruntime.utilitynetworks.UtilityTraceParameters;
import com.esri.arcgisruntime.utilitynetworks.UtilityTraceResult;
import com.esri.arcgisruntime.utilitynetworks.UtilityTraceType;

public class PerformValveIsolationTraceController {

  @FXML private MapView mapView;
  @FXML private ProgressIndicator progressIndicator;
  @FXML private ComboBox<UtilityCategory> categorySelectionComboBox;
  @FXML private Button traceButton;
  @FXML private Label statusLabel;
  @FXML private CheckBox includeIsolatedFeaturesCheckbox;

  private UtilityNetwork utilityNetwork;
  private UtilityTraceConfiguration traceConfiguration;
  private UtilityElement startingLocation;

  public void initialize() {
    try {

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a map with the streets night basemap style and set it to the map view
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS_NIGHT);
      mapView.setMap(map);

      // load the utility network data from the feature service and create feature layers
      String featureServiceURL =
              "https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleGas/FeatureServer";

      ServiceFeatureTable distributionLineFeatureTable = new ServiceFeatureTable(featureServiceURL + "/3");
      FeatureLayer distributionLineLayer = new FeatureLayer(distributionLineFeatureTable);

      ServiceFeatureTable deviceFeatureTable = new ServiceFeatureTable(featureServiceURL + "/0");
      FeatureLayer deviceLayer = new FeatureLayer(deviceFeatureTable);

      // add the feature layers to the map
      map.getOperationalLayers().addAll(Arrays.asList(distributionLineLayer, deviceLayer));

      // create a graphics overlay for the starting location and add it to the map view
      GraphicsOverlay startingLocationGraphicsOverlay = new GraphicsOverlay();
      mapView.getGraphicsOverlays().add(startingLocationGraphicsOverlay);

      // create and apply a renderer for the starting point graphics overlay
      SimpleMarkerSymbol startingPointSymbol =
              new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CROSS, ColorUtil.colorToArgb(Color.LIGHTGREEN), 25);
      startingLocationGraphicsOverlay.setRenderer(new SimpleRenderer(startingPointSymbol));

      // create and load the utility network
      utilityNetwork = new UtilityNetwork(featureServiceURL);

      // set user credentials to authenticate with the service
      // NOTE: a licensed user is required to perform utility network operations
      UserCredential userCredential = new UserCredential("viewer01", "I68VGU^nMurF");
      utilityNetwork.setCredential(userCredential);

      utilityNetwork.loadAsync();
      utilityNetwork.addDoneLoadingListener(() -> {
        if (utilityNetwork.getLoadStatus() == LoadStatus.LOADED) {

          // get a trace configuration from a tier
          UtilityNetworkDefinition networkDefinition = utilityNetwork.getDefinition();
          UtilityDomainNetwork domainNetwork = networkDefinition.getDomainNetwork("Pipeline");
          UtilityTier tier = domainNetwork.getTier("Pipe Distribution System");
          traceConfiguration = tier.getTraceConfiguration();

          // create a trace filter
          traceConfiguration.setFilter(new UtilityTraceFilter());

          // get a default starting location
          UtilityNetworkSource networkSource = networkDefinition.getNetworkSource("Gas Device");
          UtilityAssetGroup assetGroup = networkSource.getAssetGroup("Meter");
          UtilityAssetType assetType = assetGroup.getAssetType("Customer");
          startingLocation = utilityNetwork.createElement(assetType, UUID.fromString("98A06E95-70BE-43E7-91B7-E34C9D3CB9FF"));

          // get the first feature for the starting location, and get its geometry
          ListenableFuture<List<ArcGISFeature>> elementFeaturesFuture =
                  utilityNetwork.fetchFeaturesForElementsAsync(Collections.singletonList(startingLocation));

          elementFeaturesFuture.addDoneListener(() -> {
            try {
              List<ArcGISFeature> startingLocationFeatures = elementFeaturesFuture.get();

              if (!startingLocationFeatures.isEmpty()) {
                Geometry startingLocationGeometry = startingLocationFeatures.get(0).getGeometry();

                if (startingLocationGeometry instanceof Point) {
                  Point startingLocationGeometryPoint = (Point) startingLocationGeometry;

                  // create a graphic for the starting location and add it to the graphics overlay
                  Graphic startingLocationGraphic = new Graphic(startingLocationGeometry, startingPointSymbol);
                  startingLocationGraphicsOverlay.getGraphics().add(startingLocationGraphic);

                  // set the map's viewpoint to the starting location
                  mapView.setViewpointAsync(new Viewpoint(startingLocationGeometryPoint, 3000));

                  // build the choice list for categories populated with the 'Name' property of each 'UtilityCategory' in the 'UtilityNetworkDefinition'
                  categorySelectionComboBox.getItems().addAll(networkDefinition.getCategories());
                  categorySelectionComboBox.getSelectionModel().select(0);
                  categorySelectionComboBox.setCellFactory(param -> new UtilityCategoryListCell());
                  categorySelectionComboBox.setButtonCell(new UtilityCategoryListCell());

                  // enable the UI
                  enableUI(true);

                  // update the status text
                  statusLabel.setText("Utility network loaded. Ready to perform trace...");
                }

              } else {
                new Alert(Alert.AlertType.ERROR, "Error getting starting location geometry.").show();
              }
            } catch (ExecutionException | InterruptedException e) {
              new Alert(Alert.AlertType.ERROR, "Error getting starting location feature.").show();
            }
          });

        } else {
          new Alert(Alert.AlertType.ERROR, "Error loading Utility Network.").show();
        }
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Uses the starting location and the selected filter barrier category to perform a valve isolation trace, then
   * selects all connected elements found in the trace to highlight them.
   */
  @FXML
  private void handleTraceClick() {
    try {
      // clear previous selection from the layers
      mapView.getMap().getOperationalLayers().forEach(layer -> {
        if (layer instanceof FeatureLayer) {
          ((FeatureLayer) layer).clearSelection();
        }
      });

      // disable the UI, show the progress indicator and update the status text
      enableUI(false);
      statusLabel.setText("Running isolation trace...");

      // get the selected utility category
      UtilityCategory selectedCategory = categorySelectionComboBox.getSelectionModel().getSelectedItem();
      if (selectedCategory != null) {
        // create a category comparison for the trace
        // 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(selectedCategory, UtilityCategoryComparisonOperator.EXISTS);
        // set the category comparison to the barriers of the configuration's trace filter
        traceConfiguration.getFilter().setBarriers(categoryComparison);
      }

      // set the configuration to include or leave out isolated features
      traceConfiguration.setIncludeIsolatedFeatures(includeIsolatedFeaturesCheckbox.isSelected());

      // build parameters for the isolation trace
      UtilityTraceParameters traceParameters = new UtilityTraceParameters(UtilityTraceType.ISOLATION, Collections.singletonList(startingLocation));
      traceParameters.setTraceConfiguration(traceConfiguration);

      // run the trace and get the result
      ListenableFuture<List<UtilityTraceResult>> utilityTraceResultsFuture = utilityNetwork.traceAsync(traceParameters);
      utilityTraceResultsFuture.addDoneListener(() -> {
        try {
          List<UtilityTraceResult> utilityTraceResults = utilityTraceResultsFuture.get();

          if (utilityTraceResults.get(0) instanceof UtilityElementTraceResult) {
            UtilityElementTraceResult utilityElementTraceResult = (UtilityElementTraceResult) utilityTraceResults.get(0);

            if (!utilityElementTraceResult.getElements().isEmpty()) {

              // iterate through the map's feature layers
              mapView.getMap().getOperationalLayers().forEach(layer -> {
                if (layer instanceof FeatureLayer) {

                  // create query parameters to find features whose network source name matches the layer's feature
                  // table name
                  QueryParameters queryParameters = new QueryParameters();
                  utilityElementTraceResult.getElements().forEach(utilityElement -> {

                    String networkSourceName = utilityElement.getNetworkSource().getName();
                    String featureTableName = ((FeatureLayer) layer).getFeatureTable().getTableName();

                    if (networkSourceName.equals(featureTableName)) {
                      queryParameters.getObjectIds().add(utilityElement.getObjectId());
                    }
                  });

                  // select features that match the query
                  ListenableFuture<FeatureQueryResult> featureQueryResultListenableFuture =
                          ((FeatureLayer) layer).selectFeaturesAsync(queryParameters, FeatureLayer.SelectionMode.NEW);

                  // wait for the selection to finish
                  featureQueryResultListenableFuture.addDoneListener(() -> {
                    // update the status text, enable the buttons and hide the progress indicator
                    statusLabel.setText("Isolation trace completed.");
                    enableUI(true);
                  });
                }
              });

            } else {
              statusLabel.setText("Isolation trace completed.");
              new Alert(Alert.AlertType.INFORMATION, "Isolation trace returned no elements.").show();
              enableUI(true);
            }

          } else {
            statusLabel.setText("Trace failed.");
            new Alert(Alert.AlertType.ERROR, "Isolation trace result is not a utility element.").show();
            enableUI(true);
          }

        } catch (Exception e) {
          statusLabel.setText("Trace failed.");
          new Alert(Alert.AlertType.ERROR, "Error getting isolation trace result.").show();
          enableUI(true);
        }
      });

    } catch (Exception e) {
      new Alert(Alert.AlertType.ERROR, "Error performing isolation trace.").show();
      enableUI(true);
    }
  }


  /**
   * Enables/disables the UI and hides/shows the progress indicator.
   *
   * @param enable whether to enable or disable the UI
   */
  private void enableUI(boolean enable) {
    progressIndicator.setVisible(!enable);
    traceButton.setDisable(!enable);
    categorySelectionComboBox.setDisable(!enable);
    includeIsolatedFeaturesCheckbox.setDisable(!enable);
  }

  /**
   * Stops and releases all resources used in application.
   */
  public void terminate() {

    if (mapView != null) {
      mapView.dispose();
    }
  }
}

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