Learn how to execute a SQL query to access polygon
Use the selector to choose a SQL where clause to query and display features.
A feature layer
In this tutorial, you will perform server-side SQL queries to return a subset of features
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
- 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. 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. :
- Privileges
- Location services > Basemaps
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
API key credentials are an item that contains the parameters used to create and manage long-lived access tokens for API key authentication. They are a type of developer credential. access to the layer item. Learn more in Item access privileges.
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
- 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 SQL selector
ArcGIS feature layers
-
Add a Calcite Select component to the
top-rightslot of the map. This component has child option components, each with a different SQL query.33 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></calcite-option></calcite-select>93 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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;queryFeatureLayer(viewElement.extent);});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error.error);});}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);}</script></body></html> -
Verify that the
selectcomponent is created.
Add modules and event listeners
-
Add a
<script>tag in the<body>following the<arcgis-map>component. Use$arcgis.import()to add theFeatureLayermodule.The ArcGIS Maps SDK for JavaScript
ArcGIS Maps SDK for JavaScript, previously known as ArcGIS API for JavaScript, is a developer product for building mapping and spatial analysis applications for the web. 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, select, and the default option components. Create a
whereClausevariable to store the first option value.47 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;</script>5 collapsed lines</body></html> -
Wait for the map to be ready with viewOnReady.
47 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();</script>5 collapsed lines</body></html> -
Create an event listener to listen for the
selectcomponent changes and update thewhereClausevariable to the selected value.57 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;});6 collapsed lines</script></body></html>
Create a feature layer to query
Use the FeatureLayer class to access the LA County Parcel feature layer
- Create a
parcelLayerand set theurlproperty to access the feature layer in the feature service.57 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});6 collapsed lines</script></body></html>
Execute a query
Use the queryFeatures method to perform a SQL query against the feature layerQuery will be autocast when the method is called.
-
Create a
queryFeatureLayerfunction withextentparameter. Define aparcelQueryelement and set thewhereproperty to thewhereClause. Set thespatialPropertyto only return featuresA 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. that intersect thegeometry, which is restricted to the visibleextentof the map. TheoutFieldsproperty will return only a subset of the attributesAttributes 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. . Lastly, setreturnGeometrytotrueso that the features can be displayed.66 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}8 collapsed lines</script></body></html> -
Call the
queryFeaturesmethod on theparcelLayerusingparcelQuery. To view the number of featuresA 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. returned, write the result length to the console. This will be updated in the next step.71 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);}).catch((error) => {console.log(error.error);});}40 collapsed linesfunction displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);}</script></body></html> -
Update the event handler to call the
queryFeatureLayerfunction when the selector changes.59 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;queryFeatureLayer(viewElement.extent);});71 collapsed lines// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error.error);});}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);}</script></body></html> -
At the top-right, click Run. Choose a SQL query from the selector. At the bottom left, click Console to view the 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. returned from each query.
Display features
To display the features
-
Create a
displayResultsfunction withresultsas a parameter. Define asymbolandpopupTemplatevariable to style and display a pop-upA pop-up is a visual element used to display data for features or graphics in a map. for polygon graphicsA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. . The attributesAttributes 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. referenced match theoutFieldsspecified in the query earlier.91 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;queryFeatureLayer(viewElement.extent);});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error.error);});}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};}7 collapsed lines</script></body></html> -
Assign the
symbolandpopupTemplateelements to each featureA 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. returned from the query.110 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;queryFeatureLayer(viewElement.extent);});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error.error);});}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});8 collapsed lines}</script></body></html> -
Clear the existing graphics and pop-up
A pop-up is a visual element used to display data for features or graphics in a map. , and then add the new featuresA 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. returned to theview.101 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);9 collapsed lines}</script></body></html> -
Update the
queryFeatureLayerfunction to call thedisplayResultsfunction. Remove theconsole.log.83 collapsed lines<html><head><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: Query a feature layer (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 id="defaultOption" value="1=0" label="Choose a SQL where clause..."></calcite-option><calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option><calcite-option value="UseType = 'Government'" label="UseType = 'Government'"></calcite-option><calcite-option value="UseType = 'Irrigated Farm'" label="UseType = 'Irrigated Farm'"></calcite-option><calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option><calcite-option value="TaxRateArea = 10860" label="TaxRateArea = 10860"></calcite-option><calcite-option value="TaxRateArea = 08637" label="TaxRateArea = 08637"></calcite-option><calcite-option value="Roll_LandValue > 1000000" label="Roll_LandValue > 1000000"></calcite-option><calcite-option value="Roll_LandValue < 1000000" label="Roll_LandValue < 1000000"></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");const defaultOption = document.querySelector("#defaultOption");let whereClause = defaultOption.value;await viewElement.viewOnReady();// Event listenerselectFilter.addEventListener("calciteSelectChange", (event) => {whereClause = event.target.value;queryFeatureLayer(viewElement.extent);});// Get query layer and set up queryconst parcelLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",});function queryFeatureLayer(extent) {const parcelQuery = {where: whereClause, // Set by select elementspatialRelationship: "intersects", // Relationship operation to applygeometry: extent, // Restricted to visible extent of the mapoutFields: ["APN", "UseType", "TaxRateCity", "Roll_LandValue"], // Attributes to returnreturnGeometry: true,};parcelLayer.queryFeatures(parcelQuery).then((results) => {console.log("Feature count: " + results.features.length);displayResults(results);}).catch((error) => {console.log(error.error);});42 collapsed lines}function displayResults(results) {// Create a blue polygonconst symbol = {type: "simple-fill",color: [20, 130, 200, 0.5],outline: {color: "white",width: 0.5,},};const popupTemplate = {title: "Parcel {APN}",content:"Type: {UseType} <br> Land value: {Roll_LandValue} <br> Tax Rate City: {TaxRateCity}",};// Assign styles and popup to featuresresults.features.map((feature) => {feature.symbol = symbol;feature.popupTemplate = popupTemplate;return feature;});// Clear displayviewElement.closePopup();viewElement.graphics.removeAll();// Add features to graphics layerviewElement.graphics.addMany(results.features);}</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. The resulting features
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: