Show result of spatial operations

View on GitHubSample viewer app

Find the union, intersection, or difference of two geometries.

Image of show result of spatial operations

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

  1. Create two GraphicsOverlays, one to show polygons and another to show the relationships between them.
  2. Define a PolygonBuilder of each Geometry.
  3. Add the overlapping polygons to a graphics overlay.
  4. 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.
  5. 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.kt
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
/* Copyright 2022 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.arcgismaps.sample.showresultofspatialoperations

import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.Color
import com.arcgismaps.geometry.Geometry
import com.arcgismaps.geometry.GeometryEngine
import com.arcgismaps.geometry.MutablePart
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.geometry.PolygonBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.esri.arcgismaps.sample.showresultofspatialoperations.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

private val Color.Companion.blue: Color
    get() {
        return fromRgba(0, 0, 255, 255)
    }

class MainActivity : AppCompatActivity() {

    // set up data binding for the activity
    private val activityMainBinding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    private val mapView by lazy {
        activityMainBinding.mapView
    }

    // enum to keep track of the selected operation to display on the map
    enum class SpatialOperation(val menuPosition: Int) {
        NO_OPERATION(0),
        INTERSECTION(1),
        UNION(2),
        DIFFERENCE(3),
        SYMMETRIC_DIFFERENCE(4)
    }

    // create the graphic overlays
    private val inputGeometryGraphicsOverlay: GraphicsOverlay by lazy { GraphicsOverlay() }
    private val resultGeometryGraphicsOverlay: GraphicsOverlay by lazy { GraphicsOverlay() }

    // simple black line symbol for outlines
    private val lineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.black, 1f)

    // red fill symbol for result
    private val resultFillSymbol =
        SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.red, lineSymbol)

    // the two polygons for perform spatial operations
    private lateinit var inputPolygon1: Polygon
    private lateinit var inputPolygon2: Polygon

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // authentication with an API key or named user is
        // required to access basemaps and other location services
        ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
        lifecycle.addObserver(mapView)

        // set up the adapter
        val arrayAdapter = ArrayAdapter(
            this,
            com.esri.arcgismaps.sample.sampleslib.R.layout.custom_dropdown_item,
            resources.getStringArray(R.array.operation)
        )
        activityMainBinding.bottomListItems.apply {
            setAdapter(arrayAdapter)
            setOnItemClickListener { _, _, i, _ ->
                updateGeometry(i)
            }
        }

        // set up the MapView
        mapView.apply {
            // create an ArcGISMap with a light gray basemap
            map = ArcGISMap(BasemapStyle.ArcGISLightGray)
            // create graphics overlays to show the inputs and results of the spatial operation
            graphicsOverlays.add(inputGeometryGraphicsOverlay)
            graphicsOverlays.add(resultGeometryGraphicsOverlay)
        }

        // create input polygons and add graphics to display these polygons in an overlay
        createPolygons()

        // center the map view on the input geometries
        val envelope = GeometryEngine.union(inputPolygon1, inputPolygon2).extent
        lifecycleScope.launch {
            mapView.setViewpointGeometry(envelope, 20.0)
        }
    }

    private fun updateGeometry(position: Int) {
        // clear previous operation result
        resultGeometryGraphicsOverlay.graphics.clear()
        // create a result geometry of the spatial operation
        var resultGeometry: Geometry? = null
        // get the selected operation
        when (SpatialOperation.entries.find { it.menuPosition == position }) {
            SpatialOperation.NO_OPERATION -> { /* No operation needed */
            }
            SpatialOperation.INTERSECTION -> {
                resultGeometry = GeometryEngine.intersectionOrNull(inputPolygon1, inputPolygon2)
            }
            SpatialOperation.UNION -> {
                resultGeometry = GeometryEngine.union(inputPolygon1, inputPolygon2)
            }
            SpatialOperation.DIFFERENCE -> {
                resultGeometry = GeometryEngine.differenceOrNull(inputPolygon1, inputPolygon2)
            }
            SpatialOperation.SYMMETRIC_DIFFERENCE -> {
                resultGeometry = GeometryEngine.symmetricDifferenceOrNull(inputPolygon1, inputPolygon2)
            }
            null -> {}
        }

        // add a graphic from the result geometry, showing result in red
        if (resultGeometry != null) {
            val resultGraphic = Graphic(resultGeometry, resultFillSymbol).also {
                // select the result to highlight it
                it.isSelected = true
            }
            // add the result graphic to the graphic overlay
            resultGeometryGraphicsOverlay.graphics.add(resultGraphic)
        }
    }

    private fun createPolygons() {
        // create input polygon 1
        val polygonBuilder1 = PolygonBuilder(SpatialReference.webMercator()) {
            // add points to the point collection
            addPoint(Point(-13160.0, 6710100.0))
            addPoint(Point(-13300.0, 6710500.0))
            addPoint(Point(-13760.0, 6710730.0))
            addPoint(Point(-14660.0, 6710000.0))
            addPoint(Point(-13960.0, 6709400.0))
        }
        inputPolygon1 = polygonBuilder1.toGeometry()

        // create and add a blue graphic to show input polygon 1
        val blueFill = SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.blue, lineSymbol)
        inputGeometryGraphicsOverlay.graphics.add(Graphic(inputPolygon1, blueFill))

        // outer ring
        val outerRing = MutablePart.createWithPoints(
            listOf(
                Point(-13060.0, 6711030.0),
                Point(-12160.0, 6710730.0),
                Point(-13160.0, 6709700.0),
                Point(-14560.0, 6710730.0),
                Point(-13060.0, 6711030.0),
            ),
            SpatialReference.webMercator()
        )

        // inner ring
        val innerRing = MutablePart.createWithPoints(
            listOf(
                Point(-13060.0, 6710910.0),
                Point(-12450.0, 6710660.0),
                Point(-13160.0, 6709900.0),
                Point(-14160.0, 6710630.0),
                Point(-13060.0, 6710910.0)
            ),
            SpatialReference.webMercator()
        )

        // add both parts (rings) to a polygon and create a geometry from it
        inputPolygon2 = Polygon(listOf(outerRing, innerRing))

        // create and add a green graphic to show input polygon 2
        val greenFill = SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.green, lineSymbol)
        inputGeometryGraphicsOverlay.graphics.add(Graphic(inputPolygon2, greenFill))
    }
}

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