Perform a spatial query

You can perform detailed spatial queries on feature layer attributes.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<html>
  <head>
    <meta charset="utf-8" />
    <title>Perform a spatial query</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.3/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>
      <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>
      <button id="executeQuery">Run Query</button>
    </div>
    <script>
      const apiKey = "YOUR_API_KEY";

      const map = L.map('map').setView([45.525, -122.628], 11);

      L.esri.Vector.vectorBasemapLayer('arcgis/outdoor', {
        apikey: apiKey
      }).addTo(map);

      const neighborhoods = L.esri.featureLayer({
        url: 'https://www.portlandmaps.com/arcgis/rest/services/Public/Zoning/MapServer/32',
        style: {
          color: '#000',
          weight: 1,
          opacity: 0.4
        }
      }).addTo(map);

      const marker = L.marker([45.571034, -122.686386]).addTo(map); // Create a marker object to query against

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

      L.rectangle(bounds, { // Create a rectangle to visualize the bounds
        color: 'blue',
        weight: 2
      }).addTo(map);

      const line = L.polyline([ // Create a line to query against
        [45.559256, -122.611885],
        [45.502256, -122.562790],
        [45.483244, -122.620468]
      ], {
        color: 'blue',
        weight: 2
      }).addTo(map);

      const polygon = L.polygon([ // Create a polygon to query against
        [45.484894, -122.493696],
        [45.512025, -122.492199],
        [45.517669, -122.561457],
        [45.487343, -122.558573]
      ], {
        color: 'blue',
        weight: 2
      }).addTo(map);

      const geometries = {
        bounds: bounds,
        line: line,
        polygon: polygon,
        point: marker
      };

      const relationship = document.getElementById('relationSelect');
      const geometry = document.getElementById('geometrySelect');
      const runQueryButton = document.getElementById('executeQuery');
      let previousIds = [];

      function reset () {  // Reset all features back to their regularly defined styles
        for (let i = previousIds.length - 1; i >= 0; i--) {
          neighborhoods.resetStyle(previousIds[i]);
        }
    }

      function query () {
        reset();
        const inputGeometry = geometries[geometry.value];
        neighborhoods.query()[relationship.value](inputGeometry).ids(function (error, ids) {
          if (error) {
            console.log('Error with query: ' + error);
          } else if (ids) {
            previousIds = ids;
            for (let i = ids.length - 1; i >= 0; i--) {
              neighborhoods.setFeatureStyle(ids[i], { color: 'red', weight: 2 });
            }
          }
        });
      }

      runQueryButton.addEventListener('click', query);

      neighborhoods.once('load', function () {
        query();
      });
    </script>
  </body>
</html>

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.