esri-leaflet
Version:
Leaflet plugins for consuming ArcGIS Online and ArcGIS Server services.
370 lines (325 loc) • 22.3 kB
HTML
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="not-ie"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>L.esri.Tasks.Query | Esri Leaflet</title>
<meta name="description" content="Esri Leaflet">
<meta name="viewport" content="width=device-width">
<!--[if lt IE 9]>
<script src="//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv-printshiv.js"></script>
<![endif]-->
<!-- google fonts -->
<link href='//fonts.googleapis.com/css?family=Vollkorn:400italic,700italic,400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//brick.a.ssl.fastly.net/Source+Code+Pro:300">
<link href='//fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<!-- stylesheet -->
<link rel="stylesheet" href="../../css/style.css">
<!-- leaflet -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/leaflet/0.7.7/leaflet.css" />
<!-- require combined leaflet, esri leaflet and jsdelivr-rum -->
<script src="//cdn.jsdelivr.net/leaflet/0.7.7/leaflet.js"></script>
<!--script src="//cdn.jsdelivr.net/leaflet.esri/1.0.3/esri-leaflet.js"></script-->
<script src="../../js/esri-leaflet-src.js"></script>
<script src="//cdn.jsdelivr.net/jsdelivr-rum/latest/jsdelivr-rum.min.js"></script>
<!-- 'livereload' for development -->
<script src="//localhost:35729/livereload.js"></script>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-44963317-3', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
</head>
<body>
<div class="page-content ">
<div class="container">
<a class="site-nav" href="../../" class="left logo">Esri Leaflet</a>
<nav class="right">
<a class="site-nav" href="../../examples/">Examples</a>
<a class="site-nav" href="../../api-reference/">API Reference</a>
<a class="site-nav" href="../../download/">Download</a>
<a class="site-nav" href="https://github.com/Esri/esri-leaflet">View on GitHub</a>
</nav>
</div>
<div id="background-map" class="background-map"></div>
<div class="container white">
<aside class="sidebar">
<h5>L.esri.Layers</h5>
<nav>
<a href="../../api-reference/layers/basemap-layer.html">BasemapLayer</a>
<a href="../../api-reference/layers/feature-layer.html">FeatureLayer</a>
<a href="../../api-reference/layers/tiled-map-layer.html">TiledMapLayer</a>
<a href="../../api-reference/layers/dynamic-map-layer.html">DynamicMapLayer</a>
<a href="../../api-reference/layers/image-map-layer.html">ImageMapLayer</a>
<a href="../../api-reference/layers/clustered-feature-layer.html">ClusteredFeatureLayer</a>
<a href="../../api-reference/layers/heatmap-feature-layer.html">HeatmapFeatureLayer</a>
</nav>
<h5>L.esri.Tasks</h5>
<nav>
<a href="../../api-reference/tasks/query.html">Query</a>
<a href="../../api-reference/tasks/find.html">Find</a>
<a href="../../api-reference/tasks/identify-features.html">Identify Features</a>
<a href="../../api-reference/tasks/identify-image.html">Identify Image</a>
<a href="../../api-reference/tasks/task.html">Task</a>
</nav>
<h5>Misc.</h5>
<nav>
<a href="../../api-reference/request.html">Request</a>
<a href="../../api-reference/util.html">Util</a>
<a href="../../api-reference/events.html">Events</a>
</nav>
<h5>Base Classes</h5>
<nav>
<a href="../../api-reference/services/image-service.html">ImageService</a>
<a href="../../api-reference/services/feature-layer.html">FeatureLayerService</a>
<a href="../../api-reference/services/map-service.html">MapService</a>
<a href="../../api-reference/services/service.html">Service</a>
</nav>
</aside>
<div class="main-content">
<h1 id="l-esri-tasks-query">L.esri.Tasks.Query</h1>
<p><code>L.esri.Tasks.Query</code> 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.</p>
<p><strong>Note</strong> 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.</p>
<h3 id="constructor">Constructor</h3>
<table>
<thead>
<tr>
<th>Constructor</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>L.esri.Tasks.query(<nobr class="param"><span><Object></span> <code>options</code></nobr>)</code><br><br>
<code>L.esri.Tasks.query(<nobr class="param"><span><<a href="../../api-reference/services/feature-layer.html">FeatureLayer</a>></span> <code>endpoint</code></nobr>)</code><br><br>
<code>L.esri.Tasks.query(<nobr class="param"><span><<a href="../../api-reference/services/map-service.html">MapService</a>></span> <code>endpoint</code></nobr>)</code><br><br>
<code>L.esri.Tasks.query(<nobr class="param"><span><<a href="../../api-reference/services/image-service.html">ImageService</a>></span> <code>endpoint</code></nobr>)</code>
</td>
<td>Accepts either an <code>options</code> object or an instance of <a href="../..//api-reference/services/map-service.html">MapService</a>, <a href="../..//api-reference/services/feature-layer-service.html">FeatureLayer</a> or <a href="../..//api-reference/service/image-service.html">ImageService</a>.</td>
</tr>
</tbody>
</table>
<h3 id="options">Options</h3>
<table>
<thead>
<tr>
<th>Option</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>url</code></td>
<td><code>String</code></td>
<td><code>''</code></td>
<td>URL of the ArcGIS Server or ArcGIS Online service you would like to consume.</td>
</tr>
<tr>
<td><code>proxy</code></td>
<td><code>String</code></td>
<td><code>false</code></td>
<td>URL of an <a href="https://developers.arcgis.com/javascript/jshelp/ags_proxy.html">ArcGIS API for JavaScript proxy</a> or <a href="https://github.com/Esri/resource-proxy">ArcGIS Resource Proxy</a> to use for proxying POST requests.</td>
</tr>
<tr>
<td><code>useCors</code></td>
<td><code>Boolean</code></td>
<td><code>true</code></td>
<td>If this task should use CORS when making GET requests.</td>
</tr>
</tbody>
</table>
<h3 id="methods">Methods</h3>
<table>
<thead>
<tr>
<th>Method</th>
<th>Returns</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>within(<nobr class="param"><span><Geometry></span> <code>geometry</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features from the service within (fully contained by) the passed geometry object. <code>geometry</code> can be an instance of <code>L.Marker</code>, <code>L.Polygon</code>, <code>L.Polyline</code>, <code>L.LatLng</code>, <code>L.LatLngBounds</code> and <code>L.GeoJSON</code>. It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon.</td>
</tr>
<tr>
<td><code>contains(<nobr class="param"><span><Geometry></span> <code>geometry</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features from the service that fully contain the passed geometry object. <code>geometry</code> can be an instance of <code>L.Marker</code>, <code>L.Polygon</code>, <code>L.Polyline</code>, <code>L.LatLng</code>, <code>L.LatLngBounds</code> and <code>L.GeoJSON</code>. It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon.</td>
</tr>
<tr>
<td><code>intersects(<nobr class="param"><span><Geometry></span> <code>geometry</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features from the service that intersect (touch anywhere) the passed geometry object. <code>geometry</code> can be an instance of <code>L.Marker</code>, <code>L.Polygon</code>, <code>L.Polyline</code>, <code>L.LatLng</code>, <code>L.LatLngBounds</code> and <code>L.GeoJSON</code>. It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon.</td>
</tr>
<tr>
<td><code>overlap(<nobr class="param"><span><Geometry></span> <code>geometry</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features from the service that overlap (touch but are not fully contained by) the passed geometry object. <code>geometry</code> can be an instance of <code>L.Marker</code>, <code>L.Polygon</code>, <code>L.Polyline</code>, <code>L.LatLng</code>, <code>L.LatLngBounds</code> and <code>L.GeoJSON</code>. It can also accept valid GeoJSON Point, Polyline, Polygon objects and GeoJSON Feature objects containing Point, Polyline, Polygon.</td>
</tr>
<tr>
<td><code>nearby(<nobr class="param"><span><<a href="http://leafletjs.com/reference.html#latlng">LatLng</a>></span> <code>latlng</code></nobr>, <nobr class="param"><span><Integer></span> <code>distance</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features a given distance in meters around a <a href="http://leafletjs.com/reference.html#latlng">LatLng</a>. <small>Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3.</small></td>
</tr>
<tr>
<td><code>where(<nobr class="param"><span><String></span> <code>where</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Adds a <code>where</code> clause to the query. String values should be denoted using single quotes ie: <code>query.where("FIELDNAME = 'field value'");</code> More info about valid SQL can be found <a href="http://resources.arcgis.com/en/help/main/10.2/index.html#/SQL_reference_for_query_expressions_used_in_ArcGIS/00s500000033000000/">here</a>.</td>
</tr>
<tr>
<td><code>offset(<nobr class="param"><span><Integer></span> <code>offset</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Define the offset of the results, when combined with <code>limit</code> can be used for paging. <small>Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3.</small></td>
</tr>
<tr>
<td><code>limit(<nobr class="param"><span><Integer></span> <code>limit</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Limit the number of results returned by this query, when combined with <code>offset</code> can be used for paging. <small>Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3.</small></td>
</tr>
<tr>
<td><code>between(<nobr class="param"><span><Date></span> <code>from</code></nobr>, <nobr class="param"><span><Date></span> <code>to</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Queries features within a given time range. <small>Only available for Layers/Services with <code>timeInfo</code> in their metadata.</small></td>
</tr>
<tr>
<td><code>fields(<nobr class="param"><span><Array></span> <code>fields</code></nobr> or <nobr class="param"><span><String></span> <code>fields</code></nobr>)</code></td>
<td><code>this</code></td>
<td>An array of associated fields to request for each feature.</td>
</tr>
<tr>
<td><code>returnGeometry(<nobr class="param"><span><Boolean></span> <code>returnGeometry</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Return geometry with results. Default is <code>true</code>.</td>
</tr>
<tr>
<td><code>simplify(<nobr class="param"><span><<a href="http://leafletjs.com/reference.html#map">Map</a>></span> <code>map</code></nobr>, <nobr class="param"><span><Integer></span> <code>factor</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Simplify the geometries of the output features for the current map view. the <code>factor</code> parameter controls the amount of simplification between 0 (no simplification) and 1 (simplify to the most basic shape possible).</td>
</tr>
<tr>
<td><code>orderBy(<nobr class="param"><span><String></span> <code>fieldName</code></nobr>, <nobr class="param"><span><String></span> <code>order</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Order the output features on certain field either ascending or descending. This can be called multiple times to define a very detailed sort order.</td>
</tr>
<tr>
<td><code>featureIds(<nobr class="param"><span><Array></span> <code>ids</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Return only specific feature IDs if they match other query parameters.</td>
</tr>
<tr>
<td><code>precision(<nobr class="param"><span><Integer></span> <code>precision</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Return only this many decimal points of precision in the output geometries.</td>
</tr>
<tr>
<td><code>token(<nobr class="param"><span><String></span> <code>token</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Adds a token to this request if the service requires authentication. Will be added automatically if used with a service.</td>
</tr>
<tr>
<td><code>layer(<nobr class="param"><span><String or Integer></span> <code>layer</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Used to select which layer inside a Map Service to perform the query on. <br><small>Only available for Map Services.</small></td>
</tr>
<tr>
<td><code>pixelSize(<nobr class="param"><span><<a href="http://leafletjs.com/reference.html#point">Point</a>></span> <code>point</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Override the default pixelSize when querying an Image Service. <br><small>Only available for Image Services.</small></td>
</tr>
<tr>
<td><code>run(<nobr class="param"><span><Function></span> <code>callback</code></nobr>, <nobr class="param"><span><Object></span> <code>context</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Exectues the query request with the current parameters, features will be passed to <code>callback</code> as a <a href="http://geojson.org/geojson-spec.html#feature-collection-objects">GeoJSON FeatureCollection</a>. Accepts an optional function context.</td>
</tr>
<tr>
<td><code>count(<nobr class="param"><span><Function></span> <code>callback</code></nobr>, <nobr class="param"><span><Object></span> <code>context</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Exectues the query request with the current parameters, passing only the number of features matching the query to callback as an <code>Integer</code>. Accepts an optional function context.</td>
</tr>
<tr>
<td><code>ids(<nobr class="param"><span><Function></span> <code>callback</code></nobr>, <nobr class="param"><span><Object></span> <code>context</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Exectues the query request with the current parameters, passing only an array of the feature ids matching the query to callback<code>callback</code>. Accepts an optional function context.</td>
</tr>
<tr>
<td><code>bounds(<nobr class="param"><span><Function></span> <code>callback</code></nobr>, <nobr class="param"><span><Object></span> <code>context</code></nobr>)</code></td>
<td><code>this</code></td>
<td>Executes the query request with the current parameters, passing only the <a href="http://leafletjs.com/reference.html#latlngbounds"><code>LatLngBounds</code></a> of all features matching the query in the <code>callback</code>. Accepts an optional function context. <small>Only available for Feature Layers hosted on ArcGIS Online or ArcGIS Server 10.3.1.</small></td>
</tr>
</tbody>
</table>
<h3 id="examples">Examples</h3>
<h5 id="finding-features-with-map-bounds">Finding features with map bounds</h5>
<pre><code class="language-js"><span class="keyword">var</span> southWest = L.latLng(<span class="number">45.51</span>, -<span class="number">122.70</span>);
<span class="keyword">var</span> northEast = L.latLng(<span class="number">45.52</span>, -<span class="number">122.64</span>);
<span class="keyword">var</span> bounds = L.latLngBounds(southWest, northEast);
<span class="keyword">var</span> query = L.esri.Tasks.query({
url:<span class="string">'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'</span>
});
query.within(bounds);
query.run(<span class="function"><span class="keyword">function</span><span class="params">(error, featureCollection, response)</span>{</span>
console.log(<span class="string">'Found '</span> + featureCollection.features.length + <span class="string">' features'</span>);
});</code></pre>
<h5 id="finding-the-bounds-of-all-features">Finding the bounds of all features</h5>
<pre><code class="language-js"><span class="keyword">var</span> map = L.map(<span class="string">'map'</span>).setView([<span class="number">41.64</span>, -<span class="number">53.70</span>], <span class="number">3</span>);
L.esri.basemapLayer(<span class="string">'Gray'</span>).addTo(map);
<span class="keyword">var</span> query = L.esri.Tasks.query({
url: <span class="string">'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'</span>
});
query.bounds(<span class="function"><span class="keyword">function</span><span class="params">(error, latLngBounds, response)</span>{</span>
map.fitBounds(latLngBounds);
});</code></pre>
<h5 id="querying-features-near-a-latlng">Querying features near a latlng</h5>
<pre><code class="language-js"><span class="keyword">var</span> latlng = L.latLng(<span class="number">45.51</span>, -<span class="number">122.70</span>);
<span class="keyword">var</span> query = L.esri.Tasks.query({
url:<span class="string">'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'</span>
});
query.nearby(latlng, <span class="number">500</span>);
query.run(<span class="function"><span class="keyword">function</span><span class="params">(error, featureCollection, response)</span>{</span>
console.log(<span class="string">'Found '</span> + featureCollection.features.length + <span class="string">' features'</span>);
});</code></pre>
<h5 id="combining-multiple-options">Combining multiple options</h5>
<pre><code class="language-js"><span class="keyword">var</span> latlng = L.latLng(<span class="number">45.51</span>, -<span class="number">122.70</span>);
<span class="keyword">var</span> query = L.esri.Tasks.query({
url: <span class="string">'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'</span>
});
query.nearby(latlng, <span class="number">2000</span>).where(<span class="string">"direction='East'"</span>).orderBy(<span class="string">'stop_id'</span>, <span class="string">'ASC'</span>);
query.count(<span class="function"><span class="keyword">function</span><span class="params">(error, count, response)</span>{</span>
console.log(<span class="string">'Found '</span> + count + <span class="string">' features'</span>);
});
query.ids(<span class="function"><span class="keyword">function</span><span class="params">(error, ids, response)</span>{</span>
console.log(ids.join(<span class="string">', '</span>) + <span class="string">'match the provided parameters'</span>);
});</code></pre>
<h5 id="getting-the-bounds-of-the-query-result">Getting the bounds of the query result</h5>
<pre><code class="language-js"><span class="keyword">var</span> map = L.map(<span class="string">'map'</span>).setView([<span class="number">41.64</span>, -<span class="number">53.70</span>], <span class="number">3</span>);
L.esri.basemapLayer(<span class="string">'Gray'</span>).addTo(map);
<span class="keyword">var</span> query = L.esri.Tasks.query({
url:<span class="string">'http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/stops/FeatureServer/0'</span>
});
query.where(<span class="string">"zone_id='B'"</span>).bounds(<span class="function"><span class="keyword">function</span><span class="params">(error, latLngBounds, response)</span>{</span>
map.fitBounds(latLngBounds);
});</code></pre>
<p><a href="http://github.com/esri/esri-leaflet/edit/master/site/source/pages/api-reference/tasks/query.md">Edit this page on GitHub</a></p>
</div>
<div style='clear:both;'></div>
</div>
<div class="container centered-text">
<p class="copyright">Esri Leaflet is a project from the <a href="http://pdx.esri.com">Esri PDX R&D Center</a> and the <a href="https://github.com/Esri/esri-leaflet/graphs/contributors">Esri Community</a></p>
</div>
</div>
<script src="../../js/script.js"></script>
</body>
</html>