Learn how to use a SQL query to limit features displayed in a feature layer.
A hosted feature layer can contain a large number of features. To display a subset of the features, you can filter features on the server-side with a definition expression. Definition expressions are different than feature layer queries: they only support a SQL where clause without a geometry (spatial) parameter, and are only used to filter features at the time they are displayed in a map or scene. They cannot be used to get features like a feature layer query.
In this tutorial, you will apply a server-side SQL query with a definition
to filter the LA County Parcels feature layer .
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services used in this tutorial.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set
esri
to your access token.Config.api Key Use dark colors for code blocks var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };
To learn about other ways to get an access token, go to Types of authentication.
Create a selector
Use a Calcite Select component to provide a list of SQL queries for the LA County Parcels feature layer.
-
Add an
arcgis-placement
component after thearcgis-zoom
component within the<arcgis-map
to place the selector in the> top-right
corner of the map.Use dark colors for code blocks <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-placement position="top-right"> </arcgis-placement> </arcgis-map>
-
Add a Calcite Select component within the
arcgis-placement
component. This component has child Option components, each with a different SQL query.Use dark colors for code blocks <arcgis-placement position="top-right"> <calcite-select id="sqlSelect"> <calcite-option value="1=0" label="Choose a SQL where clause..."></calcite-option> <calcite-option value="Roll_LandValue < 200000" label="Roll_LandValue < 200000"></calcite-option> <calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option> <calcite-option value="Bedrooms5 > 0" label="Bedrooms5 > 0"></calcite-option> <calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option> <calcite-option value="Roll_RealEstateExemp > 0" label="Roll_RealEstateExemp > 0"></calcite-option> </calcite-select> </arcgis-placement>
-
Verify that the
select
component is created.
Add modules and a map component event listener
-
Add a
<script
tag in the> <body
following the> <arcgis-map
component with a> require
statement. In therequire
statement, add theFeature
module.Layer The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/layers/
for loading the FeatureLayer module. After the modules are loaded, they are passed as parameters (e.g.Feature Layer" Feature
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Layer Within the require statement, use the document.querySelector() method to access the map and select components.
Use dark colors for code blocks <body> <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-placement position="top-right"> <calcite-select id="sqlSelect"> <calcite-option value="1=0" label="Choose a SQL where clause..."></calcite-option> <calcite-option value="Roll_LandValue < 200000" label="Roll_LandValue < 200000"></calcite-option> <calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option> <calcite-option value="Bedrooms5 > 0" label="Bedrooms5 > 0"></calcite-option> <calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option> <calcite-option value="Roll_RealEstateExemp > 0" label="Roll_RealEstateExemp > 0"></calcite-option> </calcite-select> </arcgis-placement> </arcgis-map> <script> require(["esri/layers/FeatureLayer"], (FeatureLayer) => { const arcgisMap = document.querySelector("arcgis-map"); const selectFilter = document.querySelector("#sqlSelect"); }); </script> </body>
-
Create an event listener to listen for the map component's
arcgis
event.View Ready Change Use dark colors for code blocks <script> require(["esri/layers/FeatureLayer"], (FeatureLayer) => { const arcgisMap = document.querySelector("arcgis-map"); const selectFilter = document.querySelector("#sqlSelect"); arcgisMap.addEventListener("arcgisViewReadyChange", () => { }); }); </script>
Create a feature layer to filter
Use the Feature
class to access the LA County Parcels feature layer. Since you are performing a server-side query, the feature layer does not need to be added to the map. However, to view the results of the query, the feature layer will be added to the map.
- Create a
feature
and set theLayer url
property to access the feature layer in the feature service. Set theout
property to return all attributes on the client and theFields popup
to display the parcel description and land value. Set theTemplate definition
toExpression 1=0
so no features are displayed when the layer is loaded. Addfeature
to theLayer map
.Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewReadyChange", () => { const featureLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0", outFields: ["*"], popupTemplate: { title: "{UseType}", content: "Description: {UseDescription}. Land value: {Roll_LandValue}" }, definitionExpression: "1=0" }); arcgisMap.addLayer(featureLayer); });
Apply the SQL expression
The definition
is the SQL where clause. Use the expression to apply a filter and limit features displayed in the map.
-
Create a
set
function with anFeature Layer Filter expression
parameter. Set thedefinition
to filter the feature layer with theExpression expression
.Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewReadyChange", () => { const featureLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0", outFields: ["*"], popupTemplate: { title: "{UseType}", content: "Description: {UseDescription}. Land value: {Roll_LandValue}" }, definitionExpression: "1=0" }); arcgisMap.addLayer(featureLayer); // Server-side filter function setFeatureLayerFilter(expression) { featureLayer.definitionExpression = expression; } });
-
Add an event listener to call the
set
function when a SQL where clause is chosen from the selector.Feature Layer Filter Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewReadyChange", () => { const featureLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0", outFields: ["*"], popupTemplate: { title: "{UseType}", content: "Description: {UseDescription}. Land value: {Roll_LandValue}" }, definitionExpression: "1=0" }); arcgisMap.addLayer(featureLayer); // Server-side filter function setFeatureLayerFilter(expression) { featureLayer.definitionExpression = expression; } // Event listener selectFilter.addEventListener("calciteSelectChange", (event) => { setFeatureLayerFilter(event.target.value); }); });
Run the app
In CodePen, run your code to display the map.
When the map displays, you should be able to choose a SQL query from the selector that applies a definition expression to the visible extent of the map. Only the features that match are added to the feature layer and displayed in the view.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: