Skip to content

Documentation / @interstellar-tools/maps / downsampleGrid

Function: downsampleGrid() ​

ts
function downsampleGrid(grid: GridInterface, factor: number): GridInterface;

Defined in: terrain/downsample-grid.ts:42

Downsample a grid: each cell of the result is the mean of a block of factor × factor cells, leaving out missing ones.

zr,c′=1n∑i=0f−1∑j=0f−1zrf+i,cf+j

Where:

  • f is factor, and the sum is over the n cells of the block that aren't missing

Units

  • The cell size grows by factor both ways; values keep their units.

Notes

  • Cell (r,c) of the result is centred on position (rf+(f−1)/2, cf+(f−1)/2) of the grid.
  • A part block at the far edges, past the last whole one, is left out.
  • A block with every cell missing is NaN. The result has no noData: missing cells are NaN.
  • For a coarser mesh to draw or to work on quickly. A mean smooths: peaks get lower and slopes gentler.

Parameters ​

ParameterTypeDescription
gridGridInterfaceThe grid.
factornumberCells to a side of each block (an integer ≥ 1, no more than the grid's rows or columns).

Returns ​

GridInterface

The coarser grid.

Throws ​

  • If a size or cell size is not finite, or a cell holds an infinite value.
  • If the grid's parts don't fit together, or factor is not an integer from 1 to the grid's rows and columns.

Example ​

ts
// A 256 × 256 grid of 80 m cells as a 64 × 64 mesh of 320 m cells
const mesh = downsampleGrid(grid, 4);

See ​

cropGrid