Find features in a feature table which match an SQL query.

Use case
Query expressions can be used in ArcGIS to select a subset of features from a feature table. This is most useful in large or complicated data sets. A possible use case might be on a feature table marking the location of street furniture through a city. A user may wish to query by a TYPE column to return “benches”. In this sample, we query a U.S. state by STATE_NAME from a feature table containing all U.S. states.
How to use the sample
Input the name of a U.S. state into the text field. When you click “search”, a query is performed and the matching features are highlighted or an error is returned.
How it works
- Create a
ServiceFeatureTableusing the URL of a feature service. - Create a
QueryParameterswith a where clause specified usingsetWhereClause(). - Perform the query using
queryFeaturesAsync(query)on the service feature table. - When complete, the query will return a
FeatureQueryResultwhich can be iterated over to get the matching features.
About the data
This sample uses U.S. State polygon features from the USA 2016 Daytime Population feature service.
Relevant API
- FeatureLayer
- FeatureQueryResult
- QueryParameters
- ServiceFeatureTable
Tags
Search and Query
Sample Code
// [WriteFile Name=FeatureLayerQuery, Category=Features]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "FeatureLayerQuery.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "Feature.h"#include "FeatureIterator.h"#include "FeatureLayer.h"#include "FeatureQueryResult.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"#include "Point.h"#include "QueryParameters.h"#include "ServiceFeatureTable.h"#include "SimpleFillSymbol.h"#include "SimpleLineSymbol.h"#include "SimpleRenderer.h"#include "SpatialReference.h"#include "SymbolTypes.h"#include "Viewpoint.h"
// Qt headers#include <QColor>#include <QFuture>#include <QList>#include <QUrl>#include <QUuid>
// STL headers#include <memory>
using namespace Esri::ArcGISRuntime;
FeatureLayerQuery::FeatureLayerQuery(QQuickItem* parent) : QQuickItem(parent){}
FeatureLayerQuery::~FeatureLayerQuery() = default;
void FeatureLayerQuery::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<FeatureLayerQuery>("Esri.Samples", 1, 0, "FeatureLayerQuerySample");}
void FeatureLayerQuery::componentComplete(){ QQuickItem::componentComplete();
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView"); m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);
// Create a map using the topographic basemap m_map = new Map(BasemapStyle::ArcGISTopographic, this); m_map->setInitialViewpoint(Viewpoint(Point(-11e6, 5e6, SpatialReference(102100)), 9e7));
// Set map to map view m_mapView->setMap(m_map);
// create the feature table m_featureTable = new ServiceFeatureTable(QUrl("https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/USA_Daytime_Population_2016/FeatureServer/0"), this); // create the feature layer using the feature table m_featureLayer = new FeatureLayer(m_featureTable, this);
// line symbol for the outline SimpleLineSymbol* outline = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("black"), 2.0f, this); // fill symbol SimpleFillSymbol* sfs = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(255, 255, 0, 153), outline, this); // create the renderer using the symbology created above SimpleRenderer* renderer = new SimpleRenderer(sfs, this); // set the renderer for the feature layer m_featureLayer->setRenderer(renderer); m_featureLayer->setMaxScale(10000);
// add the feature layer to the map m_map->operationalLayers()->append(m_featureLayer);
connect(m_featureTable, &ServiceFeatureTable::loadStatusChanged, this, [this](LoadStatus loadStatus) { loadStatus == LoadStatus::Loaded ? m_initialized = true : m_initialized = false; emit layerInitializedChanged(); });}
bool FeatureLayerQuery::layerInitialized() const{ return m_initialized;}
void FeatureLayerQuery::runQuery(const QString& stateName){ // create a query parameter object and set the where clause QueryParameters queryParams; queryParams.setWhereClause(QString("STATE_NAME LIKE '" + formatStateNameForQuery(stateName) + "%'")); m_featureTable->queryFeaturesAsync(queryParams).then(this, [this](FeatureQueryResult* rawQueryResult) { auto queryResult = std::unique_ptr<FeatureQueryResult>(rawQueryResult);
if (queryResult && !queryResult->iterator().hasNext()) { m_queryResultsCount = 0; emit queryResultsCountChanged(); return; }
// clear any existing selection m_featureLayer->clearSelection(); QList<Feature*> features;
// iterate over the result object while (queryResult->iterator().hasNext()) { Feature* feature = queryResult->iterator().next(this); // add each feature to the list features.append(feature); }
// select the feature m_featureLayer->selectFeatures(features); // zoom to the first feature m_mapView->setViewpointGeometryAsync(features.at(0)->geometry(), 30); // set the count for QML property m_queryResultsCount = static_cast<int>(features.count()); emit queryResultsCountChanged(); });}
QString FeatureLayerQuery::formatStateNameForQuery(const QString& stateName) const{ // format state names as expected by the service, for instance "Rhode Island" if (stateName.isEmpty()) return QString();
const QStringList words = stateName.split(" ", Qt::SkipEmptyParts); QStringList formattedWords;
for (const QString& word : words) { QString formattedWord = word.toLower(); formattedWord[0] = formattedWord[0].toUpper(); formattedWords.append(formattedWord); }
return QString(formattedWords.join(" "));}
int FeatureLayerQuery::queryResultsCount() const{ return m_queryResultsCount;}// [WriteFile Name=FeatureLayerQuery, Category=Features]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifndef FEATURE_LAYER_QUERY_H#define FEATURE_LAYER_QUERY_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class Map; class MapQuickView; class FeatureLayer; class ServiceFeatureTable;}
class FeatureLayerQuery : public QQuickItem{ Q_OBJECT
Q_PROPERTY(bool layerInitialized READ layerInitialized NOTIFY layerInitializedChanged) Q_PROPERTY(int queryResultsCount READ queryResultsCount NOTIFY queryResultsCountChanged)
public: explicit FeatureLayerQuery(QQuickItem* parent = nullptr); ~FeatureLayerQuery() override;
void componentComplete() override; static void init(); Q_INVOKABLE void runQuery(const QString& stateName);
signals: void layerInitializedChanged(); void queryResultsCountChanged();
private: bool layerInitialized() const; int queryResultsCount() const;
private: QString formatStateNameForQuery(const QString& stateName) const;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_featureLayer = nullptr; Esri::ArcGISRuntime::ServiceFeatureTable* m_featureTable = nullptr; bool m_initialized = false; int m_queryResultsCount = 0;};
#endif // FEATURE_LAYER_QUERY_H// [WriteFile Name=FeatureLayerQuery, Category=Features]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
FeatureLayerQuerySample { id: featureLayerQuerySample width: 800 height: 600
// add a mapView component MapView { id: mapView anchors.fill: parent objectName: "mapView"
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
Row { id: findRow
anchors { top: parent.top bottom: mapView.top left: parent.left right: parent.right margins: 5 } spacing: 5
TextField { id: findText
width: parent.width * 0.25 placeholderText: "Enter a state name to select" inputMethodHints: Qt.ImhNoPredictiveText selectByMouse: true validator: RegularExpressionValidator{ regularExpression: /^[a-zA-Z ]*$/ } Keys.onReturnPressed: { // Call C++ invokable function to run the query featureLayerQuerySample.runQuery(findText.text); } }
Button { text: "Find and Select" enabled: featureLayerQuerySample.layerInitialized onClicked: { // Call C++ invokable function to run the query featureLayerQuerySample.runQuery(findText.text); } } }
Dialog { id: errorMsgDialog modal: true x: Math.round(parent.width - width) / 2 y: Math.round(parent.height - height) / 2 standardButtons: Dialog.Ok property alias text : textLabel.text Text { id: textLabel text: "No state named " + findText.text.toUpperCase() + " exists." } }
onQueryResultsCountChanged: { // Use the C++ property to determine if no features were returned if (featureLayerQuerySample.queryResultsCount === 0) errorMsgDialog.visible = true; }}// [Legal]// Copyright 2015 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// sample headers#include "FeatureLayerQuery.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>#include <QSettings>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("FeatureLayerQuery"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample FeatureLayerQuery::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive); #endif
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/Features/FeatureLayerQuery/FeatureLayerQuery.qml"));
view.show();
return app.exec();}