Click or drag to resize
Code Example: EqualGeometries

Demonstrates the differences in comparing a few polygon geometries to via the Geometry.IsEqual and GeometryEngine.Equals Methods.

Code Example
Equal Geometries

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="EqualGeometries.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="LayoutRoot">

        <!-- Create several SimpleFillSymbol objects to place on Graphics. -->
        <Grid.Resources>
            <esri:SimpleFillSymbol x:Key="MyPolygonSymbol4" Style="Null">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="White" Style="Solid" Width="10"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            <esri:SimpleFillSymbol x:Key="MyPolygonSymbol3" Style="Null">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Red" Style="Solid" Width="20"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            <esri:SimpleFillSymbol x:Key="MyPolygonSymbol2" Style="Null">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Green" Style="Solid" Width="30"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            <esri:SimpleFillSymbol x:Key="MyPolygonSymbol1" Style="Null">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Yellow" Style="Solid" Width="40"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <StackPanel x:Name="StackPanel1" Orientation="Vertical">

            <!-- Add a button to compare several polygon geometries. -->
            <Button Content="Compare polygon geometries for equality."  Click="Button_Click" Margin="0,0,0,10"/>

            <!-- Add a Map with a blank GraphicsLayer. -->
            <esri:MapView x:Name="MapView1" BorderBrush="Black" BorderThickness="2" Height="531" Margin="10,0,10,0">
                <esri:Map >
                    <esri:GraphicsLayer x:Name="MyPolygonGraphicsLayer" />
                </esri:Map>
            </esri:MapView>
        </StackPanel>

    </Grid>
</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "EqualGeometries.MainWindow" to be just "MainWindow".

// INSTRUCTIONS:
// Click the button to compare a few polygon geometries to see how the Geometry.IsEqual and GeometryEngine.Equals
// Methods differ in their results. 

namespace EqualGeometries
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Set an initial extent in the map to draw some simple polygons. 
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-2, -2, 12, 12);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);
            MapView1.Map.SpatialReference = Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator;
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the empty GraphicsLayer defined in XAML. 
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)MapView1.Map.Layers["MyPolygonGraphicsLayer"];

            // Simple triangle shape #1.
            System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint> myListOfMapPoints1 = new System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint>();
            myListOfMapPoints1.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(3, 3));
            myListOfMapPoints1.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(5, 6));
            myListOfMapPoints1.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(7, 3));
            Esri.ArcGISRuntime.Geometry.Polygon geometry1 = new Esri.ArcGISRuntime.Geometry.Polygon(myListOfMapPoints1, Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator);
            Esri.ArcGISRuntime.Layers.Graphic myGraphic1 = new Esri.ArcGISRuntime.Layers.Graphic();
            myGraphic1.Geometry = geometry1;
            myGraphic1.Symbol = (Esri.ArcGISRuntime.Symbology.Symbol)LayoutRoot.Resources["MyPolygonSymbol1"];
            MyPolygonGraphicsLayer.Graphics.Add(myGraphic1);

            // Triangle #2 is the same shape as #1 but point order is reversed. 
            System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint> myListOfMapPoints2 = new System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint>();
            myListOfMapPoints2.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(7, 3));
            myListOfMapPoints2.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(5, 6));
            myListOfMapPoints2.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(3, 3));
            Esri.ArcGISRuntime.Geometry.Polygon geometry2 = new Esri.ArcGISRuntime.Geometry.Polygon(myListOfMapPoints2, Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator);
            Esri.ArcGISRuntime.Layers.Graphic myGraphic2 = new Esri.ArcGISRuntime.Layers.Graphic();
            myGraphic2.Geometry = geometry2;
            myGraphic2.Symbol = (Esri.ArcGISRuntime.Symbology.Symbol)LayoutRoot.Resources["MyPolygonSymbol2"];
            MyPolygonGraphicsLayer.Graphics.Add(myGraphic2);

            // Triangle #3 is exactly the same at Triangle #1. 
            System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint> myListOfMapPoints3 = new System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint>();
            myListOfMapPoints3.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(3, 3));
            myListOfMapPoints3.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(5, 6));
            myListOfMapPoints3.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(7, 3));
            Esri.ArcGISRuntime.Geometry.Polygon geometry3 = new Esri.ArcGISRuntime.Geometry.Polygon(myListOfMapPoints3, Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator);
            Esri.ArcGISRuntime.Layers.Graphic myGraphic3 = new Esri.ArcGISRuntime.Layers.Graphic();
            myGraphic3.Geometry = geometry3;
            myGraphic3.Symbol = (Esri.ArcGISRuntime.Symbology.Symbol)LayoutRoot.Resources["MyPolygonSymbol3"];
            MyPolygonGraphicsLayer.Graphics.Add(myGraphic3);

            // Triangle #4 is same shape of Triangle #1 but the starting position of the coordinates are in a different order. 
            System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint> myListOfMapPoints4 = new System.Collections.Generic.List<Esri.ArcGISRuntime.Geometry.MapPoint>();
            myListOfMapPoints4.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(5, 6));
            myListOfMapPoints4.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(7, 3));
            myListOfMapPoints4.Add(new Esri.ArcGISRuntime.Geometry.MapPoint(3, 3));
            Esri.ArcGISRuntime.Geometry.Polygon geometry4 = new Esri.ArcGISRuntime.Geometry.Polygon(myListOfMapPoints4, Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator);
            Esri.ArcGISRuntime.Layers.Graphic myGraphic4 = new Esri.ArcGISRuntime.Layers.Graphic();
            myGraphic4.Geometry = geometry4;
            myGraphic4.Symbol = (Esri.ArcGISRuntime.Symbology.Symbol)LayoutRoot.Resources["MyPolygonSymbol4"];
            MyPolygonGraphicsLayer.Graphics.Add(myGraphic4);


            // LESSONS LEARNED:
            // ================

            // ----------------------------------------------------------------------------------------------------------------------------------------
            // Observation #1: 
            // Both Geometry.IsEqual and GeometryEngine.Equals return the same results (False). This means that 
            // orientation very much matters when testing if two geometries are the identical. If the two geometries
            // do not have the same orientation then they are not identical.
            bool theResult1 = false;
            theResult1 = geometry1.IsEqual(geometry2);

            bool theResult2 = false;
            theResult2 = Esri.ArcGISRuntime.Geometry.GeometryEngine.Equals(geometry1, geometry2);
            // ----------------------------------------------------------------------------------------------------------------------------------------
            // Observation #2: 
            // Both Geometry.IsEqual and GeometryEngine.Equals return the same results (True). This means that 
            // if two geometries are comprised of the same components (orientation, coordinate order, etc.) they are the same. 

            bool theResult3 = false;
            theResult3 = geometry1.IsEqual(geometry3);

            bool theResult4 = false;
            theResult4 = Esri.ArcGISRuntime.Geometry.GeometryEngine.Equals(geometry1, geometry3);
            // ----------------------------------------------------------------------------------------------------------------------------------------
            // Observation #3: 
            // The Geometry.IsEqual and GeometryEngine.Equals return opposite results. This means that 
            // if two geometries have the same shape and orientation but the coordinate order is different then
            // Geometry.IsEqual provides a more strict test to determine that two geometries are not identical.

            bool theResult5 = false;
            theResult5 = geometry1.IsEqual(geometry4);

            bool theResult6 = false;
            theResult6 = Esri.ArcGISRuntime.Geometry.GeometryEngine.Equals(geometry1, geometry4);
            // ----------------------------------------------------------------------------------------------------------------------------------------

            System.Windows.MessageBox.Show(
                "geometry1 = Yellow | geometry2 = Green" + System.Environment.NewLine +
                "geometry1.IsEqual(geometry2): " + theResult1.ToString() + System.Environment.NewLine +
                "GeometryEngine.Equals(geometry1, geometry2): " + theResult2.ToString() + System.Environment.NewLine +
                System.Environment.NewLine + 
                "geometry1 = Yellow | geometry3 = Red" + System.Environment.NewLine +
                "geometry1.IsEqual(geometry3): " + theResult3.ToString() + System.Environment.NewLine +
                "GeometryEngine.Equals(geometry1, geometry3): " + theResult4.ToString() + System.Environment.NewLine +
                System.Environment.NewLine +
                "geometry1 = Yellow | geometry4 = White" + System.Environment.NewLine +
                "geometry1.IsEqual(geometry4): " + theResult5.ToString() + System.Environment.NewLine + 
                "GeometryEngine.Equals(geometry1, geometry4): " + theResult6.ToString()
                );
        }    

    }
}