Introduction to querying data

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

  1. Reference the ArcGIS REST JS request and feature-service modules.
  2. Find the URL of the service against which you want to query.
  3. Define the SQL or spatial query parameters.
  4. Execute the query using the REST JS queryFeatures method.

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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    <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>

Tutorials

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.