Feature Layer Selection

Download Samples Repository

Description

This sample demonstrates how to select and unselect features in a feature layer using the SelectFeatures, UnSelectFeatures and ClearSelecteion methods of the FeatureLayer class.The sample uses the MapView.RequestShapeAsync method to allow the user to draw a selection rectangle intersecting features that he would like to manage.Once the selection rectangle is returned, a SpatialQueryFilter is used to spatially select features in the feature layer using FeatureLayer.QueryAsync.When the features are identified the selected features list of the feature layer is managed using the FeatureLayer selection management methods.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FeatureLayerSelection"
             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>
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        </Grid.Resources>

		<esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-14675766,2695407,-6733121,6583994,3857">
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />

                <esri:FeatureLayer x:Name="cities" ID="featureLayer">
					<esri:FeatureLayer.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleMarkerSymbol Color="LightBlue" Style="Circle" Size="12">
								<esri:SimpleMarkerSymbol.Outline>
									<esri:SimpleLineSymbol Color="Blue" Width="2" Style="Solid" />
								</esri:SimpleMarkerSymbol.Outline>
							</esri:SimpleMarkerSymbol>
						</esri:SimpleRenderer>
					</esri:FeatureLayer.Renderer>
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0"
                            Where="pop2000 > 200000" OutFields="*" />
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top" 
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<StackPanel>
                <Button Content="Add Selected Features" Click="AddSelectButton_Click" Margin="4"/>
                <Button Content="Remove Selected Features" Click="RemoveSelectButton_Click" Margin="4"/>
                <Button Content="Clear Selected Features" Click="ClearSelectButton_Click" Margin="4"/>

                <TextBlock Text="{Binding ElementName=cities, Path=SelectedFeatureIDs.Count, StringFormat='Selection Count: {0}'}" 
                           Margin="4" HorizontalAlignment="Center" FontWeight="Bold" />
            </StackPanel>
        </Border>

        <Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="25"
                HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <TextBlock Text="Draw a selection rectangle intersecting the points to select / unselect." Margin="8" FontSize="14" 
                       Visibility="{Binding ElementName=MyMapView, Path=Editor.IsActive, Converter={StaticResource BooleanToVisibilityConverter}}" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how to select and unselect features in a feature layer using the SelectFeatures, UnSelectFeatures and ClearSelecteion methods of the FeatureLayer class.  The sample uses the MapView.RequestShapeAsync method to allow the user to draw a selection rectangle intersecting features that he would like to manage.  Once the selection rectangle is returned, a SpatialQueryFilter is used to spatially select features in the feature layer using FeatureLayer.QueryAsync.  When the features are identified the selected features list of the feature layer is managed using the FeatureLayer selection management methods.
    /// </summary>
    /// <title>Feature Layer Selection</title>
	/// <category>Layers</category>
	/// <subcategory>Feature Layers</subcategory>
	public partial class FeatureLayerSelection : UserControl
    {
        /// <summary>Construct Feature Layer Selection sample control</summary>
        public FeatureLayerSelection()
        {
            InitializeComponent();
        }

        private async void AddSelectButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                var featureIds = await FindIntersectingFeaturesAsync();
                cities.SelectFeatures(featureIds);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Selection Error: " + ex.Message, "Feature Layer Selection Sample");
            }
        }

        private async void RemoveSelectButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                var featureIds = await FindIntersectingFeaturesAsync();
                cities.UnselectFeatures(featureIds);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Selection Error: " + ex.Message, "Feature Layer Selection Sample");
            }
        }

        private void ClearSelectButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                cities.ClearSelection();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Selection Error: " + ex.Message, "Feature Layer Selection Sample");
            }
        }

        private async Task<long[]> FindIntersectingFeaturesAsync()
        {
			var rect = await MyMapView.Editor.RequestShapeAsync(DrawShape.Rectangle);

            SpatialQueryFilter filter = new SpatialQueryFilter();
            filter.Geometry = GeometryEngine.Project(rect, cities.FeatureTable.SpatialReference);
            filter.SpatialRelationship = SpatialRelationship.Intersects;
            filter.MaximumRows = 1000;
            var features = await cities.FeatureTable.QueryAsync(filter);

            return features
                .Select(f => Convert.ToInt64(f.Attributes[cities.FeatureTable.ObjectIDField]))
                .ToArray();
        }
    }
}
Feedback on this topic?