Filter features displayed on a map using a definition expression or a display filter.
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
Tap on the apply expression button to limit the features requested from the feature layer to those specified by the SQL query definition expression. This option not only narrows down the results that are drawn, but also removes those features from the layer's attribute table. To filter the results being drawn without modifying the attribute table, tap on the apply filter button. Tap on the reset button to remove the definition expression or display filter on the feature layer, which returns all the records.
The feature count value shows the current number of features in the current map view extent. When a definition expression is applied to narrow down the list of features being drawn, the count is updated to reflect this change. However if a display filter is applied, the features which are not visible on the map will still be included in the total feature count.
How it works
- Create a service feature table from a URL.
- Create a feature layer from the service feature table.
- Filter features on your feature layer using a
featureLayer.definitionExpression
to view a subset of features and modify the attribute table. - Filter features on your feature layer using a
featureLayer.displayFilterDefinition
to view a subset of features without modifying the attribute table.
Relevant API
- DefinitionExpression
- FeatureLayer
- ServiceFeatureTable
About the data
The San Francisco 311 incidents layer in this sample 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
//
// Copyright 2024 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
//
// https://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.
//
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:flutter/material.dart';
import '../../utils/sample_state_support.dart';
class FilterByDefinitionExpressionOrDisplayFilter extends StatefulWidget {
const FilterByDefinitionExpressionOrDisplayFilter({super.key});
@override
State<FilterByDefinitionExpressionOrDisplayFilter> createState() =>
_FilterByDefinitionExpressionOrDisplayFilterState();
}
class _FilterByDefinitionExpressionOrDisplayFilterState
extends State<FilterByDefinitionExpressionOrDisplayFilter>
with SampleStateSupport {
// Create a map view controller.
final _mapViewController = ArcGISMapView.createController();
// Create a feature layer.
final _featureLayer = FeatureLayer.withFeatureTable(
ServiceFeatureTable.withUri(
Uri.parse(
'https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0',
),
),
);
// Create a display filter definition.
ManualDisplayFilterDefinition? _displayFilterDefinition;
// Create a definition expression.
var _definitionExpression = '';
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: false,
child: Column(
children: [
Expanded(
// Add a map view to the widget tree and set a controller.
child: ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady,
),
),
// Add a text widget to display the feature count.
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// Add a button to apply a definition expression.
ElevatedButton(
onPressed: applyDefinitionExpression,
child: const Text(
'Apply \nExpression',
textAlign: TextAlign.center,
),
),
// Add a button to apply a display filter.
ElevatedButton(
onPressed: applyDisplayFilter,
child: const Text(
'Apply \nFilter',
textAlign: TextAlign.center,
),
),
// Add a button to reset the definition expression and display filter.
ElevatedButton(
onPressed: reset,
child: const Text('Reset'),
),
],
),
],
),
),
);
}
void onMapViewReady() {
// Create a map with a topographic basemap style.
final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
// Add the feature layer to the map.
map.operationalLayers.add(_featureLayer);
// Set the initial viewpoint.
map.initialViewpoint = Viewpoint.withLatLongScale(
latitude: 37.7759,
longitude: -122.45044,
scale: 100000,
);
// Set the map to the map view.
_mapViewController.arcGISMap = map;
}
void applyDefinitionExpression() async {
// Remove the display filter.
_displayFilterDefinition = null;
// Apply a definition expression to the feature layer.
_definitionExpression = "req_Type = 'Tree Maintenance or Damage'";
// Count the number of features.
await calculateFeatureCount();
}
void applyDisplayFilter() async {
// Remove the definition expression.
_definitionExpression = '';
// Apply a display filter to the feature layer.
final displayFilter = DisplayFilter.withWhereClause(
name: 'Damaged Trees',
whereClause: "req_type LIKE '%Tree Maintenance%'",
);
// Create a manual display filter definition.
final manualDisplayFilterDefinition =
ManualDisplayFilterDefinition.withFilters(
activeFilter: displayFilter,
availableFilters: [displayFilter],
);
_displayFilterDefinition = manualDisplayFilterDefinition;
// Count the number of features.
await calculateFeatureCount();
}
void reset() async {
// Remove the definition expression and display filter.
_displayFilterDefinition = null;
_definitionExpression = '';
// Count the number of features.
await calculateFeatureCount();
}
Future<void> calculateFeatureCount() async {
_featureLayer.displayFilterDefinition = _displayFilterDefinition;
_featureLayer.definitionExpression = _definitionExpression;
// Get the current extent of the map view.
final extent = _mapViewController
.getCurrentViewpoint(ViewpointType.boundingGeometry)
?.targetGeometry
.extent;
// Create query parameters.
final queryParameters = QueryParameters();
queryParameters.geometry = extent;
// Query the feature count.
final featureCount =
await _featureLayer.featureTable!.queryFeatureCount(queryParameters);
// Show the feature count in an alert dialog.
if (mounted) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text(
'Current Feature Count',
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
content: Text(
'$featureCount features',
textAlign: TextAlign.center,
),
);
},
);
}
}
}