Spatial relationships

View inC++QMLView on GitHubSample viewer app

Determine spatial relationships between two geometries.

screenshot

Use case

In case of a natural disaster, emergency services can represent the affected areas using polygons. By determining the spatial relationships between these and any other existing features such as populated areas, infrastructure, or natural resources, it is possible to quickly determine which of the existing features might be affected or is in further danger, helping to assess risk and define further action.

How to use the sample

Select one of the three graphics. The tree view will list the relationships the selected graphic has to the other graphic geometries.

How it works

  1. Get the geometry from two different graphics. In this example the geometry of the selected graphic is compared to the geometry of each unselected graphic.
  2. Use the methods in GeometryEngine to check the relationship between the geometries, e.g. contains, disjoint, intersects, etc. If the method returns true, the relationship exists.

Relevant API

  • Geometry
  • GeometryEngine
  • GeometryEngine.contains
  • GeometryEngine.crosses
  • GeometryEngine.disjoint
  • GeometryEngine.intersects
  • GeometryEngine.overlaps
  • GeometryEngine.touches
  • GeometryEngine.within
  • GeometryType
  • Graphic
  • Point
  • Polygon
  • Polyline

Tags

geometries, relationship, spatial analysis

Sample Code

SpatialRelationships.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
// [WriteFile Name=SpatialRelationships, Category=Geometry]
// [Legal]
// Copyright 2018 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]

import QtQuick 2.6
import Esri.ArcGISRuntime 100.15

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

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        SelectionProperties {
            color: "yellow"
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISTopographic
            }
        }

        // Add a GraphicsOverlay
        GraphicsOverlay {
            id: graphicsOverlay

            // Add Polygon Graphic
            Graphic {
                id: polygonGraphic

                SimpleFillSymbol {
                    SimpleLineSymbol {
                        color: "green"
                        width: 2
                    }
                    style: Enums.SimpleFillSymbolStyleForwardDiagonal
                    color: "green"
                }

                Component.onCompleted: {
                    const polyBuilder = ArcGISRuntimeEnvironment.createObject("PolygonBuilder", {spatialReference: Factory.SpatialReference.createWebMercator()});
                    polyBuilder.addPointXY(-5991501.677830, 5599295.131468);
                    polyBuilder.addPointXY(-6928550.398185, 2087936.739807);
                    polyBuilder.addPointXY(-3149463.800709, 1840803.011362);
                    polyBuilder.addPointXY(-1563689.043184, 3714900.452072);
                    polyBuilder.addPointXY(-3180355.516764, 5619889.608838);
                    polygonGraphic.geometry = polyBuilder.geometry;
                }
            }

            // Add Polyline Graphic
            Graphic {
                id: polylineGraphic

                SimpleLineSymbol {
                    style: Enums.SimpleLineSymbolStyleDash
                    color: "red"
                    width: 4
                }

                Component.onCompleted: {
                    const polyBuilder = ArcGISRuntimeEnvironment.createObject("PolylineBuilder", {spatialReference: Factory.SpatialReference.createWebMercator()});
                    polyBuilder.addPointXY(-4354240.726880, -609939.795721);
                    polyBuilder.addPointXY(-3427489.245210, 2139422.933233);
                    polyBuilder.addPointXY(-2109442.693501, 4301843.057130);
                    polyBuilder.addPointXY(-1810822.771630, 7205664.366363);
                    polylineGraphic.geometry = polyBuilder.geometry;
                }
            }

            // Add Point Graphic
            Graphic {
                id: pointGraphic

                SimpleMarkerSymbol {
                    color: "blue"
                    style: Enums.SimpleMarkerSymbolStyleCircle
                    size: 10
                }

                Point {
                    id: pointGeometry
                    x: -4487263.495911
                    y: 3699176.480377

                    SpatialReference {wkid: 3857}

                    Component.onCompleted: mapView.setViewpointCenterAndScale(pointGeometry, 200000000)
                }
            }
        }

        // connect to mouse click signal
        onMouseClicked: {
            mapView.identifyGraphicsOverlay(graphicsOverlay, mouse.x, mouse.y, 1 /*tolerance*/, false /*returnPopupsOnly*/);
        }

        // connect to identify signal
        onIdentifyGraphicsOverlayStatusChanged: {
            if (identifyGraphicsOverlayStatus !== Enums.TaskStatusCompleted)
                return;

            const identifiedGraphics = identifyGraphicsOverlayResult.graphics;
            if (identifiedGraphics.length < 1)
                return;

            // get the first identified graphic
            const graphic = identifiedGraphics[0];

            // select the graphic
            graphicsOverlay.clearSelection();
            graphic.selected = true;

            // get the geometry
            const selectedGeometry = graphic.geometry;
            const selectedGeometryType = selectedGeometry.geometryType;

            // reset the output text
            pointText.text = "";
            lineText.text = "";
            polygonText.text = "";

            // populate the view with the spatial relationships the selected graphic has to the other graphics
            // ignore testing relationships between the geometry and itself
            if (selectedGeometryType !== Enums.GeometryTypePoint) {
                const pointRelationships = getSpatialRelationships(selectedGeometry, pointGraphic.geometry).toString();
                pointText.text = "Point: %1".arg(pointRelationships);
            }
            if (selectedGeometryType !== Enums.GeometryTypePolyline) {
                const polylineRelationships = getSpatialRelationships(selectedGeometry, polylineGraphic.geometry).toString();
                lineText.text = "Polyline: %1".arg(polylineRelationships);
            }
            if (selectedGeometryType !== Enums.GeometryTypePolygon) {
                const polygonRelationships = getSpatialRelationships(selectedGeometry, polygonGraphic.geometry).toString();
                polygonText.text = "Polygon: %1".arg(polygonRelationships);
            }
        }
    }

    // function to return list of relaionships
    function getSpatialRelationships(geom1, geom2) {
        const relationships = [];
        if (GeometryEngine.crosses(geom1, geom2))
            relationships.push("CROSSES");
        if (GeometryEngine.contains(geom1, geom2))
            relationships.push("CONTAINS");
        if (GeometryEngine.disjoint(geom1, geom2))
            relationships.push("DISJOINT");
        if (GeometryEngine.intersects(geom1, geom2))
            relationships.push("INTERSECTS");
        if (GeometryEngine.overlaps(geom1, geom2))
            relationships.push("OVERLAPS");
        if (GeometryEngine.touches(geom1, geom2))
            relationships.push("TOUCHES");
        if (GeometryEngine.within(geom1, geom2))
            relationships.push("WITHIN");
        return relationships;
    }

    Rectangle {
        anchors {
            fill: relationshipColumn
            margins: -10
        }
        opacity: 0.85
        radius: 5
        color: "#e2e2e2"
        border {
            color: "darkgray"
            width: 1
        }
    }

    Column {
        id: relationshipColumn
        anchors {
            left: parent.left
            top: parent.top
            margins: 15
        }
        spacing: 5

        Text {
            text: "Relationships:"
            font {
                pixelSize: 16
                bold: true
                family: "helvetica"
            }
        }

        Text {
            id: pointText
            visible: text.length > 0
            font.family: "helvetica"
        }

        Text {
            id: lineText
            visible: text.length > 0
            font.family: "helvetica"
        }

        Text {
            id: polygonText
            visible: text.length > 0
            font.family: "helvetica"
        }
    }
}

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