Esri Leaflet

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.

Neighborhoods
<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="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"
    integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ=="
    crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"
    integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log=="
    crossorigin=""></script>


    <!-- Load Esri Leaflet from CDN -->
    <script src="https://unpkg.com/esri-leaflet@2.1.1/dist/esri-leaflet.js"
    integrity="sha512-ECQqaYZke9cSdqlFG08zSkudgrdF6I1d8ViSa7I3VIszJyVqw4ng1G8sehEXlumdMnFYfzY0tMgdQa4WCs9IUw=="
    crossorigin=""></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: 1000;
    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.525, -122.628], 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,
      opacity: 0.4
    }
  }).addTo(map);

  // create a bounds object to query against
  var bounds = L.latLngBounds([
    [45.494556, -122.691536],
    [45.538100, -122.608452]
  ]);

  // create a marker object to query against
  var marker = L.marker([45.571034, -122.686386]).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.559256, -122.611885],
    [45.502256, -122.562790],
    [45.483244, -122.620468]
  ], {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // create a polygon to query against
  var polygon = L.polygon([
    [45.489261, -122.737198],
    [45.486373, -122.688446],
    [45.462539, -122.682266],
    [45.441104, -122.693596],
    [45.444717, -122.722435],
    [45.461094, -122.738228],
    [45.482521, -122.734108],
    [45.489261, -122.737198]
  ], {
    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>