Skip to content

Documentation / @interstellar-tools/maps / mapToCell

Function: mapToCell() ​

ts
function mapToCell(
  transform: GeoTransformInterface,
  point: ProjectedPointInterface
): GridPositionInterface;

Defined in: terrain/geotransform.ts:137

Find the grid position of a map point: the fractional row and column under it, through the grid's GeoTransformInterface. The inverse of cellToMap.

c=x−x0Δx,r=y0−yΔy

Units

  • Inputs: { x, y } on the map and the transform, in m.
  • Output: { row, column } in cells, fractional.

Notes

  • Round both to the nearest integer for the cell the point falls in. A point off the grid gives a row or column outside it: check before reading the cell.
  • A projection, such as mercator, takes latitude and longitude to the map first.

Parameters ​

ParameterTypeDescription
transformGeoTransformInterfaceWhere the grid lies on the map.
pointProjectedPointInterfaceThe map position, in m (each finite).

Returns ​

GridPositionInterface

The row and column, fractional.

Throws ​

  • If the transform or the point is not finite.
  • If a cell size is <= 0.

Example ​

ts
// Which cell of an elevation tile is the Matterhorn's summit in?
const transform = tileGeoTransform(tile, 256, 256, 6378137);
const { row, column } = mapToCell(
  transform,
  mercator(toRad(45.9763), toRad(7.6586), 6378137)
);
gridValueAt(grid, Math.round(row), Math.round(column));

See ​