Attribute Query

Download Samples Repository

Description

This sample demonstrates performing an attribute query, adding the results to the map and UI, and zooming to the resulting geometry. To use the sample, select a state from the drop-down menu. A QueryTask is used to query a map service layer based on the selected state name. The result is then added as a Graphic to the map and the attributes displayed in a list control.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.AttributeQuery"
             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 x:Name="layoutGrid">
        <Grid.Resources>
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Color="#500000FF">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Width="1" Color="Blue" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

            <esri:SimpleRenderer x:Key="SimpleRenderer" Symbol="{StaticResource DefaultFillSymbol}" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15000000, 2000000, -7000000, 8000000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicsOverlay" Renderer="{StaticResource SimpleRenderer}" />
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>
		
		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<Grid>
				<Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="Query a layer (search for a state)" Margin="0,0,0,6" HorizontalAlignment="Center" />

                <ComboBox x:Name="QueryComboBox" Grid.Row="1" Width="150" SelectionChanged="QueryComboBox_SelectionChanged">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Attributes[STATE_NAME]}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <ProgressBar x:Name="progress" Grid.Row="2" IsIndeterminate="True" Margin="10" Visibility="Collapsed"/>

                <ListView x:Name="resultsGrid" Grid.Row="2" Margin="0,20,0,0" Visibility="Collapsed">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Key}" Width="100" FontWeight="Bold" />
                                <TextBlock Text="{Binding Value}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates performing an attribute query, adding the results to the map and UI, and zooming to the resulting geometry. To use the sample, select a state from the drop-down menu. A QueryTask is used to query a map service layer based on the selected state name. The result is then added as a Graphic to the map and the attributes displayed in a list control.
    /// </summary>
    /// <title>Attribute Query</title>
	/// <category>Tasks</category>
	/// <subcategory>Query</subcategory>
	public partial class AttributeQuery : UserControl
    {
		private GraphicsOverlay _graphicsOverlay;

        /// <summary>Construct Attribute Query sample control</summary>
        public AttributeQuery()
        {
            InitializeComponent();

			_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];
			InitializeComboBox();
        }

        // Load the combobox with state data from the map service
        private async void InitializeComboBox()
        {
            try
            {
                QueryTask queryTask = new QueryTask(
                    new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"));
                Query query = new Query("1=1") { ReturnGeometry = false };
                query.OutFields.Add("STATE_NAME");

                var result = await queryTask.ExecuteAsync(query);

                QueryComboBox.ItemsSource = result.FeatureSet.Features.OrderBy(g => g.Attributes["STATE_NAME"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Attribute Query Sample");
            }
        }

        // Query the map service for the selected state information
        private async void QueryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                progress.Visibility = Visibility.Visible;
                resultsGrid.Visibility = Visibility.Collapsed;
                resultsGrid.ItemsSource = null;
				_graphicsOverlay.Graphics.Clear();

                QueryTask queryTask = new QueryTask(
                    new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"));

                var stateName = (string)(QueryComboBox.SelectedItem as Graphic).Attributes["STATE_NAME"];
                Query query = new Query(string.Format("STATE_NAME = '{0}'", stateName))
                {
                    OutFields = OutFields.All,
                    ReturnGeometry = true,
                    OutSpatialReference = MyMapView.SpatialReference
                };

                var result = await queryTask.ExecuteAsync(query);

                var featureSet = result.FeatureSet;
                if (featureSet != null && featureSet.Features.Count > 0)
                {
                    var graphic = featureSet.Features.First();
					_graphicsOverlay.Graphics.Add(graphic as Graphic);

                    var selectedFeatureExtent = graphic.Geometry.Extent;
                    Envelope displayExtent = selectedFeatureExtent.Expand(1.3);
                    MyMapView.SetView(displayExtent);

                    resultsGrid.ItemsSource = graphic.Attributes;
                    resultsGrid.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Attribute Query Sample");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?