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
// fetch tiles visible in the viewfetchTile: function (level, row, col, options) {
const url = this.getTileUrl(level, row, col);
// requested encoded elevation information// the signal option ensures that obsolete requests are abortedreturn geosceneRequest(url, {
responseType: "array-buffer",
signal: options && options.signal
})
.then((response) => {
// create a canvas to draw the processed imageconst canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const width = this.tileInfo.size[0];
const height = this.tileInfo.size[1];
canvas.width = width;
canvas.height = height;
// uncompress raw elevation values in lerc format// into a pre-allocated array of elevation values.const lerc = LercDecode.decode(response.data, { noDataValue: 0 });
// Array of elevation values width*height.const pixels = lerc.pixels[0];
// stats for min, max and no data// values for uncompressed elevationconst stats = lerc.statistics[0];
// set the min and max elevation values set// by the layerconst min = this.minElevation;
const max = this.maxElevation;
const noDataValue = stats.noDataValue;
// Create a new blank image data object with the specified// dimensions.const imageData = context.createImageData(width, height);
// get Uint8ClampedArray representing a one-dimensional array// containing the data in the RGBA order, with integer values// between 0 and 255 (included).const data = imageData.data;
const factor = 256 / (max - min);
let value = 0;
let j;
// Loop through elevation array from lerc to generate an// image that will be displayed.// `pixels` is a flat array of color values and alpha// [r, g, b, a, r, g, b, a, ...]// We need to iterate through elevation and assign color// and alpha values respectively.for (let i = 0; i < width * height; i++) {
// map tile size is 256x256. Elevation values have a// tile size of 257 so we skip the last elevation// whenever "i" is incremented by 256 to jump to the next row. j = i + Math.floor(i / width);
// read the elevation value at the given index from the elevation// array and multiply it by the factor. This will define// the shade of yellow color for the pixel. value = (pixels[j] - min) * factor;
// create RGBA value for the pixel data[i * 4] = value;
data[i * 4 + 1] = value;
data[i * 4 + 2] = 0;
data[i * 4 + 3] = pixels[i] === noDataValue ? 0 : value;
}
// The elevation change image and ready for display context.putImageData(imageData, 0, 0);
return canvas;
});
}