library(terra)
## terra 1.7.78
x <- rast()
x
## class : SpatRaster
## dimensions : 180, 360, 1 (nrow, ncol, nlyr)
## resolution : 1, 1 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
x <- rast(ncol=36, nrow=18, xmin=-1000, xmax=1000, ymin=-100, ymax=900)
res(x)
## [1] 55.55556 55.55556
res(x) <- 100
res(x)
## [1] 100 100
## [1] 100 100
ncol(x)
## [1] 20
## [1] 20
# change the numer of columns (affects resolution)
ncol(x) <- 18
ncol(x)
## [1] 18
## [1] 18
res(x)
## [1] 111.1111 100.0000
## [1] 111.1111 100.0000
crs(x) <- "+proj=utm +zone=48 +datum=WGS84"
x
## class : SpatRaster
## dimensions : 10, 18, 1 (nrow, ncol, nlyr)
## resolution : 111.1111, 100 (x, y)
## extent : -1000, 1000, -100, 900 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=48 +datum=WGS84 +units=m +no_defs
## class : SpatRaster
## dimensions : 10, 18, 1 (nrow, ncol, nlyr)
## resolution : 111.1111, 100 (x, y)
## extent : -1000, 1000, -100, 900 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=48 +datum=WGS84 +units=m +no_defs
r <- rast(ncol=10, nrow=10)
ncell(r)
## [1] 100
## [1] 100
hasValues(r)
## [1] FALSE
## [1] FALSE
# use the 'values' function, e.g.,
values(r) <- 1:ncell(r)
# or
set.seed(0)
values(r) <- runif(ncell(r))
hasValues(r)
## [1] TRUE
## [1] TRUE
sources(r)
## [1] ""
## [1] ""
values(r)[1:10]
## [1] 0.8966972 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897
## [8] 0.9446753 0.6607978 0.6291140
## [1] 0.8966972 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897
## [8] 0.9446753 0.6607978 0.6291140
plot(r, main='Raster with 100 cells')
hasValues(r)
## [1] TRUE
## [1] TRUE
res(r)
## [1] 36 18
## [1] 36 18
dim(r)
## [1] 10 10 1
## [1] 10 10 1
xmax(r)
## [1] 180
## [1] 180
# change the maximum x coordinate of the extent (bounding box) of the SpatRaster
xmax(r) <- 0
hasValues(r)
## [1] TRUE
## [1] TRUE
res(r)
## [1] 18 18
## [1] 18 18
dim(r)
## [1] 10 10 1
## [1] 10 10 1
ncol(r) <- 6
hasValues(r)
## [1] FALSE
## [1] FALSE
res(r)
## [1] 30 18
## [1] 30 18
dim(r)
## [1] 10 6 1
## [1] 10 6 1
xmax(r)
## [1] 0
## [1] 0
Supported formats for reading include GeoTIFF, ESRI, ENVI, and ERDAS
filename <- system.file("ex/meuse.tif", package="terra")
filename
## [1] "C:/Users/154455/AppData/Local/R/win-library/4.4/terra/ex/meuse.tif"
## [1] "C:/soft/R/R-4.3.2/library/terra/ex/meuse.tif"
r <- rast(filename)
sources(r)
## [1] "C:/Users/154455/AppData/Local/R/win-library/4.4/terra/ex/meuse.tif"
## [1] "C:/soft/R/R-4.3.2/library/terra/ex/meuse.tif"
hasValues(r)
## [1] TRUE
## [1] TRUE
plot(r, main='SpatRaster from file')
# 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))
s <- c(r1, r2, r3)
s
## class : SpatRaster
## dimensions : 10, 10, 3 (nrow, ncol, nlyr)
## resolution : 36, 18 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
## source(s) : memory
## names : lyr.1, lyr.1, lyr.1
## min values : 0.01307758, 0.02778712, 0.06380247
## max values : 0.99268406, 0.98156346, 0.99607737
## class : SpatRaster
## dimensions : 10, 10, 3 (nrow, ncol, nlyr)
## resolution : 36, 18 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
## source(s) : memory
## names : lyr.1, lyr.1, lyr.1
## min values : 0.01307758, 0.02778712, 0.06380247
## max values : 0.99268406, 0.98156346, 0.99607737
nlyr(s)
## [1] 3
filename <- system.file("ex/logo.tif", package="terra")
filename
## [1] "C:/Users/154455/AppData/Local/R/win-library/4.4/terra/ex/logo.tif"
## [1] "C:/soft/R/R-4.3.2/library/terra/ex/logo.tif"
b <- rast(filename)
b
## class : SpatRaster
## dimensions : 77, 101, 3 (nrow, ncol, nlyr)
## resolution : 1, 1 (x, y)
## extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
## coord. ref. : Cartesian (Meter)
## source : logo.tif
## colors RGB : 1, 2, 3
## names : red, green, blue
## min values : 0, 0, 0
## max values : 255, 255, 255
## class : SpatRaster
## dimensions : 77, 101, 3 (nrow, ncol, nlyr)
## resolution : 1, 1 (x, y)
## extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
## coord. ref. : Cartesian (Meter)
## source : logo.tif
## colors RGB : 1, 2, 3
## names : red, green, blue
## min values : 0, 0, 0
## max values : 255, 255, 255
nlyr(b)
## [1] 3
## [1] 3
r <- b[[2]]