Union

Download Samples Repository

Description

Example of using the GeometryEngine.Union method to calculate the geometric union of feature geometries and a given polygon. To use this sample, the user draws a polygon over the feature polygons and the system then retrieves the union of the feature geometries and the new polygon. Resulting polygons are shown in a graphics overlay on the map.

"Desktop" "Store" Available for Desktop, Store

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.UnionGeometry"
             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">
    <Grid x:Name="layoutGrid">
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

            <esri:SimpleFillSymbol x:Key="FillSymbol" Color="#660000FF" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Blue" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15053000,2749000,-6540000,6590000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="resultsOverlay" />
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				 HorizontalAlignment="Right" VerticalAlignment="Top"
				 Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Click the 'Union Geometries' button to digitize a polygon and show the union of polygon features selected by the polygon. Resulting polygon will be shown as a blue graphic."
                           Width="400" TextWrapping="Wrap" />
                <Button Content="Union Geometries" Margin="12,12,12,0" HorizontalAlignment="Center" Click="UnionButton_Click"/>
            </StackPanel>
        </Border>

        <Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="25"
                HorizontalAlignment="Center" VerticalAlignment="Bottom"
                Visibility="{Binding ElementName=MyMapView, Path=Editor.IsActive, Converter={StaticResource BooleanToVisibilityConverter}}">
            <TextBlock Text="Digitize a polygon on the map." Margin="8" FontSize="14" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Example of using the GeometryEngine.Union method to calculate the geometric union of feature geometries and a given polygon. To use this sample, the user draws a polygon over the feature polygons and the system then retrieves the union of the feature geometries and the new polygon. Resulting polygons are shown in a graphics overlay on the map.
    /// </summary>
    /// <title>Union</title>
    /// <category>Geometry</category>
    public partial class UnionGeometry : UserControl
    {
        private const string GDB_PATH = @"..\..\..\samples-data\maps\usa.geodatabase";

        private Symbol _fillSymbol;
        private FeatureLayer _statesLayer;
        private GraphicsOverlay _resultGraphics;

        /// <summary>Construct Union sample control</summary>
        public UnionGeometry()
        {
            InitializeComponent();

            _fillSymbol = layoutGrid.Resources["FillSymbol"] as Symbol;
            _resultGraphics = MyMapView.GraphicsOverlays["resultsOverlay"];

            CreateFeatureLayers();
        }

        // Creates a feature layer from a local .geodatabase file
        private async void CreateFeatureLayers()
        {
            try
            {
                var gdb = await Geodatabase.OpenAsync(GDB_PATH);

                var table = gdb.FeatureTables.First(ft => ft.Name == "US-States");
                _statesLayer = new FeatureLayer() { ID = table.Name, FeatureTable = table };
                MyMapView.Map.Layers.Insert(1, _statesLayer);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error creating feature layer: " + ex.Message, "Samples");
            }
        }

        // Unions feature geometries with a user defined polygon.
        private async void UnionButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _resultGraphics.Graphics.Clear();

                // wait for user to draw a polygon
                var poly = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon);

                // Take account of WrapAround
                var normalizedPoly = GeometryEngine.NormalizeCentralMeridian(poly) as Polygon;

                // get intersecting features from the feature layer
                SpatialQueryFilter filter = new SpatialQueryFilter();
                filter.Geometry = GeometryEngine.Project(normalizedPoly, _statesLayer.FeatureTable.SpatialReference);
                filter.SpatialRelationship = SpatialRelationship.Intersects;
                filter.MaximumRows = 52;
                var stateFeatures = await _statesLayer.FeatureTable.QueryAsync(filter);

                // Union the geometries and add to graphics layer
                var states = stateFeatures.Select(feature => feature.Geometry);
                var unionPolys = states.ToList();


                var unionPoly = GeometryEngine.Union(unionPolys);
                var unionGraphic = new Graphic(unionPoly, _fillSymbol);

                _resultGraphics.Graphics.Add(unionGraphic);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Union Error: " + ex.Message, "Union Sample");
            }
        }
    }
}
Feedback on this topic?