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

ConstructorDescription
L.esri.query(<Object>options)L.esri.query(<FeatureLayerService>endpoint)L.esri.query(<MapService>endpoint)L.esri.query(<ImageService>endpoint)Accepts either an options object or an instance of MapService, FeatureLayerService or ImageService.

Options

OptionTypeDefaultDescription
urlString''URL of the ArcGIS Server or ArcGIS Online service you would like to consume.
proxyStringfalseURL of an ArcGIS API for JavaScript proxy or ArcGIS Resource Proxy to use for proxying POST requests.
useCorsBooleantrueIf this task should use CORS when making GET requests.

Methods

MethodReturnsDescription
within(<Geometry>geometry)thisQueries 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)thisQueries 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 Polygon containing Point, Polyline, Polygon.
intersects(<Geometry>geometry)thisQueries 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 Polygon containing Point, Polyline, Polygon.
bboxIntersects(<Geometry>geometry)thisQueries 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 Polygon containing Point, Polyline, Polygon.
overlaps(<Geometry>geometry)thisQueries 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 Polygon containing Point, Polyline, Polygon.
nearby(<LatLng>latlng, <Integer>distance)thisQueries 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 Polygon containing Point, Polyline, Polygon.
where(<String>where)thisAdds 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)thisDefine 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)thisLimit 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)thisQueries features within a given time range. Only available for Layers/Services with timeInfo in their metadata.
fields(<Array>fields or <String>fields)thisAn array of associated fields to request for each feature.
returnGeometry(<Boolean>returnGeometry)thisReturn geometry with results. Default is true.
simplify(<Map>map, <Number>factor)thisSimplify 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>fieldName, <String>order)thisSort 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>ids)thisReturn only specific feature IDs if they match other query parameters.
precision(<Integer>precision)thisReturn only this many decimal points of precision in the output geometries.
token(<String>token)thisAdds a token to this request if the service requires authentication. Will be added automatically if used with a service.
layer(<String or Integer>layer)thisUsed to select which layer inside a Map Service to perform the query on. Only available for Map Services.
pixelSize(<Point>point)thisOverride the default pixelSize when querying an Image Service. Only available for Image Services.
transform(<Number>Number)thisThe WKID of a datum transformation for the server to apply when reprojecting output features.Only available for ArcGIS Server 10.5+.
distinct()thisEnsures that no geometry or duplicate field values will be returned in the subsequent request.
returnM(<Boolean>returnM)thisReturn geometry with four dimensional measure values in results.
run(<Function>callback, <Object>context)thisExecutes 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)thisExecutes 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)thisExecutes 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)thisExecutes 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

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var southWest = L.latLng(34.01, -118.70);
var northEast = L.latLng(34.02, -118.64);
var bounds = L.latLngBounds(southWest, northEast);

var query = L.esri.query({
    url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Tree_Data/FeatureServer'
});

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

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var map = L.map('map').setView([34.0227, -118.4877], 14);

L.esri.Vector.vectorBasemapLayer("arcgis/streets", {
  token: "YOUR_ACCESS_TOKEN"
}).addTo(map);

var query = L.esri.query({
    url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Tree_Data/FeatureServer'
});

query.bounds(function (error, latLngBounds, response) {
    if (error) {
    console.log(error);
    return;
    }
    map.fitBounds(latLngBounds);
});

Querying features near a latlng

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var latlng = L.latLng(34.0227, -118.4877);

var query = L.esri.query({
    url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Tree_Data/FeatureServer'
});

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

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
var latlng = L.latLng(34.0227, -118.4877);

var query = L.esri.query({
    url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Tree_Data/FeatureServer'
});

query.nearby(latlng, 2000).where("cycle_numb='5A'").orderBy('botanicaln', '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

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var map = L.map('map').setView([34.0227, -118.4877], 14);

L.esri.Vector.vectorBasemapLayer("arcgis/streets", {
  token: "YOUR_ACCESS_TOKEN"
}).addTo(map);

var query = L.esri.query({
    url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Tree_Data/FeatureServer'
});

query.where("biological='Minor'").bounds(function (error, latLngBounds, response) {
    if (error) {
    console.log(error);
    return;
    }
    map.fitBounds(latLngBounds);
});

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