Spatial Queries on a Feature Layer
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.
<html>
<head>
<meta charset=utf-8 />
<title>Spatial Queries on a Feature Layer</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Load Leaflet from CDN-->
<link rel="stylesheet" href="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.css" />
<script src="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.js"></script>
<!-- Load Esri Leaflet from CDN -->
<script src="//cdn.jsdelivr.net/leaflet.esri/1.0.3/esri-leaflet.js"></script>
<style>
body { margin:0; padding:0; }
#map { position: absolute; top:0; bottom:0; right:0; left:0; }
</style>
</head>
<body>
<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>
</body>
</html>
Esri Leaflet is a project from the Esri PDX R&D Center and the Esri Community