Filter by definition expression or display filter

View on GitHubSample viewer app

Filter features displayed on a map using a definition expression or a display filter.

screenshot

Use case

Definition queries allow you to define a subset of features to work with in a layer by filtering which features are retrieved from the dataset by the layer. This means that a definition query affects not only drawing, but also which features appear in the layer's attribute table and therefore which features can be selected, labeled, identified, and processed by geoprocessing tools.

Alternatively, display filters limit which features are drawn, but retain all features in queries and when processing. Definition queries and display filters can be used together on a layer, but definition queries limit the features available in the layer, while display filters only limit which features are displayed.

In this sample you can filter a dataset of tree quality selecting for only those trees which require maintenance or are damaged.

How to use the sample

Press 'Apply Expression' to limit the features requested from the feature layer to those specified by the SQL query definition expression. Press 'Apply Display Filter' to limit the features shown on the feature layer to those specified by the SQL query without modifying the feature table. Click 'Reset' to remove the definition expression on the feature layer, which returns all the records.

How it works

  1. Create a service feature table from a URL.
  2. Create a feature layer from the service feature table.
  3. Filter features on your feature layer using a DefinitionExpression to view a subset of features and modify the attribute table.
  4. Filter features on your feature layer using a DisplayFilter to view a subset of features without modifying the attribute table.

Relevant API

  • DefinitionExpression
  • DisplayFilter
  • FeatureLayer
  • ServiceFeatureTable

About the data

This map displays point features related to crime incidents such as grafitti and tree damage that have been reported by city residents.

Tags

definition expression, display filter, filter, limit data, query, restrict data, SQL, where clause

Sample Code

FilterByDefinitionExpressionOrDisplayFilter.cppFilterByDefinitionExpressionOrDisplayFilter.cppFilterByDefinitionExpressionOrDisplayFilter.hFilterByDefinitionExpressionOrDisplayFilter.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
172
173
174
175
176
177
178
179
180
181
// [WriteFile Name=FilterByDefinitionExpressionOrDisplayFilter, 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 "FilterByDefinitionExpressionOrDisplayFilter.h"

// ArcGIS Maps SDK headers
#include "Basemap.h"
#include "DisplayFilter.h"
#include "DisplayFilterDefinition.h"
#include "FeatureLayer.h"
#include "LayerListModel.h"
#include "ManualDisplayFilterDefinition.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "Point.h"
#include "QueryParameters.h"
#include "ServiceFeatureTable.h"
#include "SpatialReference.h"
#include "Viewpoint.h"

// Qt headers
#include <QFuture>
#include <QUrl>
#include <QUuid>

using namespace Esri::ArcGISRuntime;

FilterByDefinitionExpressionOrDisplayFilter::FilterByDefinitionExpressionOrDisplayFilter(QQuickItem* parent) :
  QQuickItem(parent)
{
}

FilterByDefinitionExpressionOrDisplayFilter::~FilterByDefinitionExpressionOrDisplayFilter() = default;

void FilterByDefinitionExpressionOrDisplayFilter::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<FilterByDefinitionExpressionOrDisplayFilter>("Esri.Samples", 1, 0, "FilterByDefinitionExpressionOrDisplayFilterSample");
}

void FilterByDefinitionExpressionOrDisplayFilter::componentComplete()
{
  QQuickItem::componentComplete();

  //! [Obtain the instantiated map view in Cpp]
  // 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);
  const Point center = Point(-13630484, 4545415, SpatialReference(102100));
  constexpr double scale = 300000.0;
  m_map->setInitialViewpoint(Viewpoint(center, scale));

  // Set map to map view
  m_mapView->setMap(m_map);
  //! [Obtain the instantiated map view in Cpp]

  // create the feature table
  m_featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0"), this);
  // create the feature layer using the feature table
  m_featureLayer = new FeatureLayer(m_featureTable, this);

  connect(m_featureLayer, &FeatureLayer::loadStatusChanged, this, [this](LoadStatus loadStatus)
  {
    loadStatus == LoadStatus::Loaded ? m_initialized = true : m_initialized = false;

    // Initalize the feature count when the feature layer first loads
    queryFeatureCountInCurrentExtent();
    emit layerInitializedChanged();
  });

  // add the feature layer to the map
  m_map->operationalLayers()->append(m_featureLayer);
}

bool FilterByDefinitionExpressionOrDisplayFilter::layerInitialized() const
{
  return m_initialized;
}

int FilterByDefinitionExpressionOrDisplayFilter::currentFeatureCount() const
{
  return m_currentFeatureCount;
}

void FilterByDefinitionExpressionOrDisplayFilter::setDefExpression(const QString& whereClause)
{
  // reset display filter parameters before setting definition expression parameters
  resetDisplayFilterParams();

  m_featureLayer->setDefinitionExpression(whereClause);

  // Feature count in the extent should not change with different definition expressions.
  // If the extent changes and user clicks the button to reapply definition expression
  // the feature count number will update to reflect the number of feature count in the new extent
  queryFeatureCountInCurrentExtent();
}

void FilterByDefinitionExpressionOrDisplayFilter::setDisplayFilter(const QString& whereClause)
{
  // reset definition expression parameters before setting display filter parameters
  resetDefExpressionParams();

  DisplayFilter* displayFilter = new DisplayFilter("Damaged Trees", whereClause, this);
  const QList<DisplayFilter*> availableFilters{displayFilter};

  ManualDisplayFilterDefinition* displayFilterDefinition = new ManualDisplayFilterDefinition(displayFilter, availableFilters, this);
  m_featureLayer->setDisplayFilterDefinition(displayFilterDefinition);

  queryFeatureCountInCurrentExtent();
}

void FilterByDefinitionExpressionOrDisplayFilter::queryFeatureCountInCurrentExtent()
{
  QueryParameters parameters;
  parameters.setGeometry(m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry());

  m_featureTable->queryFeatureCountAsync(parameters).then(this, [this](int countResult)
  {
    m_currentFeatureCount = countResult;
    emit currentFeatureCountChanged();
  });
}

void FilterByDefinitionExpressionOrDisplayFilter::resetDisplayFilterParams()
{
  DisplayFilter* displayFilter = new DisplayFilter("No Filter", "1=1", this);
  const QList<DisplayFilter*> availableFilters{displayFilter};

  ManualDisplayFilterDefinition* displayFilterDefinition = new ManualDisplayFilterDefinition(displayFilter, availableFilters, this);
  m_featureLayer->setDisplayFilterDefinition(displayFilterDefinition);

  queryFeatureCountInCurrentExtent();
}

void FilterByDefinitionExpressionOrDisplayFilter::resetDefExpressionParams()
{
  m_featureLayer->setDefinitionExpression("");

  connect(m_mapView, &MapQuickView::drawStatusChanged, this, [this](DrawStatus drawStatus)
  {
    if (drawStatus == DrawStatus::Completed)
    {
      m_mapDrawing = false;
      queryFeatureCountInCurrentExtent();
    }
    else
    {
      m_mapDrawing = true;
    }

    emit mapDrawStatusChanged();
  });
}

bool FilterByDefinitionExpressionOrDisplayFilter::mapDrawing() const
{
  return m_mapDrawing;
}

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