# Spatial relationships

Determine spatial relationships between two geometries.

## 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

``````import QtQuick 2.6
import Esri.ArcGISRuntime 100.9

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

MapView {
id: mapView
anchors.fill: parent

SelectionProperties {
color: "yellow"
}

Map {
BasemapTopographic {}
}

GraphicsOverlay {
id: graphicsOverlay

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()});
polygonGraphic.geometry = polyBuilder.geometry;
}
}

Graphic {
id: polylineGraphic

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

Component.onCompleted: {
const polyBuilder = ArcGISRuntimeEnvironment.createObject("PolylineBuilder", {spatialReference: Factory.SpatialReference.createWebMercator()});
polylineGraphic.geometry = polyBuilder.geometry;
}
}

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: {
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
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"
}
}
}``````