Skip to content

Learn how to buffer, intersect, and union geometries.

Click the actions on the right to create a buffer and execute intersect and union methods on the polygon geometry.

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 operators to buffer, intersect, and union geometries.

Prerequisites

Steps

Create a map with graphics

  1. 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.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s) :
    • Privileges
      • Location services > Basemaps
  2. In CodePen, set the apiKey property on the global esriConfig variable to your access token.
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };

To learn about other ways to get an access token, go to Types of authentication.

Add modules

  1. Use $arcgis.import to add the required modules.

    35 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    98 collapsed lines
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    </script>
    </body>
    </html>

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.

  1. Add a calcite-action-bar with calcite-actions to the top-right slot of the map to calculate the buffer, intersection, union, and to remove all resulting graphics with a reset action.

    28 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    112 collapsed lines
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    </script>
    </body>
    </html>
  2. In the script, create variables to store references to the newly added action elements.

    59 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    93 collapsed lines
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    </script>
    </body>
    </html>
  3. 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.

  1. Create an additional instance of a GraphicsLayer for the results of the geometry calculations and add both layers to the Map.
    66 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    89 collapsed lines
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    </script>
    </body>
    </html>

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 intersectionOperator and unionOperator operations.

  1. Create the global variable bufferGraphic and the createBuffer function. If there is a bufferGraphic, return.

    157 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Create a 1 kilometer buffer around the Point graphic using the geodesicBufferOperator.

    157 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "kilometers",
    });
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. 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 the graphicsLayer. Enable the other actions and disable the Buffer action.

    157 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  4. Add an event listener to the buffer action that will call the createBuffer function when it is clicked.

    147 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    41 collapsed lines
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    </script>
    </body>
    </html>
  5. 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.

  1. Create the resetGraphics function. Remove the bufferGraphic from the graphicsLayer and removeAll graphics from the resultsLayer. Set the bufferGraphic to null. Disable all actions except the Buffer action.

    173 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Add an event listener to the Reset action to call the resetGraphics function when the action is clicked.

    157 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    51 collapsed lines
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    </script>
    </body>
    </html>
  3. 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 findIntersect function and add the resulting graphic to the resultsLayer.

  1. Define the findIntersect function.

    195 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. removeAll graphics from the resultsLayer. If there is not a bufferGraphic with which to execute the intersectionOperator, then return.

    205 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Use the intersectionOperator to find the intersection between the two geometries.

    205 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    polygonGraphic.geometry,
    bufferGraphic.geometry,
    );
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  4. Create a graphic to display the intersecting geometry and add it to the resultsLayer.

    205 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    polygonGraphic.geometry,
    bufferGraphic.geometry,
    );
    const intersectionGraphic = new Graphic({
    geometry: intersectGeom,
    symbol: {
    type: "simple-fill",
    style: "cross",
    color: "green",
    outline: {
    color: "green",
    },
    },
    });
    resultsLayer.add(intersectionGraphic);
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  5. Add an event listener to the Intersect action to call the findIntersect function when the action is clicked.

    159 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    78 collapsed lines
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    polygonGraphic.geometry,
    bufferGraphic.geometry,
    );
    const intersectionGraphic = new Graphic({
    geometry: intersectGeom,
    symbol: {
    type: "simple-fill",
    style: "cross",
    color: "green",
    outline: {
    color: "green",
    },
    },
    });
    resultsLayer.add(intersectionGraphic);
    }
    </script>
    </body>
    </html>
  6. 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 polygonGraphic geometries and add the result to the resultsLayer.

  1. Define the createUnion function.

    214 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    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() {
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. removeAll graphics from the resultsLayer. If there is not a bufferGraphic with which to execute the unionOperator operation, then return.

    234 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    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() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Use the unionOperator to find the union between the two geometries.

    234 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    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() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const unionGeom = unionOperator.execute(polygonGraphic.geometry, bufferGraphic.geometry);
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  4. Create a graphic to display the resulting geometry and add it to the resultsLayer.

    234 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    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() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const unionGeom = unionOperator.execute(polygonGraphic.geometry, bufferGraphic.geometry);
    const unionGraphic = new Graphic({
    geometry: unionGeom,
    symbol: {
    type: "simple-fill",
    style: "cross",
    color: "green",
    outline: {
    color: "green",
    },
    },
    });
    resultsLayer.add(unionGraphic);
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  5. Add an event listener to the Union action to call the createUnion function when the action is clicked.

    157 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Calculate geometries</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map center="-118.805, 34.020" zoom="13">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    <calcite-action-bar slot="top-right" 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-map>
    <script type="module">
    const [
    Graphic,
    Map,
    geodesicBufferOperator,
    intersectionOperator,
    unionOperator,
    GraphicsLayer,
    ] = await $arcgis.import([
    "@arcgis/core/Graphic.js",
    "@arcgis/core/Map.js",
    "@arcgis/core/geometry/operators/geodesicBufferOperator.js",
    "@arcgis/core/geometry/operators/intersectionOperator.js",
    "@arcgis/core/geometry/operators/unionOperator.js",
    "@arcgis/core/layers/GraphicsLayer.js",
    ]);
    const bufferAction = document.querySelector("#buffer");
    const intersectAction = document.querySelector("#intersect");
    const unionAction = document.querySelector("#union");
    const resetAction = document.querySelector("#reset");
    const viewElement = document.querySelector("arcgis-map");
    const graphicsLayer = new GraphicsLayer();
    const resultsLayer = new GraphicsLayer();
    viewElement.map = new Map({
    basemap: "arcgis/topographic",
    layers: [graphicsLayer, resultsLayer],
    });
    const point = {
    //Create a point
    type: "point",
    longitude: -118.80657463861,
    latitude: 34.0005930608889,
    };
    const simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // Orange
    outline: {
    color: [255, 255, 255], // White
    width: 1,
    },
    };
    const pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol,
    });
    graphicsLayer.add(pointGraphic);
    // Create a line geometry
    const polyline = {
    type: "polyline",
    paths: [
    [-118.821527826096, 34.0139576938577], //Longitude, latitude
    [-118.814893761649, 34.0080602407843], //Longitude, latitude
    [-118.808878330345, 34.0016642996246], //Longitude, latitude
    ],
    };
    const simpleLineSymbol = {
    type: "simple-line",
    color: [226, 119, 40], // Orange
    width: 2,
    };
    const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: simpleLineSymbol,
    });
    graphicsLayer.add(polylineGraphic);
    // Create a polygon geometry
    const polygon = {
    type: "polygon",
    rings: [
    [-118.818984489994, 34.0137559967283], //Longitude, latitude
    [-118.806796597377, 34.0215816298725], //Longitude, latitude
    [-118.791432890735, 34.0163883241613], //Longitude, latitude
    [-118.79596686535, 34.008564864635], //Longitude, latitude
    [-118.808558110679, 34.0035027131376], //Longitude, latitude
    ],
    };
    const simpleFillSymbol = {
    type: "simple-fill",
    color: [227, 139, 79, 0.8], // Orange, opacity 80%
    outline: {
    color: [255, 255, 255],
    width: 1,
    },
    };
    const popupTemplate = {
    title: "{Name}",
    content: "{Description}",
    };
    const attributes = {
    Name: "Graphic",
    Description: "I am a polygon",
    };
    const polygonGraphic = new Graphic({
    geometry: polygon,
    symbol: simpleFillSymbol,
    attributes: attributes,
    popupTemplate: popupTemplate,
    });
    graphicsLayer.add(polygonGraphic);
    bufferAction?.addEventListener("click", createBuffer);
    resetAction?.addEventListener("click", resetGraphics);
    intersectAction?.addEventListener("click", findIntersection);
    unionAction?.addEventListener("click", createUnion);
    102 collapsed lines
    let bufferGraphic;
    async function createBuffer() {
    if (bufferGraphic) {
    return;
    }
    if (!geodesicBufferOperator.isLoaded()) {
    await geodesicBufferOperator.load();
    }
    const buffer = geodesicBufferOperator.execute(pointGraphic.geometry, 1, {
    unit: "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;
    }
    function resetGraphics() {
    graphicsLayer.remove(bufferGraphic);
    resultsLayer.removeAll();
    bufferGraphic = null;
    bufferAction.disabled = false;
    resetAction.disabled = true;
    intersectAction.disabled = true;
    unionAction.disabled = true;
    }
    async function findIntersection() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const intersectGeom = intersectionOperator.execute(
    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() {
    resultsLayer.removeAll();
    if (!bufferGraphic) {
    return;
    }
    const unionGeom = unionOperator.execute(polygonGraphic.geometry, bufferGraphic.geometry);
    const unionGraphic = new Graphic({
    geometry: unionGeom,
    symbol: {
    type: "simple-fill",
    style: "cross",
    color: "green",
    outline: {
    color: "green",
    },
    },
    });
    resultsLayer.add(unionGraphic);
    }
    </script>
    </body>
    </html>
  6. 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 SDK features and ArcGIS services in these tutorials: