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 on 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 access token.Config.api Key Use dark colors for code blocks var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };
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/layers/
for loading the FeatureLayer module. After the modules are loaded, they are passed as parameters (e.g.Feature Layer" Feature
) 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.Layer Use dark colors for code blocks require(["esri/Graphic", "esri/geometry/geometryEngine", "esri/layers/GraphicsLayer"], ( Graphic, geometryEngine, GraphicsLayer ) => {
Add HTML elements
Add a simple user interface by placing actions in the arcgis-map
that will either call a geometry operation or remove graphics from the arcgis-map
.
-
Inside of an
arcgis-placement
, add acalcite-action-bar
withcalcite-action
s to calculate the buffer, intersection, union, and to remove all resulting graphics with a reset action.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-placement position="top-right"> <calcite-action-bar expand-disabled expanded> <calcite-action id="buffer" text="Buffer" icon="circle-area"></calcite-action> <calcite-action id="intersect" disabled text="Intersect" icon="preserve"></calcite-action> <calcite-action id="union" disabled text="Union" icon="dissolve-features"></calcite-action> <calcite-action id="reset" disabled text="Reset" icon="reset"></calcite-action> </calcite-action-bar> </arcgis-placement> </arcgis-map>
-
In the script, create variables to store references to the newly added action elements.
Use dark colors for code blocks const bufferAction = document.querySelector("#buffer"); const intersectAction = document.querySelector("#intersect"); const unionAction = document.querySelector("#union"); const resetAction = document.querySelector("#reset");
-
Run the app to verify that the action bar has been added to the arcgis-map.
Add new graphics layers
A graphics layer is a container for graphics. It is used with an arcgis-map
component to display graphics on a map. You can add more than one graphics layer to a map. 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, use theLayer add
method.Layers Use dark colors for code blocks const graphicsLayer = new GraphicsLayer(); const resultsLayer = new GraphicsLayer(); arcgisMap.addLayers([graphicsLayer, resultsLayer]);
Create a buffer
A buffer is a polygon surrounding 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 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 buffer graphic to thegraphics
. Enable the other actions and disable the Buffer action.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); bufferAction.disabled = true; resetAction.disabled = false; intersectAction.disabled = false; unionAction.disabled = false; }
-
Add an event listener to the buffer action that will call the
create
function when it is clicked.Buffer Use dark colors for code blocks const polygonGraphic = new Graphic({ geometry: polygon, symbol: simpleFillSymbol, attributes: attributes, popupTemplate: popupTemplate }); graphicsLayer.add(polygonGraphic); bufferAction?.addEventListener("click", createBuffer);
-
Run the app and click on the Buffer action to view the buffer graphic.
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
graphics from theAll results
. Set theLayer buffer
toGraphic null
. Disable all actions except the Buffer action.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); bufferAction.disabled = true; resetAction.disabled = false; intersectAction.disabled = false; unionAction.disabled = false; } function resetGraphics() { graphicsLayer.remove(bufferGraphic); resultsLayer.removeAll(); bufferGraphic = null; bufferAction.disabled.disabled = false; resetAction.disabled.disabled = true; intersectAction.disabled = true; unionAction.disabled = true; }
-
Add an event listener to the Reset action to call the
reset
function when the action is clicked.Graphics Use dark colors for code blocks bufferAction?.addEventListener("click", createBuffer); resetAction?.addEventListener("click", resetGraphics);
-
Run the app. Switch between the Buffer and Reset actions to create a buffer and then remove it from the
arcgis-map
.
Intersect geometries
Two geometries intersect
when they have a geometric area in common.
When the Intersect action 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; bufferAction.disabled.disabled = false; resetAction.disabled.disabled = true; intersectAction.disabled = true; unionAction.disabled = true; } function findIntersection() { }
-
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 findIntersection() { resultsLayer.removeAll(); if (!bufferGraphic) { return; } }
-
Call the
intersect
method to find the intersection between the two geometries.Use dark colors for code blocks function findIntersection() { 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 findIntersection() { 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 action to call the
find
function when the action is clicked.Intersect Use dark colors for code blocks resetAction?.addEventListener("click", resetGraphics); intersectAction?.addEventListener("click", findIntersection);
-
Run the app. Click the Buffer action followed by the Intersect action to calculate and display the resulting geometry.
Union geometries
A union
is the result of combining two or more input geometries. When the Union action 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 action to call the
create
function when the action is clicked.Union Use dark colors for code blocks bufferAction?.addEventListener("click", createBuffer); resetAction?.addEventListener("click", resetGraphics); intersectAction?.addEventListener("click", findIntersection); unionAction?.addEventListener("click", createUnion);
-
Run the application. Click the Buffer action followed by the Union or Intersect actions.
Run the app
In CodePen, run your code to display the map.
The map should load with a point, line and polygon displayed on the map. Click the Buffer action to add a buffer graphic around the point graphic. Then, when you click on the intersect or union actions, 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: