Click or drag to resize

GeometryEngineDifference Method

Performs the Topological difference operation on the two geometries.

Namespace:  Esri.ArcGISRuntime.Geometry
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.9.0
public static Geometry Difference(
	Geometry geometry1,
	Geometry geometry2


Type: Esri.ArcGISRuntime.GeometryGeometry
is the Geometry instance on the left hand side of the subtraction.
Type: Esri.ArcGISRuntime.GeometryGeometry
is the Geometry on the right hand side being subtracted.

Return Value

Type: Geometry
Returns the result of subtraction. The result has the dimension of the geometry1.

The GeometryEngine.Difference method performs a spatial subtraction from the two input geometries. This means that the order of the two input geometry arguments of the GeometryEngine.Difference Static Method will have different results if they are switched. In a theoretical variable format you can think of the GeometryEngine.Difference equation like:
A (Difference) B = C
B (Difference) A = D

If you were looking for the "complete" difference between two geometries you will need to run the GeometryEngine.Difference twice by switching the order of the input arguments and then add the two resultant geometries together (most likely via the GeometryEngine.Union Static Method). This "complete" difference means that you would be interested in obtaining those parts of the two input geometries that have nothing in common. In a theoretical variable format to get the "complete" different between two geometries you can think of the GeometryEngine.Difference equation like:
[A (Difference) B] + [B (Difference) A] = Complete Difference

The following is a visual illustration of two polygon geometries and how the output result would be from the GeometryEngine.Difference operation:

GeometryEngine.Difference visual examples.



Example Name: SpatialOperations

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

Code example screen shot.

// Copyright 2018 Esri.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using System.Collections.Generic;
using System.Windows;

namespace ArcGISRuntime.WPF.Samples.SpatialOperations
        name: "Perform spatial operations",
        category: "Geometry",
        description: "Find the union, intersection, or difference of two geometries.",
        instructions: "The sample provides an option to select a spatial operation. When an operation is selected, the resulting geometry is shown in red. The 'reset operation' button undoes the action and allow selecting a different operation.",
        tags: new[] { "analysis", "combine", "difference", "geometry", "intersection", "merge", "polygon", "union" })]
    public partial class SpatialOperations
        // GraphicsOverlay to hold the polygon graphics.
        private GraphicsOverlay _polygonsOverlay;

        // Polygon graphics to run spatial operations on.
        private Graphic _graphicOne;
        private Graphic _graphicTwo;

        // Graphic to display the spatial operation result polygon.
        private Graphic _resultGraphic;

        public SpatialOperations()

            // Create the map, set the initial extent, and add the polygon graphics.

        private void Initialize()
            // Create the map with a gray canvas basemap and an initial location centered on London, UK.
            Map spatialOperationsMap = new Map(BasemapType.LightGrayCanvas, 51.5017, -0.12714, 15);

            // Add the map to the map view.
            MyMapView.Map = spatialOperationsMap;

            // Create and add two overlapping polygon graphics to operate on.

            // Fill the combo box with some spatial operations to run on the polygon graphics.
            SpatialOperationComboBox.Items.Add("Symmetric difference");

        // Handle the spatial operation selection by performing the operation and showing the result polygon.
        private void SpatialOperationComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            // If an operation hasn't been selected, return.
            if (SpatialOperationComboBox.SelectedItem == null) { return; }

            // Remove any currently displayed result.

            // Polygon geometry from the input graphics.
            Geometry polygonOne = _graphicOne.Geometry;
            Geometry polygonTwo = _graphicTwo.Geometry;

            // Result polygon for spatial operations.
            Geometry resultPolygon = null;

            // Run the selected spatial operation on the polygon graphics and get the result geometry.
            string operation = (string)SpatialOperationComboBox.SelectedItem;
            switch (operation)
                case "Union":
                    resultPolygon = GeometryEngine.Union(polygonOne, polygonTwo);
                case "Difference":
                    resultPolygon = GeometryEngine.Difference(polygonOne, polygonTwo);
                case "Symmetric difference":
                    resultPolygon = GeometryEngine.SymmetricDifference(polygonOne, polygonTwo);
                case "Intersection":
                    resultPolygon = GeometryEngine.Intersection(polygonOne, polygonTwo);

            // Create a black outline symbol to use for the result polygon.
            SimpleLineSymbol outlineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Black, 1);

            // Create a solid red fill symbol for the result polygon graphic.
            SimpleFillSymbol resultSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.Red, outlineSymbol);

            // Create the result polygon graphic and add it to the graphics overlay.
            _resultGraphic = new Graphic(resultPolygon, resultSymbol);

        private void ResetOperationButton_Click(object sender, RoutedEventArgs e)
            // Remove any currently displayed result.

            // Clear the selected spatial operation.
            SpatialOperationComboBox.SelectedIndex = -1;

        private void CreatePolygonsOverlay()
            // Create a black outline symbol to use for the polygons.
            SimpleLineSymbol outlineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Black, 1);

            // Create a point collection to define polygon vertices.
            PointCollection polygonVertices = new PointCollection(SpatialReferences.WebMercator)
                new MapPoint(-13960, 6709400),
                new MapPoint(-14660, 6710000),
                new MapPoint(-13760, 6710730),
                new MapPoint(-13300, 6710500),
                new MapPoint(-13160, 6710100)

            // Create a polygon graphic with a blue fill.
            SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Vertical, System.Drawing.Color.Blue, outlineSymbol);
            Polygon polygonOne = new Polygon(polygonVertices);
            _graphicOne = new Graphic(polygonOne, fillSymbol);

            // Create a point collection to define outer polygon ring vertices.
            PointCollection outerRingVerticesCollection = new PointCollection(SpatialReferences.WebMercator)
                new MapPoint(-13060, 6711030),
                new MapPoint(-12160, 6710730),
                new MapPoint(-13160, 6709700),
                new MapPoint(-14560, 6710730)

            // Create a point collection to define inner polygon ring vertices ("donut hole").
            PointCollection innerRingVerticesCollection = new PointCollection(SpatialReferences.WebMercator)
                new MapPoint(-13060, 6710910),
                new MapPoint(-14160, 6710630),
                new MapPoint(-13160, 6709900),
                new MapPoint(-12450, 6710660)

            // Create a list to contain the inner and outer ring point collections.
            List<PointCollection> polygonParts = new List<PointCollection>

            // Create a polygon graphic with a green fill.
            fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Horizontal, System.Drawing.Color.Green, outlineSymbol);
            _graphicTwo = new Graphic(new Polygon(polygonParts), fillSymbol);

            // Create a graphics overlay in the map view to hold the polygons.
            _polygonsOverlay = new GraphicsOverlay();

            // Add the polygons to the graphics overlay.
        <esri:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
                <TextBlock Margin="5" Text="Select a spatial operation:" />
                    SelectionChanged="SpatialOperationComboBox_SelectionChanged" />
                    Content="Reset operation" />
See Also
Additional Examples
Hyperlink to ExampleDescription
SpatialOperationsFind the union, intersection, or difference of two geometries.