Skip to content

Line of sight, shadows and profiles 0.x ​

An elevation grid answers more than how steep the ground is. This page asks it what a camera sees from a point, where the Sun's shadows fall, how the ground rises and falls along a line or a route, and what the ground is like inside an area such as a landing ellipse.

The example is the hill from contour lines: 95 m high, falling off as a bell curve 80 m wide, on 41 × 41 cells of 10 m, so 400 m on a side. Its steepest slope is 35.5°, 80 m from the top.

ts
import { createGrid } from '@interstellar-tools/maps';

const values: number[] = [];

for (let row = 0; row < 41; row++)
  for (let column = 0; column < 41; column++) {
    const r = Math.hypot(row - 20, column - 20) * 10; // m from the top

    values.push(95 * Math.exp(-(r * r) / (2 * 80 * 80)));
  }

const hill = createGrid({
  rows: 41,
  columns: 41,
  values,
  cellSize: { x: 10, y: 10 }
});
const R = 6_371_000; // Earth's radius, m

Positions are in grid coordinates: fractional rows and columns, whole numbers at the cells' centres, row 0 to the north. Every number on this page is checked by a test.

Heights between cells ​

gridValueAt reads a cell. gridValueBetween reads anywhere on the grid, blending the four cells round the place:

ts
import { gridValueBetween } from '@interstellar-tools/maps';

gridValueBetween(hill, { row: 20, column: 20.5 }); // ≈ 94.63 m

That's halfway between the top's 95 m and its neighbour's 94.26 m. A place can lie up to half a cell past the outer cells' centres, to the grid's edge, where the outer cells' heights carry on.

Heights along a line or a route ​

profile samples the heights along a straight line, about a cell apart, and profileStats sums them up:

ts
import { profile, profileStats } from '@interstellar-tools/maps';

// West to east, over the top: 41 samples, a cell apart
const across = profile(hill, { row: 20, column: 0 }, { row: 20, column: 40 });

profileStats(across);
// length 400 m, rise 0 m, highest 95 m, lowest ≈ 4.17 m,
// steepestGrade ≈ 35.6°, in radians

Each sample has its distance from the start along the ground and its height, ready to chart. The steepest grade is between neighbouring samples, along the line: across the hill's flank it's the ground's own slope, since the line runs straight down it.

A route is a path of straight legs. pathProfile samples each leg and carries the distance on, sampling each corner once:

ts
import { pathProfile } from '@interstellar-tools/maps';

// Up to the top from the west, then down to the north: 41 samples, over 400 m
const route = pathProfile(hill, [
  { row: 20, column: 0 },
  { row: 20, column: 20 },
  { row: 0, column: 20 }
]);

What can be seen ​

viewshedGrid gives the ground seen from a point, a height above it: 1 where a cell is seen, 0 where it's hidden, the same shape as the grid. Rays run from the point to every cell on the grid's edge, and a cell is seen where nothing nearer on its ray rises above the line to it.

ts
import { viewshedGrid } from '@interstellar-tools/maps';

// From the foot of the hill on the west, 2 m up
const seen = viewshedGrid(hill, { row: 20, column: 0 }, 2, R);
// 311 of the 1 681 cells seen

A bell-shaped hill bulges: from its foot you see up its flank, but not over the bulge, so along the row through the top, everything from column 17 on is hidden, the top itself too. From 100 m up, 1 057 cells are seen, and the row is hidden only past the top, from column 21 on.

The last argument is the body's radius, in metres. Its curve drops far ground below the horizon: from 2 m up on flat ground, the horizon is 2Rh away, 5.0 km on Earth, 3.7 km on Mars and 2.6 km on the Moon. Pass Infinity for flat ground.

Light and shadow ​

hillshadeGrid shades the ground by how squarely it faces the Sun, but it doesn't know that a hill can stand between the Sun and the ground behind it. sunlitGrid finds those cast shadows: 1 where the Sun reaches a cell, 0 in shadow.

ts
import { sunlitGrid } from '@interstellar-tools/maps';

// The Sun in the west, 10° up
const lit = sunlitGrid(hill, toRad(270), toRad(10), R);
// 463 cells in shadow: along the row through the top, columns 22 to 40

The hill's shadow runs east to the grid's edge. With the Sun 30° up, it shrinks to 91 cells, columns 26 to 36 along that row.

Multiply the two for light and shadow together:

ts
import { hillshadeGrid } from '@interstellar-tools/maps';

const shade = hillshadeGrid(hill, toRad(270), toRad(10));
const light = shade.values.map((s, i) => s * lit.values[i]!);
// row 20, column 15: ≈ 0.66, a western slope facing the Sun
// row 20, column 25: 0, facing away, and in the hill's shadow

To light a real site at a real moment, find the Sun's azimuth and elevation there with sunInSky, from geodesy.

Ground in an area ​

ellipseCells keeps a grid's cells inside an ellipse on the ground, and makes the rest NaN. Pass it any grid of the same shape, slopes for example, to ask about the ground inside:

ts
import { ellipseCells, slopeGrid } from '@interstellar-tools/maps';

const ellipse = {
  centre: { row: 20, column: 20 }, // on the top
  semiMajorAxis: 150, // m
  semiMinorAxis: 80, // m
  azimuth: toRad(45) // the long axis to the north-east
};
const slopes = ellipseCells(slopeGrid(hill), ellipse).values.filter(
  (s) => !Number.isNaN(s)
);
// 371 cells with a slope, 320 of them steeper than 25°

ellipseOutline gives places round it to draw it: the first is the end of the long axis, at row ≈ 9.39, column ≈ 30.61 here, 150 m to the north-east.

Smaller and coarser grids ​

cropGrid cuts a grid to a rectangle, the cells whose centres lie in it. downsampleGrid averages blocks of cells into a coarser grid, for a quick look or a 3D mesh:

ts
import { cropGrid, downsampleGrid } from '@interstellar-tools/maps';

cropGrid(hill, { top: 10, bottom: 30, left: 10, right: 30 }); // 21 × 21 cells
downsampleGrid(hill, 4); // 10 × 10 cells of 40 m

Averaging smooths: the coarse grid's highest cell is ≈ 90.0 m, not 95 m.

Edges and gaps ​

How they behave

  • Missing cells. A missing cell casts no shadow and blocks no view. Next to one, a ray takes the nearest cell's own height, so the ground beside a gap still counts.
  • The edges of what's seen lean towards seen by up to a cell: a cell many rays cross is seen if any of them sees it.
  • Light bending in an atmosphere, which on Earth lifts the Sun about half a degree at the horizon and carries sight a little past it, is left out.
  • The grid is flat, with north up. That suits a few kilometres on a map with little distortion there. Over larger areas, the curve is in the viewshed and the shadows, but distances and directions are the map's.
  • A low Sun over a large grid is slow. Each cell's ray runs until it's higher than any ground, so with the Sun a degree or two up it can cross the whole grid. Work on a downsampleGrid first.

Next ​