Spatial relationships

View inQMLC++
View on GitHub
Sample 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.cppSpatialRelationships.cppSpatialRelationships.hSpatialRelationships.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
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "SpatialRelationships.h"

#include "Map.h"
#include "MapQuickView.h"
#include "GraphicsOverlay.h"
#include "Graphic.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "Point.h"
#include "PolylineBuilder.h"
#include "Polyline.h"
#include "PolygonBuilder.h"
#include "Polygon.h"
#include "GeometryEngine.h"
#include <QStringList>
#include <memory>

using namespace Esri::ArcGISRuntime;

SpatialRelationships::SpatialRelationships(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void SpatialRelationships::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<SpatialRelationships>("Esri.Samples", 1, 0, "SpatialRelationshipsSample");
}

void SpatialRelationships::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setSelectionProperties(SelectionProperties(QColor(Qt::yellow)));

  // Create a map using the topographic basemap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);

  // Set map to map view
  m_mapView->setMap(m_map);

  // Create GraphicsOverlay
  m_graphicsOverlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

  // Add Graphics
  addGraphics();

  // Set viewpoint
  m_mapView->setViewpointCenter(m_pointGraphic->geometry(), 200000000);

  // connect signals
  connectSignals();
}

void SpatialRelationships::addGraphics()
{
  addPolygonGraphic();
  addPolylineGraphic();
  addPointGraphic();
}

void SpatialRelationships::addPolygonGraphic()
{
  // create polygon geometry
  PolygonBuilder polyBuilder(SpatialReference::webMercator());
  polyBuilder.addPoint(-5991501.677830, 5599295.131468);
  polyBuilder.addPoint(-6928550.398185, 2087936.739807);
  polyBuilder.addPoint(-3149463.800709, 1840803.011362);
  polyBuilder.addPoint(-1563689.043184, 3714900.452072);
  polyBuilder.addPoint(-3180355.516764, 5619889.608838);
  Polygon geom = polyBuilder.toPolygon();

  // create symbol
  SimpleLineSymbol* outline = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("green"), 2.0f /*width*/, this);
  SimpleFillSymbol* sfs = new SimpleFillSymbol(SimpleFillSymbolStyle::ForwardDiagonal, QColor("green"), outline, this);

  // create graphic
  m_polygonGraphic = new Graphic(geom, sfs);
  m_graphicsOverlay->graphics()->append(m_polygonGraphic);
}

void SpatialRelationships::addPolylineGraphic()
{
  // create poyline geometry
  PolylineBuilder polyBuilder(SpatialReference::webMercator());
  polyBuilder.addPoint(-4354240.726880, -609939.795721);
  polyBuilder.addPoint(-3427489.245210, 2139422.933233);
  polyBuilder.addPoint(-2109442.693501, 4301843.057130);
  polyBuilder.addPoint(-1810822.771630, 7205664.366363);
  Polyline geom = polyBuilder.toPolyline();

  // create symbol
  SimpleLineSymbol* sls = new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor("red"), 4.0f /*width*/, this);

  // create graphic
  m_polylineGraphic = new Graphic(geom, sls);
  m_graphicsOverlay->graphics()->append(m_polylineGraphic);
}

void SpatialRelationships::addPointGraphic()
{
  // Create point geometry
  Point geom(-4487263.495911, 3699176.480377, SpatialReference::webMercator());

  // create symbol
  SimpleMarkerSymbol* sms = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("blue"), 10.0f /*size*/, this);

  // create graphic
  m_pointGraphic = new Graphic(geom, sms, this);
  m_graphicsOverlay->graphics()->append(m_pointGraphic);
}


void SpatialRelationships::connectSignals()
{
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& mouseEvent)
  {
    // identify graphics
    m_mapView->identifyGraphicsOverlay(m_graphicsOverlay, mouseEvent.x(), mouseEvent.y(), 1.0 /*tolerance*/, false /*returnPopupsOnly*/);
  });

  connect(m_mapView, &MapQuickView::identifyGraphicsOverlayCompleted, this, [this](QUuid, IdentifyGraphicsOverlayResult* rawResult)
  {
    // Delete rawReslt when we leave scope.
    auto result = std::unique_ptr<IdentifyGraphicsOverlayResult>(rawResult);

    const QList<Graphic*> identifiedGraphics = result->graphics();
    if (identifiedGraphics.isEmpty())
      return;

    // get the first identified graphic
    Graphic* graphic = identifiedGraphics.at(0);

    // select the graphic
    m_graphicsOverlay->clearSelection();
    graphic->setSelected(true);

    // get the geometry
    const Geometry selectedGeometry = graphic->geometry();
    const GeometryType selectedGeometryType = selectedGeometry.geometryType();

    // reset the output text
    m_pointRelationships = "";
    m_polylineRelationships = "";
    m_polygonRelationships = "";

    // 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 != GeometryType::Point)
    {
      const QString pointRelationships = getSpatialRelationships(selectedGeometry, m_pointGraphic->geometry()).join(",");
      m_pointRelationships = QString("Point: %1").arg(pointRelationships);
    }
    if (selectedGeometryType != GeometryType::Polyline)
    {
      const QString polylineRelationships = getSpatialRelationships(selectedGeometry, m_polylineGraphic->geometry()).join(",");
      m_polylineRelationships = QString("Polyline: %1").arg(polylineRelationships);
    }
    if (selectedGeometryType != GeometryType::Polygon)
    {
      const QString polygonRelationships = getSpatialRelationships(selectedGeometry, m_polygonGraphic->geometry()).join(",");
      m_polygonRelationships = QString("Polygon: %1").arg(polygonRelationships);
    }

    emit relationshipsChanged();
  });
}

// function to return list of relaionships
QStringList SpatialRelationships::getSpatialRelationships(const Geometry& geom1, const Geometry& geom2)
{
  QStringList relationships;
  if (GeometryEngine::crosses(geom1, geom2))
    relationships.append("CROSSES");
  if (GeometryEngine::contains(geom1, geom2))
    relationships.append("CONTAINS");
  if (GeometryEngine::disjoint(geom1, geom2))
    relationships.append("DISJOINT");
  if (GeometryEngine::intersects(geom1, geom2))
    relationships.append("INTERSECTS");
  if (GeometryEngine::overlaps(geom1, geom2))
    relationships.append("OVERLAPS");
  if (GeometryEngine::touches(geom1, geom2))
    relationships.append("TOUCHES");
  if (GeometryEngine::within(geom1, geom2))
    relationships.append("WITHIN");
  return relationships;
}

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