View on GitHub Sample viewer app
Discover connected features in a utility network using connected, subnetwork, upstream, and downstream traces.
Use case
You can use a trace to visualize and validate the network topology of a utility network for quality assurance. Subnetwork traces are used for validating whether subnetworks, such as circuits or zones, are defined or edited appropriately.
How to use the sampleTap on one or more features while 'Add starting locations' or 'Add barriers' is selected. When a junction feature is identified, you may be prompted to select a terminal. When an edge feature is identified, the distance from the tapped location to the beginning of the edge feature will be computed. Select the type of trace using the drop down menu. Click 'Trace' to initiate a trace on the network. Click 'Reset' to clear the trace parameters and start over.
How it worksMapView
and connect to its mouseClicked
signal.Map
with a web map item URL that contains a UtilityNetwork
.UtilityNetwork
from the web map.ServiceGeodatabase
from the utility network and fetch the line FeatureLayer
from the ServiceGeodatabase
's tables.GraphicsOverlay
with symbology that distinguishes starting locations from barriers.Graphic
that represents its purpose (starting point or barrier) at the location of each identified feature.UtilityElement
for the identified feature.NetworkSource::SourceType
property.terminal
property with the selected terminal.fractionAlongEdge
property using GeometryEngine::fractionAlong
.UtilityElement
to a collection of starting locations or barriers.UtilityTraceParameters
with the selected trace type along with the collected starting locations and barriers (if applicable).UtilityTraceParameters::traceConfiguration
with the utility tier's traceConfiguration
property.UtilityNetwork::traceAsync
with the specified parameters.FeatureLayer
in the map, select the features using the UtilityElement::objectId
from the filtered list of UtilityElementTraceResult::elements
.The Naperville Electric Web Map, hosted on ArcGIS Online (authentication required: this is handled within the sample code), contains a utility network used to run the subnetwork-based trace shown in this sample.
Additional informationUsing 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.
Credentials:
condition barriers, downstream trace, network analysis, subnetwork trace, trace configuration, traversability, upstream trace, utility network, validate consistency
Sample CodeTraceUtilityNetwork.cpp TraceUtilityNetwork.cpp TraceUtilityNetwork.h TraceUtilityNetwork.qml
Use dark colors for code blocks Copy
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
// [WriteFile Name=TraceUtilityNetwork, Category=UtilityNetwork]
// [Legal]
// Copyright 2019 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.
// [Legal]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
// sample headers
#include "TraceUtilityNetwork.h"
// ArcGIS Maps SDK headers
#include "ArcGISRuntimeEnvironment.h"
#include "ArcGISFeature.h"
#include "AttributeListModel.h"
#include "Authentication/AuthenticationManager.h"
#include "Authentication/ArcGISAuthenticationChallenge.h"
#include "Authentication/TokenCredential.h"
#include "Envelope.h"
#include "Error.h"
#include "ErrorException.h"
#include "FeatureLayer.h"
#include "FeatureQueryResult.h"
#include "GeometryEngine.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "IdentifyLayerResult.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Polyline.h"
#include "QueryParameters.h"
#include "ServiceFeatureTable.h"
#include "ServiceGeodatabase.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "UniqueValue.h"
#include "UniqueValueListModel.h"
#include "UniqueValueRenderer.h"
#include "UtilityAssetGroup.h"
#include "UtilityAssetType.h"
#include "UtilityDomainNetwork.h"
#include "UtilityElement.h"
#include "UtilityElementTraceResult.h"
#include "UtilityNetwork.h"
#include "UtilityNetworkDefinition.h"
#include "UtilityNetworkListModel.h"
#include "UtilityNetworkSource.h"
#include "UtilityNetworkTypes.h"
#include "UtilityTerminalConfiguration.h"
#include "UtilityTier.h"
#include "UtilityTraceParameters.h"
#include "UtilityTraceResultListModel.h"
#include "Viewpoint.h"
// Qt headers
#include <QFuture>
// Other headers
#include "TaskCanceler.h"
using namespace Esri::ArcGISRuntime;
using namespace Esri::ArcGISRuntime::Authentication;
TraceUtilityNetwork::TraceUtilityNetwork(QObject* parent /* = nullptr */):
ArcGISAuthenticationChallengeHandler(parent),
m_map(new Map(QUrl("https://sampleserver7.arcgisonline.com/portal/home/item.html?id=be0e4637620a453584118107931f718b"), this)),
m_startingSymbol(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Cross, QColor(Qt::green), 20, this)),
m_barrierSymbol(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::X, QColor(Qt::red), 20, this)),
m_mediumVoltageSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(Qt::darkCyan), 3, this)),
m_lowVoltageSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor(Qt::darkCyan), 3, this)),
m_serviceGeodatabase(new ServiceGeodatabase(m_serviceUrl, this)),
m_graphicParent(new QObject()),
m_taskCanceler(std::make_unique<TaskCanceler>())
{
ArcGISRuntimeEnvironment::authenticationManager()->setArcGISAuthenticationChallengeHandler(this);
m_map->setInitialViewpoint(Viewpoint(Envelope(-9813547.35557238, 5129980.36635111, -9813185.0602376, 5130215.41254146, SpatialReference::webMercator())));
connect(m_map, &Map::doneLoading, this, &TraceUtilityNetwork::loadUtilityNetwork);
}
void TraceUtilityNetwork::createFeatureLayers()
{
setBusyIndicator(false);
// Get the feature table from the 4th table (index = 3) in the serviceGeodatabase
m_serviceGeodatabase = m_utilityNetwork->serviceGeodatabase();
m_lineFeatureTable = m_serviceGeodatabase->table(3);
m_lineLayer = qobject_cast<FeatureLayer*>(m_lineFeatureTable->layer());
}
void TraceUtilityNetwork::createRenderers()
{
// create unique renderer
m_uniqueValueRenderer = new UniqueValueRenderer(this);
m_uniqueValueRenderer->setFieldNames(QStringList("ASSETGROUP"));
UniqueValue* mediumVoltageUniqueValue = createUniqueValue(QString("Medium Voltage"), m_mediumVoltageSymbol, 5);
UniqueValue* lowVoltageUniqueValue = createUniqueValue(QString("Low Voltage"), m_lowVoltageSymbol, 3);
// append to UniqueValueRenderer
m_uniqueValueRenderer->uniqueValues()->append(mediumVoltageUniqueValue);
m_uniqueValueRenderer->uniqueValues()->append(lowVoltageUniqueValue);
// set unique value renderer to the line layer
m_lineLayer->setRenderer(m_uniqueValueRenderer);
}
void TraceUtilityNetwork::loadUtilityNetwork(const Error& error)
{
if (hasErrorOccurred(error))
return;
m_utilityNetwork = m_map->utilityNetworks()->first();
m_utilityNetwork->load();
// Create graphics overlay and append to mapview
m_graphicsOverlay = new GraphicsOverlay(this);
m_mapView->graphicsOverlays()->append(m_graphicsOverlay);
connect(m_utilityNetwork, &UtilityNetwork::errorOccurred, this, &TraceUtilityNetwork::hasErrorOccurred);
connect(m_utilityNetwork, &UtilityNetwork::doneLoading, this, [this](const Error& error)
{
if (hasErrorOccurred(error))
return;
createFeatureLayers();
createRenderers();
connectSignals();
});
setBusyIndicator(true);
}
bool TraceUtilityNetwork::hasErrorOccurred(const Error& error)
{
if (error.isEmpty())
return false;
m_dialogText = QString(error.message() + " - " + error.additionalMessage());
emit dialogVisibleChanged();
return true;
}
void TraceUtilityNetwork::onTaskFailed_(const Esri::ArcGISRuntime::ErrorException& exception)
{
m_dialogText = QString(exception.error().message() + " - " + exception.error().additionalMessage());
emit dialogVisibleChanged();
}
void TraceUtilityNetwork::connectSignals()
{
// identify layers on mouse click
connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& mouseEvent)
{
if (m_map->loadStatus() != LoadStatus::Loaded)
return;
constexpr double tolerance = 10.0;
constexpr bool returnPopups = false;
m_clickPoint = m_mapView->screenToLocation(mouseEvent.position().x(), mouseEvent.position().y());
m_taskCanceler->addTask(m_mapView->identifyLayersAsync(mouseEvent.position(), tolerance, returnPopups).then(this, [this](const QList<IdentifyLayerResult*>& results)
{
onIdentifyLayersCompleted_(results);
}));
});
}
TraceUtilityNetwork::~TraceUtilityNetwork() = default;
void TraceUtilityNetwork::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<TraceUtilityNetwork>("Esri.Samples", 1, 0, "TraceUtilityNetworkSample");
}
MapQuickView* TraceUtilityNetwork::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void TraceUtilityNetwork::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
return;
m_mapView = mapView;
m_mapView->setMap(m_map);
emit mapViewChanged();
}
void TraceUtilityNetwork::multiTerminalIndex(int index)
{
if (m_terminals.isEmpty())
return;
if (!m_feature)
return;
UtilityElement* element = m_utilityNetwork->createElementWithArcGISFeature(m_feature, m_terminals[index]);
updateTraceParams(element);
}
void TraceUtilityNetwork::updateTraceParams(UtilityElement* element)
{
if (m_startingLocationsEnabled)
{
m_startingLocations.append(element);
Graphic* traceLocation = new Graphic(m_clickPoint, m_startingSymbol, m_graphicParent.get());
m_graphicsOverlay->graphics()->append(traceLocation);
}
else
{
m_barriers.append(element);
Graphic* traceLocation = new Graphic(m_clickPoint, m_barrierSymbol, m_graphicParent.get());
m_graphicsOverlay->graphics()->append(traceLocation);
}
}
void TraceUtilityNetwork::trace(int index)
{
setBusyIndicator(true);
delete m_traceParams;
switch (index)
{
case 0:
m_traceParams = new UtilityTraceParameters(UtilityTraceType::Connected, {}, this);
break;
case 1:
m_traceParams = new UtilityTraceParameters(UtilityTraceType::Subnetwork, {}, this);
break;
case 2:
m_traceParams = new UtilityTraceParameters(UtilityTraceType::Upstream, {}, this);
break;
case 3:
m_traceParams = new UtilityTraceParameters(UtilityTraceType::Downstream, {}, this);
break;
default:
return;
}
if (m_mediumVoltageTier)
m_traceParams->setTraceConfiguration(m_mediumVoltageTier->defaultTraceConfiguration());
m_traceParams->setStartingLocations(m_startingLocations);
m_traceParams->setBarriers(m_barriers);
// Perform a connected trace on the utility network
m_taskCanceler->addTask(m_utilityNetwork->traceAsync(m_traceParams).then(this, [this](QList<UtilityTraceResult*>)
{
onTraceCompleted_();
}).onFailed([this](const ErrorException& exception)
{
onTaskFailed_(exception);
}));
}
void TraceUtilityNetwork::reset()
{
m_startingLocations.clear();
m_barriers.clear();
if (m_traceParams)
{
m_traceParams->setStartingLocations(m_startingLocations);
m_traceParams->setBarriers(m_barriers);
}
m_graphicsOverlay->graphics()->clear();
m_graphicParent.reset(new QObject());
for (Layer* layer : *m_map->operationalLayers())
{
FeatureLayer* featureLayer = dynamic_cast<FeatureLayer*>(layer);
if (!featureLayer)
return;
featureLayer->clearSelection();
}
}
void TraceUtilityNetwork::onIdentifyLayersCompleted_(const QList<IdentifyLayerResult*>& results)
{
if (results.isEmpty())
{
m_dialogText = QString("Could not identify location.");
emit dialogTextChanged();
m_dialogVisible = true;
emit dialogVisibleChanged();
return;
}
// Get domain network
const UtilityDomainNetwork* domainNetwork = m_utilityNetwork->definition()->domainNetwork("ElectricDistribution");
m_mediumVoltageTier = domainNetwork->tier("Medium Voltage Radial");
const IdentifyLayerResult* result = results[0];
m_feature = static_cast<ArcGISFeature*>(std::as_const(result)->geoElements()[0]);
UtilityElement* element = nullptr;
const UtilityNetworkSource* networkSource = m_utilityNetwork->definition()->networkSource(m_feature->featureTable()->tableName());
if (networkSource->sourceType() == UtilityNetworkSourceType::Junction)
{
m_junctionSelected = true;
emit junctionSelectedChanged();
const QString assetGroupFieldName = static_cast<ArcGISFeatureTable*>(m_feature->featureTable())->subtypeField();
const int assetGroupCode = m_feature->attributes()->attributeValue(assetGroupFieldName).toInt();
UtilityAssetGroup* assetGroup = nullptr;
const auto groups = networkSource->assetGroups();
for (UtilityAssetGroup* group : groups)
{
if (group->code() == assetGroupCode)
{
assetGroup = group;
break;
}
}
if (!assetGroup)
return;
const int assetTypeCode = m_feature->attributes()->attributeValue("assettype").toInt();
UtilityAssetType* assetType = nullptr;
const auto types = assetGroup->assetTypes();
for (UtilityAssetType* type : types)
{
if (type->code() == assetTypeCode)
{
assetType = type;
break;
}
}
if (!assetType)
return;
m_terminals = assetType->terminalConfiguration()->terminals();
if (m_terminals.size() > 1)
{
m_terminalDialogVisisble = true;
emit terminalDialogVisisbleChanged();
return;
}
else if (m_terminals.size() == 1)
element = m_utilityNetwork->createElementWithArcGISFeature(m_feature, m_terminals[0]);
else
return;
}
else if (networkSource->sourceType() == UtilityNetworkSourceType::Edge)
{
m_junctionSelected = false;
emit junctionSelectedChanged();
element = m_utilityNetwork->createElementWithArcGISFeature(m_feature, nullptr, this);
// Compute how far tapped location is along the edge feature.
if (m_feature->geometry().geometryType() == GeometryType::Polyline)
{
const Polyline line = geometry_cast<Polyline>(GeometryEngine::removeZ(m_feature->geometry()));
// Set how far the element is along the edge.
element->setFractionAlongEdge(GeometryEngine::fractionAlong(line, m_clickPoint, -1));
m_fractionAlongEdge = element->fractionAlongEdge();
emit fractionAlongEdgeChanged();
}
}
else
{
return;
}
updateTraceParams(element);
}
void TraceUtilityNetwork::onTraceCompleted_()
{
m_dialogVisible = true;
emit dialogVisibleChanged();
if (m_utilityNetwork->traceResult()->isEmpty())
{
setBusyIndicator(false);
return;
}
m_dialogText = QString("Trace completed.");
emit dialogTextChanged();
UtilityTraceResult* result = m_utilityNetwork->traceResult()->at(0);
const QList<UtilityElement*> elements = static_cast<UtilityElementTraceResult*>(result)->elements(this);
QueryParameters lineParams;
QList<qint64> lineObjIds;
for (UtilityElement* item : elements)
{
if (item->networkSource()->name() == "Electric Distribution Line")
lineObjIds.append(item->objectId());
}
lineParams.setObjectIds(lineObjIds);
m_taskCanceler->addTask(m_lineLayer->selectFeaturesAsync(lineParams, SelectionMode::Add).then(this, [this](FeatureQueryResult*)
{
setBusyIndicator(false);
}));
}
UniqueValue* TraceUtilityNetwork::createUniqueValue(const QString& label, Esri::ArcGISRuntime::Symbol* fillSymbol, int value)
{
// add state's attribute value for field "STATE_ABBR" to QVariantList
QVariantList labelValue;
labelValue.append(value);
// set value for a State to be rendered. (label, description, attribute value list, symbol, parent)
UniqueValue* uniqueValue = new UniqueValue(label, "", labelValue, fillSymbol, this);
// return Unique value created
return uniqueValue;
}
void TraceUtilityNetwork::setBusyIndicator(bool status)
{
m_busy = status;
emit busyChanged();
return;
}
void TraceUtilityNetwork::handleArcGISAuthenticationChallenge(ArcGISAuthenticationChallenge* challenge)
{
TokenCredential::createWithChallengeAsync(challenge, "viewer01", "I68VGU^nMurF", {}, this).then(this, [challenge](TokenCredential* tokenCredential)
{
challenge->continueWithCredential(tokenCredential);
}).onFailed(this, [challenge](const ErrorException& e)
{
challenge->continueWithError(e.error());
});
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4