rastWithSquareCells: Create a raster with square cells

View source: R/rastWithSquareCells.r

rastWithSquareCellsR Documentation

Create a raster with square cells

Description

This function creates a raster from an object with an extent (i.e., another raster or a Spatial object) with square cells. The user can specify cell resolution (linear dimension) or the approximate number of cells desired.

Usage

rastWithSquareCells(x, numCells = NULL, res = NULL, vals = 1:101)

Arguments

x

An object with a spatial extent property (e.g., a Raster*, SpatRaster, or Spatial* object).

numCells

Positive integer, approximate number of cells desired. If this is specified, then res is ignored. If this number of cells cannot be fit into the desired extent exactly, then the actual number of cells will be larger.

res

Positive numeric. Size of a cell in the units of the projection of x (typically meters). Ignored if numCells is not NULL.

vals

Numeric, value to assign to cells. Note that if this is shorter than the number of cells in the output, then values will be recycled. If longer, then values will be truncated. The default is to generate random values in the range [0, 1].

Value

Raster object. The raster will have an extent of the same size or larger than the extent of x.

See Also

squareRastCells

Examples

# project outline of Madagascar to equal-area:
data(mad0)
mad0Ea <- sp::spTransform(mad0, getCRS('madAlbers', TRUE))

numCells <- 101
cellSize_meters <- 10E4
byNumCells <- rastWithSquareCells(mad0Ea, numCells=numCells)
byCellSize <- rastWithSquareCells(mad0Ea, res=cellSize_meters)

par(mfrow=c(1, 2))
main1 <- paste0('Cells: ', numCells, ' desired, ',
ncell(byNumCells), ' actual')
plot(byNumCells, main=main1)
plot(mad0Ea, add=TRUE)
main2 <- paste0('Cells ', cellSize_meters, ' m on a side')
plot(byCellSize, main=main2)
plot(mad0Ea, add=TRUE)
# Note that they look the same, but the one on the left has one less row
# than the one on the right.


adamlilith/enmSdm documentation built on Jan. 6, 2023, 11 a.m.