Can the SketchEditor be used to update existing Geometries of features?

1078
4
08-04-2018 03:49 AM
VijitWadhwa
New Contributor III

Hi,

Does the SketchEditor allows editing only on the geometries created by SketchEditor itself ?

Can't we update the geometries of existing features in static FeatureCollection using SketchEdior ?

0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor

SketchEditor can also be used for editing existing geometries in a FeatureCollectionLayer. You can look at the following sample.

Code below is applicable to other FeatureTable types. However, identify logic here is specific to FeatureCollectionLayer where feature is expected to be part of SubLayerResult.

However you get the feature, from an identify or query, you can hide the original feature and pass along its geometry to SketchEditor. When edit is completed, you can update the feature and reset its visibility on the layer. You can either wire up SketchEditor.CompleteCommand or programmatically call SketchEditor.Stop. 

public MainWindow()
{
    InitializeComponent();

    var map = new Map(Basemap.CreateTopographic());
    var g = new Graphic()
    {
        Geometry = new Polygon(new MapPoint[] {
                    new MapPoint(0, 0),
                    new MapPoint(-1, 0),
                    new MapPoint(-1, 1),
                    new MapPoint(0, 1)
                }, SpatialReferences.Wgs84),
    };
    g.Attributes["created"] = DateTimeOffset.UtcNow;
    g.Attributes["edited"] = DateTimeOffset.UtcNow;
    var table = new FeatureCollectionTable(new[] { g },
        new[] { Field.CreateDate("created"), Field.CreateDate("edited") })
    {
        Renderer = new SimpleRenderer(new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Red, null))
    };
    var layer = new FeatureCollectionLayer(new FeatureCollection(new[] { table }));
    map.OperationalLayers.Add(layer);
    MyMapView.Map = map;
    MyMapView.GeoViewTapped += OnGeoViewTapped;
    MyMapView.GeoViewDoubleTapped += (s, e) =>
    {
        if (MyMapView.SketchEditor.Geometry != null)
        {
            e.Handled = true;
            MyMapView.SketchEditor.Stop();
        }
    };
}

private async void OnGeoViewTapped(object sender, GeoViewInputEventArgs e)
{
    var result = await MyMapView.IdentifyLayersAsync(e.Position, 2, false);
    var layerResult = result?.FirstOrDefault(r => r.SublayerResults.Any(s => s.GeoElements.Any()))?.SublayerResults?.FirstOrDefault(s => s.GeoElements.Any());
    var layer = layerResult?.LayerContent as FeatureLayer;
    var feature = layerResult?.GeoElements?.FirstOrDefault() as Feature;
    var table = feature?.FeatureTable as FeatureCollectionTable;
    if (feature?.Geometry == null || table == null || layer == null)
        return;
    try
    {
        layer.SetFeatureVisible(feature, false);
        var geometry = feature.Geometry;
        var project = !MyMapView.SpatialReference.IsEqual(geometry.SpatialReference);
        if (project)
        {
            geometry = GeometryEngine.Project(geometry, MyMapView.SpatialReference);
        }
        geometry = await MyMapView.SketchEditor.StartAsync(geometry);
        if(project)
        {
            geometry = GeometryEngine.Project(geometry, feature.Geometry.SpatialReference);
        }
        feature.Geometry = geometry;
        feature.Attributes["edited"] = DateTimeOffset.UtcNow;
        await table.UpdateFeatureAsync(feature);
    }
    catch(TaskCanceledException)
    { }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message, ex.GetType().Name);
    }
    finally
    {
        if(layer != null && feature != null)
            layer.SetFeatureVisible(feature, true);
    }
}
VijitWadhwa
New Contributor III

Hi Jenifer,

Thank You for your help, your guidance is helping me a lot to understand.

But I have one small doubt, when I try to edit any feature like Polygon or Polyline , It allows me to drag only one point out of so many highlighted vertices, I also tried to set the edit configuration of SketchEditor , but I am able to edit using only one point out of all the highlighted vertices.

// Set the sketch editor configuration to allow vertex editing, resizing, and moving
layerSelected.SetFeatureVisible(featureSelected,true);
var config = myMapView.SketchEditor.EditConfiguration;
config.RequireSelectionBeforeDrag = true;
config.AllowRotate = true;
config.VertexEditMode = SketchVertexEditMode.InteractionEdit;
config.AllowVertexEditing = true;
config.ResizeMode = SketchResizeMode.Uniform;
config.AllowMove = true;
var project = !myMapView.SpatialReference.IsEqual(geometry.SpatialReference);
if (project)
{
geometry = GeometryEngine.Project(geometry, myMapView.SpatialReference);
}
geometry = await myMapView.SketchEditor.StartAsync(geometry,SketchCreationMode.FreehandPolygon,config);
if (project)
{
geometry = GeometryEngine.Project(geometry, featureSelected.Geometry.SpatialReference);
}
featureSelected.Geometry = geometry;

await featureSelected.FeatureTable.UpdateFeatureAsync(featureSelected);

How can I allow editing from all the vertices ? As I am able to edit by only one vertex even after setting the edit configuration of Sketch Editor.

Thanks in advance

0 Kudos
JenniferNery
Esri Regular Contributor

I tried your code and it works for me. With RequireSelectionBeforeDrag=True (default), you'll need to select the handle for vertex, scale, rotate, geometry outline before you're able to move them. Maybe you're attempting to drag another vertex while the last one is selected? If you expect the handles to move with pointer or touch gesture move, you can set this to property to false so it does not require you to tap on the handle first. 

VijitWadhwa
New Contributor III

Thankyou so much for the help Jenifer, It worked

0 Kudos