Query client and server-side (Turf.js)

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.

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
<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.8.0/dist/leaflet.css" crossorigin="" />
    <script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js" crossorigin=""></script>
    <!-- Load Esri Leaflet from CDN -->
    <script src="https://unpkg.com/esri-leaflet@^3.0.8/dist/esri-leaflet.js"></script>
    <!-- Load Esri Leaflet Vector from CDN -->
    <script src="https://unpkg.com/esri-leaflet-vector@4.0.0/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 apiKey = "YOUR_API_KEY";

     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", {
        apikey: apiKey
      }).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>

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