Learn how to execute a spatial query to access features from a feature layer.
A feature layer can contain a large number of features stored in ArcGIS. To access a subset of the features, you can execute either a SQL or spatial query, or both at the same time. You can return feature attributes, geometry, or both attributes and geometry for each record. SQL and spatial queries are useful when you want to access just a subset of your hosted data.
In this tutorial, you use the Sketch widget to draw a feature and then perform a spatial query against a feature layer. The query layer is the LA County Parcels feature layer containing ±2.4 million features. The spatial query uses the sketched feature to return all of the parcels that intersect.
Prerequisites
You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.
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/Map" for loading the Map module. After the modules are loaded, they are passed as parameters (e.g. Map) 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. For more information on the different types of modules, visit the Introduction to Tooling guide topic.
Use the Sketch and GraphicsLayer classes to create a graphic. The graphic will be added to the map in a graphics layer. The event handler will listen for a change from the sketch widget and update the query accordingly.
Create a graphicsLayerSketch and add it to the map.
You should see the sketch widget at the top right of the view. Click on one of the options in the widget to draw on the map.
Create a feature layer to query
Use the FeatureLayer class to perform a query against 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.
Create a parcelLayer and set the url property to access the feature layer in the feature service.
Feature layers are referenced by an index number at the end of the url. To determine the index number, visit the LA County Parcels feature service. In this case the index is 0.
Define a parcelQuery and use the FeatureLayerqueryFeatures method to execute a query.
Create a queryFeaturelayer function with geometry as a parameter and define parcelQuery. Set the spatialRelationship to intersects and use the geometry from the sketch widget. Limit the attributes returned by setting the outFields property to a list of fields. Lastly, set returnGeometry to true so the feature geometriescan be displayed.
Call the queryFeatures method on the parcelLayer using the parameters defined in the parcelQuery element. To view the number of features returned, write the result length to the console. This will be updated in the next step.
Update the event handler to call the queryFeatureLayer function every time a graphic is sketched on the map. It will also listen for any reshape or move changes made to the graphic.
Use the widget to draw a graphic. At the bottom left, click Console to view the number of features returned from the query.
Display features
To display the parcel features returned from the query, add them to the view as polygon graphics. Before the graphics are added, define a symbol and a pop-up so that the attributes can be displayed when a feature is clicked.
Create a displayResults function. Define a symbol and popupTemplate variable to style and display a pop-up for polygon graphics. The attributes referenced match the outFields specified in the query earlier.
When you use the widget to sketch a feature on the map, the spatial query runs against the feature layer and returns all parcels that intersect the sketched feature.