L.esri.Tasks.Query

L.esri.Tasks.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.Tasks.query(<Object> options)

L.esri.Tasks.query(<FeatureLayer> endpoint)

L.esri.Tasks.query(<MapService> endpoint)

L.esri.Tasks.query(<ImageService> endpoint)
Accepts either an options object or an instance of MapService, FeatureLayer 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> 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> 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> 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.
overlap(<Geometry> 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> latlng, <Integer> distance) 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.
where(<String> where) 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> offset) 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> limit) 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> from, <Date> to) this Queries features within a given time range. Only available for Layers/Services with timeInfo in their metadata.
fields(<Array> fields or <String> fields) this An array of associated fields to request for each feature.
returnGeometry(<Boolean> returnGeometry) this Return geometry with results. Default is true.
simplify(<Map> map, <Integer> factor) 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 (simplify to the most basic shape possible).
orderBy(<String> fieldName, <String> order) this Order the output features on certain field either ascending or descending. This can be called multiple times to define a very detailed sort order.
featureIds(<Array> ids) this Return only specific feature IDs if they match other query parameters.
precision(<Integer> precision) this Return only this many decimal points of precision in the output geometries.
token(<String> token) 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> layer) this Used to select which layer inside a Map Service to perform the query on.
Only available for Map Services.
pixelSize(<Point> point) this Override the default pixelSize when querying an Image Service.
Only available for Image Services.
run(<Function> callback, <Object> context) this Exectues the query request with the current parameters, features will be passed to callback as a GeoJSON FeatureCollection. Accepts an optional function context.
count(<Function> callback, <Object> context) this Exectues 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> callback, <Object> context) this Exectues 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> callback, <Object> context) 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.Tasks.query({
    url:'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});

query.within(bounds);

query.run(function(error, featureCollection, response){
    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.Tasks.query({
    url: 'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});

query.bounds(function(error, latLngBounds, response){
    map.fitBounds(latLngBounds);
});
Querying features near a latlng
var latlng = L.latLng(45.51, -122.70);

var query = L.esri.Tasks.query({
    url:'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});

query.nearby(latlng, 500);

query.run(function(error, featureCollection, response){
    console.log('Found ' + featureCollection.features.length + ' features');
});
Combining multiple options
var latlng = L.latLng(45.51, -122.70);

var query = L.esri.Tasks.query({
    url: 'http://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){
    console.log('Found ' + count + ' features');
});

query.ids(function(error, ids, response){
    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.Tasks.query({
    url:'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'
});

query.where("zone_id='B'").bounds(function(error, latLngBounds, response){
    map.fitBounds(latLngBounds);
});

Edit this page on GitHub