Depending on what you need to do with the results, you can work with the appropriate return type you configured in the trace parameters. The following types of results may be returned when executing the trace:
-
Elements—Element results provide the utility elements that are found by a trace. Use these results when you need access to individual utility elements, their corresponding features, and their attributes. This is the default trace result type.
-
Geometry—Geometry results contains multipart geometries that represent the union of the geometry for all elements returned. These results are best for displaying the trace result on a map.
-
Functions—A function is a trace configuration that allows you to run calculations on network attributes associated with traced features. A function output is returned for every function defined in the configuration.
utilityTraceResults.forEach { utilityTraceResult in
switch utilityTraceResult {
case let elementTraceResult as UtilityElementTraceResult:
// Process the geometry result.
let elements = elementTraceResult.elements
case let geometryTraceResult as UtilityGeometryTraceResult:
// Process the geometry result.
let nearestNeighbor = geometryTraceResult.nearestNeighborResult
case let functionTraceResult as UtilityFunctionTraceResult:
// Process the function result.
let functionOutputs = functionTraceResult.functionOutputs
default:
break
}
}
Element results
If you need fine-grained access to the results, such as the ability to work with individual utility elements from the trace, you need to obtain the corresponding features for these elements from the utility element results.
You can use the element's network source to match its table against the layer's table by instance or name.
let groupedElements = Dictionary(grouping: elementTraceResult.elements, by: { $0.networkSource.name })
for (networkSourceName, elements) in groupedElements {
// Ensure that the features' layer exists in the map.
guard let layer = map.operationalLayers.first(
where: { ($0 as? FeatureLayer)?.featureTable?.tableName == networkSourceName }) as? FeatureLayer
else { continue }
do {
// Get the features that correspond to the elements.
featuresResults = try await utilityNetwork.features(for: elements)
} catch {
print("Error getting features.")
}
}
Select the features that correspond to the trace result or process as required.
// Select features to highlight result.
layer.selectFeatures(features)
Function results
If function results are included, they will contain a UtilityTraceFunctionOutput
for every UtilityTraceFunction
that was defined in the UtilityTraceConfiguration
. Each UtilityTraceFunctionOutput
contains the original function definition as well as the function result.
for output in functionTraceResult.functionOutputs {
// Get the original function (as defined in the trace configuration).
let function = output.function
// Get the network attribute that was used in the function.
let networkAttribute = function.networkAttribute
// Get the type of function.
let functionType = function.functionType
}
Geometry results
Geometry results make it easy to display the trace result as graphics in the map view. At most, geometry results will contain three (multipart) geometries: one multipoint, one polyline, and one polygon. Each geometry represents the union of the geometry of the results of that spatial type. The UtilityGeometryTraceResult
exposes the geometry result for each potential geometry type. If the result does not include a certain geometry type, the corresponding property will be null.
Get the geometry results from the trace results. Depending how the trace parameters were defined (i.e. which result types were requested), there may be more than one result type. Create a new graphic for each geometry in the geometry results, check if there's any need to reproject to the map's spatial reference, use a symbology appropriate for the geometry type, and add them to a graphics overlay in the map view.
// If there are no features of a particular geometry type, it will be nil.
if let polygonResult = geometryTraceResult.polygon {
resultsGraphicsOverlay.addGraphic(
Graphic(geometry: polygonResult, symbol: simpleFillSymbol)
)
}
if let polylineResult = geometryTraceResult.polyline {
resultsGraphicsOverlay.addGraphic(
Graphic(geometry: polylineResult, symbol: simpleLineSymbol)
)
}
if let multipointResult = geometryTraceResult.multipoint {
resultsGraphicsOverlay.addGraphic(
Graphic(geometry: multipointResult, symbol: simpleMarkerSymbol)
)
}