# 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
• GeometryType
• Graphic
• Point
• Polygon
• Polyline

## Tags

geometries, relationship, spatial analysis

## Sample Code

SpatialRelationshipsSample.java
Use dark colors for code blocksCopy
``````/*
*
* use this file except in compliance with the License. You may obtain a copy of
*
*
* Unless required by applicable law or agreed to in writing, software
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
*/

package com.esri.samples.spatial_relationships;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.IdentifyGraphicsOverlayResult;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;

public class SpatialRelationshipsSample extends Application {

private MapView mapView;

private enum SpatialRelationship {
CROSSES, CONTAINS, DISJOINT, INTERSECTS, OVERLAPS, TOUCHES, WITHIN
}

@Override
public void start(Stage stage) {

try {
// create stack pane and application scene
StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);

// set title, size, and add scene to stage
stage.setTitle("Spatial Relationships Sample");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(scene);
stage.show();

// authentication with an API key or named user is required to access basemaps and other location services
String yourAPIKey = System.getProperty("apiKey");
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

// create a map with the topographic basemap style
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

// create a map view and set the map to it
mapView = new MapView();
mapView.setMap(map);

// create a graphics overlay
GraphicsOverlay graphicsOverlay = new GraphicsOverlay();

// make selection outline yellow
mapView.getSelectionProperties().setColor(Color.YELLOW);

// create a polygon graphic
PointCollection polygonPoints = new PointCollection(SpatialReferences.getWebMercator());
Polygon polygon = new Polygon(polygonPoints);
SimpleFillSymbol polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.FORWARD_DIAGONAL, Color.LIME,
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.LIME, 2));
Graphic polygonGraphic = new Graphic(polygon, polygonSymbol);

// create a polyline graphic
PointCollection polylinePoints = new PointCollection(SpatialReferences.getWebMercator());
Polyline polyline = new Polyline(polylinePoints);
Graphic polylineGraphic = new Graphic(polyline, new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, Color.RED,
4));

// create a point graphic
Point point = new Point(-4487263.495911, 3699176.480377, SpatialReferences.getWebMercator());
SimpleMarkerSymbol locationMarker = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.BLUE, 10);
Graphic pointGraphic = new Graphic(point, locationMarker);

// create a tree view to show the spatial relationships between each graphic and the selected graphic
TreeView<String> relationships = new TreeView<>();
relationships.setMaxSize(200, 300);
TreeItem<String> rootItem = new TreeItem<>("Relationships");
rootItem.setExpanded(true);
relationships.setRoot(rootItem);
TreeItem<String> pointRelationships = new TreeItem<>("Point");
pointRelationships.setExpanded(true);
TreeItem<String> polylineRelationships = new TreeItem<>("Polyline");
polylineRelationships.setExpanded(true);
TreeItem<String> polygonRelationships = new TreeItem<>("Polygon");
polygonRelationships.setExpanded(true);

// and a mouse click listener to identify the selected graphic
mapView.setOnMouseClicked(e -> {
if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
// identify the clicked graphic(s)
Point2D point2D = new Point2D(e.getX(), e.getY());
ListenableFuture<IdentifyGraphicsOverlayResult> identifyGraphics = mapView.identifyGraphicsOverlayAsync(graphicsOverlay,
point2D, 1, false);
try {
// get the first identified graphic
IdentifyGraphicsOverlayResult result = identifyGraphics.get();
List<Graphic> identifiedGraphics = result.getGraphics();
if (identifiedGraphics.size() > 0) {
// clear previous results
pointRelationships.getChildren().clear();
polylineRelationships.getChildren().clear();
polygonRelationships.getChildren().clear();

// select the identified graphic
graphicsOverlay.clearSelection();
Graphic identifiedGraphic = identifiedGraphics.get(0);
identifiedGraphic.setSelected(true);
Geometry selectedGeometry = identifiedGraphic.getGeometry();
GeometryType selectedGeometryType = selectedGeometry.getGeometryType();

// populate the tree 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) {
getSpatialRelationships(selectedGeometry, pointGraphic.getGeometry()).forEach(relationship ->
}
if (selectedGeometryType != GeometryType.POLYLINE) {
getSpatialRelationships(selectedGeometry, polylineGraphic.getGeometry()).forEach(relationship ->
}
if (selectedGeometryType != GeometryType.POLYGON) {
getSpatialRelationships(selectedGeometry, polygonGraphic.getGeometry()).forEach(relationship ->
}
}
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
});
}
});

// add the map view and label box to the stack pane
StackPane.setMargin(relationships, new Insets(10, 0, 0, 10));
StackPane.setAlignment(relationships, Pos.TOP_LEFT);
} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}

/**
* Gets a list of spatial relationships that the first geometry has to the second geometry.
*
* @param a first geometry
* @param b second geometry
* @return list of relationships a has to b
*/
private List<SpatialRelationship> getSpatialRelationships(Geometry a, Geometry b) {
List<SpatialRelationship> relationships = new ArrayList<>();
return relationships;
}

/**
* Stops and releases all resources used in application.
*/
@Override
public void stop() {

if (mapView != null) {
mapView.dispose();
}
}

/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/
public static void main(String[] args) {

Application.launch(args);
}

}``````