Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Cut

Download Samples Repository

Description

Example of using the GeometryEngine.Cut method to cut feature geometries with a given polyline. To use this sample, the user draws a cut polyline intersecting the feature polygons and the system then cuts the intersecting feature geometries and displays the resulting polygons in a graphics overlay on the map.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.CutGeometry"
             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:SimpleLineSymbol x:Key="CutLineSymbol" Color="Red" Style="Solid" Width="1" />
            <esri:SimpleFillSymbol x:Key="CutFillSymbol" 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 'Cut Geometries' button to digitize a polyline and cut the polygon feature geometries with the digitized polyline. Resulting polygons will be shown as blue graphics."
                           Width="400" TextWrapping="Wrap" />
                <Button Content="Cut Geometries" Margin="12,12,12,0" HorizontalAlignment="Center" Click="CutButton_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 cutting polyline 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.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Example of using the GeometryEngine.Cut method to cut feature geometries with a given polyline. To use this sample, the user draws a cut polyline intersecting the feature polygons and the system then cuts the intersecting feature geometries and displays the resulting polygons in a graphics overlay on the map.
    /// </summary>
    /// <title>Cut</title>
	/// <category>Geometry</category>
	public partial class CutGeometry : UserControl
    {
        private const string GDB_PATH = @"..\..\..\samples-data\maps\usa.geodatabase";

        private Symbol _cutLineSymbol;
        private Symbol _cutFillSymbol;
        private FeatureLayer _statesLayer;
		private GraphicsOverlay _resultGraphicsOverlay;

        /// <summary>Construct Cut Geometry sample control</summary>
        public CutGeometry()
        {
            InitializeComponent();

            _cutLineSymbol = layoutGrid.Resources["CutLineSymbol"] as Symbol;
            _cutFillSymbol = layoutGrid.Resources["CutFillSymbol"] as Symbol;
			_resultGraphicsOverlay = 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.Add(_statesLayer);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error creating feature layer: " + ex.Message, "Samples");
            }
        }

        // Cuts feature geometries with a user defined cut polyline.
        private async void CutButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
				_resultGraphicsOverlay.Graphics.Clear();

                // wait for user to draw cut line
                var cutLine = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polyline, _cutLineSymbol) as Polyline;

				Polyline polyline = GeometryEngine.NormalizeCentralMeridian(cutLine) as Polyline;

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

                // Cut the feature geometries and add to graphics layer
                var states = stateFeatures.Select(feature => feature.Geometry);
                var cutGraphics = states
                    .Where(geo => !GeometryEngine.Within(polyline, geo))
                    .SelectMany(state => GeometryEngine.Cut(state, polyline))
                    .Select(geo => new Graphic(geo, _cutFillSymbol));

				_resultGraphicsOverlay.Graphics.AddRange(cutGraphics);
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                MessageBox.Show("Cut Error: " + ex.Message, "Cut Geometry");
            }
        }
    }
}
Feedback on this topic?