Skip to content

Documentation / @interstellar-tools/maps / cropGrid

Function: cropGrid() ​

ts
function cropGrid(
  grid: GridInterface,
  rectangle: GridRectangleInterface
): GridInterface;

Defined in: terrain/crop-grid.ts:50

Crop a grid to a rectangle: the cells whose centres lie in it, edges included, as a grid of their own.

Notes

  • The rectangle can reach past the grid: the part on it is kept.
  • Values are copied as they are, noData with them, so a missing cell stays missing.
  • Cell (0, 0) of the result is the grid's cell at the rectangle's top-left centre: row ⌈top⌉ and column ⌈left⌉, or 0 past the grid's edge.

Parameters ​

ParameterTypeDescription
gridGridInterfaceThe grid.
rectangleGridRectangleInterfaceThe rectangle, in grid coordinates (finite; top ≤ bottom, left ≤ right).

Returns ​

GridInterface

The cells in the rectangle.

Throws ​

  • If a size, cell size or edge is not finite.
  • If the grid's parts don't fit together, the edges are the wrong way round, or no cell's centre is in the rectangle.

Example ​

ts
// The 2 × 2 cells round the middle of a 4 × 4 grid
cropGrid(grid, { top: 1, bottom: 2, left: 1, right: 2 }); // 2 rows, 2 columns

See ​

downsampleGrid