3.4 Raster objects in R
The R package raster
provides three main classes of raster object (more details
here):
- RasterLayer imports a single-layer (variable) raster,
- RasterStack imports in one single object several single-layer (variable) rasters stored in one or different files,
- RasterBrick imports in one single object several single-layer (variable) rasters stored in one single file.
Using RasterStack and RasterBrick requires that the geometry of all raster data is equal.
Package raster
define three classes of rater object we detail below.
3.4.1 RasterLayer
library(raster)
val1 <- matrix(runif(100 * 100, 0, 10), ncol = 100, nrow = 100)
ras1 <- raster(
val1,
crs = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"),
xmn = -82, xmx = +80, ymn = +42, ymx = +44
)
class(ras1)
#R> [1] "RasterLayer"
#R> attr(,"package")
#R> [1] "raster"
#R> [1] 100 100 1
#R> [1] 0.4088394 4.4199565 3.4096969 8.3125694 7.5901618 5.2882679
#R> [1] "+proj=longlat +datum=WGS84 +no_defs"
3.4.2 RasterStack and RasterBrick
http://www.rspatial.org/spatial/rst/4-rasterdata.html#rasterstack-and-rasterbrick
Let’s first create another raster (with the same CRS)
val2 <- matrix(rnorm(100 * 100), ncol = 100, nrow = 100)
ras2 <- raster(
val2,
crs = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"),
xmn = -82, xmx = +80, ymn = +42, ymx = +44
)
class(ras2)
#R> [1] "RasterLayer"
#R> attr(,"package")
#R> [1] "raster"
Let’s stack()
ras1
and ras2
:
#R> [1] "RasterStack"
#R> attr(,"package")
#R> [1] "raster"
Let’s brick()
them:
#R> [1] "RasterBrick"
#R> attr(,"package")
#R> [1] "raster"