You can perform a SQL or spatial query to access a subset of the data in a feature service. The results of the query can contain the attributes, geometry, or both attributes and geometry, for each matching record. These results can be used for further processing or can be displayed in an application.
How to query a feature service
OpenLayers does not directly support the construction of SQL or spatial queries against a hosted feature service. To access the feature service in your application, use the feature-service
and request
modules from ArcGIS REST JS. The feature-service
module allows you to query and edit features in a feature layer.
Steps
- Reference the ArcGIS REST JS
request
andfeature-service
modules. - Find the URL of the service against which you want to query.
- Define the SQL or spatial query parameters.
- Execute the query using the REST JS
query
method.Features
Example
Query a feature layer (spatial)
This example performs a spatial query by using the ArcGIS REST JS request
and feature-service
modules to find which parcels intersect a given polygon. Available spatial relationships include: within, contains, crosses, touches, intersects, and overlaps.
<script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
<script
src="https://unpkg.com/@esri/arcgis-rest-feature-service@4.0.0/dist/bundled/feature-service.umd.js"></script>
<script>
arcgisRest
.queryFeatures({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
geometry: geometry,
geometryType: "esriGeometryPolygon",
spatialRel: "esriSpatialRelIntersects",
f: "json",
returnGeometry: true
})
.then((response) => {
const features = new ol.format.EsriJSON().readFeatures(response);
parcelLayer.getSource().addFeatures(features);
});
</script>