Learn how to buffer, intersect, and union geometries.
A geometry calculation is an operation you can use to buffer, intersect, or union geometries to create a new geometry. The resulting geometry is commonly used to display a graphic in a map or as input for another analysis.
In this tutorial, you will use the geometry
to buffer, intersect, and union geometries.
Prerequisites
Steps
Create a map with graphics
- To get started, complete the Add a point, line, and polygon tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services used in this tutorial.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set
esri
to your API key..Config.api Key Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service });
To learn about other ways to get an access token, go to Types of authentication.
Add the geometryEngine module
-
In the
require
statement, add thegeometry
module.Engine The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map" Map
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Use dark colors for code blocks require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/Graphic", "esri/layers/GraphicsLayer", "esri/geometry/geometryEngine" ], function(esriConfig, Map, MapView, Graphic, GraphicsLayer, geometryEngine) {
Add HTML elements
Add a simple user interface for the user by placing buttons in the view
that will either call a calculation operation or remove graphics from the view
.
-
Add a
div
with threebuttons
to calculate the intersection, the union, and the last to remove all resulting geometries. Use theesri-widget
andesri-button
classes to give the elements consistent styling.Use dark colors for code blocks <div id="viewDiv"></div> <div id="controls" class="esri-widget"> <button id="buffer" class="esri-button">Buffer</button> <button id="intersect" class="esri-button">Intersect</button> <button id="union" class="esri-button">Union</button> <button id="reset" class="esri-button esri-button--secondary">Reset</button> </div>
-
Add CSS to style the HTML elements that were just added.
Use dark colors for code blocks html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; font-family: "Avenir Next" } #controls { width: 250px; padding: 0px 5px 0px 5px; } .esri-button { margin: 5px 0px 5px 0px; }
-
Add the
controls
div to the viewUI
.Use dark colors for code blocks view.ui.add(document.getElementById("controls"), "top-right");
-
Run the app to verify that the buttons have been added to the view.
Add new graphics layers
A graphics layer is a container for graphics. It is used with a map view to display graphics on a map. You can add more than one graphics layer to a map view. Graphics layers are displayed on top of all other layers.
- Add an additional instance of the
Graphics
for the results of the geometry calculations. To add multiple layers to the map at once, useLayer map.add
.Many Use dark colors for code blocks const graphicsLayer = new GraphicsLayer(); const resultsLayer = new GraphicsLayer(); map.addMany([graphicsLayer, resultsLayer]);
Create a buffer
A buffer is a polygon that surrounds the input geometry at a specified distance. Use a buffer to help better visualize the geometries created from the intersect and union operations.
-
Create the global variable
buffer
and theGraphic create
function. If there is aBuffer buffer
,Graphic return
.Use dark colors for code blocks view.ui.add(document.getElementById("controls"), "top-right"); let bufferGraphic; function createBuffer() { if (bufferGraphic) { return; }
-
Create a
1
kilometer buffer around the Point graphic using thegeodesic
method on theBuffer geometry
.Engine Use dark colors for code blocks let bufferGraphic; function createBuffer() { if (bufferGraphic) { return; } const buffer = geometryEngine.geodesicBuffer( pointGraphic.geometry, 1, "kilometers" );
-
Create a
Graphic
to display the buffer geometry in your map. Set the geometry of the graphic to the buffer you just calculated, and set the symbol to a SimpleFillSymbol. Add the graphic to thebuffer
.Layer Use dark colors for code blocks let bufferGraphic; function createBuffer() { if (bufferGraphic) { return; } const buffer = geometryEngine.geodesicBuffer( pointGraphic.geometry, 1, "kilometers" ); bufferGraphic = new Graphic({ geometry: buffer, symbol: { type: "simple-fill", color: [227, 139, 79, 0.5], outline: { color: [255, 255, 255, 255], }, }, }); graphicsLayer.add(bufferGraphic); }
-
Add an event listener to the
Buffer
button that will call thecreate
function when it is clicked.Buffer Use dark colors for code blocks view.ui.add(document.getElementById("controls"), "top-right"); document.getElementById("buffer").addEventListener("click", createBuffer);
-
Run the app to view the buffer.
Remove graphics from the result layer
Create a function that will reset and remove all graphics from the graphics layer.
-
Create the
reset
function. Remove theGraphics buffer
from theGraphic graphics
andLayer remove
geometries from theAll results
. Set theLayer buffer
toGraphic null
.Use dark colors for code blocks bufferGraphic = new Graphic({ geometry: buffer, symbol: { type: "simple-fill", color: [227, 139, 79, 0.5], outline: { color: [255, 255, 255, 255], }, }, }); graphicsLayer.add(bufferGraphic); } function resetGraphics() { graphicsLayer.remove(bufferGraphic); resultsLayer.removeAll(); bufferGraphic = null; }
-
Add an event listener to the
Reset
button to call thereset
function when the button is clicked.Graphics Use dark colors for code blocks document.getElementById("buffer").addEventListener("click", createBuffer); document.getElementById("reset").addEventListener("click", resetGraphics);
-
Run the app. Switch between the Buffer and Reset buttons to create a buffer and then remove it from the
view
.
Intersect geometries
Two geometries intersect
when they have a geometric area in common.
When the Intersect
button is clicked, call the find
function and add the resulting graphic to the results
.
-
Define the
find
function.Intersect Use dark colors for code blocks function resetGraphics() { graphicsLayer.remove(bufferGraphic); resultsLayer.removeAll(); bufferGraphic = null; } function findIntersect() { }
-
remove
graphics from theAll results
. If there is not aLayer buffer
with which to execute theGraphic intersect
operation, thenreturn
.Use dark colors for code blocks function findIntersect() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } }
-
Call the
intersect
method to find the intersection between the two geometries.Use dark colors for code blocks function findIntersect() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } const intersectGeom = geometryEngine.intersect(polygonGraphic.geometry, bufferGraphic.geometry); }
-
Create a graphic to display the intersecting geometry and add it to the
results
.Layer Use dark colors for code blocks function findIntersect() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } const intersectGeom = geometryEngine.intersect(polygonGraphic.geometry, bufferGraphic.geometry); const intersectionGraphic = new Graphic({ geometry: intersectGeom, symbol: { type: "simple-fill", style: "cross", color: "green", outline: { color: "green" } } }); resultsLayer.add(intersectionGraphic); }
-
Add an event listener to the
Intersect
button to call thefind
function when the button is clicked.Intersect Use dark colors for code blocks document.getElementById("reset").addEventListener("click", resetGraphics); document.getElementById("intersect").addEventListener("click", findIntersect);
-
Run the app. If you click the Intersect button first, the
intersect
method will not be called because it needs thebuffer
. Click the Buffer button followed by the Intersect button to view the resulting geometry.Graphic
Union geometries
A union
is the result of combining two or more input geometries. When the Union
button is clicked, calculate the union between the buffer and polygon
geometries and add the result to the results
.
-
Define the
create
function.Union Use dark colors for code blocks const intersectGeom = geometryEngine.intersect(polygonGraphic.geometry, bufferGraphic.geometry); const intersectionGraphic = new Graphic({ geometry: intersectGeom, symbol: { type: "simple-fill", style: "cross", color: "green", outline: { color: "green" } } }); resultsLayer.add(intersectionGraphic); } function createUnion() { }
-
remove
graphics from theAll results
. If there is not aLayer buffer
with which to execute theGraphic union
operation, thenreturn
.Use dark colors for code blocks function createUnion() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } }
-
Call the
union
method to find the union between the two geometries.Use dark colors for code blocks function createUnion() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } const unionGeom = geometryEngine.union(polygonGraphic.geometry, bufferGraphic.geometry); }
-
Create a graphic to display the resulting geometry and add it to the
results
.Layer Use dark colors for code blocks function createUnion() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } const unionGeom = geometryEngine.union(polygonGraphic.geometry, bufferGraphic.geometry); const unionGraphic = new Graphic({ geometry: unionGeom, symbol: { type: "simple-fill", style: "cross", color: "green", outline: { color: "green" } } }); resultsLayer.add(unionGraphic); }
-
Add an event listener to the
Union
button to call thecreate
function when the button is clicked.Union Use dark colors for code blocks document.getElementById("buffer").addEventListener("click", createBuffer); document.getElementById("reset").addEventListener("click", resetGraphics); document.getElementById("intersect").addEventListener("click", findIntersect); document.getElementById("union").addEventListener("click", createUnion);
-
Run the application. If you click the Union button first, the method will not be called because it needs the
buffer
. Click the Buffer button followed by the Union button to view the resulting geometry. to view the resulting geometry.Graphic
Run the app
In CodePen, run your code to display the map.
The map should load with a buffer graphic around the point graphic. When you click on the intersect or union buttons, it should create a graphic for the results of those calculations.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: