Skip to content

This sample shows how to generate a chart on the client-side for an ImageryLayer. As the user clicks on or drags the pointer across the view, the app reads and processes the pixel data that is within one mile of the pointer location. The Land Cover Types chart immediately updates with new data as the pointer location changes.

This app displays National Land Cover Database (NLCD) 2001 land cover classification rasters of the conterminous US.

How it works

The land cover imagery layer is initialized with lerc format which returns the raw pixel values for the requested images.

Expand
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
      // Initialize the imagery layer with the LERC format.
      const imageryLayer = new ImageryLayer({ url, format: "lerc" });
      viewElement.map.add(imageryLayer);
      const layerView = await viewElement.whenLayerView(imageryLayer);
Expand

Since lerc format returns raw pixel values of images, we can access the pixel values in the browser. These raw values can be accessed via ImageryLayerView's pixelData and they are updated whenever user zooms or pans the view. The application watches the LayerView's updating property to get the updated pixel values. These values then can be used to create the Land Cover Types chart whenever the user clicks on or drags the pointer over the ImageryLayer.

Expand
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
      const initializeLayerView = (layerView) => {
        // Store the pixel data when the LayerView finishes updating.
        reactiveUtils.watch(
          () => layerView.updating,
          (updating) => (pixelData = !updating ? layerView.pixelData : null),
        );

        handleEventBinding(); // Enable the land cover tool.

        const graphics = imageryLayer.serviceRasterInfo.attributeTable.features;

        // Extract color and class name from each graphic feature.
        graphics.forEach(({ attributes }) => {
          const { Red, Green, Blue, ClassName, Value } = attributes;
          const hexColor = rgbToHex(Red, Green, Blue);
          rasterAttributeGraphics[Value] = { ClassName, hexColor };
        });

        createLandCoverChart(); // Initialize the land cover doughnut chart.
      };

      initializeLayerView(layerView);
Expand

As user clicks on or drags the pointer over the view, the getLandCoverPixelInfo() function is called. In this function, we execute a logic to read and store pixel values that fall within one mile of the pointer location.

Expand
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
      // Get land cover pixel info within one mile of the pointer location.
      const getLandCoverPixelInfo = promiseUtils.debounce(({ x, y }) => {
        if (!(pixelData && pixelData.pixelBlock && pixelData.pixelBlock.pixels)) return;

        const { xmin, xmax } = pixelData.extent;
        const { width, pixels } = pixelData.pixelBlock;

        const center = viewElement.toMap({ x, y }); // Derive mapPoint from pixel coords.
        const screenX = Math.round(x); // Store integer for screen pixel coord x.
        const screenY = Math.round(y); // Store integer for screen pixel coord y.

        const pixelMeterSizeX = Math.abs(xmax - xmin) / width;
        const bufferDim = Math.ceil(1609 / pixelMeterSizeX); // Find pixels per mile.

        // Determine the two-mile extent around the pointer location.
        const bufferLeftPx = screenX - bufferDim < 0 ? 0 : screenX - bufferDim;
        const bufferTopPx = screenY - bufferDim < 0 ? 0 : screenY - bufferDim;
        const startPixel = bufferTopPx * width + bufferLeftPx;
        const bandOne = pixels[0]; // Store the single band of land cover codes.
        const radiusSq = bufferDim * bufferDim;

        // Reuse existing mask; only rebuild if size change (e.g., on zoom).
        if (bufferDim !== cachedBufferDim) {
          cachedMask = createCircleMask(bufferDim, radiusSq);
          cachedBufferDim = bufferDim;
        }

        const { mask, diameter } = cachedMask;
        let oneMilePixelValues = [];

        // Loop over bounding box, using mask to skip irrelevant pixels.
        // Raster is 1D in memory but represents a 2D grid.
        // (row * width) moves down; + col moves right to get pixel index.
        for (let rowOffset = 0; rowOffset < diameter; rowOffset++) {
          const rowStartIndex = startPixel + rowOffset * width;
          for (let colOffset = 0; colOffset < diameter; colOffset++) {
            if (mask[rowOffset * diameter + colOffset]) {
              const pixelIndex = rowStartIndex + colOffset;
              const pixelValue = bandOne[pixelIndex];
              if (pixelValue !== undefined) {
                oneMilePixelValues.push(pixelValue);
              }
            }
          }
        }

        // Clear counts, then tally pixel values in the 1‑mile area.
        pixelValueCount = {};
        for (let i = 0; i < oneMilePixelValues.length; i++) {
          const value = oneMilePixelValues[i];
          pixelValueCount[value] = 1 + (pixelValueCount[value] || 0);
        }

        circleGraphic.geometry = new Circle({ center, radius: 1, radiusUnit: "miles" });
        updateLandCoverChart();
      });
Expand

Once the raw pixel values are processed for the chart, updateLandCovertChart() is called and the chart is updated to reflect the land cover types for the new location.

Expand
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
      // Update the chart with pixel counts and colors within one mile of the pointer.
      const updateLandCoverChart = () => {
        if (infoDiv.style.display !== "block") infoDiv.style.display = "block";
        landCoverChart.data.datasets[0].data = [];
        let landCoverTypeColors = [];
        let landCoverTypeLabels = [];

        // Use the counter object to set chart data, colors, and labels.
        for (const index in pixelValueCount) {
          if (index === "0") {
            landCoverTypeColors.push("rgba(255, 255, 255, 1)");
            landCoverTypeLabels.push("NoData");
          } else {
            const color = rasterAttributeGraphics[index].hexColor;
            landCoverTypeColors.push(color);
            landCoverTypeLabels.push(rasterAttributeGraphics[index].ClassName);
          }
          landCoverChart.data.datasets[0].data.push(pixelValueCount[index]);
        }

        // Apply colors and labels to the chart, then refresh without animation.
        landCoverChart.data.datasets[0].backgroundColor = landCoverTypeColors;
        landCoverChart.data.labels = landCoverTypeLabels;
        landCoverChart.update("none");
      };
Expand

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