Querying the Map

In the ArcGIS Runtime SDK for WPF the Query task allows you to retrieve feature geometries and attributes from a local or online service in response to a query containing a WHERE clause and/or spatial query. To execute a Query task, you need to instantiate the Query task, specify a URL that represents the numeric ID of the layer that will be queried, wire up the task's event handlers, initialize the task's query parameters, then call the task's execution method.

Using the Query Task in your application

The Query task does not specify the user interface, therefore you will need to implemented the functionality to allow the users to define the user input and display the geometries and attributes of the results in the WPF Application. The best practice is to separate an application's presentation layer from its business logic. In WPF, XAML is intended to house the presentation layer, while the code-behind is for an application's business logic. There are numerous ways in which the inputs to the process can be provided by the user through the interface of your WPF application. It might be by selecting features on a map, typing in a value in a textbox or by selecting a value from a listbox. The results can be displayed in various ways such as in a DataGrid, through MapTips, by populating a listbox and by displaying on the map as a graphics layer.

Using the Query Task with online Data

To use the Query task with online data you will be required to pass the URL of an online service or layer. To find the URL, you can use the ArcGIS Services Directory. An example is below:

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

Using the Query Task with local data

Create a new ESRI.ArcGIS.Client.Local.LocalMapService with the path to an ArcGIS map package:

LocalMapService.GetServiceAsync("Path to datasource", ms =>
{  
{
});

Declare and instantiate the ESRI.ArcGIS.Client.Tasks.QueryTask class. Set the map service layer that the task will perform the query on by passing the local map service's URL and the layer ID to the task's constructor (or by setting the Url property.

LocalMapService.GetServiceAsync("Path to datasource", ms =>
{  
QueryTask queryTask = new QueryTask(ms.UrlMapService + "/0");
{
});

Input Parameters

Before using one of the Query task's operations, you will need to define the operation's input parameters. These parameters will vary depending on the operation. The Query object is used to define the execution parameters for Query tasks. Below is an example of initializing the parameters for the Query task:

Declare a Query object and instantiate it.

Query query = new Query();

If you want to draw the query results on the map, specify that the query return geometry with the results.

query.ReturnGeometry = true;

Define the fields to return with the query results. Here you will specify that the query return the city name and population fields. Note that only the fields required by your application should be returned so that any network traffic is minimized.

query.OutFields.AddRange(new string[] { "AREANAME", "POP2000" });

Specify the where clause for the query. The where clause defines the conditions that features must satisfy to be returned in the query results. Any legal SQL where clause operating on the fields in the layer is allowed, for example: where=POP2000 > 350000 . You could also specify it as the text contained in a WPF Texbox control.

query.Where = QueryTextBox.Text;

Results

The Query task has a property named LastResult that contains the most recently generated set of results. This property is useful for using WPF data binding to automatically update results displays whenever a task has executed or for storing the set of results for later reference.

To access the results for a query operation a handler will need to be declared for the QueryTask_ExecuteCompleted event. This handler will be invoked when a query operation is complete. A FeatureSet containing the features that satisfy the query is passed to the handler's args parameter.

There are various output interfaces available to display the results in your WPF application. To display the results on the map you can loop through the features found, create a graphic for each feature and display the graphic on the map by adding it to a graphic layer.

1/27/2015