Documentation / @interstellar-tools/maps / decodeElevationTile
Function: decodeElevationTile()
ts
function decodeElevationTile(
pixels: RgbaPixelsInterface,
tile: TileInterface,
R: number,
encoding?: ElevationEncodingType
): GridInterface;Defined in: tiles/elevation-tile.ts:108
Decode an elevation tile, a web map tile whose colours hold heights, into an elevation grid that the terrain functions and contours read.
Each pixel becomes a cell, row 0 at the north as GridInterface has it. The cells' size comes from the tile's place on the Web Mercator map, at the latitude of its middle:
Where:
is the sphere's radius (m) the tiles were cut for is the zoom, the tile's row, and its size in pixels is the latitude of the tile's middle
Units
- Inputs:
Rin m; pixels as bytes. - Output: an elevation grid of heights in m,
heightrows bywidthcolumns, cell size in m.
Notes
- A transparent pixel, alpha 0, is a missing cell:
NaN. Tiles mark the sea or the edge of the data that way, if at all. - The cell size is right at the tile's middle only. Web Mercator stretches the map by
, so it is off by about at the tile's north and south edges, where is half the tile's height in radians: about 0.3 % at 60° at zoom 10, and half that a zoom deeper. Slopes are off by the same factor; use zoom 8 or deeper for terrain. - Cells are square on the map, and so on the ground, for a square tile: Web Mercator keeps shapes.
- Earth's tiles are cut for WGS 84's equatorial radius, 6 378 137 m. Tiles for another body are cut for its own radius.
- The pixels must be decoded exactly: draw the image to a canvas without smoothing or colour management, and read it with
getImageData. A JPEG tile can't be decoded at all, since compression changes the colours.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
pixels | RgbaPixelsInterface | undefined | The tile's pixels: a canvas's ImageData, or the same shape. |
tile | TileInterface | undefined | The tile, { x, y, z }, as tileAt gives it. |
R | number | undefined | Radius of the sphere the tiles were cut for, in m (finite, > 0). |
encoding? | ElevationEncodingType | 'terrarium' | How the colours hold the heights. Defaults to 'terrarium'. |
Returns
The elevation grid.
Throws
- If
Ris not finite, orencodingis not a known encoding. - If the tile doesn't exist,
Ris<= 0,widthorheightis not a positive integer, ordatadoesn't holdwidth × height × 4integers in [0, 255].
Example
ts
// A Terrarium tile from AWS's open terrain tiles, over the Matterhorn
const tile = tileAt(toRad(45.9763), toRad(7.6586), 10); // { x: 533, y: 364, z: 10 }
const image = await createImageBitmap(
await (
await fetch(
`https://s3.amazonaws.com/elevation-tiles-prod/terrarium/${tile.z}/${tile.x}/${tile.y}.png`
)
).blob(),
{ colorSpaceConversion: 'none', premultiplyAlpha: 'none' }
);
const canvas = new OffscreenCanvas(image.width, image.height);
const context = canvas.getContext('2d')!;
context.drawImage(image, 0, 0);
const grid = decodeElevationTile(
context.getImageData(0, 0, image.width, image.height),
tile,
6378137
);See
- tileBounds
- https://github.com/tilezen/joerd/blob/master/docs/formats.md Terrarium and other formats of the open terrain tiles
- https://docs.mapbox.com/data/tilesets/reference/mapbox-terrain-rgb-v1/ Mapbox Terrain-RGB