Learn how to execute a SQL query to return features from a feature layer based on spatial and attribute criteria.
A feature layer can contain a large number of features stored in ArcGIS. You can query a layer to access a subset of its features using any combination of spatial and attribute criteria. You can control whether or not each feature's geometry is returned, as well as which attributes are included in the results. Queries allow you to return a well-defined subset of your hosted data for analysis or display in your app.
In this tutorial, you'll write code to perform SQL queries that return a subset of features in the LA County Parcel feature layer (containing over 2.4 million features). Features that meet the query criteria are selected in the map.
Prerequisites
The following are required for this tutorial:
An ArcGIS account to access API keys. If you don't have an account, sign up for free.
To start this tutorial, complete the Display a map tutorial or download
and unzip the solution.
Open the display_a_map project in Qt Creator.
If you downloaded the Display a map solution, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
Go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In the Projects window, in the Sources folder, open the main.cpp file.
Modify the code to set the API key. Paste the API key, acquired from your dashboard, between the quotes. Then save and close the file.
Add a feature layer, query features, and select results
You will add three functions to add a feature layer from ArcGIS online, execute a query of that layer, and create a features list from the query result.
Add code to create the addFeatureLayer function. This function adds the LA_County_Parcels feature layer hosted on ArcGIS Online to the map.
Add the following function to implement the query. The runQuery function executes a query of the parcels layer. This function creates a QueryParameters object that includes the currently visible map extent and a SQL expression, and then executes the query on the parcels table. Note that the SQL expression will come from a ComboBox that you will implement in a coming step.
Next, add the selectFeatures function. When a query completes, this code iterates the returned results and adds those features to a list. That list is then used to select features in the parcels layer.
By default, selected features in the map view are displayed in cyan. Modify the setMapView function to call your new functions and also set the color for displaying selected parcel layer features (or any other selection) in your map view. Then save the file.
Show a list of query expressions for the user to choose from
You will create a UI that allows the user to choose from a list of predefined query expressions. The expressions are presented in a ComboBox control and when the user makes a choice, the expression is passed to the runQuery method to find parcels that meet the selected criterion within the currently visible map extent.
In Projects, double-click Resources > qml\qml.qrc/qml/Display_a_mapForm.qml to open the file.
Add the following code to create a ComboBox control to execute a predefined query expression.
The app loads with the map centered on the Santa Monica Mountains in California with the parcels feature layer displayed. Choose an attribute expression, and parcels in the current extent that meet the selected criteria will display in the specified selection color.