Display utility associations

View on GitHub
Sample viewer app

Create graphics for utility associations in a utility network.

Image of display utility associations

Use case

Visualizing utility associations can help you to better understand trace results and the topology of your utility network. For example, connectivity associations allow you to model connectivity between two junctions that don't have geometric coincidence (are not in the same location); structural attachment associations allow you to model equipment that may be attached to structures; and containment associations allow you to model features contained within other features.

How to use the sample

Pan and zoom around the map. Observe graphics that show utility associations between junctions.

How it works

  1. Create an UtilityNetwork with a feature service URL, add it to the ArcGISMap's utility networks list, then load it.
  2. Add a FeatureLayer to the map for every UtilityNetworkSource of type EDGE or JUNCTION.
  3. Create a GraphicsOverlay for the utility associations.
  4. Add a NavigationChangedListener to listen for NavigationChangedEvents.
  5. When the sample starts and every time the viewpoint changes:

    • Get the geometry of the map view's extent.
    • Get the associations that are within the current extent using getAssociationsAsync(extent).
    • Get the UtilityAssociationType for each association.
    • Create a Graphic using the Geometry property of the association and a preferred symbol.
    • Add the graphic to the graphics overlay.

Relevant API

  • GraphicsOverlay
  • UtilityAssociation
  • UtilityAssociationType
  • UtilityNetwork

About the data

The Naperville electrical network feature service, hosted on ArcGIS Online, contains a utility network used to display the utility associations 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

associating, association, attachment, connectivity, containment, relationships

Sample Code

DisplayUtilityAssociationsSample.java
                                                                                                                                                                                                                                                                            
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
/*
 * 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.display_utility_associations;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.layers.FeatureLayer;
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.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer;
import com.esri.arcgisruntime.utilitynetworks.UtilityAssociation;
import com.esri.arcgisruntime.utilitynetworks.UtilityAssociationType;
import com.esri.arcgisruntime.utilitynetworks.UtilityNetwork;
import com.esri.arcgisruntime.utilitynetworks.UtilityNetworkSource;

public class DisplayUtilityAssociationsSample extends Application {

  private GraphicsOverlay associationsOverlay;
  private MapView mapView;
  private UtilityNetwork utilityNetwork;

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Display Utility Associations Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();

      // 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 topographic basemap style
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

      // create a viewpoint to focus on the utility networks' extent
      Viewpoint viewPoint = new Viewpoint(41.8057655, -88.1489692, 23);

      // set the initial viewpoint
      map.setInitialViewpoint(viewPoint);

      // create a map view, set the map, and add the graphics overlay
      mapView = new MapView();
      mapView.setMap(map);

      // create the utility network
      utilityNetwork = new UtilityNetwork("https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer");

      // 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);

      // add the utility network to the map before loading
      map.getUtilityNetworks().add(utilityNetwork);
      // load the utility network and get all of the edges and junctions in the network
      utilityNetwork.loadAsync();
      utilityNetwork.addDoneLoadingListener(() -> {
        List<UtilityNetworkSource> networkSources = utilityNetwork.getDefinition().getNetworkSources();

        for (UtilityNetworkSource networkSource : networkSources)
          // add all edges that are not subnet lines to the map
          if (networkSource.getSourceType() == UtilityNetworkSource.Type.EDGE && networkSource.getSourceUsageType() != UtilityNetworkSource.UsageType.SUBNET_LINE) {
            map.getOperationalLayers().add(new FeatureLayer(networkSource.getFeatureTable()));
          }
          // add all junctions to the map
          else if (networkSource.getSourceType() == UtilityNetworkSource.Type.JUNCTION) {
            map.getOperationalLayers().add(new FeatureLayer(networkSource.getFeatureTable()));
          }

        // add association graphics at the initial view point
        addAssociationsGraphics();
        // listen for navigation changes
        mapView.addNavigationChangedListener(event -> addAssociationsGraphics());
      });

      // create labels for the preview
      Label attachmentLabel = new Label("Attachment");
      Label connectivityLabel = new Label("Connectivity");

      // create symbols for the associations and preview
      SimpleLineSymbol attachmentSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DOT, 0xFF00FF00, 5);
      SimpleLineSymbol connectivitySymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DOT, 0xFFFF0000, 5);

      // create image views for the association symbols to display them in the legend
      ImageView attachmentImageView = addSymbolToImageView(attachmentSymbol);
      ImageView connectivityImageView = addSymbolToImageView(connectivitySymbol);

      // create a unique value for each association type with the created symbols
      UniqueValueRenderer.UniqueValue attachmentUniqueValue = new UniqueValueRenderer.UniqueValue(
        "Attachment", "", attachmentSymbol, Collections.singletonList(UtilityAssociationType.ATTACHMENT.toString()));
      UniqueValueRenderer.UniqueValue connectivityUniqueValue = new UniqueValueRenderer.UniqueValue(
        "Connectivity", "", connectivitySymbol, Collections.singletonList(UtilityAssociationType.CONNECTIVITY.toString()));

      // create a unique value renderer using the values
      UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer(
        Collections.singletonList("AssociationType"), Arrays.asList(attachmentUniqueValue, connectivityUniqueValue), "", null);

      // create a graphics overlay for the associations and set the renderer
      associationsOverlay = new GraphicsOverlay();
      associationsOverlay.setRenderer(uniqueValueRenderer);
      mapView.getGraphicsOverlays().add(associationsOverlay);

      // create a legend
      GridPane gridPane = new GridPane();
      gridPane.getColumnConstraints().addAll(Arrays.asList(new ColumnConstraints(25), new ColumnConstraints(100)));
      gridPane.add(attachmentImageView, 0, 0);
      gridPane.add(attachmentLabel, 1, 0);
      gridPane.add(connectivityImageView, 0, 1);
      gridPane.add(connectivityLabel, 1, 1);
      gridPane.setMaxWidth(100);
      gridPane.setMaxHeight(80);
      gridPane.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(255,255,255,0.3)"), CornerRadii.EMPTY,
              Insets.EMPTY)));
      gridPane.setPadding(new Insets(10));
      gridPane.setVgap(20);
      gridPane.setAlignment(Pos.CENTER);

      // add the map view and legend to the stack pane
      stackPane.getChildren().addAll(mapView, gridPane);
      StackPane.setAlignment(gridPane, Pos.TOP_LEFT);
      StackPane.setMargin(gridPane, new Insets(10, 0, 0, 10));

    } catch (Exception e) {
      // on any error, display the stack trace
      e.printStackTrace();
    }
  }

  /**
   * Get the utility association type for each association within the extent, create graphics and add it to the graphics overlay.
   */
  private void addAssociationsGraphics() {
    try {
      // if the current viewpoint is within the max scale, add association graphics
      if (!(mapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE).getTargetScale() >= 2000)) {
        // check if the current viewpoint has an extent
        Envelope extent = mapView.getCurrentViewpoint(Viewpoint.Type.BOUNDING_GEOMETRY).getTargetGeometry().getExtent();
        if (extent != null) {

          // get all of the associations in the extent of the viewpoint
          ListenableFuture<List<UtilityAssociation>> associationsFuture = utilityNetwork.getAssociationsAsync(extent);
          associationsFuture.addDoneListener(() -> {
            try {
              List<UtilityAssociation> associations = associationsFuture.get();

              // iterate through all associations
              associations.forEach(association -> {

                // check if the graphics overlay already contains the association
                if (associationsOverlay.getGraphics().stream().noneMatch(graphic ->
                  graphic.getAttributes().containsKey("GlobalId")
                    && graphic.getAttributes().get("GlobalId") == association.getGlobalId())
                  && association.getGeometry() != null) {

                  // create attributes for the graphic so that the renderers display the appropriate symbol
                  Map<String, Object> attributes = Map.of(
                    "GlobalId", association.getGlobalId(),
                    "AssociationType", association.getAssociationType().toString());

                  // create and add the graphic for the association
                  Graphic newGraphic = new Graphic(association.getGeometry(), attributes);
                  associationsOverlay.getGraphics().add(newGraphic);
                }
              });
              // this is thrown when there are too many associations in the extent
            } catch (InterruptedException | ExecutionException e) {
              Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage());
              alert.setResizable(true);
              alert.show();
            }
          });
        }
      }
    } catch (Exception e) {
      new Alert(Alert.AlertType.ERROR, e.getMessage()).show();
    }
  }

  /**
   * Returns an imageView populated with a symbol.
   *
   * @param symbol the symbol to display in the imageView
   * @return imageView the imageView populated with the symbol
   */
  private ImageView addSymbolToImageView(SimpleLineSymbol symbol) {

    // add the connectivity symbol to the image view to display it in the legend
    ImageView imageView = new ImageView();
    ListenableFuture<Image> connectivityImage = symbol.createSwatchAsync(0x00000000);
    connectivityImage.addDoneListener(() -> {
      try {
        // display the image in the image view
        imageView.setImage(connectivityImage.get());
      } catch (InterruptedException | ExecutionException e) {
        new Alert(Alert.AlertType.ERROR, "Error creating preview ImageView from provided symbol").show();
      }
    });
    return imageView;
  }

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

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

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}

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