## ----raster-1a---------------------------------------------------------------- library(raster) # RasterLayer with the default parameters x <- raster() x # With other parameters x <- raster(ncol=36, nrow=18, xmn=-1000, xmx=1000, ymn=-100, ymx=900) # that can be changed res(x) # change resolution res(x) <- 100 res(x) ncol(x) # change the numer of columns (affects resolution) ncol(x) <- 18 ncol(x) res(x) # set the coordinate reference system (CRS) (define the projection) projection(x) <- "+proj=utm +zone=48 +datum=WGS84" x ## ----raster-1b, fig=TRUE , echo=TRUE------------------------------------------ r <- raster(ncol=10, nrow=10) ncell(r) hasValues(r) # use the 'values' function # e.g., values(r) <- 1:ncell(r) # or set.seed(0) values(r) <- runif(ncell(r)) hasValues(r) inMemory(r) values(r)[1:10] plot(r, main='Raster with 100 cells') ## ----raster-1c, echo=TRUE----------------------------------------------------- hasValues(r) res(r) dim(r) xmax(r) # change the maximum x coordinate of the extent (bounding box) of the RasterLayer xmax(r) <- 0 hasValues(r) res(r) dim(r) ncol(r) <- 6 hasValues(r) res(r) dim(r) xmax(r) ## ----raster-2a, fig=TRUE , echo=TRUE------------------------------------------ # get the name of an example file installed with the package # do not use this construction of your own files filename <- system.file("external/test.grd", package="raster") filename r <- raster(filename) filename(r) hasValues(r) inMemory(r) plot(r, main='RasterLayer from file') ## ----raster-2b---------------------------------------------------------------- # create three identical RasterLayer objects r1 <- r2 <- r3 <- raster(nrow=10, ncol=10) # Assign random cell values values(r1) <- runif(ncell(r1)) values(r2) <- runif(ncell(r2)) values(r3) <- runif(ncell(r3)) # combine three RasterLayer objects into a RasterStack s <- stack(r1, r2, r3) s nlayers(s) # combine three RasterLayer objects into a RasterBrick b1 <- brick(r1, r2, r3) # equivalent to: b2 <- brick(s) # create a RasterBrick from file filename <- system.file("external/rlogo.grd", package="raster") filename b <- brick(filename) b nlayers(b) # extract a single RasterLayer r <- raster(b, layer=2) # equivalent to creating it from disk r <- raster(filename, band=2)