Skip to content

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:

Δx=2πRcos⁡φc2zw,Δy=2πRcos⁡φc2zh,φc=arctan⁡sinh(π(1−2y+12z))

Where:

  • R is the sphere's radius (m) the tiles were cut for
  • z is the zoom, y the tile's row, and w×h its size in pixels
  • φc is the latitude of the tile's middle

Units

  • Inputs: R in m; pixels as bytes.
  • Output: an elevation grid of heights in m, height rows by width columns, 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 1/cos⁡φ, so it is off by about tan⁡φcΔφ 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 ​

ParameterTypeDefault valueDescription
pixelsRgbaPixelsInterfaceundefinedThe tile's pixels: a canvas's ImageData, or the same shape.
tileTileInterfaceundefinedThe tile, { x, y, z }, as tileAt gives it.
RnumberundefinedRadius 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 ​

GridInterface

The elevation grid.

Throws ​

  • If R is not finite, or encoding is not a known encoding.
  • If the tile doesn't exist, R is <= 0, width or height is not a positive integer, or data doesn't hold width × height × 4 integers 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 ​