Skip to content

Documentation / @interstellar-tools/maps / stitchTiles

Function: stitchTiles() ​

ts
function stitchTiles(
  tiles: readonly TileGridInterface[],
  R: number
): StitchedTilesInterface;

Defined in: tiles/stitch-tiles.ts:92

Stitch neighbouring tiles into one grid, such as a block of elevation tiles for an area larger than a tile, and find where it lies on the map.

The tiles must fill a rectangle of tiles at one zoom, each with a grid of the same size. Its cells' size, on the ground, is taken at the middle of the whole block, as decodeElevationTile takes a single tile's:

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

Where:

  • R is the sphere's radius (m) the tiles were cut for
  • z is the zoom, w a tile's width in cells, and Δy likewise with its height
  • ytop and ybottom are the block's first and last rows of tiles

Units

  • Inputs: R in m.
  • Output: the grid, cell size in m on the ground, and its transform onto the Mercator map in m.

Notes

  • The tiles may come in any order. A tile that couldn't be fetched can be stood in for by a grid of NaN.
  • Missing cells, noData or NaN in any tile, are NaN in the stitched grid.
  • The cell size is right at the block's middle only, so the error toward its north and south edges grows with the block's height: keep blocks a few tiles high, or project the grid, for slopes far from the middle.
  • Tiles on both sides of the antimeridian don't form a rectangle, as columns 0 and 2z−1 aren't next to each other here: stitch each side apart.

Parameters ​

ParameterTypeDescription
tilesreadonly TileGridInterface[]The tiles and their grids.
RnumberRadius of the sphere the tiles were cut for, in m (finite, > 0).

Returns ​

StitchedTilesInterface

The stitched grid and its transform.

Throws ​

  • If R, a size or a cell size is not finite, or a value is infinite.
  • If there are no tiles, a tile doesn't exist or is given twice, the zooms or grid sizes differ, the tiles don't fill a rectangle, a grid's parts don't fit together, or R is <= 0.

Example ​

ts
// The 2 × 2 tiles around a point, decoded and stitched
const { x, y, z } = tileAt(phi, lambda, 12);
const tiles = [
  { x, y, z },
  { x: x + 1, y, z },
  { x, y: y + 1, z },
  { x: x + 1, y: y + 1, z }
];
const { grid, transform } = stitchTiles(
  tiles.map((tile, i) => ({
    tile,
    grid: decodeElevationTile(images[i], tile, 6378137)
  })),
  6378137
);

See ​