Find the union, intersection, or difference of two geometries.
Use case
The different spatial operations (union, difference, symmetric difference, and intersection) can be used for a variety of spatial analyses. For example, government authorities may use the intersect operation to determine whether a proposed road cuts through a restricted piece of land such as a nature reserve or a private property.
When these operations are chained together, they become even more powerful. An analysis of food deserts within an urban area might begin by union-ing service areas of grocery stores, farmer's markets, and food co-ops. Taking the difference between this single geometry of all services areas and that of a polygon delineating a neighborhood would reveal the areas within that neighborhood where access to healthy, whole foods may not exist.
How to use the sample
Select a spatial operation from the overflow menu. When an operation is selected, the resulting geometry is shown in red.
How it works
Create two GraphicsOverlays, one to show polygons and another to show the relationships between them.
Define a PointCollection of each Geometry.
Add the overlapping polygons to a graphics overlay.
Perform spatial relationships between the polygons by using the appropriate operation:
GeometryEngine.union(geometry1, geometry2) - This method returns the two geometries united together as one geometry.
GeometryEngine.difference(geometry1, geometry2) - This method returns any part of Geometry2 that does not intersect Geometry1.
GeometryEngine.symmetricDifference(geometry1, geometry2) - This method returns any part of Geometry1 or Geometry2 which do not intersect.
GeometryEngine.intersection(geometry1, geometry2) - This method returns the intersection of Geometry1 and Geometry2.
Add the resultant geometry to a new Graphic and display it by adding it to the second GraphicsOverlay.
Relevant API
Geometry
GeometryEngine
GeometryEngine.difference
GeometryEngine.intersection
GeometryEngine.symmetricDifference
GeometryEngine.union
Graphic
GraphicsOverlay
Tags
analysis, combine, difference, geometry, intersection, merge, polygon, union
Sample Code
MainActivity.java
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
/*
* Copyright 2016 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.
*/package com.esri.arcgisruntime.sample.performspatialoperations;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Part;
import com.esri.arcgisruntime.geometry.PartCollection;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polygon;
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.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
publicclassMainActivityextendsAppCompatActivity{
finalprivate GraphicsOverlay inputGeometryOverlay = new GraphicsOverlay();
finalprivate GraphicsOverlay resultGeometryOverlay = new GraphicsOverlay();
// simple black (0xFF000000) line symbol for outlinesfinalprivate SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF000000, 1);
finalprivate SimpleFillSymbol resultFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0xFFE91F1F,
lineSymbol);
private Polygon inputPolygon1;
private Polygon inputPolygon2;
// The spatial operation switching menu items.private MenuItem noOperationMenuItem = null;
private MenuItem intersectionMenuItem = null;
private MenuItem unionMenuItem = null;
private MenuItem differenceMenuItem = null;
private MenuItem symmetricDifferenceMenuItem = null;
private MapView mMapView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// authentication with an API key or named user is required to access basemaps and other// location services ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);
mMapView = findViewById(R.id.mapView);
// create ArcGISMap with topographic basemap ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_LIGHT_GRAY);
mMapView.setMap(map);
// create graphics overlays to show the inputs and results of the spatial operation mMapView.getGraphicsOverlays().add(inputGeometryOverlay);
mMapView.getGraphicsOverlays().add(resultGeometryOverlay);
// create input polygons and add graphics to display these polygons in an overlay createPolygons();
// center the map view on the input geometries Geometry viewpointGeom = GeometryEngine.union(inputPolygon1, inputPolygon2).getExtent();
mMapView.setViewpointGeometryAsync(viewpointGeom, 20);
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the menu items that perform spatial operations. noOperationMenuItem = menu.getItem(0);
intersectionMenuItem = menu.getItem(1);
unionMenuItem = menu.getItem(2);
differenceMenuItem = menu.getItem(3);
symmetricDifferenceMenuItem = menu.getItem(4);
// set the 'no-op' menu item checked by default noOperationMenuItem.setChecked(true);
returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item){
// handle menu item selectionint itemId = item.getItemId();
// clear previous operation result resultGeometryOverlay.getGraphics().clear();
// perform spatial operations and add results as graphics, depending on the option selected// if-else is used because this sample is used elsewhere as a Library moduleif (itemId == R.id.action_no_operation) {
// no spatial operation - graphics have been cleared previously noOperationMenuItem.setChecked(true);
returntrue;
} elseif (itemId == R.id.action_intersection) {
intersectionMenuItem.setChecked(true);
showGeometry(GeometryEngine.intersection(inputPolygon1, inputPolygon2));
returntrue;
} elseif (itemId == R.id.action_union) {
unionMenuItem.setChecked(true);
showGeometry(GeometryEngine.union(inputPolygon1, inputPolygon2));
returntrue;
} elseif (itemId == R.id.action_difference) {
differenceMenuItem.setChecked(true);
// note that the difference method gives different results depending on the order of input geometries showGeometry(GeometryEngine.difference(inputPolygon1, inputPolygon2));
returntrue;
} elseif (itemId == R.id.action_symmetric_difference) {
symmetricDifferenceMenuItem.setChecked(true);
showGeometry(GeometryEngine.symmetricDifference(inputPolygon1, inputPolygon2));
returntrue;
} else {
returnsuper.onOptionsItemSelected(item);
}
}
privatevoidshowGeometry(Geometry resultGeometry){
// add a graphic from the result geometry, showing result in red (0xFFE91F1F) Graphic resultGraphic = new Graphic(resultGeometry, resultFillSymbol);
resultGeometryOverlay.getGraphics().add(resultGraphic);
// select the result to highlight it resultGraphic.setSelected(true);
}
privatevoidcreatePolygons(){
// create input polygon 1 PointCollection pointsPoly = new PointCollection(SpatialReferences.getWebMercator());
pointsPoly.add(new Point(-13160, 6710100));
pointsPoly.add(new Point(-13300, 6710500));
pointsPoly.add(new Point(-13760, 6710730));
pointsPoly.add(new Point(-14660, 6710000));
pointsPoly.add(new Point(-13960, 6709400));
inputPolygon1 = new Polygon(pointsPoly);
// create and add a blue graphic to show input polygon 1 SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0x990000CC, lineSymbol);
inputGeometryOverlay.getGraphics().add(new Graphic(inputPolygon1, fillSymbol));
// create input polygon 2 with a green (0xFF009900) symbol// outer ring PointCollection outerRingSegmentCollection = new PointCollection(SpatialReferences.getWebMercator());
outerRingSegmentCollection.add(new Point(-13060, 6711030));
outerRingSegmentCollection.add(new Point(-12160, 6710730));
outerRingSegmentCollection.add(new Point(-13160, 6709700));
outerRingSegmentCollection.add(new Point(-14560, 6710730));
outerRingSegmentCollection.add(new Point(-13060, 6711030));
Part outerRing = new Part(outerRingSegmentCollection);
// inner ring PointCollection innerRingSegmentCollection = new PointCollection(SpatialReferences.getWebMercator());
innerRingSegmentCollection.add(new Point(-13060, 6710910));
innerRingSegmentCollection.add(new Point(-12450, 6710660));
innerRingSegmentCollection.add(new Point(-13160, 6709900));
innerRingSegmentCollection.add(new Point(-14160, 6710630));
innerRingSegmentCollection.add(new Point(-13060, 6710910));
Part innerRing = new Part(innerRingSegmentCollection);
// add both parts (rings) to a part collection and create a geometry from it PartCollection polygonParts = new PartCollection(outerRing);
polygonParts.add(innerRing);
inputPolygon2 = new Polygon(polygonParts);
// create and add a green graphic to show input polygon 2 fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0x99009900, lineSymbol);
inputGeometryOverlay.getGraphics().add(new Graphic(inputPolygon2, fillSymbol));
}
@OverrideprotectedvoidonPause(){
mMapView.pause();
super.onPause();
}
@OverrideprotectedvoidonResume(){
super.onResume();
mMapView.resume();
}
@OverrideprotectedvoidonDestroy(){
super.onDestroy();
mMapView.dispose();
}
}