Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Display utility associations

Sample Viewer View Sample on GitHub

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

  1. Create and load a UtilityNetwork with a feature service URL.
  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 currentViewpointExtent.extent.
  7. Get the associations that are within the current extent using associationsWithEnvelope(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
  • UtilityAssociation
  • UtilityAssociationType
  • UtilityNetwork

About the data

The feature service in this sample represents an electric network in Naperville, Illinois, which contains a utility network used to run the subnetwork-based trace.

Tags

associating, association, attachment, connectivity, containment, relationships

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Esri.ArcGISRuntime 100.9

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    readonly property url featureServerUrl: "https://sampleserver7.arcgisonline.com/arcgis/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer"
    readonly property int maxScale: 2000
    readonly property int viewpointScale: 40

    MapView {
        id: mapView
        anchors.fill: parent

        Control {
            background: Rectangle {
                border.color: "black"
                border.width: 1
            }

            padding: 5
            anchors {
                top: parent.top
                left: parent.left
                margins: 20
            }

            contentItem: GridLayout {
                id: grid
                columns: 2
                anchors.horizontalCenter: parent.horizontalCenter

                Label {
                    text: "Utility association types"
                    Layout.alignment: Qt.AlignHCenter
                    Layout.fillWidth: true
                    Layout.columnSpan: 2
                }

                Image {
                    id: attachmentImage
                    fillMode: Image.PreserveAspectFit
                }
                Label {
                    id: attachmentLabel
                    text: "Attachment symbol"
                }

                Image {
                    id: connectivityImage
                }
                Label {
                    id: connectivityLabel
                    text: "Connectivity symbol"
                }
            }
        }

        Point {
            id: centerPoint
            x: -9812698.37297436
            y: 5131928.33743317
            SpatialReference {wkid: 3857}
        }

        Map {
            id: map
            BasemapTopographicVector {}

            Component.onCompleted: {
                utilityNetwork.load();
            }

            UtilityNetwork {
                id: utilityNetwork
                url: featureServerUrl

                onLoadStatusChanged: {
                    if (loadStatus !== Enums.LoadStatusLoaded)
                        return;

                    let edges = [];
                    let junctions = [];

                    // get all the edges and junctions in the network
                    for (let i = 0; i < definition.networkSources.length; i++) {
                        if (definition.networkSources[i].sourceType === Enums.UtilityNetworkSourceTypeEdge) {
                            edges.push(definition.networkSources[i]);
                        } else if (definition.networkSources[i].sourceType === Enums.UtilityNetworkSourceTypeJunction) {
                            junctions.push(definition.networkSources[i]);
                        }
                    }

                    // add all edges that are not subnet lines to the map
                    for (let j = 0; j < edges.length; j++) {
                        if (edges[j].sourceUsageType !== Enums.UtilityNetworkSourceUsageTypeSubnetLine && edges[j].featureTable !== null) {
                            map.operationalLayers.append(ArcGISRuntimeEnvironment.createObject("FeatureLayer", {featureTable: edges[j].featureTable}));
                        }
                    }

                    // add all junctions to the map
                    for (let k = 0; k < junctions.length; k++) {
                        if (junctions[k].featureTable !== null) {
                            map.operationalLayers.append(ArcGISRuntimeEnvironment.createObject("FeatureLayer", {featureTable: junctions[k].featureTable}));
                        }
                    }

                    attachmentValue.values = [Enums.UtilityAssociationTypeAttachment];
                    connectivityValue.values = [Enums.UtilityAssociationTypeConnectivity];

                    mapView.setViewpointCenterAndScale(centerPoint, viewpointScale);
                }

                onAssociationsStatusChanged: {
                    if (associationsStatus !== Enums.TaskStatusCompleted)
                        return;

                    let isGraphicUnique = (graphics, id) => {
                        for (let j = 0; j < graphics.count; j++) {
                            const attributes = graphics.get(j).attributes;
                            if (attributes.containsAttribute("GlobalId") && attributes.attributeValue("GlobalId") === id) {
                                return false;
                            }
                        }
                        return true;
                    }

                    for (let i = 0; i < associationsResult.length; i++) {
                        const association = associationsResult[i];

                        if (isGraphicUnique(associationsOverlay.graphics, association.globalId)) {
                            const graphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: association.geometry});
                            graphic.attributes.insertAttribute("GlobalId", association.globalId);
                            graphic.attributes.insertAttribute("AssociationType", association.associationType);
                            associationsOverlay.graphics.append(graphic);
                        }
                    }
                }
            }
        }

        GraphicsOverlay {
            id: associationsOverlay

            // create a renderer for the associations
            UniqueValueRenderer {
                id: uniqueValueRenderer
                fieldNames: ["AssociationType"]

                UniqueValue {
                    id: attachmentValue
                    label: "Attachment"
                    SimpleLineSymbol {
                        id: attachmentSymbol
                        style: Enums.SimpleLineSymbolStyleDot
                        color: "lime"
                        width: 5

                        // create swatch image for the legend
                        Component.onCompleted: {
                            createSwatch();
                        }
                        onSwatchImageChanged: {
                            connectivityImage.source = swatchImage;
                        }
                    }
                }

                UniqueValue {
                    id: connectivityValue
                    label: "Connectivity"
                    SimpleLineSymbol {
                        id: connectivitySymbol
                        style: Enums.SimpleLineSymbolStyleDot
                        color: "red"
                        width: 5

                        // create swatch image for the legend
                        Component.onCompleted: {
                            createSwatch();
                        }
                        onSwatchImageChanged: {
                            attachmentImage.source = swatchImage;
                        }
                    }
                }
            }
        }

        // add associations to the map view
        function addAssociations() {
            // check if outside maximum scale
            if (currentViewpointCenter.targetScale >= maxScale)
                return;

            let currentExtent = currentViewpointExtent.extent;
            if (currentExtent.empty) {
                console.warn("Extent not valid.");
                return;
            }

            utilityNetwork.associationsWithEnvelope(currentExtent);
        }

        onSetViewpointCompleted: {
            if (!succeeded)
                return;

            addAssociations();
        }

        // add new associations when navigation is finished
        onNavigatingChanged: {
            if (navigating)
                return;

            addAssociations();
        }
    }
}