Learn how to execute a spatial query to access polygon features from a feature service.
A feature layer can contain a large number of features stored in ArcGIS. To access a subset of these features, you can execute an SQL or spatial query, either together or individually. The results can contain the attributes, geometry, or both for each record. SQL and spatial queries are useful when a feature layer is very large and you want to access only a subset of its data.
In this tutorial, you use the OpenLayers Draw interaction to sketch a feature on the map and ArcGIS REST JS to perform a spatial query against the LA County Parcels hosted feature layer. The layer contains ±2.4 million features. The spatial query returns all of the parcels that intersect the sketched feature. A pop-up is also used to display feature attributes.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
You will need two Vector layers. interactionLayer will display the query polygon while the user is drawing it. Once the query returns, parcelLayers will display the individual property parcels in the default style. Each contains a Vector source that initially contains no features.
In order to be able to draw shapes, you create a Draw Interaction, connected to your interactionLayer. By setting its type to Polygon, it only allows polygon features to be drawn.
Add a map load handler to the olms initialization. Inside, define Vector layers parcelLayer and interactionLayer. Provide a black Stroke and blue Fill style for parcelLayer. Add each to the map with map.addLayer.
In the top right, click Run. You can now click to create multiple polygons.
Execute the query
Use the ArcGIS REST JS queryFeatures method to find features in the LA County Parcels feature layer that intersect the sketched feature. When the matching parcels are returned, use an EsriJSON feature format to read the features and add them to the parcel layer's source.
Create a function called executeQuery with a geometry parameter and then call arcgisRest.queryFeatures.
Pass the geometry. Specify JSON as the return type and specify returnGeometry. All of the features within the geometry will be returned.
There are many other spatial relationships that you can specify with spatialRel. For example, you can use esriSpatialRelContains to only return parcels within the sketched polygon. See the ArcGIS services reference for details.
When the user completes a polygon, the drawend event is fired, with an object containing the new polygon as a feature attribute. You can use this feature to call executeQuery.
Use the EsriJSON feature format to convert the query polygon into the ArcGIS geometry format. It is not necessary to reproject the data.
Add a handler for the drawEnd event. Inside, use an EsriJSON feature format to convert the feature into an ArcGIS JSON format. Ensure the map's spatial reference information is included, by setting the featureProjection property. Call executeQuery with this feature's geometry.
At the top right, click Run. When you draw a polygon, a spatial query will run against the feature layer and display all land parcels within the boundary of the feature. Interaction polygons and parcels returned from previous queries remain visible. You will address this next.
Add a pop-up
The parcels returned from the query contain many attributes, such as land value and land use type. You can make the parcels interactive by showing a pop-up when the user clicks on a parcel. You create a Popup to display parcel attributes. It is a type of Overlay so you add it to the map with map.addOverlay.
In order for clicking to trigger a pop-up, and not draw another polygon, you need to enable and disable the draw interaction using setActive. Disable the interaction when the user has drawn a complete polygon. Enable it again when the user clicks somewhere other than a parcel, to reset the map. Clear the parcels and interaction layer at the same time.
Create a Popup and save it to a popup variable. Add it to the map with map.addOverlay.
Create a map click event handler. Inside, use map.getFeaturesAtPixel to get the clicked parcel, if any. If there is one, use popup.show to display a pop-up containing the parcel's APN, use type, land value and taxation city.
If the user did not click a parcel, hide the pop-up with popup.hide. Clear each layer by calling clear on its Source. Use setActive to enabledrawInteraction.
When you click on the map to draw a polygon, a spatial query will run against the feature layer and display all land parcels that intersect the boundary of the feature. You can click on a parcel to see a pop-up with information about the parcel, or click somewhere else to reset the map.