Spatial Query

Download Samples Repository

Description

This sample demonstrates how to spatially search your data using a QueryTask with its geometry attribute set. The sample spatially queries a map service using the buffer geometry around a user's click point and displays the results in a graphics layer on the map.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.SpatialQuery"
             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:PictureMarkerSymbol x:Key="DefaultMarkerSymbol"/>
            <esri:SimpleRenderer x:Key="DefaultRenderer" Symbol="{StaticResource DefaultMarkerSymbol}" />
            
            <esri:SimpleFillSymbol x:Key="BufferSymbol" Color="#66BB0000">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="#88CC0000" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            <esri:SimpleRenderer x:Key="BufferRenderer" Symbol="{StaticResource BufferSymbol}" />
            
            <esri:SimpleFillSymbol x:Key="ParcelSymbol" Color="#440000FF">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Blue" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            <esri:SimpleRenderer x:Key="ParcelRenderer" Symbol="{StaticResource ParcelSymbol}" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True" MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-9270434,5246977,-9269261,5247570">
				<esri:ArcGISTiledMapServiceLayer 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="parcelOverlay" RenderingMode="Static" Renderer="{StaticResource ParcelRenderer}"/>
				<esri:GraphicsOverlay ID="bufferOverlay" Renderer="{StaticResource BufferRenderer}"/>
				<esri:GraphicsOverlay ID="graphicsOverlay" Renderer="{StaticResource DefaultRenderer}"/>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

        <Border Background="White" BorderBrush="Black" BorderThickness="1" 
				Margin="30" Padding="20"
                HorizontalAlignment="Right" VerticalAlignment="Top">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
            <TextBlock Text="Click on the map to set a location. A buffer will be used to display parcels within 100 meters of the location." 
                       Width="200" HorizontalAlignment="Left" TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how to spatially search your data using a QueryTask with its geometry attribute set. The sample spatially queries a map service using the buffer geometry around a user's click point and displays the results in a graphics layer on the map.
    /// </summary>
    /// <title>Spatial Query</title>
    /// <category>Tasks</category>
    /// <subcategory>Query</subcategory>
    public partial class SpatialQuery : UserControl
    {
        /// <summary>Construct Spatial Query sample control</summary>
        public SpatialQuery()
        {
            InitializeComponent();

            InitializePictureMarkerSymbol();
        }

        // Initialize PushPin symbol
        private async void InitializePictureMarkerSymbol()
        {
            try
            {
                var pms = layoutGrid.Resources["DefaultMarkerSymbol"] as PictureMarkerSymbol;
				await pms.SetSourceAsync(new Uri("pack://application:,,,/ArcGISRuntimeSamplesDesktop;component/Assets/RedPushpin.png"));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        // buffer the click point, query the map service with the buffer geometry as the filter and add graphics to the map
        private async void MyMapView_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
        {
            try
            {
                var graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];
                if (!(graphicsOverlay.Graphics.Count == 0))
                    graphicsOverlay.Graphics.Clear();

                graphicsOverlay.Graphics.Add(new Graphic() { Geometry = e.Location });

                var bufferOverlay = MyMapView.GraphicsOverlays["bufferOverlay"];
                if (!(bufferOverlay.Graphics.Count == 0))
                    bufferOverlay.Graphics.Clear();

                var bufferResult = GeometryEngine.Buffer(e.Location, 100);
                bufferOverlay.Graphics.Add(new Graphic() { Geometry = bufferResult });

                var queryTask = new QueryTask(
                    new Uri("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer/2"));
                var query = new Query("1=1")
                {
                    ReturnGeometry = true,
                    OutSpatialReference = MyMapView.SpatialReference,
                    Geometry = bufferResult
                };
                query.OutFields.Add("OWNERNME1");

                var queryResult = await queryTask.ExecuteAsync(query);
                if (queryResult != null && queryResult.FeatureSet != null)
                {
                    var resultOverlay = MyMapView.GraphicsOverlays["parcelOverlay"];
                    if (!(resultOverlay.Graphics.Count == 0))
                        resultOverlay.Graphics.Clear();

                    resultOverlay.Graphics.AddRange(queryResult.FeatureSet.Features.OfType<Graphic>());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Spatial Query Sample");
            }
        }
    }
}
Feedback on this topic?