Click or drag to resize
Code Example - PolygonBuilder_MakeIslands

Shows how to use the PolygonBuilder to turn multiple independent Polygons into a single Polygon (use case: grouping island Polygons together into one record).

Code Example
Polygon Builder Make Islands

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="PolygonBuilder_MakeIslands.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">


        <!-- 
        Define a set of symbols that will be used in the code-behind to draw graphics on the screen. 
        -->
        <Grid.Resources>

            <esri:SimpleFillSymbol x:Key="MyPolygonSymbol" Style="Null">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Black" Style="Solid" Width="2"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

            <esri:SimpleFillSymbol x:Key="MyIslandChainPolygonSymbol" Color="Yellow" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Black" Style="Solid" Width="2"/>
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

        </Grid.Resources>

        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1" Background="White" HorizontalAlignment="Left" Margin="10,126,0,0"  VerticalAlignment="Top" 
                   Height="433" Width="772">

            <!-- Add a Map. -->
            <esri:Map x:Name="Map1" >

                <!-- Add a backdrop ArcGISTiledMapServiceLayer. -->
                <esri:ArcGISTiledMapServiceLayer ID="World_Light_Gray_Base" 
                              ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"/>

                <!-- Add an empty GraphicsLayer to hold graphics that are generated by the user with the Editor control. -->
                <esri:GraphicsLayer ID="MyGraphicsLayer"/>

            </esri:Map>
        </esri:MapView>


        <!-- Add a Button to begin the Editor.RequestShapeAsync() method to trace polygons. -->
        <Button Content="Add a polygon" Height="29" HorizontalAlignment="Left" Margin="166,94,0,0" 
          Name="Button1" VerticalAlignment="Top" Width="184" Click="Button1_Click"/>

        <!-- Add a Button to convert multiple polygon graphics into a single polygon via the PolygonBuilder. -->
        <Button Content="Merge individual polygons into one" Height="29" HorizontalAlignment="Left" Margin="369,94,0,0" 
          Name="Button2" VerticalAlignment="Top" Width="223" Click="Button2_Click"/>

        <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="89" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="772" 
                   TextWrapping="Wrap" Margin="10,0,0,0" />
    </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 "PolygonBuilder_MakeIslands.MainWindow" to be just "MainWindow".

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

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "This example shows how the PolygonBuilder can be used to edit existing polygons. -- When the application loads the map extent is zoomed in to a " + 
                "few of the Hawaiian islands. Click the 'Add a polygon' button and using the 'Freehand' Editor option hold the left mouse button down and trace the outline of " +
                "an island and then let up the mouse button to complete the polygon. Repeat the tracing again on another island. Then click the 'Merge individual polygons into " +
                "one' button and the individual polygons will become a single polygon graphic. You can also use this same example to create dough-nut type polygons.";

            // Set the initial extent of the Map to some of the Hawaiian islands.
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(102100);
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-17535402.8, 2321369.85, -17299365.24, 2458039.26, mySpatialReference);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);

            // Don't allow polygons to be combined just yet (we need at least one on the screen).
            Button2.IsEnabled = false;

        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            // This function lets the user use the Editor DrawShape.Freehand tool to trace polygons on the screen simply by holding down the left mouse button.


            // Obtain the GraphicsLayer by its ID from the LayerCollection of the Map.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)MapView1.Map.Layers["MyGraphicsLayer"];

            // Create a new empty graphic to hold the geometry from the RequestShapeAsync Method.
            Esri.ArcGISRuntime.Layers.Graphic myGraphic = null;

            // Get the Geometry from the Editor.RequestShapeAsync that the user draws in the Map.
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = await MapView1.Editor.RequestShapeAsync(Esri.ArcGISRuntime.Controls.DrawShape.Freehand);

            // The DrawShape.Freehand option actually returns a Polyline.
            Esri.ArcGISRuntime.Geometry.Polyline myPolyline = (Esri.ArcGISRuntime.Geometry.Polyline)myGeometry;

            // Get the MapPoints that make up the Polyline via a ReadOnlyPartCollection.
            Esri.ArcGISRuntime.Geometry.ReadOnlyPartCollection myReadOnlyPartCollection = myPolyline.Parts;

            // Define a new Polygon using the same MapPoints of the Polyline.
            Esri.ArcGISRuntime.Geometry.Polygon myPolygon = new Esri.ArcGISRuntime.Geometry.Polygon(myReadOnlyPartCollection);

            // Get the correct Symbol from what was defined in XAML that corresponds to the DrawShape enumeration.
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol myPolygonSymbol = (Esri.ArcGISRuntime.Symbology.SimpleFillSymbol)LayoutRoot.Resources["MyPolygonSymbol"];

            // Create a graphic using the Geometry and Symbol defined.
            myGraphic = new Esri.ArcGISRuntime.Layers.Graphic(myPolygon, myPolygonSymbol);

            // Add the Graphic to the GraphicsLayer. This will automatically cause a refresh of the Map, displaying the graphic visually.
            myGraphicsLayer.Graphics.Add(myGraphic);

            // Now enable the button so that multiple Polygons can be merged into a single Polygon. 
            Button2.IsEnabled = true;

        }

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            // This function shows how you can take the multiple Polygons and merge them into one single Polygon using the PolygonBuilder.


            // Obtain the GraphicsLayer by its ID from the LayerCollection of the Map.
            Esri.ArcGISRuntime.Layers.GraphicsLayer myGraphicsLayer = (Esri.ArcGISRuntime.Layers.GraphicsLayer)MapView1.Map.Layers["MyGraphicsLayer"];

            // Get all of the Graphics in the GraphicsLayer.
            Esri.ArcGISRuntime.Layers.GraphicCollection myGraphicCollection = myGraphicsLayer.Graphics;

            // Create a new PolygonBuilder object based up the same SpatialReference as the MapView. 
            // NOTE: The PolygonBuilder has the ability edit polygons once they have already been created. You can not edit a Polygon object because it is 
            // immutable (this is a fancy way of saying that once the object is created it can not be changed). 
            Esri.ArcGISRuntime.Geometry.PolygonBuilder myIslandChainPolygon = new Esri.ArcGISRuntime.Geometry.PolygonBuilder(MapView1.SpatialReference);

            // Loop through each Graphic in the GraphicCollection
            foreach (Esri.ArcGISRuntime.Layers.Graphic oneGraphic in myGraphicCollection)
            {

                // Get the Geometry of the Graphic.
                Esri.ArcGISRuntime.Geometry.Geometry myGeometry = oneGraphic.Geometry;

                // Do a sanity check to ensure that we are only processing existing Polygon type of geometries.
                if (myGeometry.GeometryType == Esri.ArcGISRuntime.Geometry.GeometryType.Polygon)
                {

                    // Cast the generic Geometry to a Polygon.
                    Esri.ArcGISRuntime.Geometry.Polygon myPolygon = (Esri.ArcGISRuntime.Geometry.Polygon)myGeometry;

                    // Get the ReadOnlyPartCollection that make up the Polygon.
                    Esri.ArcGISRuntime.Geometry.ReadOnlyPartCollection myReadOnlyPartCollection = myPolygon.Parts;

                    // Use the ReadOnlyPartCollection.GetPartsAsPoints to formally get the complex structure of MapPoints that define the Polygon.
                    System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<Esri.ArcGISRuntime.Geometry.MapPoint>> myMapPoints = myReadOnlyPartCollection.GetPartsAsPoints();

                    // Add the MapPoints to the PolygonBuilder object. Remember the purpose if to loop through multiple individual Polygons and 
                    // add their shape into one bigger Polygon via the editable PolygonBuilder.
                    myIslandChainPolygon.AddParts(myMapPoints);

                }
            }

            // Clear out the existing polygon Graphics of the GraphicsLayer.
            myGraphicsLayer.Graphics.Clear();

            // Create a new Graphic that will be used to hold the singular combine Polygon.
            Esri.ArcGISRuntime.Layers.Graphic myGraphic = new Esri.ArcGISRuntime.Layers.Graphic();

            // Set the geometry of the Graphic to that of the PolygonBuilder. 
            myGraphic.Geometry = myIslandChainPolygon.ToGeometry();

            // Get the polygon symbol from the XAML resources.
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol myPolygonSymbol = (Esri.ArcGISRuntime.Symbology.SimpleFillSymbol)LayoutRoot.Resources["MyIslandChainPolygonSymbol"];

            // Apply the symbol to the Graphic.
            myGraphic.Symbol = myPolygonSymbol;

            // Add the Graphic to the GraphicsLayer. This will automatically cause a refresh of the Map, displaying the graphic visually.
            myGraphicsLayer.Graphics.Add(myGraphic);

        }
    }
}