Click or drag to resize
Code Example - FeatureLayer_SelectFeaturesByRectangle

Demonstrates selecting features in a FeatureLayer by dragging a rectangle across the map. Features are then highlighted in the FeatureLayer and their selected ID values are displayed in a Listbox.

Code Example
Feature Layer Select Features By Rectangle

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="FeatureLayer_SelectFeaturesByRectangle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid>

        <!-- Create a simple renderer with a black, diagonal cross hatch pattern. The outline of the polygon is black. -->
        <Grid.Resources>
            <esri:SimpleRenderer x:Key="mySimpleRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleFillSymbol Color="Black"  Style="DiagonalCross">
                        <esri:SimpleFillSymbol.Outline>
                            <esri:SimpleLineSymbol Color="Black" Width="1" Style="Solid"/>
                        </esri:SimpleFillSymbol.Outline>
                    </esri:SimpleFillSymbol>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
        </Grid.Resources>

        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical">

                <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
                <TextBlock Height="58" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- Add a button to allow the user to select features in the FeatureLayer. -->
                <Button Content="Click me to draw a rectangle on the map to select features" Margin="3,3" Width="785"
                        x:Name="ButtonDrawRectangle" Click="ButtonDrawRectangle_Click"/>

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="600" Height="480" VerticalAlignment="Top" Margin="2,2,2,2">

                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1" >

                        <!-- 
                        Add a FeatureLayer. Define the renderer for the polygons. Set the ServiceFeatureTable  ServiceUri to 
                        public ArcGIS Server web service. Because no where clause is set on the ServiceFeatureTable, all 
                        records in the FeatureLayer will be drawn.
                        -->
                        <esri:FeatureLayer ID="USA" Renderer="{StaticResource mySimpleRenderer}" >
                            <esri:FeatureLayer.FeatureTable>
                                <esri:ServiceFeatureTable 
                                    ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
                            </esri:FeatureLayer.FeatureTable>
                        </esri:FeatureLayer>

                    </esri:Map>
                </esri:MapView>

                <!-- Display the FeatureLayer feature ID values that were selected by the user in the map. -->
                <StackPanel Orientation="Vertical">
                    <Label Content="Selected Feature IDs:"/>
                    <ListBox x:Name="ListBoxSelectedIds" Width="187" Height="456" />
                </StackPanel>

            </StackPanel>
        </StackPanel>
    </Grid>

</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "FeatureLayer_SelectFeaturesByRectangle.MainWindow" to be just "MainWindow".

// NOTE: This example code only works when the map is in its default orientation (meaning the map is not rotated). If the map were rotated
// then the example code would need to be altered significantly to account for the System.Windows.Rect that is used by the FeatureLayer.HitTestAsync
// Method. For a rotated map scenario, consider using ServiceFeatureTable.QueryAsync(QueryFilter) to obtain attribute records
// that could be selected.

namespace FeatureLayer_SelectFeaturesByRectangle
{

    public partial class MainWindow : System.Windows.Window
    {

        public MainWindow()
        {
            // This call is required by the designer.
            InitializeComponent();

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads a FeatureLayer will automatically be added to the Map (it was specified in XAML). Click the Button to " + 
                "drag a rectangle across the map and select some state features. The features that are selected will highlight and their ID values will display in the " + 
                "Listbox. Clicking the button again will reset the feature selection process.";

            // Set the initial extent of the Map to the continental US.
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(4269);
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-126.95, 12.9, -66.1, 61.59, mySpatialReference);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);
        }


        private async void ButtonDrawRectangle_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                // Get the FeatureLayer from the Map.
                Esri.ArcGISRuntime.Controls.Map myMap = MapView1.Map;
                Esri.ArcGISRuntime.Layers.LayerCollection myLayerCollection = myMap.Layers;
                Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = (Esri.ArcGISRuntime.Layers.FeatureLayer)myLayerCollection[0];

                // Clear out an existing selected features in the FeatureLayer and the ID values in the ListBox.
                myFeatureLayer.ClearSelection();
                ListBoxSelectedIds.ItemsSource = null;

                // Get the Editor associated with the MapView. The Editor enables drawing and editing graphic objects.
                Esri.ArcGISRuntime.Controls.Editor myEditor = MapView1.Editor;

                // Get the Envelope that the user draws on the Map. Execution of the code stops here until the user is done drawing the rectangle.
                Esri.ArcGISRuntime.Geometry.Geometry myGeometry = await myEditor.RequestShapeAsync(Esri.ArcGISRuntime.Controls.DrawShape.Rectangle);
                Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = myGeometry.Extent; // The Extent of the returned geometry should be exactly the same shape as the DrawShape.Rectangle.

                if (myEnvelope != null)
                {
                    // Get the lower-left MapPoint (real world coordinates) from the Envelope the user drew on the Map and then translate it into 
                    // a Microsoft Point object.
                    Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint1 = new Esri.ArcGISRuntime.Geometry.MapPoint(myEnvelope.Extent.XMin, myEnvelope.YMin);
                    System.Windows.Point myWindowsPoint_1 = MapView1.LocationToScreen(myMapPoint1);

                    // Get the upper-right MapPoint (real world coordinates) from the Envelope the user drew on the Map and then translate it into 
                    // a Microsoft Point object.
                    Esri.ArcGISRuntime.Geometry.MapPoint myMapPoint2 = new Esri.ArcGISRuntime.Geometry.MapPoint(myEnvelope.Extent.XMax, myEnvelope.YMax);
                    System.Windows.Point myWindowsPoint_2 = MapView1.LocationToScreen(myMapPoint2);

                    // Create a Windows Rectangle from the Windows Point objects.
                    System.Windows.Rect myWindowsRect = new System.Windows.Rect(myWindowsPoint_1, myWindowsPoint_2);

                    // Get the FeatureTable from the FeatureLayer.
                    Esri.ArcGISRuntime.Data.FeatureTable myFeatureTable = myFeatureLayer.FeatureTable;

                    // Get the number of records in the FeatureTable.
                    long myRowCount = myFeatureTable.RowCount;

                    // Use the FeatureLayer.HitTestAsync Method to retrieve the FeatureLayer IDs that are within or cross the envelope that was
                    // drawn on the Map by the user. It is important to note that by passing in the variable myRowCount (which is the maximum
                    // number of features in the FeatureLayer), you are able to select up to the number of features in the FeatureLayer. If
                    // you were to leave off this optional last parameter then the HitTestAsync would only return one record! 
                    long[] myFeatureLayerRowIDs = await myFeatureLayer.HitTestAsync(MapView1, myWindowsRect, System.Convert.ToInt32(myRowCount));

                    if (myFeatureLayerRowIDs.Length > 0)
                    {
                        // We have at least one record in the FeatureLayer selected.

                        // Cause the features in the FeatureLayer to highlight (cyan) in the Map.
                        myFeatureLayer.SelectFeatures(myFeatureLayerRowIDs);

                        // Display the selected FeatureLayer ID's in the ListBox.
                        ListBoxSelectedIds.ItemsSource = myFeatureLayerRowIDs;
                    }
                }
            }
            catch (System.Threading.Tasks.TaskCanceledException)
            {
                // This exception occurred because the user has already clicked the button but has not drawn a rectangle on the Map yet.
                System.Windows.MessageBox.Show("Drag a rectangle across the map to select some features.");
            }
            catch (System.Exception ex)
            {
                // We had some kind of issue. Display to the user so it can be corrected.
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
    }
}