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 sample
Pan and zoom around the map. Observe graphics that show utility associations between junctions.
How it works
- Create a
UtilityNetwork
with a feature service URL, add it to the map'sutilityNetworks
list, then load it. - Add a
FeatureLayer
to the map for everyUtilityNetworkSource
of typeEdge
orJunction
. - Create a
GraphicsOverlay
for the utility associations. - Add connection for the
ViewpointChanged
signal of theMapView
. - When the sample starts and every time the viewpoint changes, do the following steps.
- Get the geometry of the mapview's extent using
currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry.extent
. - Get the associations that are within the current extent using
associationsAsync(extent)
. - Get the
UtilityAssociationType
for each association. - Create a
Graphic
using theGeometry
property of the association and a preferred symbol. - 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
// [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 "ArcGISFeatureTable.h"
#include "AttributeListModel.h"
#include "Credential.h"
#include "Envelope.h"
#include "Error.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 "UtilityNetworkSource.h"
#include "UtilityNetworkTypes.h"
#include "Viewpoint.h"
// Qt headers
#include <QFuture>
#include <QImage>
#include <QList>
#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;
}