Display utility associations

View inC++QMLView on GitHubSample viewer app

Create graphics for utility associations in a utility network.

screenshot

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 sample

Pan and zoom around the map. Observe graphics that show utility associations between junctions.

How it works

  1. Create a UtilityNetwork with a feature service URL, add it to the map's utilityNetworks list, then load it.
  2. Add a FeatureLayer to the map for every UtilityNetworkSource of type Edge or Junction.
  3. Create a GraphicsOverlay for the utility associations.
  4. Add connection for the ViewpointChanged signal of the MapView.
  5. When the sample starts and every time the viewpoint changes, do the following steps.
  6. Get the geometry of the mapview's extent using currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry.extent.
  7. Get the associations that are within the current extent using associationsAsync(extent).
  8. Get the UtilityAssociationType for each association.
  9. Create a Graphic using the Geometry property of the association and a preferred symbol.
  10. Add the graphic to the graphics overlay.

Relevant API

  • GraphicsOverlay
  • ServiceGeodatabase
  • UtilityAssociation
  • UtilityAssociationType
  • UtilityNetwork

About the data

The Naperville electrical network feature service, hosted on ArcGIS Online (authentication required: this is handled within the sample code), contains a utility network used to run the subnetwork-based trace in this sample.

Additional information

Using 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:

  • Username: viewer01
  • Password: I68VGU^nMurF

Tags

associating, association, attachment, connectivity, containment, relationships

Sample Code

DisplayUtilityAssociations.cppDisplayUtilityAssociations.cppDisplayUtilityAssociations.hSymbolImageProvider.cppSymbolImageProvider.hDisplayUtilityAssociations.qml
Use dark colors for code blocksCopy
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
// [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

#include "DisplayUtilityAssociations.h"
#include "SymbolImageProvider.h"

#include "ArcGISFeatureTable.h"
#include "AttributeListModel.h"
#include "FeatureLayer.h"
#include "GraphicsOverlay.h"
#include "Graphic.h"
#include "Map.h"
#include "MapQuickView.h"
#include "SimpleLineSymbol.h"
#include "UniqueValueRenderer.h"
#include "UtilityAssociation.h"
#include "UtilityNetwork.h"
#include "UtilityNetworkDefinition.h"
#include "UtilityNetworkListModel.h"
#include "UtilityNetworkSource.h"
#include "UtilityNetworkTypes.h"
#include "MapTypes.h"
#include "SymbolTypes.h"
#include "Error.h"
#include "GraphicsOverlayListModel.h"
#include "GraphicListModel.h"
#include "AttributeListModel.h"
#include "LayerListModel.h"
#include "Credential.h"
#include "UniqueValue.h"
#include "Viewpoint.h"
#include "Envelope.h"
#include "SpatialReference.h"
#include "Point.h"

#include <QFuture>
#include <QList>
#include <QImage>
#include <QQmlContext>
#include <QUuid>

using namespace Esri::ArcGISRuntime;

namespace
{
const QString featureServerUrl("https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer");
const int maxScale = 2000;
constexpr int targetScale = 50;
}

DisplayUtilityAssociations::DisplayUtilityAssociations(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISTopographic, this)),
  m_cred(new Credential("viewer01", "I68VGU^nMurF", this)),
  m_associationsOverlay(new GraphicsOverlay(this)),
  m_utilityNetwork(new UtilityNetwork(featureServerUrl, m_cred, this)),
  m_attachmentSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Dot, Qt::green, 5, this)),
  m_connectivitySymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Dot, Qt::red, 5, this))
{
  m_map->utilityNetworks()->append(m_utilityNetwork);

  connectSignals();
}

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();
  });

  m_utilityNetwork->load();

  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();
    });

    // get all the edges and junctions in the network
    QList<UtilityNetworkSource*> edges;
    QList<UtilityNetworkSource*> junctions;
    const QList<UtilityNetworkSource*> allSources = m_utilityNetwork->definition()->networkSources();

    for (UtilityNetworkSource* networkSource : allSources)
    {
      if (networkSource->sourceType() == UtilityNetworkSourceType::Edge)
        edges.append(networkSource);
      else if (networkSource->sourceType() == UtilityNetworkSourceType::Junction)
        junctions.append(networkSource);
    }

    // add all edges that are not subnet lines to the map
    for (UtilityNetworkSource* source : edges)
    {
      if (source->sourceUsageType() != UtilityNetworkSourceUsageType::SubnetLine && source->featureTable() != nullptr)
        m_map->operationalLayers()->append(new FeatureLayer(source->featureTable(), this));
    }

    // add all junctions to the map
    for (UtilityNetworkSource* source : junctions)
    {
      if (source->featureTable())
        m_map->operationalLayers()->append(new FeatureLayer(source->featureTable(), this));
    }

    // 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;
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.