Esri Leaflet works with client-side libraries that can perform spatial analysis in the browser like Turf.js. This demo shows how the output from a spatial query can be augmented to isolate features that have a spatial relationship with another arbitrary geometry.
<html>
<head>
<meta charset="utf-8" />
<title>Query client and server-side (Turf.js)</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" crossorigin=""></script>
<!-- Load Esri Leaflet from CDN -->
<script src="https://unpkg.com/esri-leaflet@3.0.10/dist/esri-leaflet.js"></script>
<!-- Load Esri Leaflet Vector from CDN -->
<script src="https://unpkg.com/esri-leaflet-vector@4.2.5/dist/esri-leaflet-vector.js" crossorigin=""></script>
<style>
html,
body,
#map {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #323232;
}
</style>
</head>
<body>
<script src="https://unpkg.com/@turf/turf@5.1.6/turf.min.js"></script>
<style>
#info-pane {
position: absolute;
top: 10px;
right: 10px;
z-index: 1000;
padding: 1em;
background: white;
max-width: 150px;
}
</style>
<div id="map"></div>
<div id="info-pane" class="leaflet-bar">
<label> Census block points that intersect both bounding boxes are red. </label>
</div>
<script type="text/javascript">
const accessToken = "YOUR_ACCESS_TOKEN";
const boundingBoxes = { //Two GeoJSON bounding boxes
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {
color: "orange"
},
geometry: {
type: "Polygon",
coordinates: [
[
[-84.39010620117188, 33.747965492070236],
[-84.39010620117188, 33.75431694675655],
[-84.37311172485352, 33.75431694675655],
[-84.37311172485352, 33.747965492070236],
[-84.39010620117188, 33.747965492070236]
]
]
}
},
{
type: "Feature",
properties: {
color: "#0ceb70"
},
geometry: {
type: "Polygon",
coordinates: [
[
[-84.39963340759277, 33.744254312044156],
[-84.39963340759277, 33.75817040902938],
[-84.38444137573242, 33.75817040902938],
[-84.38444137573242, 33.744254312044156],
[-84.39963340759277, 33.744254312044156]
]
]
}
}
]
};
const map = L.map("map").setView([33.752, -84.385], 14.5);
L.esri.Vector.vectorBasemapLayer("arcgis/imagery/standard", {
token: accessToken
}).addTo(map);
L.geoJSON(boundingBoxes, { //Display the bounding boxes on the map
style: function (feature) {
return { color: feature.properties.color };
}
}).addTo(map);
const query = L.esri.query({
url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/0"
});
query.intersects(boundingBoxes.features[0]);
query.run(function (err, censusCollection, raw) {
if (err) {
return;
}
const features = censusCollection.features;
for (let i = 0; i < features.length; i++) {
if (turf.inside(features[i], boundingBoxes.features[1])) {
L.geoJSON(features[i], {
pointToLayer: function (geoJsonPoint, latlng) {
return L.circleMarker(latlng, {
color: "#ff0066"
});
}
}).addTo(map);
} else {
L.geoJSON(features[i], { //If the smaller box is not contained by the bigger box, make it gray and partially transparent.
pointToLayer: function (geoJsonPoint, latlng) {
return L.circleMarker(latlng, {
radius: 10,
color: "gray",
opacity: 0.2
});
}
}).addTo(map);
}
}
});
</script>
</body>
</html>