## ----raster-1a---------------------------------------------------------------- library(terra) x <- rast() x ## ----raster-1aa--------------------------------------------------------------- x <- rast(ncol=36, nrow=18, xmin=-1000, xmax=1000, ymin=-100, ymax=900) res(x) ## ----raster-1aaa-------------------------------------------------------------- res(x) <- 100 res(x) ncol(x) # change the numer of columns (affects resolution) ncol(x) <- 18 ncol(x) res(x) ## ----raster-1aaab------------------------------------------------------------- crs(x) <- "+proj=utm +zone=48 +datum=WGS84" x ## ----raster-1b, fig=TRUE , fig.height=3.5, fig.width=7------------------------ r <- rast(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) sources(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 SpatRaster 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("ex/meuse.tif", package="terra") filename r <- rast(filename) sources(r) hasValues(r) plot(r, main='SpatRaster from file') ## ----raster-2b---------------------------------------------------------------- # create three identical SpatRaster objects r1 <- r2 <- r3 <- rast(nrow=10, ncol=10) # Assign random cell values values(r1) <- runif(ncell(r1)) values(r2) <- runif(ncell(r2)) values(r3) <- runif(ncell(r3)) ## ----raster-2bb--------------------------------------------------------------- s <- c(r1, r2, r3) s nlyr(s) ## ----raster-2bbb-------------------------------------------------------------- filename <- system.file("ex/logo.tif", package="terra") filename b <- rast(filename) b nlyr(b) ## ----raster-2bbbb------------------------------------------------------------- r <- b[[2]]