Learn how to use a SQL query to limit features A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. Learn more displayed in a feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more .

Use the selector to choose a SQL where clause to filter and display features on the map.

A hosted feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more can contain a large number of features A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. Learn more . 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 A geometry is a geometric shape, such as a point, polyline, or polygon, that contains one or more coordinates and a spatial reference. Learn more (spatial) parameter, and are only used to filter features at the time they are displayed in a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more or scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more . 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 definitionExpression to filter the LA County Parcels feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more .

Prerequisites

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Get an access token

You need an access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with the correct privileges to access the location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key An API key is a long-lived access token created using API key credentials. They are valid for up to one year and are typically embedded directly into client applications. Learn more with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :
    • Privileges
      • Location services > Basemaps
  2. In CodePen, set the apiKey property on the global esriConfig variable 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 feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more .

  1. Add a Calcite Select component within the arcgis-map component in the top-right slot. 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 filter
    function setFeatureLayerFilter(expression) {
    featureLayer.definitionExpression = expression;
    }
    // Event listener
    selectFilter.addEventListener("calciteSelectChange", (event) => {
    setFeatureLayerFilter(event.target.value);
    });
    </script>
    </body>
    </html>
  2. Verify that the select component is created.

Add modules and a map component event listener

  1. Add a <script> tag in the <body> following the <arcgis-map> component. Use $arcgis.import() to add the FeatureLayer module.

    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>
  2. 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 feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more . 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.

  1. Create a featureLayer and set the url property to access the feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more in the feature service A feature service is a data service that provides access to spatial and non-spatial data in feature layers, feature layer views, and tables. Learn more . Set the outFields property to return all attributes Attributes are fields and values for a single feature or non-spatial record. They are typically stored in a database or service such as a feature service. Learn more on the client and the popupTemplate to display the parcel description and land value. Set the definitionExpression to 1=0 so no features are displayed when the layer is loaded. Add featureLayer to the map.
    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 features A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. Learn more displayed in the map.

  1. Create a setFeatureLayerFilter function with an expression parameter. Set the definitionExpression to filter the feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more with the expression.

    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 filter
    function setFeatureLayerFilter(expression) {
    featureLayer.definitionExpression = expression;
    }
    6 collapsed lines
    </script>
    </body>
    </html>
  2. Add an event listener to call the setFeatureLayerFilter function 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 filter
    function setFeatureLayerFilter(expression) {
    featureLayer.definitionExpression = expression;
    }
    // Event listener
    selectFilter.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 extent An extent is a bounding rectangle with points that delineate an area for a map or scene. Learn more of the map. Only the features A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. Learn more that match are added to the feature layer A feature layer (server-side) is a spatially-enabled table in a feature service. All features in a feature layer share the same geometry type and set of fields. Learn more and displayed in the view A view is a UI component that draws the layers in a map or scene and controls their extent (area) and geographic features. Learn more .

What’s next?

Learn how to use additional SDK features and ArcGIS services in these tutorials: