Query for layers and features

The QueryTask allows you to retrieve features from a single layer or table in an ArcGIS for Server map service or feature service. The task also allows you to query metadata of an ArcGIS for Server image service that is based on a mosaic dataset.

A layer in a map or feature service can be one of two types: a vector layer, also known as a feature layer, or a raster layer. A vector layer stores geographic data in the form of shapes or vectors. A raster layer stores data in the form of imagery. The QueryTask works with vector layers, not raster layers. In some cases, a vector layer may not store any geographic data and may only contain attribute data. In such cases, the layer can be considered a table. The QueryTask also works with tables. When a vector layer is queried, the results are features that contain geometry information. When a table is queried, the results are features without geometry information.

ArcGIS for Server map, feature, and image services are accessible on the web as SOAP and REST web services. These services provide the operations that the QueryTask relies on. For example, a layer in a map service provides Query and Query Related Records operations. While publishing a service, the administrator can disable some operations. Verify that the REST resource you intend to use supports the necessary operations.

You can use the ArcGIS Server Services Directory to find details about the service you want to use, and you can determine which query operations are supported.

Query operations

If you're familiar with the ArcGIS Server REST API, you can invoke query operations directly from a browser using the Services Directory.

Use the QueryTask in your application

To instantiate a QueryTask, provide a URL to REST resources that support query operations. If the service is secured, provide the credentials that can be used to access the service.

ServiceREST resource supporting query operationsURL example

Map service

Vector layer or table

http://<server>/<instance>/services/<service>/MapServer/<layer_or_table_ID>

Feature service

Vector layer or table

http://<server>/<instance>/services/<service>/FeatureServer/<layer_or_table_ID>

Image service

Image service

http://<server>/<instance>/services/<service>/ImageServer

The QueryTask does not specify the user interface; therefore, you need to implement the functionality to allow users to define the user input and display the geometries and attributes of the results in the application. There are numerous ways that the user can provide inputs to the process with the user interface of your application. The user might select features on a map, type a value in a text box, or select a value from a list box. The results can be displayed in various ways, such as in a DataGrid, populating a list box, or displaying as graphics in a graphics layer.

Use the QueryTask with online data

To use the QueryTask with online data, you must pass the URL of a service, or of a layer in a service. To find the URL, you can use the ArcGIS Services Directory. The following code snippet shows how to create a QueryTask for the states layer in the ESRI_Census_USA map service on ArcGIS Online:

QueryTask {
        id: queryTask
        url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"
}

Input parameters

Before using one of the QueryTask's operations, you need to define the input parameters. Input parameters vary depending on the operation. The Query object is used to define the execution parameters for QueryTasks. The following is an example of initializing a Query component.

Create a Query component with the following properties:

  • geometry—Define the geometry to query.
  • returnGeometry—Return the geometry.
  • where—Conditions that features must satisfy to be returned.
  • outfields—The actual fields to return.
Query {
    id: queryParams
    geometry: aoiPolygon
    returnGeometry: true
    where: "POP2000 > 350000"
    outFields: ["STATE_NAME","POP2000"]
}

Results

To access the results of an asynchronous query operation, the QueryTask must be complete. Use the QueryComplete value of the queryTaskStatus property to trigger use of the Query result.

QueryTask {
        id: queryTask
        url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"

        onQueryTaskStatusChanged: {

            if (queryTaskStatus === Enums.QueryTaskStatusCompleted)
            {
                tableModel.clear();
                queryResultText.text = "";
                if (queryResult.graphics.length === 0)
                    queryResultText.text = "No records found!";

                // add attributes of all states obtained after executing the query.
                for (var count = 0; count < queryResult.graphics.length; count++)
                {
                    queryTable.model.insert(count,
                                                   {"STATE_NAME": queryResult.graphics[count].attributes["STATE_NAME"].toString(),
                                                    "SUB_REGION": queryResult.graphics[count].attributes["SUB_REGION"].toString(),
                                                    "STATE_FIPS": queryResult.graphics[count].attributes["STATE_FIPS"].toString(),
                                                    "STATE_ABBR": queryResult.graphics[count].attributes["STATE_ABBR"].toString(),
                                                    "POP2000": queryResult.graphics[count].attributes["POP2000"].toString(),
                                                    "POP2007": queryResult.graphics[count].attributes["POP2007"].toString(),});

                }
            }

            else if (queryTaskStatus === Enums.QueryTaskStatusErrored)
            {
                console.log("Query Error: ", queryError.message);
            }
        }
    }

There are various output interfaces available to display the results in your 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 graphics layer.