This content has moved to developers.arcgis.com. Please update your bookmarks!
L.esri.Query
Extends L.esri.Task
L.esri.Query
is an abstraction for the query API included in Feature Layers and Image Services. It provides a chainable API for building request parameters and executing queries.
Note Depending on the type of service you are querying (Feature Layer, Map Service, Image Service) and the version of ArcGIS Server that hosts the service some of these options may not be available.
Constructor
Constructor | Description |
---|---|
L.esri.query(<Object> L.esri.query(<FeatureLayerService> L.esri.query(<MapService> L.esri.query(<ImageService>
|
Accepts either an options object or an instance of MapService, FeatureLayerService or ImageService. |
Options
Option | Type | Default | Description |
---|---|---|---|
url |
String |
'' |
URL of the ArcGIS Server or ArcGIS Online service you would like to consume. |
proxy |
String |
false |
URL of an ArcGIS API for JavaScript proxy or ArcGIS Resource Proxy to use for proxying POST requests. |
useCors |
Boolean |
true |
If this task should use CORS when making GET requests. |
Methods
Method | Returns | Description |
---|---|---|
within(<Geometry> |
this |
Queries features from the service within (fully contained by) the passed geometry object. geometry can be an instance of L.Marker , L.Polygon , L.Polyline , L.LatLng , L.LatLngBounds and L.GeoJSON . It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon. |
contains(<Geometry> |
this |
Queries features from the service that fully contain the passed geometry object. geometry can be an instance of L.Marker , L.Polygon , L.Polyline , L.LatLng , L.LatLngBounds and L.GeoJSON . It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon. |
intersects(<Geometry> |
this |
Queries features from the service that intersect (touch anywhere) the passed geometry object. geometry can be an instance of L.Marker , L.Polygon , L.Polyline , L.LatLng , L.LatLngBounds and L.GeoJSON . It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon. |
bboxIntersects(<Geometry> |
this |
Queries features from the service that have a bounding box that intersects the bounding box of the passed geometry object. geometry can be an instance of L.Marker , L.Polygon , L.Polyline , L.LatLng , L.LatLngBounds and L.GeoJSON . It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon. |
overlaps(<Geometry> |
this |
Queries features from the service that overlap (touch but are not fully contained by) the passed geometry object. geometry can be an instance of L.Marker , L.Polygon , L.Polyline , L.LatLng , L.LatLngBounds and L.GeoJSON . It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon. |
nearby(<LatLng> |
this |
Queries features a given distance in meters around a LatLng. Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3+ that include the capability supportsQueryWithDistance . |
where(<String> |
this |
Adds a where clause to the query. String values should be denoted using single quotes ie: query.where("FIELDNAME = 'field value'"); More info about valid SQL can be found here. |
offset(<Integer> |
this |
Define the offset of the results, when combined with limit can be used for paging. Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3+. |
limit(<Integer> |
this |
Limit the number of results returned by this query, when combined with offset can be used for paging. Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3. |
between(<Date> |
this |
Queries features within a given time range. Only available for Layers/Services with timeInfo in their metadata. |
fields(<Array> |
this |
An array of associated fields to request for each feature. |
returnGeometry(<Boolean> |
this |
Return geometry with results. Default is true . |
simplify(<Map> |
this |
Simplify the geometries of the output features for the current map view. the factor parameter controls the amount of simplification between 0 (no simplification) and 1 (the most basic shape possible). |
orderBy(<String> |
this |
Sort output features using values from an individual field. "ASC" (ascending) is the default sort order, but "DESC" can be passed as an alternative. This method can be called more than once to apply advanced sorting. |
featureIds(<Array> |
this |
Return only specific feature IDs if they match other query parameters. |
precision(<Integer> |
this |
Return only this many decimal points of precision in the output geometries. |
token(<String> |
this |
Adds a token to this request if the service requires authentication. Will be added automatically if used with a service. |
layer(<String or Integer> |
this |
Used to select which layer inside a Map Service to perform the query on. Only available for Map Services. |
pixelSize(<Point> |
this |
Override the default pixelSize when querying an Image Service. Only available for Image Services. |
transform(<Number> |
this |
The WKID of a datum transformation for the server to apply when reprojecting output features. Only available for ArcGIS Server 10.5+. |
distinct() |
this |
Ensures that no geometry or duplicate field values will be returned in the subsequent request. |
returnM(<Boolean> |
this |
Return geometry with four dimensional measure values in results. |
run(<Function> |
this |
Executes the query request with the current parameters, features will be passed to callback as a GeoJSON FeatureCollection. Accepts an optional function context. |
count(<Function> |
this |
Executes the query request with the current parameters, passing only the number of features matching the query to callback as an Integer . Accepts an optional function context. |
ids(<Function> |
this |
Executes the query request with the current parameters, passing only an array of the feature ids matching the query to callbackcallback . Accepts an optional function context. |
bounds(<Function> |
this |
Executes the query request with the current parameters, passing only the LatLngBounds of all features matching the query in the callback . Accepts an optional function context. Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3.1. |
Examples
Finding features with map bounds
var southWest = L.latLng(45.51, -122.70);
var northEast = L.latLng(45.52, -122.64);
var bounds = L.latLngBounds(southWest, northEast);
var query = L.esri.query({
url: 'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});
query.within(bounds);
query.run(function (error, featureCollection, response) {
if (error) {
console.log(error);
return;
}
console.log('Found ' + featureCollection.features.length + ' features');
});
Finding the bounds of all features
var map = L.map('map').setView([41.64, -53.70], 3);
L.esri.basemapLayer('Gray').addTo(map);
var query = L.esri.query({
url: 'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});
query.bounds(function (error, latLngBounds, response) {
if (error) {
console.log(error);
return;
}
map.fitBounds(latLngBounds);
});
Querying features near a latlng
var latlng = L.latLng(45.51, -122.70);
var query = L.esri.query({
url: 'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});
query.nearby(latlng, 500);
query.run(function (error, featureCollection, response) {
if (error) {
console.log(error);
return;
}
console.log('Found ' + featureCollection.features.length + ' features');
});
Combining multiple options
var latlng = L.latLng(45.51, -122.70);
var query = L.esri.query({
url: 'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});
query.nearby(latlng, 2000).where("direction='East'").orderBy('stop_id', 'ASC');
query.count(function (error, count, response) {
if (error) {
console.log(error);
return;
}
console.log('Found ' + count + ' features');
});
query.ids(function (error, ids, response) {
if (error) {
console.log(error);
return;
}
console.log(ids.join(', ') + 'match the provided parameters');
});
Getting the bounds of the query result
var map = L.map('map').setView([41.64, -53.70], 3);
L.esri.basemapLayer('Gray').addTo(map);
var query = L.esri.query({
url: 'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});
query.where("zone_id='B'").bounds(function (error, latLngBounds, response) {
if (error) {
console.log(error);
return;
}
map.fitBounds(latLngBounds);
});