Validate utility network topology

View on GitHubSample viewer app

Demonstrates how to get the network state and validate the topology of a utility network.

Image of validate utility network topology

Use case

Dirty areas are generated where edits to utility network features have not been evaluated against the network rules. Tracing across this area could result in an error or return inaccurate results. Validating the utility network updates the network topology with the edited feature data, maintaining consistency between the features and topology. Querying the network state allows you to determine if there are dirty areas or errors in a utility network, and if it supports network topology.

How to use the sample

Select features to make edits and then use 'Apply' to send edits to the server.

  • Click 'Get state' to check if validate is required or if tracing is available.
  • Click 'Validate' to validate network topology and clear dirty areas.
  • Click 'Trace' to run a trace.

How it works

  1. Create and load an ArcGISMap with a web map item URL.
  2. Load the UtilityNetwork from the web map and switch its ServiceGeodatabase to a new branch version.
  3. Add LabelDefinitions for the fields that will be updated on a feature edit.
  4. Add the UtilityNetwork.dirtyAreaTable to the map to visualize dirty areas or errors.
  5. Set a default starting location and trace parameters to stop traversability on an open device.
  6. Get the UtilityNetworkCapabilities from the UtilityNetworkDefinition and use these values to enable or disable the 'Get state', 'Validate', and 'Trace' buttons.
  7. When an ArcGISFeature is selected for editing, populate the choice list for the field value using the field's CodedValueDomain.codedValues.
  8. When 'Apply' is clicked, update the value of the selected feature's attribute value with the selected CodedValue.code and call ServiceGeodatabase.applyEdits().
  9. When 'Get state' is clicked, call UtilityNetwork.getState() and print the results.
  10. When 'Validate' is clicked, get the current map extent and call UtilityNetwork.validateNetworkTopology(...).
  11. When 'Trace' is clicked, call UtilityNetwork.trace(...) with the predefined parameters and select all features returned.
  12. When 'Clear Selection' or 'Cancel' are clicked, clear all selected features on each layer in the map and close the attribute picker.

Relevant API

  • UtilityElement
  • UtilityElementTraceResult
  • UtilityNetwork
  • UtilityNetworkCapabilities
  • UtilityNetworkState
  • UtilityNetworkValidationJob
  • UtilityTraceConfiguration
  • UtilityTraceParameters
  • UtilityTraceResult

About the data

The Naperville electric feature service contains a utility network that can be used to query the network state and validate network topology before tracing. The Naperville electric webmap uses the same feature service endpoint and is shown in this sample. Authentication is required and handled within the sample code.

Additional information

Starting from 200.4, an Advanced Editing extension license is required for editing a utility network in the following cases:

  • Stand-alone mobile geodatabase that is exported from ArcGIS Pro 2.7 or higher
  • Sync-enabled mobile geodatabase that is generated from an ArcGIS Enterprise Feature Service 11.2 or higher
  • Webmap or service geodatabase that points to an ArcGIS Enterprise Feature Service 11.2 or higher

Please refer to the "Advanced Editing" section in the extension license table in License levels and capabilities for details.

Tags

dirty areas, edit, network topology, online, state, trace, utility network, validate

Sample Code

ValidateUtilityNetworkTopologyViewModel.ktValidateUtilityNetworkTopologyViewModel.ktMainActivity.ktValidateUtilityNetworkTopologyScreen.kt
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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* Copyright 2025 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.arcgismaps.sample.validateutilitynetworktopology.components

import android.app.Application
import androidx.compose.ui.unit.dp
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.Color
import com.arcgismaps.Guid
import com.arcgismaps.arcgisservices.FeatureServiceSessionType
import com.arcgismaps.arcgisservices.ServiceVersionParameters
import com.arcgismaps.arcgisservices.VersionAccess
import com.arcgismaps.data.ArcGISFeature
import com.arcgismaps.data.CodedValue
import com.arcgismaps.data.CodedValueDomain
import com.arcgismaps.data.Field
import com.arcgismaps.data.QueryParameters
import com.arcgismaps.data.ServiceFeatureTable
import com.arcgismaps.data.ServiceGeodatabase
import com.arcgismaps.geometry.Envelope
import com.arcgismaps.geometry.Geometry
import com.arcgismaps.geometry.GeometryEngine
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.httpcore.authentication.ArcGISAuthenticationChallengeHandler
import com.arcgismaps.httpcore.authentication.ArcGISAuthenticationChallengeResponse
import com.arcgismaps.httpcore.authentication.TokenCredential
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.labeling.LabelDefinition
import com.arcgismaps.mapping.labeling.SimpleLabelExpression
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.layers.SelectionMode
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.symbology.TextSymbol
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.mapping.view.ScreenCoordinate
import com.arcgismaps.portal.Portal
import com.arcgismaps.tasks.geoprocessing.GeoprocessingExecutionType
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.arcgismaps.utilitynetworks.UtilityElement
import com.arcgismaps.utilitynetworks.UtilityElementTraceResult
import com.arcgismaps.utilitynetworks.UtilityNetwork
import com.arcgismaps.utilitynetworks.UtilityTraceParameters
import com.arcgismaps.utilitynetworks.UtilityTraceType
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import com.esri.arcgismaps.sample.validateutilitynetworktopology.components.ValidateUtilityNetworkTopologyViewModel.Companion.PASSWORD
import com.esri.arcgismaps.sample.validateutilitynetworktopology.components.ValidateUtilityNetworkTopologyViewModel.Companion.USERNAME
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.UUID

class ValidateUtilityNetworkTopologyViewModel(application: Application) :
    AndroidViewModel(application) {

    // The map containing the Naperville Electric web map
    val arcGISMap = ArcGISMap(
        item = PortalItem(
            itemId = NAPERVILLE_ELECTRIC_WEBMAP_ITEM_ID,
            portal = Portal(
                url = SAMPLE_SERVER_7_PORTAL,
                connection = Portal.Connection.Authenticated
            )
        )
    ).apply {
        initialViewpoint = Viewpoint(center = Point(-9815160.0, 5128780.0), scale = 4000.0)
        // Set the map to load in persistent session mode (workaround for server caching issue)
        // https://support.esri.com/en-us/bug/asynchronous-validate-request-for-utility-network-servi-bug-000160443
        loadSettings.featureServiceSessionType = FeatureServiceSessionType.Persistent
    }

    // Used to update map viewpoint and identify layers on tap
    val mapViewProxy = MapViewProxy()

    // Graphic to represent the starting point of the downstream trace
    private val startingLocationGraphic = Graphic(
        symbol = SimpleMarkerSymbol(
            style = SimpleMarkerSymbolStyle.Cross, color = Color.green, size = 25f
        )
    )

    // Overlay to show the starting location for tracing
    val graphicsOverlay = GraphicsOverlay(listOf(startingLocationGraphic))

    // The utility network used for tracing
    private val utilityNetwork: UtilityNetwork
        get() = arcGISMap.utilityNetworks.first()

    // ServiceGeodatabase from the utility network, used for switching to a new version
    private var serviceGeodatabase: ServiceGeodatabase? = null

    // Parameters for a downstream trace
    private var traceParameters: UtilityTraceParameters? = null

    // Track the state of utility network capabilities
    private val _canGetState = MutableStateFlow(false)
    val canGetState = _canGetState.asStateFlow()

    // Track the state of utility network topology
    private val _canTrace = MutableStateFlow(false)
    val canTrace = _canTrace.asStateFlow()

    // Track the state when we have dirty areas or errors
    private val _canValidateNetworkTopology = MutableStateFlow(false)
    val canValidateNetworkTopology = _canValidateNetworkTopology.asStateFlow()

    // Track the state when selections can be cleared from map
    private val _canClearSelection = MutableStateFlow(false)
    val canClearSelection = _canClearSelection.asStateFlow()

    // A collapsable status message to display in the UI
    private val _statusMessage = MutableStateFlow("")
    val statusMessage = _statusMessage.asStateFlow()

    // Currently selected feature that the user is editing
    private val _selectedFeature = MutableStateFlow<ArcGISFeature?>(null)
    val selectedFeature = _selectedFeature.asStateFlow()

    // Field being edited on the selected feature (e.g. "devicestatus" or "nominalvoltage")
    private var selectedField: Field? = null

    // List of coded values from the above field's domain
    private val _fieldValueOptions = MutableStateFlow<List<CodedValue>>(emptyList())
    val fieldValueOptions = _fieldValueOptions.asStateFlow()

    // Selected field coded value from the domain
    private val _selectedFieldValue = MutableStateFlow<CodedValue?>(null)
    val selectedFieldValue = _selectedFieldValue.asStateFlow()

    // Keep track of the current visible area to validate network topology
    private val _currentVisibleArea = MutableStateFlow(Envelope(Point(0.0, 0.0), Point(0.0, 0.0)))

    /**
     * Add credentials and loads the map & utility network. Then creates & switches to a
     * new version for editing. Sets up the default trace parameters and adds the dirty area
     * table as a feature layer to the map. Sets the button states based on
     * utility network capabilities.
     */
    suspend fun initialize() {
        // Authenticate and load ArcGIS map
        setupMap()
        // Load utility network, and switch to a new version to allow edits
        setupUtilityNetwork()
        // Set up the default trace parameters (downstream)
        setupTraceParameters()
        // Check capabilities from the network definition to enable/disable relevant UI.
        setButtonStatesFromCapabilities()
        // Display contextual hint
        _statusMessage.value = INIT_STATUS_MESSAGE
    }

    /**
     * Authenticate and load map.
     */
    private suspend fun setupMap() {
        ArcGISEnvironment.apply {
            applicationContext = getApplication<Application>().applicationContext
            authenticationManager.arcGISAuthenticationChallengeHandler = getAuthChallengeHandler()
        }
        // Load the Naperville electric web-map
        arcGISMap.load().onFailure {
            handleError(
                title = "Error loading the web-map: ${it.message}",
                description = it.cause.toString()
            )
        }
    }

    /**
     * Create a new version and switch to allow for attribute editing.
     */
    private suspend fun setupUtilityNetwork() {
        // Load the utility network associated with the web-map
        utilityNetwork.load().onFailure {
            handleError(
                title = "Error loading the utility network: ${it.message}",
                description = it.cause.toString()
            )
        }
        // Construct version parameters
        val parameters = ServiceVersionParameters().apply {
            name = "ValidateNetworkTopology_${UUID.randomUUID()}"
            description = "Validate network topology with ArcGIS Maps SDK."
            access = VersionAccess.Private
        }
        // Retrieve the service geodatabase from the utility network
        serviceGeodatabase = utilityNetwork.serviceGeodatabase?.apply {
            // Load the service geodatabase
            load().onFailure {
                return handleError("Error loading service geodatabase: ${it.message}")
            }
            // Create a new private service version
            val versionInfo = createVersion(newVersion = parameters).getOrElse {
                return handleError("Unable to create version", "${it.message}")
            }
            // Switch the service geodatabase to the newly created version
            switchVersion(versionName = versionInfo.name).onFailure {
                return handleError("Failed to switch to version", "${it.message}")
            }
        }
        // Add the dirty area table to the map to visualize it
        utilityNetwork.dirtyAreaTable?.let { dirtyAreaTable ->
            arcGISMap.operationalLayers.add(
                element = FeatureLayer.createWithFeatureTable(dirtyAreaTable)
            )
        }
        // Add labels to the map to visualize attribute editing
        addLabels(
            layerName = DEVICE_TABLE_NAME,
            fieldName = DEVICE_STATUS_FIELD,
            textColor = Color.blue
        )
        addLabels(
            layerName = LINE_TABLE_NAME,
            fieldName = NOMINAL_VOLTAGE_FIELD,
            textColor = Color.red
        )
    }

    /**
     * Downstream trace parameters from a known device location,
     * stopping traversal on an open device.
     */
    private suspend fun setupTraceParameters() {
        // Get the definition of the utility network
        val networkDefinition = utilityNetwork.definition ?: return
        // Look up the network source by its table name
        val utilityNetworkSource = networkDefinition.networkSources.value.first {
            it.name == DEVICE_TABLE_NAME
        }
        // Get the asset group
        val circuitBreakerGroup = utilityNetworkSource.assetGroups.first {
            it.name == "Circuit Breaker"
        }
        // Get the asset type
        val threePhaseType = circuitBreakerGroup.assetTypes.first {
            it.name == "Three Phase"
        }
        // Create the element for the known globalID
        val startingElement = utilityNetwork.createElementOrNull(
            assetType = threePhaseType,
            globalId = STARTING_LOCATION_GLOBAL_ID
        )?.apply {
            // Find the "Load" terminal
            terminal = threePhaseType.terminalConfiguration?.terminals?.first { it.name == "Load" }
        } ?: return handleError("Error creating utility network element")
        // Convert the utility element to an ArcGIS feature
        val startingFeature = utilityNetwork.getFeaturesForElements(
            elements = listOf(startingElement)
        ).getOrNull()?.firstOrNull()
        // Add a graphic to indicate the location on the map
        startingLocationGraphic.geometry = startingFeature?.geometry
        // Get the utility domain network
        val domainNetwork = networkDefinition.getDomainNetwork("ElectricDistribution") ?: return
        // Get the utility tier object
        val mediumVoltageRadial = domainNetwork.getTier("Medium Voltage Radial") ?: return
        // Create trace parameters for a downstream trace from the element
        traceParameters = UtilityTraceParameters(
            traceType = UtilityTraceType.Downstream,
            startingLocations = listOf(startingElement)
        ).apply {
            // Use the same trace config from the utility tier
            traceConfiguration = mediumVoltageRadial.getDefaultTraceConfiguration()
        }
    }

    /**
     * Enable or disable the main action buttons based on the network definition's capabilities.
     */
    private fun setButtonStatesFromCapabilities() {
        utilityNetwork.definition?.capabilities?.apply {
            _canGetState.value = supportsNetworkState
            _canTrace.value = supportsTrace
            _canValidateNetworkTopology.value = supportsValidateNetworkTopology
        }
    }

    /**
     * Adds labels for a given field name to a layer with a given name.
     */
    private fun addLabels(layerName: String, fieldName: String, textColor: Color) {
        // Create a expression for the label using the given field name
        val expression = SimpleLabelExpression(simpleExpression = "[$fieldName]")
        // Create a symbol for label's text using the given color
        val symbol = TextSymbol().apply {
            color = textColor
            size = 12f
            haloColor = Color.white
            haloWidth = 2f
        }
        // Create the definition from the expression and text symbol
        val definition = LabelDefinition(labelExpression = expression, textSymbol = symbol)
        // Add the definition to the map layer with the given layer name.
        val layer = arcGISMap.operationalLayers.first { it.name == layerName } as FeatureLayer
        layer.labelDefinitions.add(definition)
        layer.labelsEnabled = true
    }

    /**
     * Gets the current state of the utility network (hasDirtyAreas, hasErrors, isNetworkTopologyEnabled).
     */
    fun getState() {
        _statusMessage.value = "Getting utility network state…"
        viewModelScope.launch {
            val networkState = utilityNetwork.getState().getOrElse {
                return@launch handleError("Get state failed", it.message.toString())
            }
            // Update to true if there are unsaved changes or errors in the utility network state
            _canValidateNetworkTopology.value = networkState.hasDirtyAreas || networkState.hasErrors
            // Update trace availability
            _canTrace.value = networkState.isNetworkTopologyEnabled
            // Update contextual hint
            val tip = if (_canValidateNetworkTopology.value) {
                "Tap Validate before trace or expect a trace error."
            } else {
                "Tap on a feature to edit, or tap Trace."
            }
            _statusMessage.value = buildString {
                appendLine("Utility Network State:")
                appendLine("Has dirty areas: ${networkState.hasDirtyAreas}")
                appendLine("Has errors: ${networkState.hasErrors}")
                appendLine("Network topology enabled: ${networkState.isNetworkTopologyEnabled}")
                append(tip)
            }
        }
    }

    /**
     * Validates the utility network topology in the visible map extent to check
     * for check dirty areas and identify errors in the network topology.
     */
    fun validateNetworkTopology() {
        _statusMessage.value = "Validating utility network topology…"
        // Create and start the utility network validation job using the current visible extent
        val utilityNetworkValidationJob = utilityNetwork.validateNetworkTopology(
            extent = _currentVisibleArea.value,
            executionType = GeoprocessingExecutionType.SynchronousExecute
        ).also { job -> job.start() }
        viewModelScope.launch {
            // Retrieve the result from the job
            val validationResult = utilityNetworkValidationJob.result().getOrElse {
                return@launch handleError("Validation job error: ${it.message}")
            }
            // After validation, check if dirty areas remain.
            _canValidateNetworkTopology.value = validationResult.hasDirtyAreas
            _statusMessage.value = buildString {
                appendLine("Network Validation Result")
                appendLine("Has dirty areas: ${validationResult.hasDirtyAreas}")
                appendLine("Has errors: ${validationResult.hasErrors}")
                append("Tap 'Get State' to check the updated network state.")
            }
        }
    }

    /**
     * Identify a single feature from the [screenCoordinate], check if belongs to the
     * device or line layer, get the coded-value domain from it's field to allow editing.
     */
    fun identifyFeatureAt(
        screenCoordinate: ScreenCoordinate,
        selectedFieldAlias: (String) -> Unit
    ) {
        viewModelScope.launch {
            // Perform an identify operation at the given screen coords
            val identifyLayerResults = mapViewProxy.identifyLayers(
                screenCoordinate = screenCoordinate,
                tolerance = IDENTIFY_TOLERANCE.dp,
                returnPopupsOnly = false,
                maximumResults = 1
            ).getOrElse { return@launch handleError("Select feature failed: ${it.message}") }
            // Find the first result from identify results for device/line layers
            val identifyLayerResult = identifyLayerResults.firstOrNull { layerResult ->
                val layerName = layerResult.layerContent.name
                layerName == DEVICE_TABLE_NAME || layerName == LINE_TABLE_NAME
            }
            // Find the first feature from results
            val foundFeature = identifyLayerResult?.geoElements?.firstOrNull() as? ArcGISFeature
            // Return if no feature was identified
            if (foundFeature == null) {
                _statusMessage.value = "No feature identified. Tap in the device or line layer."
                return@launch
            }
            // Find the coded-value domain field to edit
            val (fieldName, fieldAlias) = when (foundFeature.featureTable?.tableName) {
                DEVICE_TABLE_NAME -> DEVICE_STATUS_FIELD to "Device status"
                LINE_TABLE_NAME -> NOMINAL_VOLTAGE_FIELD to "Nominal voltage"
                else -> null to null
            }
            if (fieldName == null) {
                _statusMessage.value = "Selected feature is not editable."
                return@launch
            }
            // Retrieve the field from the feature table
            val tableField = foundFeature.featureTable?.fields?.firstOrNull {
                it.name == fieldName
            } ?: return@launch handleError("Field '$fieldName' not found in feature table.")
            // Obtain the list of coded value fields in the selected feature
            val codedValues = (tableField.domain as? CodedValueDomain)?.codedValues ?: emptyList()
            // Update the currently selected field that the user is editing
            selectedField = tableField
            // Update the list of field value options of the selected feature
            _fieldValueOptions.value = codedValues
            // Retrieve the currently selected attribute
            _selectedFieldValue.value = codedValues.find { codedValue ->
                codedValue.code == foundFeature.attributes[fieldName]
            }
            // Clear any previous selections
            clearLayerSelections()
            // Select the identified feature on its layer
            (foundFeature.featureTable?.layer as? FeatureLayer)?.selectFeature(foundFeature)
            // Set the viewpoint to the selected feature
            foundFeature.geometry?.let { mapViewProxy.setViewpointGeometry(it, 20.0) }
            // Update the currently selected feature that the user is editing
            _selectedFeature.value = foundFeature
            // Update UI with selected feature info
            _statusMessage.value = "Select a new '$fieldAlias' value, then tap Apply."
            _canClearSelection.value = true
            selectedFieldAlias(fieldAlias.toString())
        }
    }

    /**
     * Run a simple downstream trace from the previously configured trace parameters.
     * Then select any features found by the trace.
     */
    fun trace() {
        _statusMessage.value = "Running a downstream trace…"
        viewModelScope.launch {
            // Clear previous selections
            clearLayerSelections()
            // Get the trace params
            val params = traceParameters ?: return@launch handleError("Trace parameters not set.")
            // Run trace, and obtain results, if errors/dirty areas are found the trace will fail
            val traceResults = utilityNetwork.trace(params).getOrElse {
                return@launch handleError("Trace failed", it.message + "\n" + it.cause)
            }
            // Get the first trace element result
            val elementTraceResult =
                traceResults.firstOrNull { it is UtilityElementTraceResult } as? UtilityElementTraceResult
                    ?: return@launch
            // Group elements by which layer/table they belong to and select them.
            selectTraceResultElements(elementTraceResult.elements)
            // Update contextual hint
            _statusMessage.value = "Trace completed:" +
                    "${elementTraceResult.elements.size} elements found."
        }
    }

    /**
     * Select all features that match the given list of utility elements found by the trace.
     */
    private suspend fun selectTraceResultElements(elements: List<UtilityElement>) {
        // Ensure the result is not empty
        if (elements.isEmpty()) return handleError("No elements found in the trace result")
        // Find the matching feature's geometry of each utility element in all layers
        arcGISMap.operationalLayers.filterIsInstance<FeatureLayer>()
            .forEach { featureLayer ->
                // Clear previous selection
                featureLayer.clearSelection()
                // Used to calculate the viewpoint result
                val params = QueryParameters().apply { returnGeometry = true }
                // Create query parameters to find features who's network source name matches the layer's feature table name
                elements.filter { it.networkSource.name == featureLayer.featureTable?.tableName }
                    .forEach { utilityElement -> params.objectIds.add(utilityElement.objectId) }
                // Check if any trace results were added from the above filter
                if (params.objectIds.isNotEmpty()) {
                    // Select features that match the query
                    val featureQueryResult = featureLayer.selectFeatures(
                        parameters = params,
                        mode = SelectionMode.New
                    ).getOrElse {
                        return handleError(
                            title = it.message.toString(),
                            description = it.cause.toString()
                        )
                    }
                    // Create list of all the feature result geometries
                    val resultGeometryList = mutableListOf<Geometry>()
                    featureQueryResult.iterator().forEach { feature ->
                        feature.geometry?.let {
                            resultGeometryList.add(it)
                        }
                    }
                    // Obtain the union geometry of all the feature geometries
                    GeometryEngine.unionOrNull(resultGeometryList)?.let { unionGeometry ->
                        // Set the map's viewpoint to the union result geometry
                        mapViewProxy.setViewpointAnimated(Viewpoint(boundingGeometry = unionGeometry))
                    }
                }
            }
        _canClearSelection.value = true
    }

    /**
     * Update the feature with the new coded value and apply the edit to the feature service.
     */
    fun applyEdits() {
        _statusMessage.value = "Applying edits…"
        // Get the selected feature to update
        val feature = _selectedFeature.value ?: return
        // Get the table which related to the feature
        val serviceFeatureTable = feature.featureTable as? ServiceFeatureTable
            ?: return handleError("Feature is not from a service feature table.")
        // Get the selected field value coded value
        val newCode = _selectedFieldValue.value?.code
            ?: return handleError("No new coded value selected.")
        // Update the feature's attribute
        val fieldName = selectedField?.name ?: return
        feature.attributes[fieldName] = newCode
        // Apply edits and handle results
        viewModelScope.launch {
            // Update the backing service feature table
            serviceFeatureTable.updateFeature(feature).onFailure {
                return@launch handleError("Failed to update feature", it.message + "\n" + it.cause)
            }
            // Apply all local edits in all tables to the service geodatabase
            val editResults = serviceGeodatabase?.applyEdits()?.getOrElse {
                return@launch handleError("Apply edits failed: ${it.message}")
            }
            // Check for any feature edit errors
            val hadErrors = editResults?.any { tableEditResult ->
                tableEditResult.editResults.any { featureEditResult ->
                    featureEditResult.completedWithErrors
                }
            } ?: true
            if (hadErrors) _statusMessage.value = "Apply edits completed with error(s)."
            else _statusMessage.value = "Edits applied successfully.\n" +
                    "Tap 'Get State' to check the updated network state."
            // Once edits are applied, enable to validate the network again.
            _canValidateNetworkTopology.value = true
        }
    }

    /**
     * Clears any selected features being edited on the map.
     */
    fun clearFeatureEditSelection() {
        clearLayerSelections()
        _selectedFeature.value = null
        selectedField = null
        _fieldValueOptions.value = emptyList()
        _selectedFieldValue.value = null
        _canClearSelection.value = false
        _statusMessage.value = INIT_STATUS_MESSAGE
    }

    /**
     * Clear the selection on all feature layers in the map.
     */
    private fun clearLayerSelections() {
        arcGISMap.operationalLayers.filterIsInstance<FeatureLayer>().forEach { layer ->
            layer.clearSelection()
        }
    }

    /**
     * Update the currently selected field value
     */
    fun updateSelectedValue(codedValue: CodedValue) {
        _selectedFieldValue.value = codedValue
    }

    /**
     * Update the currently visible extent
     */
    fun updateVisibleArea(polygon: Polygon) {
        _currentVisibleArea.value = polygon.extent
    }

    // Message dialog for errors
    val messageDialogVM = MessageDialogViewModel()

    /**
     * Display error and reset
     */
    private fun handleError(title: String, description: String = "") {
        reset()
        messageDialogVM.showMessageDialog(title, description)
    }

    /**
     * Resets the trace, removing graphics and clearing selections.
     */
    private fun reset() {
        arcGISMap.operationalLayers
            .filterIsInstance<FeatureLayer>()
            .forEach { it.clearSelection() }
        graphicsOverlay.graphics.clear()
        _canTrace.value = false
    }

    companion object {
        // Public credentials for the data in this sample (Editor user)
        const val USERNAME = "editor01"
        const val PASSWORD = "S7#i2LWmYH75"

        // The Naperville electric web map on sample server 7
        private const val NAPERVILLE_ELECTRIC_WEBMAP_ITEM_ID = "6e3fc6db3d0b4e6589eb4097eb3e5b9b"
        private const val SAMPLE_SERVER_7_PORTAL = "https://sampleserver7.arcgisonline.com/portal/"

        // The relevant table names/fields in Naperville electric
        private const val DEVICE_TABLE_NAME = "Electric Distribution Device"
        private const val DEVICE_STATUS_FIELD = "devicestatus"
        private const val LINE_TABLE_NAME = "Electric Distribution Line"
        private const val NOMINAL_VOLTAGE_FIELD = "nominalvoltage"

        // The known device's global ID used for the default starting location
        private val STARTING_LOCATION_GLOBAL_ID = Guid("1CAF7740-0BF4-4113-8DB2-654E18800028")

        // Tolerance in device-independent pixels for identify
        private const val IDENTIFY_TOLERANCE = 10f

        private const val INIT_STATUS_MESSAGE = "Utility network loaded.\n" +
                "Tap on a feature to edit.\n" +
                "Tap on 'Get State' to check if validating is necessary or if tracing is available.\n" +
                "Tap 'Trace' to run a trace."
    }
}

/**
 * Returns a [ArcGISAuthenticationChallengeHandler] to access the utility network URL.
 */
private fun getAuthChallengeHandler(): ArcGISAuthenticationChallengeHandler {
    return ArcGISAuthenticationChallengeHandler { challenge ->
        val result: Result<TokenCredential> = runBlocking {
            TokenCredential.create(challenge.requestUrl, USERNAME, PASSWORD, 0)
        }
        if (result.getOrNull() != null) {
            val credential = result.getOrNull()
            return@ArcGISAuthenticationChallengeHandler ArcGISAuthenticationChallengeResponse
                .ContinueWithCredential(credential!!)
        } else {
            val ex = result.exceptionOrNull()
            return@ArcGISAuthenticationChallengeHandler ArcGISAuthenticationChallengeResponse
                .ContinueAndFailWithError(ex!!)
        }
    }
}

private val Color.Companion.blue: Color
    get() {
        return fromRgba(0, 0, 255, 255)
    }

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