Skip to content

Documentation / @interstellar-tools/maps / tileAt

Function: tileAt() ​

ts
function tileAt(phi: Radians, lambda: Radians, zoom: number): TileInterface;

Defined in: tiles/tile-at.ts:55

Find the web map tile that holds a point, at a zoom level: the tile scheme web maps use ("slippy maps", XYZ) on the spherical Web Mercator map.

x=⌊λ+π2π2z⌋,y=⌊1−ln⁡(tan⁡φ+sec⁡φ)/π22z⌋

Where:

  • φ and λ are the point's latitude and longitude (rad)
  • z is the zoom level: 2z×2z tiles cover the map
  • ln⁡(tan⁡φ+sec⁡φ) is the Mercator y on a unit sphere (see mercator)

Units

  • Inputs: angles in radians, zoom an integer.
  • Output: { x, y, z }, integer tile indices.

Notes

  • Columns run east from 180° W, and rows run south from the top edge of the map (see TileInterface).
  • The map stops at arctan⁡sinh⁡π≈±85.0511°, where it is square. Latitudes beyond that, up to the poles, are clamped into the top or bottom row.
  • The longitude is taken modulo a whole turn, so 180° E is 180° W, the west edge of column 0.
  • A point exactly on an edge between tiles belongs to the tile east or south of it.
  • The scheme depends only on angles, so it tiles a spherical Mercator map of Mars or the Moon as it does Earth's. A tile is 2πR/2z wide at the equator.

Parameters ​

ParameterTypeDescription
phiRadiansLatitude φ in radians (finite, in [−π/2, π/2]).
lambdaRadiansLongitude λ in radians (finite).
zoomnumberZoom level z (an integer in [0, 30]).

Returns ​

TileInterface

The tile, { x, y, z }.

Throws ​

  • If phi or lambda is not a finite number.
  • If phi is outside [−π/2, π/2], or zoom is not an integer in [0, 30].

Example ​

ts
// Oslo, at zoom 10
tileAt(toRad(59.9139), toRad(10.7522), 10); // { x: 542, y: 297, z: 10 }

See ​