Feature layer query

View inC++QMLView on GitHubSample viewer app

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

screenshot

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

  1. Create a ServiceFeatureTable using the URL of a feature service.
  2. Create a QueryParameters with a where clause specified using setWhereClause().
  3. Perform the query using queryFeatures(query) on the service feature table.
  4. When complete, the query will return a FeatureQueryResult which 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

FeatureLayerQuery.cppFeatureLayerQuery.cppFeatureLayerQuery.hFeatureLayerQuery.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// [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

#include "FeatureLayerQuery.h"

#include "Map.h"
#include "MapQuickView.h"
#include "FeatureLayer.h"
#include "Basemap.h"
#include "SpatialReference.h"
#include "ServiceFeatureTable.h"
#include "Viewpoint.h"
#include "Point.h"
#include "SimpleLineSymbol.h"
#include "SimpleFillSymbol.h"
#include "SimpleRenderer.h"
#include "QueryParameters.h"
#include "FeatureQueryResult.h"
#include "Feature.h"
#include <QUrl>
#include <QColor>
#include <QList>
#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 signals
  connectSignals();
}

void FeatureLayerQuery::connectSignals()
{
  // iterate over the query results once the query is done
  connect(m_featureTable, &ServiceFeatureTable::queryFeaturesCompleted, this, [this](QUuid, 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->setViewpointGeometry(features.at(0)->geometry(), 30);
    // set the count for QML property
    m_queryResultsCount = features.count();
    emit queryResultsCountChanged();
  });

  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->queryFeatures(queryParams);
}

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;
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.