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
// [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
#include "TraceUtilityNetwork.h"
#include "FeatureLayer.h"
#include "GeometryEngine.h"
#include "Map.h"
#include "MapQuickView.h"
#include "ServiceFeatureTable.h"
#include "ServiceGeodatabase.h"
#include "SimpleMarkerSymbol.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 "MapTypes.h"
#include "SymbolTypes.h"
#include "TaskWatcher.h"
#include "Error.h"
#include "GraphicsOverlayListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicListModel.h"
#include "Graphic.h"
#include "AttributeListModel.h"
#include "LayerListModel.h"
#include "Credential.h"
#include "UniqueValueListModel.h"
#include "IdentifyLayerResult.h"
#include "ArcGISFeature.h"
#include "QueryParameters.h"
#include "UniqueValue.h"
#include "FeatureQueryResult.h"
#include "SimpleLineSymbol.h"
#include "SpatialReference.h"
#include "Envelope.h"
#include "Viewpoint.h"
#include "Polyline.h"
using namespace Esri::ArcGISRuntime;
TraceUtilityNetwork::TraceUtilityNetwork(QObject* parent /* = nullptr */):
QObject(parent),
m_map(new Map(BasemapStyle::ArcGISStreetsNight, this)),
m_cred(new Credential("viewer01", "I68VGU^nMurF", 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, m_cred, this)),
m_graphicParent(new QObject())
{
m_map->setInitialViewpoint(Viewpoint(Envelope(-9813547.35557238, 5129980.36635111, -9813185.0602376, 5130215.41254146, SpatialReference::webMercator())));
connect(m_serviceGeodatabase, &ServiceGeodatabase::doneLoading, this, &TraceUtilityNetwork::createFeatureLayers);
m_serviceGeodatabase->load();
connect(m_map, &Map::doneLoading, this, &TraceUtilityNetwork::loadUtilityNetwork);
}
void TraceUtilityNetwork::createFeatureLayers(const Error& error)
{
if (hasErrorOccurred(error))
return;
// Create feature table from the 1st table (index = 0) in the serviceGeodatabase
m_deviceFeatureTable = m_serviceGeodatabase->table(0);
m_deviceLayer = new FeatureLayer(m_deviceFeatureTable, this);
connect(m_deviceLayer, &FeatureLayer::selectFeaturesCompleted, this, [this]()
{
setBusyIndicator(false);
});
// Create feature table from the 4th table (index = 3) in the serviceGeodatabase
m_lineFeatureTable = m_serviceGeodatabase->table(3);
m_lineLayer = new FeatureLayer(m_lineFeatureTable, this);
m_map->operationalLayers()->append(m_lineLayer);
m_map->operationalLayers()->append(m_deviceLayer);
createRenderers();
}
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;
// Create graphics overlay and append to mapview
m_graphicsOverlay = new GraphicsOverlay(this);
m_mapView->graphicsOverlays()->append(m_graphicsOverlay);
m_utilityNetwork = new UtilityNetwork(m_serviceUrl, m_map, m_cred, this);
connect(m_utilityNetwork, &UtilityNetwork::errorOccurred, this, &TraceUtilityNetwork::hasErrorOccurred);
connect(m_utilityNetwork, &UtilityNetwork::traceCompleted, this, &TraceUtilityNetwork::onTraceCompleted);
connect(m_utilityNetwork, &UtilityNetwork::doneLoading, this, &TraceUtilityNetwork::addUtilityNetworkToMap);
m_utilityNetwork->load();
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::addUtilityNetworkToMap(const Error& error)
{
setBusyIndicator(false);
if (hasErrorOccurred(error))
return;
m_map->utilityNetworks()->append(m_utilityNetwork);
connectSignals();
}
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.pos().x(), mouseEvent.pos().y());
m_mapView->identifyLayers(mouseEvent.pos().x(), mouseEvent.pos().y(), tolerance, returnPopups);
});
// handle the identify results
connect(m_mapView, &MapQuickView::identifyLayersCompleted, this, &TraceUtilityNetwork::onIdentifyLayersCompleted);
}
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_utilityNetwork->trace(m_traceParams);
}
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(QUuid, 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*>(qAsConst(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 deviceParams;
QueryParameters lineParams;
QList<qint64> deviceObjIds;
QList<qint64> lineObjIds;
for (UtilityElement* item : elements)
{
if (item->networkSource()->name() == "Electric Distribution Device")
deviceObjIds.append(item->objectId());
else if (item->networkSource()->name() == "Electric Distribution Line")
lineObjIds.append(item->objectId());
}
deviceParams.setObjectIds(deviceObjIds);
lineParams.setObjectIds(lineObjIds);
m_deviceLayer->selectFeatures(deviceParams, SelectionMode::Add);
m_lineLayer->selectFeatures(lineParams, SelectionMode::Add);
}
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;
}