View on GitHub Sample viewer app
Create graphics for utility associations in a utility network.
Use case
Visualizing utility associations can help you to better understand trace results and the topology of your utility network. For example, connectivity associations allow you to model connectivity between two junctions that don't have geometric coincidence (are not in the same location); structural attachment associations allow you to model equipment that may be attached to structures; and containment associations allow you to model features contained within other features.
How to use the samplePan and zoom around the map. Observe graphics that show utility associations between junctions.
How it worksMap
with a web map item URL that contains a UtilityNetwork
.UtilityNetwork
from the web map.GraphicsOverlay
for the utility associations.ViewpointChanged
signal of the MapView
.currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry.extent
.associationsAsync(extent)
.UtilityAssociationType
for each association.Graphic
using the Geometry
property of the association and a preferred symbol.The Naperville electrical web map contains a utility network used to run the subnetwork-based trace 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:
associating, association, attachment, connectivity, containment, relationships
Sample CodeDisplayUtilityAssociations.cpp DisplayUtilityAssociations.cpp DisplayUtilityAssociations.h SymbolImageProvider.cpp SymbolImageProvider.h DisplayUtilityAssociations.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
// [WriteFile Name=DisplayUtilityAssociations, Category=UtilityNetwork]
// [Legal]
// Copyright 2020 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 "DisplayUtilityAssociations.h"
#include "SymbolImageProvider.h"
// ArcGIS Maps SDK headers
#include "ArcGISRuntimeEnvironment.h"
#include "Authentication/AuthenticationManager.h"
#include "Authentication/ArcGISAuthenticationChallenge.h"
#include "Authentication/TokenCredential.h"
#include "ArcGISFeatureTable.h"
#include "AttributeListModel.h"
#include "Envelope.h"
#include "ErrorException.h"
#include "FeatureLayer.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "SimpleLineSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "UniqueValue.h"
#include "UniqueValueRenderer.h"
#include "UtilityAssociation.h"
#include "UtilityNetwork.h"
#include "UtilityNetworkDefinition.h"
#include "UtilityNetworkListModel.h"
#include "UtilityNetworkTypes.h"
#include "Viewpoint.h"
// Qt headers
#include <QFuture>
#include <QImage>
#include <QList>
#include <QQmlContext>
#include <QUuid>
using namespace Esri::ArcGISRuntime;
using namespace Esri::ArcGISRuntime::Authentication;
namespace
{
const int maxScale = 2000;
constexpr int targetScale = 50;
}
DisplayUtilityAssociations::DisplayUtilityAssociations(QObject* parent /* = nullptr */):
ArcGISAuthenticationChallengeHandler(parent),
m_map(new Map(QUrl("https://sampleserver7.arcgisonline.com/portal/home/item.html?id=be0e4637620a453584118107931f718b"), this)),
m_associationsOverlay(new GraphicsOverlay(this)),
m_attachmentSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Dot, Qt::green, 5, this)),
m_connectivitySymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Dot, Qt::red, 5, this))
{
ArcGISRuntimeEnvironment::authenticationManager()->setArcGISAuthenticationChallengeHandler(this);
connect(m_map, &Map::doneLoading, this, [this](const Error& error)
{
if (!error.isEmpty() || m_map->utilityNetworks()->isEmpty())
{
return;
}
m_utilityNetwork = m_map->utilityNetworks()->first();
m_utilityNetwork->load();
connectSignals();
});
}
void DisplayUtilityAssociations::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());
});
}
void DisplayUtilityAssociations::addAssociations()
{
// check if current viewpoint is outside the max scale
if(m_mapView->currentViewpoint(ViewpointType::CenterAndScale).targetScale() >= maxScale)
return;
// check if current viewpoint has a valid extent
const Envelope extent = m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry().extent();
if (!extent.isValid())
{
qDebug("Extent not valid");
return;
}
// get all the associations in the extent of the viewpoint
m_utilityNetwork->associationsAsync(extent).then(this, [this](const QList<UtilityAssociation*>& associations)
{
const GraphicListModel* graphics = m_associationsOverlay->graphics();
for (UtilityAssociation* association : associations)
{
// check if the graphics overlay already contains the association
const bool uniqueGraphic = std::none_of(graphics->begin(), graphics->end(), [association](const Graphic* graphic)
{
const AttributeListModel* attributes = graphic->attributes();
return attributes->containsAttribute("GlobalId") && qvariant_cast<QUuid>((*graphic->attributes())["GlobalId"]) == association->globalId();
});
if (uniqueGraphic)
{
// add a graphic for the association
QVariantMap graphicAttributes;
graphicAttributes["GlobalId"] = association->globalId();
graphicAttributes["AssociationType"] = static_cast<int>(association->associationType());
Graphic* graphic = new Graphic(association->geometry(), graphicAttributes, this);
m_associationsOverlay->graphics()->append(graphic);
}
}
});
}
DisplayUtilityAssociations::~DisplayUtilityAssociations() = default;
void DisplayUtilityAssociations::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<DisplayUtilityAssociations>("Esri.Samples", 1, 0, "DisplayUtilityAssociationsSample");
}
MapQuickView* DisplayUtilityAssociations::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void DisplayUtilityAssociations::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
return;
m_mapView = mapView;
m_mapView->setMap(m_map);
m_mapView->graphicsOverlays()->append(m_associationsOverlay);
// Get the image provider from the QML Engine
QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();
m_symbolImageProvider = new SymbolImageProvider();
engine->addImageProvider(SymbolImageProvider::imageProviderId(), m_symbolImageProvider);
connect(m_mapView, &MapQuickView::navigatingChanged, this, [this]()
{
if (!m_mapView->isNavigating())
addAssociations();
});
emit mapViewChanged();
}
void DisplayUtilityAssociations::connectSignals()
{
connect(m_utilityNetwork, &UtilityNetwork::doneLoading, this, [this](const Error& error)
{
if (!error.isEmpty())
{
qDebug() << error.message() << error.additionalMessage();
return;
}
m_mapView->setViewpointAsync(Viewpoint(Point(-9812698.37297436, 5131928.33743317, SpatialReference::webMercator()), targetScale)).then(this, [this](bool succeeded)
{
if (!succeeded)
return;
addAssociations();
});
// create a renderer for the associations
UniqueValue* attachmentValue = new UniqueValue("Attachment", "",
QVariantList{static_cast<int>(UtilityAssociationType::Attachment)}, m_attachmentSymbol, this);
UniqueValue* connectivityValue = new UniqueValue("Connectivity", "",
QVariantList{static_cast<int>(UtilityAssociationType::Connectivity)}, m_connectivitySymbol, this);
UniqueValueRenderer* uniqueValueRenderer = new UniqueValueRenderer("", nullptr,
QStringList{"AssociationType"}, QList<UniqueValue*>{attachmentValue, connectivityValue}, this);
m_associationsOverlay->setRenderer(uniqueValueRenderer);
// populate the legend
m_attachmentSymbol->createSwatchAsync().then(this, [this](const QImage& image)
{
if (!m_symbolImageProvider)
return;
const QString imageId = QUuid().createUuid().toString(QUuid::WithoutBraces);
// add the image to the provider
m_symbolImageProvider->addImage(imageId, image);
// update the URL with the unique id
m_attachmentSymbolUrl = QString("image://%1/%2").arg(SymbolImageProvider::imageProviderId(), imageId);
// emit the signal to trigger the QML Image to update
emit attachmentSymbolUrlChanged();
});
m_connectivitySymbol->createSwatchAsync().then(this, [this](const QImage& image)
{
if (!m_symbolImageProvider)
return;
const QString imageId = QUuid().createUuid().toString(QUuid::WithoutBraces);
// add the image to the provider
m_symbolImageProvider->addImage(imageId, image);
// update the URL with the unique id
m_connectivitySymbolUrl = QString("image://%1/%2").arg(SymbolImageProvider::imageProviderId(), imageId);
// emit the signal to trigger the QML Image to update
emit connectivitySymbolUrlChanged();
});
});
}
QString DisplayUtilityAssociations::attachmentSymbolUrl() const
{
return m_attachmentSymbolUrl;
}
QString DisplayUtilityAssociations::connectivitySymbolUrl() const
{
return m_connectivitySymbolUrl;
}
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