0

I have a map which has a GraphicsOverlay with various points. I have given the user the ability to select a subset of the points by drawing a polygon using the SketchEditor. How can I determine which points have been selected?

Here is a subset of the code to set up the map:

private GraphicsOverlay graphicsOverlayLow;

// Graphics overlay to host sketch graphics
private GraphicsOverlay _sketchOverlay;

var symbolLow = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Colors.Green, 10d);

graphicsOverlayLow = new GraphicsOverlay() { Renderer = new SimpleRenderer(symbolLow) };

foreach (var graphic in graphicListLow)  // graphicListLow is a List of Points
    graphicsOverlayLow.Graphics.Add(graphic);

MyMapView.GraphicsOverlays = new GraphicsOverlayCollection();

MyMapView.GraphicsOverlays.Add(graphicsOverlayLow);

_sketchOverlay = new GraphicsOverlay();
MyMapView.GraphicsOverlays.Add(_sketchOverlay);

I have two buttons, one for starting the drawing of the polygon and one to click when done (this follows the esri example for the SketchEditor). The code for starting is as follows:

private async void SelectButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        // Let the user draw on the map view using the chosen sketch mode
        SketchCreationMode creationMode = SketchCreationMode.Polygon;
        Esri.ArcGISRuntime.Geometry.Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, true);

        // Create and add a graphic from the geometry the user drew
        Graphic graphic = CreateGraphic(geometry);
        _sketchOverlay.Graphics.Add(graphic);
    }
    catch (TaskCanceledException)
    {
        // Ignore ... let the user cancel drawing
    }
    catch (Exception ex)
    {
        // Report exceptions
        MessageBox.Show("Error drawing graphic shape: " + ex.Message);
    }
}


private Graphic CreateGraphic(Esri.ArcGISRuntime.Geometry.Geometry geometry)
{
    // Create a graphic to display the specified geometry
    Symbol symbol = null;
    switch (geometry.GeometryType)
    {
        // Symbolize with a fill symbol
        case GeometryType.Envelope:
        case GeometryType.Polygon:
            {
                symbol = new SimpleFillSymbol()
                {
                    Color = Colors.Red,
                    Style = SimpleFillSymbolStyle.Solid,
                };
                break;
            }

Here is the handler for the routine that is called when the user clicks the button signaling that they are done drawing the polygon. This is where I want to determine which points have been selected.

private void CompleteButton_Click(object sender, RoutedEventArgs e)
{
    // Cancel execution of the sketch task if it is already active
    if (MyMapView.SketchEditor.CancelCommand.CanExecute(null))
    {
        MyMapView.SketchEditor.CancelCommand.Execute(null);
    }
}

Note that I am using the 100.4 SDK for WPF.

enter image description here

Richard
  • 125
  • 1
  • 9

2 Answers2

0

This can be accomplished by a spatial query. You will have to use the geometry returned by the sketch editor and use it to perform a spatial query on the layer(s) using geometry filter.

Esri.ArcGISRuntime.Geometry.Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, true);

var queryparameters = new QueryParameters()
            {
                Geometry = geometry,
                SpatialRelationship = SpatialRelationship.Intersects
            };
await layer.SelectFeaturesAsync(queryparameters, Esri.ArcGISRuntime.Mapping.SelectionMode.New);
0

You can use GeometryEngine.Intersects method to check when point graphics intersect, touch, cross the selection polygon. https://community.esri.com/message/826699-re-determine-points-that-are-under-a-sketchoverlay-in-a-map?commentID=826699#comment-826699

jnery
  • 11
  • 1
  • Hi jnery. Thanks for your reply. I am getting an exception, "Value does not fall within the expected range" when I call GeometryEngine.Intersects in the handler for the button that the user presses when they are done selecting points on the map. g.IsSelected = GeometryEngine.Intersects(sketchExtent, g.Geometry.Extent); Any idea what causes this exception? Thanks – Richard Jan 24 '19 at 19:14
  • Hi again jnery. I did need to call GeometryEngine.Project which you mentioned in your response to prevent the "Value does not fall within the expected range" exception. My selection is now working! :>) – Richard Jan 24 '19 at 20:01