-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhydroxyUtils.js
More file actions
56 lines (45 loc) · 1.31 KB
/
hydroxyUtils.js
File metadata and controls
56 lines (45 loc) · 1.31 KB
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
// https://gis.stackexchange.com/a/272805
// max depth -10000 metres
function heightToRgb (h) {
const min = 65536;
const max = 100000;
const M = (max + h * 10);
const r = Math.floor(M / min);
const g = Math.floor(M / 256) - r * 256;
const b = Math.floor(M) - r * min - g * 256;
return { r, g, b };
}
// https://docs.mapbox.com/help/troubleshooting/access-elevation-data/#decode-data
// at -3449, r channel is 0 (g is 255, b is 240).
function rgbToHeight (r, g, b) {
return -10000 + (r * 256 * 256 + g * 256 + b) * 0.1;
}
// pixels - int[] with length a multiple of 4
// pixelCoords - {x: double, y: double, z: double}[]
function modifyTilePixels (pixels, pixelCoords, opts={}) {
opts = {
tileSize: 512,
zCutoff: 0.0,
...opts
};
console.log('modifying tile pixels...')
for (const i in pixelCoords) {
console.log('pixel', i, '/', pixelCoords.length);
const { x, y, z } = pixelCoords[i];
// const row = (opts.tileSize * y) * 4;
// const col = x * 4;
// const idx = row + col;
const idx = ((opts.tileSize * y) + x) * 4;
const mapboxZ = rgbToHeight(pixels[idx], pixels[idx + 1], pixels[idx + 2]);
if (mapboxZ <= opts.zCutoff) {
const { r, g, b } = heightToRgb(z);
pixels[idx] = r;
pixels[idx + 1] = g;
pixels[idx + 2] = b;
}
}
return pixels;
}
module.exports = {
modifyTilePixels
}