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> 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

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.
bboxIntersects(<Geometry> 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> 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+ that include the capability supportsQueryWithDistance.
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, <Number> 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 (the most basic shape possible).
orderBy(<String> fieldName, <String> order) 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> 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.
transform(<Number> 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> returnM) this Return geometry with four dimensional measure values in results.
run(<Function> callback, <Object> context) 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> callback, <Object> context) 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> callback, <Object> context) 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> 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.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);
});

Edit this page on GitHub