Skip to content

Commit 296497a

Browse files
authored
Merge pull request #144 from tilezen/meetar/elevation-encoding
elevation encoding details
2 parents f0c10f8 + eed0909 commit 296497a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/formats.md

+35
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,45 @@ Need help displaying raster tiles in a map? We have several [examples](build-a-m
1515

1616
**Terrarium** format _PNG_ tiles contain raw elevation data in meters, in Web Mercator projection (EPSG:3857). All values are positive with a 32,768 offset, split into the red, green, and blue channels, with 16 bits of integer and 8 bits of fraction.
1717

18+
In other words, the red channel encodes the "256s" place, the green channel the "1s" place, and the blue channel the fractional component, which is 0 - 0.99609375 (255/256) in increments of 0.00390625 (1 / 256).
19+
1820
To decode:
1921

2022
`(red * 256 + green + blue / 256) - 32768`
2123

24+
To encode, asuming a starting value of `v`:
25+
26+
```
27+
v += 32768
28+
r = floor(v/256)
29+
g = floor(v % 256)
30+
b = floor((v - floor(v)) * 256)
31+
```
32+
33+
For example, with a starting value of 2523.266:
34+
35+
```
36+
v += 32768
37+
> 35291.266
38+
r = floor(v/256)
39+
> 137
40+
g = floor(v % 256)
41+
> 219
42+
b = floor((v - floor(v)) * 256)
43+
> 68
44+
45+
> rgb(137, 219, 68)
46+
```
47+
48+
Decoded, this gives us:
49+
50+
```
51+
(r * 256 + g + b / 256) - 32768
52+
> 2523.265625
53+
```
54+
55+
The range of the elevation data (-11000 - 8900 meters) spans `rgb(85, 8, 0)` - `rgb(162, 198, 0)`, or `[0.33203125, 0.03125, 0] - [0.6328125, 0.7734375, 0]`.
56+
2257
## Normal
2358

2459
**Normal** format _PNG_ tiles are processed elevation data with the the red, green, and blue values corresponding to the direction the pixel “surface” is facing (its XYZ vector), in Web Mercator projection (EPSG:3857). The alpha channel contains **quantized elevation data** with values suitable for common hypsometric tint ranges.

0 commit comments

Comments
 (0)