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.
// Get the QList of utility trace results from the utility network via the
// trace async method (QFuture).
utilityNetwork->traceAsync(traceParameters).then(this, [utilityNetwork]
(QFuture<QList<UtilityTraceResult *>>)
{
// Get the utility trace result list model from the utility network.
UtilityTraceResultListModel* traceResults = utilityNetwork->traceResult();
// Test if the utility trace result list model has content.
if (traceResults->isEmpty())
return;
// Get the first utility element trace result from the utility trace result list model.
UtilityElementTraceResult* elementTraceResult = static_cast<UtilityElementTraceResult*>
(traceResults->first());
// Test to ensure we have at least one element in the utility element trace result.
if (elementTraceResult->elements().count() > 0)
{
// Process the results ...
}
});
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.
// Get the QList of utility elements from the list of utility elements (via a cast).
const QList<UtilityElement*> utilityElements = static_cast<UtilityElementTraceResult*>
(elementTraceResult)->elements(this);
// Get the result features from each network source layer in the map.
for (Layer* layer : *m_map->operationalLayers())
{
// Test if we have a feature layer from the operational layer (via a cast).
if (FeatureLayer* featureLayer = dynamic_cast<FeatureLayer*>(layer))
{
// Create a list of elements that belong to this layer.
QList<UtilityElement*> layerElements;
// Loop thru each utility element in the QList of utility elements.
for (UtilityElement* utilityElement : utilityElements)
{
// Select the elements that belong to this layer.
if (utilityElement->networkSource()->name() == featureLayer->
featureTable()->tableName())
{
layerElements.append(utilityElement);
}
}
// Continue if no matching elements are found.
if (layerElements.isEmpty())
continue;
// Get the QList of features from the utility network via the features for elements
// async method (QFuture).
utilityNetwork->featuresForElementsAsync(layerElements).then(this, [featureLayer,
utilityNetwork](QFuture<QList<ArcGISFeature *>>)
{
// Select the features in this layer.
featureLayer->selectFeatures(utilityNetwork->featuresForElementsResult()->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.
// Get the utility trace result list model from the utility network.
UtilityTraceResultListModel* traceResults = utilityNetwork->traceResult();
// Loop thru each utility trace result in the utility trace result list model.
for (UtilityTraceResult* result : *traceResults)
{
// Test if we have a vaild utility function trace result (not null) from the tility trace result.
if (UtilityFunctionTraceResult* functionResult = dynamic_cast<UtilityFunctionTraceResult*>(result))
{
// Loop thru each utility trace function output in the QList of utility trace function outputs,
for (UtilityTraceFunctionOutput* output : functionResult->functionOutputs())
{
// Get the original function (as defined in the trace configuration).
UtilityTraceFunction* function = output->function();
// Get the function type: Add, Average, Count, Min, Max, Subtract.
UtilityTraceFunctionType functionTypeEnum = function->functionType();
const QString functionTypeName = QList{"Add", "Average", "Count", "Max", "Min", "Subtract"}
.at((int)functionTypeEnum);
// Get the network attribute that was used in the function.
UtilityNetworkAttribute* networkAttribute = function->networkAttribute();
// Build a string to show the function description and result.
// For example: "Service Load (Add) = 378100".
const QString message = QString("%1 (%2) = %3").arg(networkAttribute->name(),
functionTypeName, output->result().toString());
// Add each output message to a list.
functionOutputMessages(message);
}
}
}
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.
// Get the utility trace result list model from the utility network.
UtilityTraceResultListModel* traceResults = utilityNetwork->traceResult();
// Get the iterator from the trace results.
auto geometryTraceResultIterator = std::find_if(traceResults->begin(),
traceResults->end(), [](UtilityTraceResult* traceResult)
{
return traceResult->traceResultObjectType() ==
UtilityTraceResultObjectType::UtilityGeometryTraceResult;
});
// Test if the we are at the end of the iterator.
if (geometryTraceResultIterator == traceResults->end())
return;
// Get the utility geometry trace result from the iterator (via a cast).
UtilityGeometryTraceResult* geometryTraceResult = static_cast<UtilityGeometryTraceResult*>
(*geometryTraceResultIterator);
If geometry results are found, create a new graphic for each geometry in the geometry results and add them to a graphics overlay in the map view. If the result does not include a geometry type, it will be null.
// Test of the utility geometry trace result is not null.
if (geometryTraceResult)
{
// Test if the polygon is valid on the the utility geometry trace result.
if (geometryTraceResult->polygon().isValid())
// Add a new polygon based graphic to the graphics overly.
graphicsOverlay->graphics()->append(new Graphic(geometryTraceResult->polygon(), this));
// Test if the polyline is valid on the the utility geometry trace result.
if (!geometryTraceResult->polyline().isValid())
// Add a new polyline based graphic to the graphics overly.
graphicsOverlay->graphics()->append(new Graphic(geometryTraceResult->polyline(), this));
// Test if the multi point is valid on the the utility geometry trace result.
if (!geometryTraceResult->multipoint().isValid())
// Add a new multi point based graphic to the graphics overly.
graphicsOverlay->graphics()->append(new Graphic(geometryTraceResult->multipoint(), this));
}