Learn how to execute a SQL query to access polygon 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 a SQL or spatial query, or both at the same time. You can also return the attributes, geometry, or both attributes and geometry for each record. SQL and spatial queries are useful when a feature layer is very large and you just want to access a subset of the data.
In this tutorial, you perform server-side SQL queries to return a subset of the features from the LA County Parcel hosted feature layer. The feature layer contains over 2.4 million features. The resulting features are displayed as graphics on the map. 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.
ArcGIS feature layers support a standard SQL query where clause. Use an HTML select element to provide a list of SQL queries for the LA County Parcel feature layer.
Create a select element containing an option element for each where clause.
Parcels returned by the query are simple GeoJSON polygons. You will create a Vector layer to display them.
Add a load event handler to the olms function call. Inside, create a Vector layer and save it to a parcelLayer variable. Add it to the map with map.addLayer.
As you are not passing a style parameter, the default styling for polygons will be used. See the OpenLayers documentation for more details.
Use the ArcGIS REST JS queryFeatures method to find features in the LA County Parcels feature layer that match the selected where clause.
To limit the query results to the visible extent of the Map, you can set the queryFeaturesgeometry property. To do so your function will take a geometry object in the required esriGeometryEnvelope format of [minx, miny, maxx, maxy]. This confines the SQL query to the geographic bounds (extent) of the map.
When the matching parcels are returned, create a new Vector source using a GeoJSON feature format, to update your parcelsLayerVector layer.
Create a function called executeQuery with whereClause and geometry parameters. Inside, create a new arcgisRest.ApiKeyManager to access the feature service. Call arcgisRest.queryFeatures. Pass the geometry and geometryType. Specify GeoJSON as the return type, requesting returnGeometry and specific outFields. All of the features within the geometry will be returned with attribute information set by the outFields property.
There are many other spatial relationships that you can specify with spatialRel. For example, you can use esriSpatialRelContains to only return parcels completely within the viewport. See the ArcGIS services reference for details.
The executeQuery function needs to execute when you select an option from the select control. You achieve this by adding a change event handler. You can access the chosen where clause with the value property of the select element.
You need to calculate the extent of the current map view in latitude and longitude. You can use View.calculateExtent to get the extent, and proj.transformExtent to convert it to the EPSG:4326 spatial reference.
Add a change event handler to the select element. Inside, use View.calculateExtent and ol.proj.transformExtent to calculate the viewport extent. Call executeQuery with the extent and where clause.
When you select a where clause using the select control, a query will run against the feature layer and display all land parcels within the current viewport matching the where clause.
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.
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.
When the map displays, you should be able to use the query selector to display parcels. Click on a parcel to show a pop-up with the feature's attributes.