Feature Layer Edit Geometry

Download Samples Repository

Description

Demonstrates how to update feature geometry in feature layer.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FeatureLayerEditGeometry"
             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>
        <esri:MapView x:Name="MyMapView"
                      MapViewTapped="MyMapView_MapViewTapped">            
            <esri:Map InitialViewpoint="-13054165,3850112,-13027133,3863559">
                <esri:ArcGISTiledMapServiceLayer ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                <!-- Makes all features at current extent available for editing -->
                <esri:FeatureLayer ID="Incidents">
                    <esri:ServiceFeatureTable ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/2"/>
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>
        <!-- Used for geometry editing -->
        <Border Background="White"
                BorderBrush="Black"
                BorderThickness="1"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="30"
                Padding="20">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <StackPanel Orientation="Horizontal"
                        DataContext="{Binding ElementName=MyMapView, Path=Editor}">
                <Button Content="Edit"
                        Margin="2"
                        IsEnabled="False"
                        x:Name="EditButton"
                        Click="EditButton_Click" />
                <Button Content="Delete Vertex"
                        Margin="2"
                        Command="{Binding DeleteVertex}" />
                <Button Content="Undo"
                        Margin="2"
                        Command="{Binding Undo}" />
                <Button Content="Redo"
                        Margin="2"
                        Command="{Binding Redo}" />
                <Button Content="Complete"
                        Margin="2"
                        Command="{Binding Complete}" />
                <Button Content="Cancel"
                        Margin="2"
                        Command="{Binding Cancel}" />
            </StackPanel>
        </Border>
        <Border Background="White"
                BorderBrush="Black"
                BorderThickness="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Margin="30"
                Padding="20">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <TextBlock Text="Click on a feature to select and edit its geometry."
                       Width="200"
                       TextAlignment="Left"
                       Margin="30,20,20,30"
                       TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates how to update feature geometry in feature layer.
    /// </summary>
    /// <title>Feature Layer Edit Geometry</title>
    /// <category>Editing</category>
    public partial class FeatureLayerEditGeometry: UserControl
    {
        public FeatureLayerEditGeometry()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Selects feature for editing.
        /// </summary>
        private async void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
        {
            // Ignore tap events while in edit mode so we do not interfere with edit geometry.
            if (MyMapView.Editor.IsActive)
                return;
            var layer = MyMapView.Map.Layers["Incidents"] as FeatureLayer;
            layer.ClearSelection();
            SetGeometryEditor();
            string message = null;
            try
            {
                // Performs hit test on layer to select feature.
                var features = await layer.HitTestAsync(MyMapView, e.Position);
                if (features == null || !features.Any())
                    return;
                var featureID = features.FirstOrDefault();
                layer.SelectFeatures(new long[] { featureID });
                var feature = await layer.FeatureTable.QueryAsync(featureID);
                SetGeometryEditor(feature);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            if (!string.IsNullOrWhiteSpace(message))
                MessageBox.Show(message);
        }

        /// <summary>
        /// Prepares GeometryEditor for editing.
        /// </summary>
        private void SetGeometryEditor(Feature feature = null)
        {
            EditButton.Tag = feature;
            EditButton.IsEnabled = feature == null ? false : true;
        }
        
        /// <summary>
        /// Enables geometry editing and submits geometry edit back to the server.
        /// </summary>
        private async void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var feature = (Feature)EditButton.Tag;
            var layer = MyMapView.Map.Layers["Incidents"] as FeatureLayer;
            var table = (ArcGISFeatureTable)layer.FeatureTable;

            // Hides feature from feature layer while its geometry is being modified.
            layer.SetFeatureVisibility(layer.SelectedFeatureIDs, false);

            string message = null;
            try
            {
                // Enables geometry editing and update its geometry 
                // using GeometryEngine to correct ring orientation.
                var geometry = await MyMapView.Editor.EditGeometryAsync(feature.Geometry);
                feature.Geometry = GeometryEngine.Simplify(geometry);
                await table.UpdateAsync(feature); 

                if (table.HasEdits)
                {
                    if (table is ServiceFeatureTable)
                    {
                        var serviceTable = (ServiceFeatureTable)table;
                        // Pushes geometry edits back to the server.
                        var result = await serviceTable.ApplyEditsAsync();
                        if (result.UpdateResults == null || result.UpdateResults.Count < 1)
                            return;
                        var updateResult = result.UpdateResults[0];
                        if (updateResult.Error != null)
                            message = updateResult.Error.Message;
                    }
                }
            }
			catch (TaskCanceledException)
			{
				// Ignore TaskCanceledException - usually happens if the editor gets cancelled or restarted
			}
            catch (Exception ex)
            {
                message = ex.Message;
            }
            finally
            {
                layer.SetFeatureVisibility(layer.SelectedFeatureIDs, true);
                layer.ClearSelection();
                SetGeometryEditor();
            }
            if (!string.IsNullOrWhiteSpace(message))
                MessageBox.Show(message);
        }
    }
}
Feedback on this topic?