Learn R Programming

NetLogoR (version 1.0.5)

spatRast2world: Convert a SpatRaster object into a worldMatrix or worldArray object

Description

Convert a SpatRaster object into a worldMatrix object or a worldArray object depending on the number of layers of the SpatRaster object.

Usage

spatRast2world(raster)

# S4 method for SpatRaster spatRast2world(raster)

Value

WorldMatrix or worldArray object depending on the number of layers of the input raster. Patches value are retained from the raster.

Arguments

raster

SpatRaster object.

Author

Sarah Bauduin

Details

See help("worldMatrix-class") or help("worldArray-class") for more details on the classes.

     If the `SpatRaster` object has only one layer, a `worldMatrix` object
     will be returned. If the `SpatRaster` object has more than one layer,
     layers must have unique names and a `worldArray` object will be returned.

The number of rows and columns, as well as the cell values of the `raster` are kept the same. However, to match the coordinates system and resolution of a `worldMatrix` or `worldArray`, the grid is shifted by a 1/2 cell to have round coordinate values at the center of the patches and patch size is equal to (1,1). The bottom left corner cell coordinates of the `worldMatrix` or `worldArray` will be (pxcor = 0, pycor = 0).

Examples

Run this code
library(terra)
r1 <- rast(xmin = 0, xmax = 10, ymin = 0, ymax = 10, nrows = 10, ncols = 10)
r1[] <- runif(100)
w1 <- spatRast2world(r1)
terra::plot(r1)
plot(w1)

r2 <- rast(xmin = 0, xmax = 10, ymin = 0, ymax = 10, nrows = 10, ncols = 10)
r2[] <- 0
r3 <- c(r1, r2)
names(r3) <- c("layer1", "layer2")
w3 <- spatRast2world(r3)
terra::plot(r3)
plot(w3)

Run the code above in your browser using DataLab