esri-leaflet
Version:
Leaflet plugins for consuming ArcGIS Online and ArcGIS Server services.
473 lines (391 loc) • 24.4 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>Spatial Queries on a Feature Layer | Esri Leaflet</title>
<meta name="description" content="Create detailed spatial queries like point within polygon or line intersects polygons with Feature Layer queries. Some combinations will not result in any features being highlighted.">
<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>Basemaps</h5>
<nav>
<a href="../examples/showing-a-basemap.html">Showing an ArcGIS basemap</a>
<a href="../examples/basemap-with-labels.html">Basemap with labels</a>
<a href="../examples/switching-basemaps.html">Switching basemaps</a>
<a href="../examples/retina-basemap.html">Retina Basemap</a>
</nav>
<h5>Feature Layers</h5>
<nav>
<a href="../examples/simple-feature-layer.html">Simple FeatureLayer</a>
<a href="../examples/styling-feature-layer-points.html">Styling points</a>
<a href="../examples/styling-feature-layer-polylines.html">Styling lines</a>
<a href="../examples/styling-feature-layer-polygons.html">Styling polygons</a>
<a href="../examples/feature-layer-popups.html">Custom popups</a>
<a href="../examples/querying-feature-layers-1.html">Querying features #1</a>
<a href="../examples/querying-feature-layers-2.html">Querying features #2</a>
<a href="../examples/spatial-queries.html">Spatial Queries</a>
<a href="../examples/simplifying-complex-features.html">Simplifying complex features</a>
<a href="../examples/zooming-to-all-features-1.html">Zoom to all features #1</a>
<a href="../examples/zooming-to-all-features-2.html">Zoom to all features #2</a>
<a href="../examples/labeling-features.html">Labeling Features</a>
<a href="../examples/clustering-feature-layers.html">Clustering points</a>
<a href="../examples/styling-clusters.html">Styling clusters</a>
<a href="../examples/visualize-points-as-a-heatmap.html">Points as a heatmap</a>
<a href="../examples/styling-heatmaps.html">Styling heatmaps</a>
<a href="../examples/visualizing-time-with-feature-layer.html">Time enabled services</a>
<a href="../examples/feature-layer-snapshot.html">Layer "snapshots"</a>
<a href="../examples/editing.html">Editing</a>
</nav>
<h5>Tile Layers</h5>
<nav>
<a href="../examples/tile-layer-1.html">Tiles from a Map Service #1</a>
<a href="../examples/tile-layer-2.html">Tiles from a Map Service #2</a>
<a href="../examples/non-mercator-projection.html">Non-mercator tiles</a>
</nav>
<h5>Dynamic Map Layer</h5>
<nav>
<a href="../examples/simple-dynamic-map-layer.html">Simple DynamicMapLayer</a>
<a href="../examples/identifying-features.html">Identifying Features</a>
<a href="../examples/finding-features.html">Finding Features</a>
<a href="../examples/customizing-popups.html">Custom popups</a>
<a href="../examples/visualizing-time-on-dynamic-map-layer.html">Time Ranges</a>
<a href="../examples/dynamic-map-layer-reprojected.html">Reprojecting on the fly</a>
</nav>
<h5>Image Map Layer</h5>
<nav>
<a href="../examples/simple-image-map-layer.html">Simple ImageMapLayer</a>
<a href="../examples/infrared-imagery.html">Infrared Imagery using BandIDs</a>
<a href="../examples/image-map-layer-rendering-rule.html">Rendering Rule</a>
<a href="../examples/identifying-imagery.html">Identify Imagery</a>
<a href="../examples/image-map-layer-mosaic-rule.html">Mosaic Rule</a>
<a href="../examples/imagery-popups.html">Custom popups</a>
</nav>
<h5>Authentication</h5>
<nav>
<a href="../examples/arcgis-online-auth.html">ArcGIS Online OAuth</a>
<a href="../examples/premium-content.html">Premium ArcGIS Online content</a>
<a href="../examples/arcgis-server-auth.html">ArcGIS Server username/password</a>
</nav>
<h5>Geocoding</h5>
<nav>
<a href="../examples/geocoding-control.html">Geocoding control</a>
<a href="../examples/search-map-service.html">Searching map service</a>
<a href="../examples/search-feature-layer.html">Searching feature layers</a>
<a href="../examples/center-map-on-address.html">Center the initial map state</a>
<a href="../examples/reverse-geocoding.html">Reverse geocoding</a>
</nav>
<h5>Other Plugins</h5>
<nav>
<a href="../examples/gp-plugin.html">Geoprocessing plugin</a>
<a href="../examples/renderers-plugin.html">Renderers plugin</a>
</nav>
<h5>Misc.</h5>
<nav>
<a href="../examples/getting-service-metadata.html">Getting service metadata</a>
<a href="../examples/parse-feature-collection.html">Parsing Feature Collections</a>
</nav>
</aside>
<div class="example-content">
<div class="wrap">
<h1>Spatial Queries on a Feature Layer</h1>
<p>Create detailed spatial queries like point within polygon or line intersects polygons with Feature Layer queries. Some combinations will not result in any features being highlighted.</p>
</div>
<div id="map-wrapper">
<style>
#panel {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
background: white;
padding: 10px;
}
</style>
<div id="map"></div>
<div id="panel" class="leaflet-bar">
Neighborhoods
<select name="relation" id="relationSelect">
<option value="within">Within<options>
<option value="contains">Contains<options>
<option value="intersects">Intersects<options>
<option value="overlaps">Overlaps<options>
</select>
<select name="geometry" id="geometrySelect">
<option value="bounds">Bounds<options>
<option value="point">Point<options>
<option value="line">Line<options>
<option value="polygon">Polygon<options>
</select>
</div>
<script>
var map = L.map("map").setView([45.525231693565054, -122.62836456298828], 11);
L.esri.basemapLayer("Topographic").addTo(map);
// create our layer
neighborhoods = L.esri.featureLayer({
url: "https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Neighborhoods_pdx/FeatureServer/0",
style: {
color: 'gray',
weight: 1
}
}).addTo(map);
// create a bounds object to query against
var bounds = L.latLngBounds([
[45.494555728818014, -122.69153594970703],
[45.538098589494005, -122.60845184326172]
]);
// create a marker object to query against
var marker = L.marker([45.57103404296416, -122.68638610839842 ]).addTo(map);
// create a rectangle to visualize the bounds
var rect = L.rectangle(bounds, {
color: 'blue',
weight: 2
}).addTo(map);
// create a line to query against
var line = L.polyline([
[45.55925642651572, -122.61188507080078],
[45.50225636595613, -122.56278991699217],
[45.48324350868221, -122.62046813964844]
], {
color: 'blue',
weight: 2
}).addTo(map);
// create a polygon to query against
var polygon = L.polygon([
[45.48926092988004, -122.73719787597655],
[45.48637264792543, -122.68844604492186],
[45.46253867087026, -122.68226623535156],
[45.441103561983795, -122.69359588623047],
[45.44471679159555, -122.7224349975586],
[45.46109386344247, -122.73822784423828],
[45.482521374942834, -122.7341079711914 ],
[45.48926092988004, -122.73719787597655]
], {
color: 'blue',
weight: 2
}).addTo(map);
// collect geometries into an object so we can reference them later
var geometries = {
bounds: bounds,
line: line,
polygon: polygon,
point: marker
};
// get references to our <select> elements
var relationship = document.getElementById('relationSelect');
var geometry = document.getElementById('geometrySelect');
var previousIds = [];
// reset all features back to their regularly defined styles
function reset(){
for (var i = previousIds.length - 1; i >= 0; i--) {
neighborhoods.resetStyle(previousIds[i]);
};
}
// query the API and highlight features
function query(){
reset();
// lookup our input geometry
var inputGeometry = geometries[geometry.value];
// query the service executing the selected relation with the selected input geometry
neighborhoods.query()[relationship.value](inputGeometry).ids(function(error, ids){
previousIds = ids;
for (var i = ids.length - 1; i >= 0; i--) {
neighborhoods.setFeatureStyle(ids[i], { color: 'red', weight: 2 });
};
});
}
// query when an input changes
geometry.addEventListener('change', query);
relationship.addEventListener('change', query);
// once all neighborhoods have loaded run the default query
neighborhoods.once('load', function(){
query();
});
</script>
</div>
<pre><code class="language-xml"><span class="tag"><<span class="title">html</span>></span>
<span class="tag"><<span class="title">head</span>></span>
<span class="tag"><<span class="title">meta</span> <span class="attribute">charset</span>=<span class="value">utf-8</span> /></span>
<span class="tag"><<span class="title">title</span>></span>Spatial Queries on a Feature Layer<span class="tag"></<span class="title">title</span>></span>
<span class="tag"><<span class="title">meta</span> <span class="attribute">name</span>=<span class="value">'viewport'</span> <span class="attribute">content</span>=<span class="value">'initial-scale=1,maximum-scale=1,user-scalable=no'</span> /></span>
<span class="comment"><!-- Load Leaflet from CDN--></span>
<span class="tag"><<span class="title">link</span> <span class="attribute">rel</span>=<span class="value">"stylesheet"</span> <span class="attribute">href</span>=<span class="value">"//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.css"</span> /></span>
<span class="tag"><<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.js"</span>></span><span class="javascript"></span><span class="tag"></<span class="title">script</span>></span>
<span class="comment"><!-- Load Esri Leaflet from CDN --></span>
<span class="tag"><<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"//cdn.jsdelivr.net/leaflet.esri/1.0.3/esri-leaflet.js"</span>></span><span class="javascript"></span><span class="tag"></<span class="title">script</span>></span>
<span class="tag"><<span class="title">style</span>></span><span class="css">
<span class="tag">body</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule"><span class="attribute">padding</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule">}</span></span>
<span class="id">#map</span> <span class="rules">{ <span class="rule"><span class="attribute">position</span>:<span class="value"> absolute</span></span>; <span class="rule"><span class="attribute">top</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule"><span class="attribute">bottom</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule"><span class="attribute">right</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule"><span class="attribute">left</span>:<span class="value"><span class="number">0</span></span></span>; <span class="rule">}</span></span>
</span><span class="tag"></<span class="title">style</span>></span>
<span class="tag"></<span class="title">head</span>></span>
<span class="tag"><<span class="title">body</span>></span>
<span class="tag"><<span class="title">style</span>></span><span class="css">
<span class="id">#panel</span> <span class="rules">{
<span class="rule"><span class="attribute">position</span>:<span class="value"> absolute</span></span>;
<span class="rule"><span class="attribute">top</span>:<span class="value"> <span class="number">10</span>px</span></span>;
<span class="rule"><span class="attribute">right</span>:<span class="value"> <span class="number">10</span>px</span></span>;
<span class="rule"><span class="attribute">z-index</span>:<span class="value"> <span class="number">10</span></span></span>;
<span class="rule"><span class="attribute">background</span>:<span class="value"> white</span></span>;
<span class="rule"><span class="attribute">padding</span>:<span class="value"> <span class="number">10</span>px</span></span>;
<span class="rule">}</span></span>
</span><span class="tag"></<span class="title">style</span>></span>
<span class="tag"><<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"map"</span>></span><span class="tag"></<span class="title">div</span>></span>
<span class="tag"><<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"panel"</span> <span class="attribute">class</span>=<span class="value">"leaflet-bar"</span>></span>
Neighborhoods
<span class="tag"><<span class="title">select</span> <span class="attribute">name</span>=<span class="value">"relation"</span> <span class="attribute">id</span>=<span class="value">"relationSelect"</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"within"</span>></span>Within<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"contains"</span>></span>Contains<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"intersects"</span>></span>Intersects<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"overlaps"</span>></span>Overlaps<span class="tag"><<span class="title">options</span>></span>
<span class="tag"></<span class="title">select</span>></span>
<span class="tag"><<span class="title">select</span> <span class="attribute">name</span>=<span class="value">"geometry"</span> <span class="attribute">id</span>=<span class="value">"geometrySelect"</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"bounds"</span>></span>Bounds<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"point"</span>></span>Point<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"line"</span>></span>Line<span class="tag"><<span class="title">options</span>></span>
<span class="tag"><<span class="title">option</span> <span class="attribute">value</span>=<span class="value">"polygon"</span>></span>Polygon<span class="tag"><<span class="title">options</span>></span>
<span class="tag"></<span class="title">select</span>></span>
<span class="tag"></<span class="title">div</span>></span>
<span class="tag"><<span class="title">script</span>></span><span class="javascript">
<span class="keyword">var</span> map = L.map(<span class="string">"map"</span>).setView([<span class="number">45.525231693565054</span>, -<span class="number">122.62836456298828</span>], <span class="number">11</span>);
L.esri.basemapLayer(<span class="string">"Topographic"</span>).addTo(map);
<span class="comment">// create our layer</span>
neighborhoods = L.esri.featureLayer({
url: <span class="string">"https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Neighborhoods_pdx/FeatureServer/0"</span>,
style: {
color: <span class="string">'gray'</span>,
weight: <span class="number">1</span>
}
}).addTo(map);
<span class="comment">// create a bounds object to query against</span>
<span class="keyword">var</span> bounds = L.latLngBounds([
[<span class="number">45.494555728818014</span>, -<span class="number">122.69153594970703</span>],
[<span class="number">45.538098589494005</span>, -<span class="number">122.60845184326172</span>]
]);
<span class="comment">// create a marker object to query against</span>
<span class="keyword">var</span> marker = L.marker([<span class="number">45.57103404296416</span>, -<span class="number">122.68638610839842</span> ]).addTo(map);
<span class="comment">// create a rectangle to visualize the bounds</span>
<span class="keyword">var</span> rect = L.rectangle(bounds, {
color: <span class="string">'blue'</span>,
weight: <span class="number">2</span>
}).addTo(map);
<span class="comment">// create a line to query against</span>
<span class="keyword">var</span> line = L.polyline([
[<span class="number">45.55925642651572</span>, -<span class="number">122.61188507080078</span>],
[<span class="number">45.50225636595613</span>, -<span class="number">122.56278991699217</span>],
[<span class="number">45.48324350868221</span>, -<span class="number">122.62046813964844</span>]
], {
color: <span class="string">'blue'</span>,
weight: <span class="number">2</span>
}).addTo(map);
<span class="comment">// create a polygon to query against</span>
<span class="keyword">var</span> polygon = L.polygon([
[<span class="number">45.48926092988004</span>, -<span class="number">122.73719787597655</span>],
[<span class="number">45.48637264792543</span>, -<span class="number">122.68844604492186</span>],
[<span class="number">45.46253867087026</span>, -<span class="number">122.68226623535156</span>],
[<span class="number">45.441103561983795</span>, -<span class="number">122.69359588623047</span>],
[<span class="number">45.44471679159555</span>, -<span class="number">122.7224349975586</span>],
[<span class="number">45.46109386344247</span>, -<span class="number">122.73822784423828</span>],
[<span class="number">45.482521374942834</span>, -<span class="number">122.7341079711914</span> ],
[<span class="number">45.48926092988004</span>, -<span class="number">122.73719787597655</span>]
], {
color: <span class="string">'blue'</span>,
weight: <span class="number">2</span>
}).addTo(map);
<span class="comment">// collect geometries into an object so we can reference them later</span>
<span class="keyword">var</span> geometries = {
bounds: bounds,
line: line,
polygon: polygon,
point: marker
};
<span class="comment">// get references to our <select> elements</span>
<span class="keyword">var</span> relationship = document.getElementById(<span class="string">'relationSelect'</span>);
<span class="keyword">var</span> geometry = document.getElementById(<span class="string">'geometrySelect'</span>);
<span class="keyword">var</span> previousIds = [];
<span class="comment">// reset all features back to their regularly defined styles</span>
<span class="function"><span class="keyword">function</span> <span class="title">reset</span><span class="params">()</span>{</span>
<span class="keyword">for</span> (<span class="keyword">var</span> i = previousIds.length - <span class="number">1</span>; i >= <span class="number">0</span>; i--) {
neighborhoods.resetStyle(previousIds[i]);
};
}
<span class="comment">// query the API and highlight features</span>
<span class="function"><span class="keyword">function</span> <span class="title">query</span><span class="params">()</span>{</span>
reset();
<span class="comment">// lookup our input geometry</span>
<span class="keyword">var</span> inputGeometry = geometries[geometry.value];
<span class="comment">// query the service executing the selected relation with the selected input geometry</span>
neighborhoods.query()[relationship.value](inputGeometry).ids(<span class="function"><span class="keyword">function</span><span class="params">(error, ids)</span>{</span>
previousIds = ids;
<span class="keyword">for</span> (<span class="keyword">var</span> i = ids.length - <span class="number">1</span>; i >= <span class="number">0</span>; i--) {
neighborhoods.setFeatureStyle(ids[i], { color: <span class="string">'red'</span>, weight: <span class="number">2</span> });
};
});
}
<span class="comment">// query when an input changes</span>
geometry.addEventListener(<span class="string">'change'</span>, query);
relationship.addEventListener(<span class="string">'change'</span>, query);
<span class="comment">// once all neighborhoods have loaded run the default query</span>
neighborhoods.once(<span class="string">'load'</span>, <span class="function"><span class="keyword">function</span><span class="params">()</span>{</span>
query();
});
</span><span class="tag"></<span class="title">script</span>></span>
<span class="tag"></<span class="title">body</span>></span>
<span class="tag"></<span class="title">html</span>></span></code></pre>
<div class="wrap">
<p><a href="http://github.com/esri/esri-leaflet/edit/master/site/source/pages/examples/spatial-queries.hbs">Edit this sample on GitHub</a></p>
</div>
</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>