View source: R/rastWithSquareCells.r
rastWithSquareCells | R Documentation |
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.
rastWithSquareCells(x, numCells = NULL, res = NULL, vals = 1:101)
x |
An object with a spatial extent property (e.g., a |
numCells |
Positive integer, approximate number of cells desired. If this is specified, then |
res |
Positive numeric. Size of a cell in the units of the projection of |
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]. |
Raster object. The raster will have an extent of the same size or larger than the extent of x
.
squareRastCells
# 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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.