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.
Where:
is factor, and the sum is over thecells of the block that aren't missing
Units
- The cell size grows by
factorboth ways; values keep their units.
Notes
- Cell
of the result is centred on position 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 nonoData: missing cells areNaN. - For a coarser mesh to draw or to work on quickly. A mean smooths: peaks get lower and slopes gentler.
Parameters
| Parameter | Type | Description |
|---|---|---|
grid | GridInterface | The grid. |
factor | number | Cells to a side of each block (an integer ≥ 1, no more than the grid's rows or columns). |
Returns
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
factoris 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);