Learn how to use a SQL query to limit
Use the selector to choose a SQL where clause to filter and display features on the map.
A hosted
In this tutorial, you will apply a server-side SQL query with a definitionExpression to filter the LA County Parcels
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.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
-
Add a Calcite Select component within the
arcgis-mapcomponent in thetop-rightslot. This component has child Option components, each with a different SQL query.34 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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>39 collapsed lines</arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");await viewElement.viewOnReady();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",});viewElement.map.add(featureLayer);// Server-side filterfunction setFeatureLayerFilter(expression) {featureLayer.definitionExpression = expression;}// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {setFeatureLayerFilter(event.target.value);});</script></body></html> -
Verify that the
selectcomponent is created.
Add modules and a map component event listener
-
Add a
<script>tag in the<body>following the<arcgis-map>component. Use$arcgis.import()to add theFeatureLayermodule.The
ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.Use the document.querySelector() method to access the map and select components.
27 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");</script></body>3 collapsed lines</html> -
Wait for the Map component to be ready with the viewOnReady method.
45 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");await viewElement.viewOnReady();</script>5 collapsed lines</body></html>
Create a feature layer to filter
Use the FeatureLayer class to access the LA County Parcels
- Create a
featureLayerand set theurlproperty to access thefeature layer in thefeature service . Set theoutFieldsproperty to return allattributes on the client and thepopupTemplateto display the parcel description and land value. Set thedefinitionExpressionto1=0so no features are displayed when the layer is loaded. AddfeatureLayerto themap.52 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");await viewElement.viewOnReady();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",});viewElement.map.add(featureLayer);6 collapsed lines</script></body></html>
Apply the SQL expression
The definitionExpression is the SQL where clause. Use the expression to apply a filter and limit
-
Create a
setFeatureLayerFilterfunction with anexpressionparameter. Set thedefinitionExpressionto filter thefeature layer with theexpression.52 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");await viewElement.viewOnReady();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",});viewElement.map.add(featureLayer);// Server-side filterfunction setFeatureLayerFilter(expression) {featureLayer.definitionExpression = expression;}6 collapsed lines</script></body></html> -
Add an event listener to call the
setFeatureLayerFilterfunction when a SQL where clause is chosen from the selector.52 collapsed lines<html><head><meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Filter a feature layer with SQL</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-select id="sqlSelect" slot="top-right"><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-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");const selectFilter = document.querySelector("#sqlSelect");await viewElement.viewOnReady();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",});viewElement.map.add(featureLayer);// Server-side filterfunction setFeatureLayerFilter(expression) {featureLayer.definitionExpression = expression;}// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {setFeatureLayerFilter(event.target.value);});7 collapsed lines</script></body></html>
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
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: