Skip to content

Documentation / @interstellar-tools/maps / viewshedGrid

Function: viewshedGrid() ​

ts
function viewshedGrid(
  grid: GridInterface,
  from: GridPositionInterface,
  height: number,
  radius: number
): GridInterface;

Defined in: terrain/viewshed.ts:58

The viewshed of a point on an elevation grid: the ground that can be seen from height m above it, over the terrain between and round the body's curve.

Rays go from the point to every cell on the grid's edge, about half a cell a step, with heights from gridValueBetween, or the nearest cell's own next to a missing one. A cell is seen where no ground nearer on the ray rises above the line to it:

tan⁡θ(d)=z(d)−d22R−(z0+h)d,seen⟺tan⁡θ(d)≥maxd′<dtan⁡θ(d′)

Where:

  • z(d) is the ground's height d along the ray, and z0 the ground's height at the point
  • h is height, and R the body's radius: d2/2R is how far its curve drops the ground below the point's horizon

Units

  • Heights, distances and the radius in m; the position in grid coordinates.

Notes

  • The result is 1 where a cell is seen and 0 where it isn't, the same shape as the grid, with NaN where it's missing. The point's own cell is seen.
  • A cell many rays cross is seen if any of them sees it, so the edges of hidden ground lean towards seen by up to a cell.
  • Light bending in an atmosphere, which on Earth carries sight a little past the geometric horizon, is left out.
  • From 2 m up, the horizon on flat ground is 2Rh away: 5.0 km on Earth, 3.7 km on Mars, 2.6 km on the Moon.

Parameters ​

ParameterTypeDescription
gridGridInterfaceElevation grid, heights in m.
fromGridPositionInterfaceThe point, in grid coordinates (on the grid; its nearest cell not missing).
heightnumberHow far above the ground the eye is, in m (finite, ≥ 0).
radiusnumberThe body's radius, in m (> 0, or Infinity for flat ground).

Returns ​

GridInterface

A grid of the same shape: 1 seen, 0 hidden, NaN missing.

Throws ​

  • If a size, cell size, the position or height is not finite, or a cell holds an infinite value.
  • If the grid's parts don't fit together, the point is off the grid or its nearest cell is missing, height is negative, or radius is not > 0.

Example ​

ts
// What Perseverance's mast camera, 2 m up, could see from where it landed
const seen = viewshedGrid(grid, landing, 2, 3_396_190);

See ​