==1064531== Memcheck, a memory error detector ==1064531== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==1064531== Using Valgrind-3.24.0 and LibVEX; rerun with -h for copyright info ==1064531== Command: /data/blackswan/ripley/R/R-devel-vg/bin/exec/R --vanilla --encoding=UTF-8 ==1064531== R Under development (unstable) (2026-03-07 r89567) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > pkgname <- "terra" > source(file.path(R.home("share"), "R", "examples-header.R")) > options(warn = 1) > library('terra') terra 1.9.1 > > base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') > base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv') > cleanEx() > nameEx("NAflag") > ### * NAflag > > flush(stderr()); flush(stdout()) > > ### Name: NAflag > ### Title: Set the NA flag > ### Aliases: NAflag<- NAflag NAflag,SpatRaster-method > ### NAflag<-,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra"))[[1]] > NAflag(s) <- 255 > plot(s) > NAflag(s) [1] 255 > > > > cleanEx() > nameEx("NIPD") > ### * NIPD > > flush(stderr()); flush(stdout()) > > ### Name: NIDP > ### Title: Number of immediate adjacent cells flowing into each cell > ### Aliases: NIDP NIDP,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > elev1 <- array(NA,c(9,9)) > elev2 <- elev1 > dx <- 1 > dy <- 1 > for (r in 1:nrow(elev1)) { + y <- (r-5)*dx + for (c in 1:ncol(elev1)) { + + x <- (c-5)*dy + elev1[r,c] <- 5*(x^2+y^2) + elev2[r,c] <- 10+5*(abs(x))-0.001*y ### 5*(x^2+y^2) + } + } > > > ## Elevation Raster > elev1 <- rast(elev1) > elev2 <- rast(elev2) > > t(array(elev1[],rev(dim(elev1)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 160 125 100 85 80 85 100 125 160 [2,] 125 90 65 50 45 50 65 90 125 [3,] 100 65 40 25 20 25 40 65 100 [4,] 85 50 25 10 5 10 25 50 85 [5,] 80 45 20 5 0 5 20 45 80 [6,] 85 50 25 10 5 10 25 50 85 [7,] 100 65 40 25 20 25 40 65 100 [8,] 125 90 65 50 45 50 65 90 125 [9,] 160 125 100 85 80 85 100 125 160 > t(array(elev2[],rev(dim(elev2)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 30.004 25.004 20.004 15.004 10.004 15.004 20.004 25.004 30.004 [2,] 30.003 25.003 20.003 15.003 10.003 15.003 20.003 25.003 30.003 [3,] 30.002 25.002 20.002 15.002 10.002 15.002 20.002 25.002 30.002 [4,] 30.001 25.001 20.001 15.001 10.001 15.001 20.001 25.001 30.001 [5,] 30.000 25.000 20.000 15.000 10.000 15.000 20.000 25.000 30.000 [6,] 29.999 24.999 19.999 14.999 9.999 14.999 19.999 24.999 29.999 [7,] 29.998 24.998 19.998 14.998 9.998 14.998 19.998 24.998 29.998 [8,] 29.997 24.997 19.997 14.997 9.997 14.997 19.997 24.997 29.997 [9,] 29.996 24.996 19.996 14.996 9.996 14.996 19.996 24.996 29.996 > > plot(elev1) > plot(elev2) > > ## Flow Direction Raster > flowdir1<- terrain(elev1,v="flowdir") > flowdir2<- terrain(elev2,v="flowdir") > > > t(array(flowdir1[],rev(dim(flowdir1)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 2 2 2 4 4 4 8 8 8 [2,] 2 2 2 4 4 4 8 8 8 [3,] 2 2 2 4 4 4 8 8 8 [4,] 1 1 1 2 4 8 16 16 16 [5,] 1 1 1 1 0 16 16 16 16 [6,] 1 1 1 128 64 32 16 16 16 [7,] 128 128 128 64 64 64 32 32 32 [8,] 128 128 128 64 64 64 32 32 32 [9,] 128 128 128 64 64 64 32 32 32 > t(array(flowdir2[],rev(dim(flowdir2)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 1 1 1 4 16 16 16 16 [2,] 1 1 1 1 4 16 16 16 16 [3,] 1 1 1 1 4 16 16 16 16 [4,] 1 1 1 1 4 16 16 16 16 [5,] 1 1 1 1 4 16 16 16 16 [6,] 1 1 1 1 4 16 16 16 16 [7,] 1 1 1 1 4 16 16 16 16 [8,] 1 1 1 1 4 16 16 16 16 [9,] 1 1 1 1 0 16 16 16 16 > > plot(flowdir1) > plot(flowdir2) > > ## > nidp1 <- NIDP((flowdir1)) > nidp2 <- NIDP((flowdir2)) > > t(array(nidp1[],rev(dim(nidp1)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 0 0 0 0 0 0 0 0 0 [2,] 0 1 1 2 1 2 1 1 0 [3,] 0 1 1 2 1 2 1 1 0 [4,] 0 2 2 3 1 3 2 2 0 [5,] 0 1 1 1 9 1 1 1 0 [6,] 0 2 2 3 1 3 2 2 0 [7,] 0 1 1 2 1 2 1 1 0 [8,] 0 1 1 2 1 2 1 1 0 [9,] 0 0 0 0 0 0 0 0 0 > t(array(nidp2[],rev(dim(nidp2)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 0 1 1 1 2 1 1 1 0 [2,] 0 1 1 1 3 1 1 1 0 [3,] 0 1 1 1 3 1 1 1 0 [4,] 0 1 1 1 3 1 1 1 0 [5,] 0 1 1 1 3 1 1 1 0 [6,] 0 1 1 1 3 1 1 1 0 [7,] 0 1 1 1 3 1 1 1 0 [8,] 0 1 1 1 3 1 1 1 0 [9,] 0 1 1 1 4 1 1 1 0 > > plot(nidp1) > plot(nidp2) > > > > > cleanEx() > nameEx("RGB") > ### * RGB > > flush(stderr()); flush(stdout()) > > ### Name: RGB > ### Title: Layers representing colors > ### Aliases: RGB RGB,SpatRaster-method RGB<- RGB<-,SpatRaster-method > ### colorize colorize,SpatRaster-method has.RGB has.RGB,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > RGB(r) [1] 1 2 3 > plot(r) > has.RGB(r) [1] TRUE > RGB(r) <- NULL > has.RGB(r) [1] FALSE > plot(r) > RGB(r) <- c(3,1,2) > # same as > # r <- RGB(r, c(3,1,2)) > > plot(r) > > RGB(r) <- 1:3 > x <- colorize(r, "col") > y <- colorize(r, "hsv") > z <- colorize(y, "rgb") > > > > cleanEx() > nameEx("SpatExtent-class") > ### * SpatExtent-class > > flush(stderr()); flush(stdout()) > > ### Name: SpatExtent-class > ### Title: Class "SpatExtent" > ### Aliases: SpatExtent SpatExtent-class Rcpp_SpatExtent-class > ### show,SpatExtent-method > ### Keywords: classes spatial > > ### ** Examples > > e <- ext(-180, 180, -90, 90) > e SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("SpatRaster-class") > ### * SpatRaster-class > > flush(stderr()); flush(stdout()) > > ### Name: SpatRaster-class > ### Title: SpatRaster class > ### Aliases: SpatRaster SpatRaster-class Rcpp_SpatRaster-class > ### PackedSpatRaster-class SpatRasterCollection SpatRasterDataset > ### SpatRasterCollection-class SpatRasterDataset-class RasterSource > ### RasterSource-class Rcpp_RasterSource-class SpatCategories > ### SpatCategories-class Rcpp_SpatCategories-class show,SpatRaster-method > ### Keywords: classes spatial > > ### ** Examples > > rast() class : SpatRaster size : 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) > > > > cleanEx() > nameEx("activeCat") > ### * activeCat > > flush(stderr()); flush(stdout()) > > ### Name: activeCat > ### Title: Active category > ### Aliases: activeCat activeCat,SpatRaster-method activeCat<- > ### activeCat<-,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10) > values(r) <- sample(3, ncell(r), replace=TRUE) + 10 > d <- data.frame(id=11:13, cover=c("forest", "water", "urban"), letters=letters[1:3], value=10:12) > levels(r) <- d > > activeCat(r) [1] 1 > activeCat(r) <- 3 > activeCat(r) [1] 3 > > > > cleanEx() > nameEx("add") > ### * add > > flush(stderr()); flush(stdout()) > > ### Name: add > ### Title: Add (in place) a SpatRaster to another SpatRaster or to a > ### SpatRasterDataset or SpatRasterCollection > ### Aliases: add<- add<-,SpatRaster,SpatRaster-method > ### add<-,SpatRasterCollection,SpatRaster-method > ### add<-,SpatRasterDataset,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=5, ncols=9, vals=1:45) > x <- c(r, r*2) > add(x) <- r*3 > x class : SpatRaster size : 5, 9, 3 (nrow, ncol, nlyr) resolution : 40, 36 (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 : 1, 2, 3 max values : 45, 90, 135 > > > > cleanEx() > nameEx("add_abline") > ### * add_abline > > flush(stderr()); flush(stdout()) > > ### Name: add_abline > ### Title: add vertical and/or horizontal lines to a map made with terra > ### Aliases: add_abline > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(system.file("ex/lux.shp", package="terra")) > atx <- seq(xmin(v), xmax(v), .1) > aty <- seq(ymin(v), ymax(v), .1) > plot(v, pax=list(xat=atx, yat=aty), ext=ext(v)+.2) > add_abline(h=aty, v=atx, lty=2, col="gray") > > > > cleanEx() > nameEx("add_mtext") > ### * add_mtext > > flush(stderr()); flush(stdout()) > > ### Name: add_mtext > ### Title: draw a box > ### Aliases: add_mtext > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > > plot(r, axes=FALSE, legend=FALSE) > add_box() > for (i in 1:4) add_mtext("margin text", i, cex=i, col=i, line=2-i) > > > > cleanEx() > nameEx("adjacent") > ### * adjacent > > flush(stderr()); flush(stdout()) > > ### Name: adjacent > ### Title: Adjacent cells or polygons > ### Aliases: adjacent adjacent,SpatRaster-method adjacent,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=10, ncols=10) > adjacent(r, cells=c(1, 5, 55), directions="queen") [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] 0 NaN NaN NaN 10 2 20 11 12 4 NaN NaN NaN 4 6 14 15 16 54 44 45 46 54 56 64 65 66 > r <- rast(nrows=10, ncols=10, crs="+proj=utm +zone=1 +datum=WGS84") > adjacent(r, cells=11, directions="rook") [,1] [,2] [,3] [,4] 10 1 NaN 12 21 > > #same as > rk <- matrix(c(0,1,0,1,0,1,0,1,0), 3, 3) > adjacent(r, cells=11, directions=rk) [,1] [,2] [,3] [,4] 10 1 NaN 12 21 > > ## note that with global lat/lon data the E and W connect > r <- rast(nrows=10, ncols=10, crs="+proj=longlat +datum=WGS84") > adjacent(r, cells=11, directions="rook") [,1] [,2] [,3] [,4] 10 1 20 12 21 > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > a <- adjacent(v, symmetrical=TRUE) > head(a) from to [1,] 1 2 [2,] 1 4 [3,] 1 5 [4,] 2 3 [5,] 2 4 [6,] 2 5 > > > > cleanEx() > nameEx("aggregate") > ### * aggregate > > flush(stderr()); flush(stdout()) > > ### Name: aggregate > ### Title: Aggregate raster or vector data > ### Aliases: aggregate aggregate,SpatRaster-method > ### aggregate,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast() > # aggregated SpatRaster, no values > ra <- aggregate(r, fact=10) > > values(r) <- runif(ncell(r)) > # aggregated raster, max of the values > ra <- aggregate(r, fact=10, fun=max) > > # aggregated raster, 'fact' parameter contains two values, max of the values > # same result as above > rb <- aggregate(r, fact=c(10,10), fun=max) > > # groups of 10 rows and 2 columns are combined into new cells > rc <- aggregate(r, fact=c(10,2), fun=max) > > # multiple layers > s <- c(r, r*2) > x <- aggregate(s, 20) > > > ## SpatVector > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > va <- aggregate(v, "ID_1") > > plot(va, "NAME_1", lwd=5, plg=list(x="topright"), mar=rep(2,4)) > lines(v, lwd=3, col="light gray") > lines(va) > text(v, "ID_1", halo=TRUE) > > > > cleanEx() > nameEx("agitate") > ### * agitate > > flush(stderr()); flush(stdout()) > > ### Name: agitate > ### Title: Add noise to (jitter) a SpatVector of points > ### Aliases: agitate agitate,SpatVector-method agitate,ANY-method > ### Keywords: spatial > > ### ** Examples > > ## SpatVector > f <- system.file("ex/lux.shp", package="terra") > v <- as.points(vect(f)[1]) > p <- agitate(v, 2500) > > > > cleanEx() > nameEx("align") > ### * align > > flush(stderr()); flush(stdout()) > > ### Name: align > ### Title: Align a SpatExtent > ### Aliases: align align,SpatExtent,SpatRaster-method > ### align,SpatExtent,numeric-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > e <- ext(-10.1, 9.9, -20.1, 19.9) > ea <- align(e, r) > e SpatExtent : -10.1, 9.9, -20.1, 19.9 (xmin, xmax, ymin, ymax) > ext(r) SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > ea SpatExtent : -10, 10, -20, 20 (xmin, xmax, ymin, ymax) > > align(e, 0.5) SpatExtent : -10.5, 10, -20.5, 20 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("all.equal") > ### * all.equal > > flush(stderr()); flush(stdout()) > > ### Name: all.equal > ### Title: Compare two SpatRaster, SpatVector, or SpatExtent objects for > ### equality > ### Aliases: all.equal all.equal,SpatRaster,SpatRaster-method > ### all.equal,SpatVector,SpatVector-method > ### all.equal,SpatExtent,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > x <- sqrt(1:100) > mat <- matrix(x, 10, 10) > r1 <- rast(nrows=10, ncols=10, xmin=0, vals = x) > r2 <- rast(nrows=10, ncols=10, xmin=0, vals = mat) > > all.equal(r1, r2) [1] "Mean relative difference: 0.3858482" > all.equal(r1, r1*1) [1] TRUE > all.equal(rast(r1), rast(r2)) [1] TRUE > > # compare geometries > compareGeom(r1, r2) [1] TRUE > > # Compare all cell values for near equality > # as floating point number imprecision can be a problem > m <- minmax(r1 - r2) > all(abs(m) < 1e-7) [1] FALSE > > # comparison of cell values to create new SpatRaster > e <- r1 == r2 > > > > cleanEx() > nameEx("animate") > ### * animate > > flush(stderr()); flush(stdout()) > > ### Name: animate > ### Title: Animate a map > ### Aliases: animate animate,SpatRaster-method animate,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > animate(s, n=1) > > v <- vect(system.file("ex/lux.shp", package="terra")) > animate(v[1:3, ], n=1) > # animate(v, vars=names(v)) > > # you can save an animation to file like this > # animation::saveGIF(terra::animate(v), "animation.gif") > > > > cleanEx() > nameEx("app") > ### * app > > flush(stderr()); flush(stdout()) > > ### Name: app > ### Title: Apply a function to the cells of a SpatRaster > ### Aliases: app app,SpatRaster-method app,SpatRasterDataset-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > x <- c(r, sqrt(r), r+50) > s <- app(x, fun=sum) > s class : SpatRaster size : 10, 10, 1 (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 name : sum min value : 53 max value : 260 > # for a few generic functions like > # "sum", "mean", and "max" you can also do > sum(x) class : SpatRaster size : 10, 10, 1 (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 name : sum min value : 53 max value : 260 > > ## SpatRasterDataset > sd <- sds(x, x*2, x/3) > a <- app(sd, max) > a class : SpatRaster size : 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 : 2, 2, 102 max values : 200, 20, 300 > # same as > max(x, x*2, x/3) class : SpatRaster size : 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 : 2, 2, 102 max values : 200, 20, 300 > # and as (but slower) > b <- app(sd, function(i) max(i)) > > > ## also works for a single layer > f <- function(i) (i+1) * 2 * i + sqrt(i) > s <- app(r, f) > # same as above, but that is not memory-safe > # and has no filename argument > s <- f(r) > > ## Not run: > ##D #### multiple cores > ##D test0 <- app(x, sqrt) > ##D test1 <- app(x, sqrt, cores=2) > ##D > ##D testfun <- function(i) { 2 * sqrt(i) } > ##D test2 <- app(x, fun=testfun, cores =2) > ##D > ##D ## this fails because testfun is not exported to the nodes > ##D # test3 <- app(x, fun=function(i) testfun(i), cores=2) > ##D ## to export it, add it as argument to fun > ##D test3 <- app(x, fun=function(i, ff) ff(i), cores =3, ff=testfun) > ## End(Not run) > > > > cleanEx() > nameEx("approximate") > ### * approximate > > flush(stderr()); flush(stdout()) > > ### Name: approximate > ### Title: Estimate values for cell values that are 'NA' by interpolating > ### between layers > ### Aliases: approximate approximate,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5) > r1 <- setValues(r, runif(ncell(r))) > r2 <- setValues(r, runif(ncell(r))) > r3 <- setValues(r, runif(ncell(r))) > r4 <- setValues(r, runif(ncell(r))) > r5 <- setValues(r, NA) > r6 <- setValues(r, runif(ncell(r))) > r1[6:10] <- NA > r2[5:15] <- NA > r3[8:25] <- NA > s <- c(r1,r2,r3,r4,r5,r6) > s[1:5] <- NA > x1 <- approximate(s) > x2 <- approximate(s, rule=2) > x3 <- approximate(s, rule=2, z=c(1,2,3,5,14,15)) > > > > > cleanEx() > nameEx("arith-generic") > ### * arith-generic > > flush(stderr()); flush(stdout()) > > ### Name: Arith-methods > ### Title: Arithmetic > ### Aliases: Arith-methods Arith,SpatExtent,numeric-method > ### Arith,numeric,SpatExtent-method Arith,SpatExtent,SpatExtent-method > ### Arith,SpatRaster,SpatRaster-method Arith,SpatVector,SpatVector-method > ### Arith,SpatRaster,missing-method Arith,numeric,SpatRaster-method > ### Arith,SpatRaster,numeric-method Arith,logical,SpatRaster-method > ### Arith,SpatRaster,logical-method Arith,missing,SpatRaster-method > ### Arith,SpatRaster,matrix-method Arith,matrix,SpatRaster-method > ### Keywords: methods math spatial > > ### ** Examples > > r1 <- rast(ncols=10, nrows=10) > v <- runif(ncell(r1)) > v[10:20] <- NA > values(r1) <- v > r2 <- rast(r1) > values(r2) <- 1:ncell(r2) / ncell(r2) > r3 <- r1 + r2 > r2 <- r1 / 10 > r3 <- r1 * (r2 - 1 / r2) > > b <- c(r1, r2, r3) > b2 <- b * 10 > > ### SpatExtent methods > x <- ext(0.1, 2.2, 0, 3) > y <- ext(-2, 1, -2,2) > # union > x + y SpatExtent : -2, 2.2, -2, 3 (xmin, xmax, ymin, ymax) > # intersection > x * y SpatExtent : 0.1, 1, 0, 2 (xmin, xmax, ymin, ymax) > > e <- x %% 2 > e SpatExtent : 0, 4, 0, 4 (xmin, xmax, ymin, ymax) > e * 2 SpatExtent : -2, 6, -2, 6 (xmin, xmax, ymin, ymax) > e / 2 SpatExtent : 1, 3, 1, 3 (xmin, xmax, ymin, ymax) > e + 1 SpatExtent : -1, 5, -1, 5 (xmin, xmax, ymin, ymax) > e - 1 SpatExtent : 1, 3, 1, 3 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("as.character") > ### * as.character > > flush(stderr()); flush(stdout()) > > ### Name: as.character > ### Title: Create a text representation of (the skeleton of) an object > ### Aliases: as.character,SpatExtent-method as.character,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > ext(r) SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > ext(c(0, 20, 0, 20)) SpatExtent : 0, 20, 0, 20 (xmin, xmax, ymin, ymax) > > > > > cleanEx() > nameEx("as.data.frame") > ### * as.data.frame > > flush(stderr()); flush(stdout()) > > ### Name: as.data.frame > ### Title: SpatRaster or SpatVector to data.frame > ### Aliases: as.data.frame as.data.frame,SpatRaster-method > ### as.data.frame,SpatVector-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > as.data.frame(v) ID_1 NAME_1 ID_2 NAME_2 AREA POP 1 1 Diekirch 1 Clervaux 312 18081 2 1 Diekirch 2 Diekirch 218 32543 3 1 Diekirch 3 Redange 259 18664 4 1 Diekirch 4 Vianden 76 5163 5 1 Diekirch 5 Wiltz 263 16735 6 2 Grevenmacher 6 Echternach 188 18899 7 2 Grevenmacher 7 Remich 129 22366 8 2 Grevenmacher 12 Grevenmacher 210 29828 9 3 Luxembourg 8 Capellen 185 48187 10 3 Luxembourg 9 Esch-sur-Alzette 251 176820 11 3 Luxembourg 10 Luxembourg 237 182607 12 3 Luxembourg 11 Mersch 233 32112 > > > > cleanEx() > nameEx("as.lines") > ### * as.lines > > flush(stderr()); flush(stdout()) > > ### Name: as.lines > ### Title: Conversion to a SpatVector of lines > ### Aliases: as.lines as.lines,SpatRaster-method as.lines,SpatVector-method > ### as.lines,SpatExtent-method as.lines,matrix-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=2, nrows=2) > values(r) <- 1:ncell(r) > > as.lines(r) class : SpatVector geometry : lines dimensions : 6, 0 (geometries, attributes) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) > > as.lines(ext(r), crs=crs(r)) class : SpatVector geometry : lines dimensions : 1, 0 (geometries, attributes) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) > > p <- as.polygons(r) > as.lines(p) class : SpatVector geometry : lines dimensions : 4, 1 (geometries, attributes) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) names : lyr.1 type : values : 1 2 3 > > > ## with a matrix > s <- cbind(1:5, 1:5) > e <- cbind(1:5, 0) > > as.lines(s) class : SpatVector geometry : lines dimensions : 1, 0 (geometries, attributes) extent : 1, 5, 1, 5 (xmin, xmax, ymin, ymax) coord. ref. : > as.lines(cbind(s, e), "+proj=longlat") class : SpatVector geometry : lines dimensions : 5, 0 (geometries, attributes) extent : 1, 5, 0, 5 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs > > > > > cleanEx() > nameEx("as.list") > ### * as.list > > flush(stderr()); flush(stdout()) > > ### Name: as.list > ### Title: Coerce a Spat* object to a list > ### Aliases: as.list as.list,SpatRaster-method > ### as.list,SpatRasterDataset-method as.list,SpatRasterCollection-method > ### as.list,SpatVector-method as.list,SpatVectorCollection-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > as.list(v) $ID_1 [1] 1 1 1 1 1 2 2 2 3 3 3 3 $NAME_1 [1] "Diekirch" "Diekirch" "Diekirch" "Diekirch" "Diekirch" [6] "Grevenmacher" "Grevenmacher" "Grevenmacher" "Luxembourg" "Luxembourg" [11] "Luxembourg" "Luxembourg" $ID_2 [1] 1 2 3 4 5 6 7 12 8 9 10 11 $NAME_2 [1] "Clervaux" "Diekirch" "Redange" "Vianden" [5] "Wiltz" "Echternach" "Remich" "Grevenmacher" [9] "Capellen" "Esch-sur-Alzette" "Luxembourg" "Mersch" $AREA [1] 312 218 259 76 263 188 129 210 185 251 237 233 $POP [1] 18081 32543 18664 5163 16735 18899 22366 29828 48187 176820 [11] 182607 32112 > > > s <- rast(system.file("ex/logo.tif", package="terra")) + 1 > as.list(s) [[1]] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : memory varname : logo name : red min value : 1 max value : 256 [[2]] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : memory varname : logo name : green min value : 1 max value : 256 [[3]] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : memory varname : logo name : blue min value : 1 max value : 256 > > > > cleanEx() > nameEx("as.points") > ### * as.points > > flush(stderr()); flush(stdout()) > > ### Name: as.points > ### Title: Conversion to a SpatVector of points > ### Aliases: as.points as.points,SpatRaster-method > ### as.points,SpatVector-method as.points,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=2, nrows=2) > values(r) <- 1:ncell(r) > > as.points(r) class : SpatVector geometry : points dimensions : 4, 1 (geometries, attributes) extent : -90, 90, -45, 45 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) names : lyr.1 type : values : 1 2 3 > > p <- as.polygons(r) > as.points(p) class : SpatVector geometry : points dimensions : 16, 1 (geometries, attributes) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) names : lyr.1 type : values : 1 1 1 > > > > cleanEx() > nameEx("as.polygons") > ### * as.polygons > > flush(stderr()); flush(stdout()) > > ### Name: as.polygons > ### Title: Conversion to a SpatVector of polygons > ### Aliases: as.polygons as.polygons,SpatRaster-method > ### as.polygons,SpatVector-method as.polygons,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=2, nrows=2) > values(r) <- 1:ncell(r) > > p <- as.polygons(r) > p class : SpatVector geometry : polygons dimensions : 4, 1 (geometries, attributes) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) names : lyr.1 type : values : 1 2 3 > > > > cleanEx() > nameEx("as.raster") > ### * as.raster > > flush(stderr()); flush(stdout()) > > ### Name: as.raster > ### Title: Coerce to a "raster" object > ### Aliases: as.raster as.raster,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(ncols=3, nrows=3) > values(r) <- 1:ncell(r) > as.raster(r) [,1] [,2] [,3] [1,] "#440154" "#472B7A" "#3A528B" [2,] "#2C718E" "#218F8D" "#27AD80" [3,] "#5BC863" "#ADDC2F" "#FDE725" > > > > cleanEx() > nameEx("atan2") > ### * atan2 > > flush(stderr()); flush(stdout()) > > ### Name: atan2 > ### Title: Two argument arc-tangent > ### Aliases: atan2,SpatRaster,SpatRaster-method atan2 > ### atan_2,SpatRaster,SpatRaster-method atan_2 > ### Keywords: math spatial > > ### ** Examples > > r1 <- rast(nrows=10, ncols=10) > r2 <- rast(nrows=10, ncols=10) > values(r1) <- (runif(ncell(r1))-0.5) * 10 > values(r2) <- (runif(ncell(r1))-0.5) * 10 > atan2(r1, r2) class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : -3.136868 max value : 3.097747 > > > > cleanEx() > nameEx("autocor") > ### * autocor > > flush(stderr()); flush(stdout()) > > ### Name: autocorrelation > ### Title: Spatial autocorrelation > ### Aliases: autocor autocor,numeric-method autocor,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > ### raster > r <- rast(nrows=10, ncols=10, xmin=0) > values(r) <- 1:ncell(r) > > autocor(r) lyr.1 0.8362573 > > # rook's case neighbors > f <- matrix(c(0,1,0,1,0,1,0,1,0), nrow=3) > autocor(r, f) lyr.1 0.8888889 > > # local > rc <- autocor(r, w=f, global=FALSE) > > ### numeric (for vector data) > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > w <- relate(v, relation="touches") > > # global > autocor(v$AREA, w) [1] -0.1531971 > > # local > v$Gi <- autocor(v$AREA, w, "Gi") > plot(v, "Gi") > > > > cleanEx() > nameEx("barplot") > ### * barplot > > flush(stderr()); flush(stdout()) > > ### Name: barplot > ### Title: Bar plot of a SpatRaster > ### Aliases: barplot barplot,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > barplot(r, digits=-1, las=2, ylab="Frequency") > > op <- par(no.readonly = TRUE) > par(mai = c(1, 2, .5, .5)) > barplot(r, breaks=10, col=c("red", "blue"), horiz=TRUE, digits=NULL, las=1) > par(op) > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("bestMatch") > ### * bestMatch > > flush(stderr()); flush(stdout()) > > ### Name: bestMatch > ### Title: bestMatch > ### Aliases: bestMatch bestMatch,SpatRaster,SpatVector-method > ### bestMatch,SpatRaster,data.frame-method > ### bestMatch,SpatRaster,matrix-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/logo.tif", package = "terra") > r <- rast(f) > > # locations of interest > pts <- vect(cbind(c(25.25, 34.324, 43.003), c(54.577, 46.489, 30.905))) > pts$code <- LETTERS[1:3] > > plot(r) > points(pts, pch=20, cex=2, col="red") > text(pts, "code", pos=4, halo=TRUE) > > x <- scale(r) > > s1 <- bestMatch(x, pts, labels=pts$code) > plot(s1) > > # same result > e <- extract(x, pts, ID=FALSE) > s2 <- bestMatch(x, e, labels=c("Ap", "Nt", "Ms")) > > > > cleanEx() > nameEx("boundaries") > ### * boundaries > > flush(stderr()); flush(stdout()) > > ### Name: boundaries > ### Title: Detect boundaries (edges) > ### Aliases: boundaries boundaries,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=18, ncols=36, xmin=0) > r[150:250] <- 1 > r[251:450] <- 2 > bi <- boundaries(r) > bo <- boundaries(r, inner=FALSE) > bc <- boundaries(r, classes=TRUE) > #plot(bc) > > > > cleanEx() > nameEx("box") > ### * box > > flush(stderr()); flush(stdout()) > > ### Name: add_box > ### Title: draw a box > ### Aliases: add_box > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(system.file("ex/lux.shp", package="terra")) > plot(v) > add_box(col="red", lwd=3, xpd=TRUE) > > > > cleanEx() > nameEx("boxplot") > ### * boxplot > > flush(stderr()); flush(stdout()) > > ### Name: boxplot > ### Title: Box plot of SpatRaster data > ### Aliases: boxplot boxplot,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r1 <- r2 <- r3 <- rast(ncols=10, nrows=10) > set.seed(409) > values(r1) <- rnorm(ncell(r1), 100, 40) > values(r2) <- rnorm(ncell(r1), 80, 10) > values(r3) <- rnorm(ncell(r1), 120, 30) > s <- c(r1, r2, r3) > names(s) <- c("Apple", "Pear", "Cherry") > > boxplot(s, notch=TRUE, col=c("red", "blue", "orange"), main="Box plot", ylab="random", las=1) > > op <- par(no.readonly = TRUE) > par(mar=c(4,6,2,2)) > boxplot(s, horizontal=TRUE, col="lightskyblue", axes=FALSE) > axis(1) > axis(2, at=0:3, labels=c("", names(s)), las=1, cex.axis=.9, lty=0) > par(op) > > ## boxplot with 2 layers > v <- vect(system.file("ex/lux.shp", package="terra")) > r <- rast(system.file("ex/elev.tif", package="terra")) > y <- rasterize(v, r, "NAME_2") > b <- boxplot(r, y) > bxp(b) > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("buffer") > ### * buffer > > flush(stderr()); flush(stdout()) > > ### Name: buffer > ### Title: Create a buffer around vector geometries or raster patches > ### Aliases: buffer buffer,SpatRaster-method buffer,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=36, nrows=18) > r[500] <- 1 > b <- buffer(r, width=5000000) > plot(b) > > v <- vect(rbind(c(170,10), c(0,60)), crs="+proj=merc") > b <- buffer(v, 20) > plot(b) > points(v) > > crs(v) <- "+proj=longlat" > b <- buffer(v, 1500000) > plot(b) > points(v) > > > > cleanEx() > nameEx("c") > ### * c > > flush(stderr()); flush(stdout()) > > ### Name: c > ### Title: Combine SpatRaster or SpatVector objects > ### Aliases: c c,SpatRaster-method c,SpatRasterDataset-method > ### c,SpatRasterCollection-method c,SpatVector-method > ### c,SpatVectorCollection-method c,SpatVector-method rbind.SpatVector > ### rbind > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=5, ncols=9) > values(r) <- 1:ncell(r) > x <- c(r, r*2, r*3) > > > > cleanEx() > nameEx("cartogram") > ### * cartogram > > flush(stderr()); flush(stdout()) > > ### Name: cartogram > ### Title: Cartogram > ### Aliases: cartogram cartogram,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v$value <- 1:12 > pnc <- cartogram(v, "value", "nc") > pcirc <- cartogram(v, "value", "circles") > plot(v, col="light gray", border="gray") > lines(pnc, col="red", lwd=2) > lines(pcirc, col="blue", lwd=2) > > > > > cleanEx() > nameEx("catalyze") > ### * catalyze > > flush(stderr()); flush(stdout()) > > ### Name: catalyze > ### Title: Factors to numeric > ### Aliases: as.numeric as.numeric,SpatRaster-method catalyze > ### catalyze,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10) > values(r) <- sample(3, ncell(r), replace=TRUE) + 10 > d <- data.frame(id=11:13, cover=c("forest", "water", "urban"), letters=letters[1:3], value=10:12) > levels(r) <- d > catalyze(r) class : SpatRaster size : 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 : cover, letters, value min values : 1, 1, 10 max values : 3, 3, 12 > > activeCat(r) <- 3 > as.numeric(r) class : SpatRaster size : 10, 10, 1 (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 name : letters min value : 1 max value : 3 > > > > cleanEx() > nameEx("cellSize") > ### * cellSize > > flush(stderr()); flush(stdout()) > > ### Name: cellSize > ### Title: Area covered by each raster cell > ### Aliases: cellSize cellSize,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > > # SpatRaster > r <- rast(nrows=18, ncols=36) > v <- 1:ncell(r) > v[200:400] <- NA > values(r) <- v > > # size of each raster cell > a <- cellSize(r) > > # illustration of distortion > r <- rast(ncols=90, nrows=45, ymin=-80, ymax=80) > m <- project(r, "+proj=merc") > > bad <- init(m, prod(res(m)) / 1000000, wopt=list(names="naive")) > good <- cellSize(m, unit="km", names="corrected") > plot(c(good, bad), nc=1, mar=c(2,2,1,6)) > > > > cleanEx() > nameEx("cells") > ### * cells > > flush(stderr()); flush(stdout()) > > ### Name: cells > ### Title: Get cell numbers > ### Aliases: cells cells,SpatRaster,missing-method > ### cells,SpatRaster,numeric-method cells,SpatRaster,SpatVector-method > ### cells,SpatRaster,SpatExtent-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > r[c(1:25, 31:100)] <- NA > r <- ifel(r > 28, r + 10, r) > > # all cell numbers of cells that are not NA > cells(r) [1] 26 27 28 29 30 > > # cell numbers that match values > x <- cells(r, c(28,38)) > x$lyr.1 [1] 28 > > # cells for points > m <- cbind(x=c(0,10,-30), y=c(40,-10,20)) > cellFromXY(r, m) [1] 26 56 35 > > v <- vect(m) > cells(r, v) ID cell [1,] 1 26 [2,] 2 56 [3,] 3 35 > cells(r, v, method="bilinear") ID c1 c2 c3 c4 w1 w2 w3 w4 [1,] 1 25 26 35 36 0.3611111 0.3611111 0.13888889 0.13888889 [2,] 2 55 56 65 66 0.2098765 0.7345679 0.01234568 0.04320988 [3,] 3 34 35 44 45 0.2037037 0.4074074 0.12962963 0.25925926 > > # cells for polygons > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > r <- rast(v) > cv <- cells(r, v) > > > > cleanEx() > nameEx("centroids") > ### * centroids > > flush(stderr()); flush(stdout()) > > ### Name: centroids > ### Title: Centroids > ### Aliases: centroids centroids,SpatVector-method > ### centroids,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > x <- centroids(v) > y <- centroids(v, TRUE) > > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > centroids(r) x y 1 6.091757 49.77608 > > > > cleanEx() > nameEx("chunk") > ### * chunk > > flush(stderr()); flush(stdout()) > > ### Name: chunk > ### Title: Make a SpatRaster method memory-safe > ### Aliases: chunk chunk,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > f <- function(x, a = 0) { + print("chunk") + sum(x) + a + } > > x <- chunk(s, f, a=100) [1] "chunk" [1] "chunk" [1] "chunk" [1] "chunk" [1] "chunk" [1] "chunk" > > > > > cleanEx() > nameEx("clamp") > ### * clamp > > flush(stderr()); flush(stdout()) > > ### Name: clamp > ### Title: Clamp values > ### Aliases: clamp clamp,SpatRaster-method clamp,numeric-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > rc <- clamp(r, 25, 75) > rc class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : 25 max value : 75 > > > > cleanEx() > nameEx("clamp_ts") > ### * clamp_ts > > flush(stderr()); flush(stdout()) > > ### Name: clamp_ts > ### Title: clamp time series data > ### Aliases: clamp_ts clamp_ts,SpatRaster-method clamp_ts,numeric-method > ### Keywords: spatial > > ### ** Examples > > sigm <- function(x) { .8 / (1 + exp(-(x-10))) + runif(length(x))/4 } > r <- rast(ncols=10, nrows=10, nlyr=50) > s <- seq(5.2, 15,.2) > set.seed(1) > values(r) <- t(replicate(100, sigm(s))) > > x <- clamp_ts(r, TRUE, TRUE) > > plot(unlist(r[4])) > lines(unlist(x[4])) > > > > > cleanEx() > nameEx("classify") > ### * classify > > flush(stderr()); flush(stdout()) > > ### Name: classify > ### Title: Classify (or reclassify) cell values > ### Aliases: classify classify,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- (0:99)/99 > > ## from-to-becomes > # classify the values into three groups > # all values >= 0 and <= 0.25 become 1, etc. > m <- c(0, 0.25, 1, + 0.25, 0.5, 2, + 0.5, 1, 3) > rclmat <- matrix(m, ncol=3, byrow=TRUE) > rc1 <- classify(r, rclmat, include.lowest=TRUE) > > ## cuts > # equivalent to the above, but now a categorical SpatRaster is returned > rc2 <- classify(r, c(0, 0.25, 0.5, 1), include.lowest=TRUE, brackets=TRUE) > freq(rc2) layer value count 1 1 [0 - 0.25] 25 2 1 (0.25 - 0.5] 25 3 1 (0.5 - 1] 50 > > ## is-becomes > x <- round(r*3) > unique(x) lyr.1 1 0 2 1 3 2 4 3 > # replace 0 with NA > y <- classify(x, cbind(0, NA)) > unique(y) lyr.1 1 1 2 2 3 3 > > # multiple replacements > m <- rbind(c(2, 200), c(3, 300)) > m [,1] [,2] [1,] 2 200 [2,] 3 300 > > rcx1 <- classify(x, m) > unique(rcx1) lyr.1 1 0 2 1 3 200 4 300 > > rcx2 <- classify(x, m, others=NA) > unique(rcx2) lyr.1 1 200 2 300 > > > > cleanEx() > nameEx("click") > ### * click > > flush(stderr()); flush(stdout()) > > ### Name: click > ### Title: Query by clicking on a map > ### Aliases: click click,SpatRaster-method click,SpatVector-method > ### click,missing-method > ### Keywords: spatial > > ### ** Examples > > ## Not run: > ##D r <-rast(system.file("ex/elev.tif", package="terra")) > ##D plot(r) > ##D click(r, n=1) > ##D ## now click on the plot (map) > ## End(Not run) > > > cleanEx() > nameEx("coerce") > ### * coerce > > flush(stderr()); flush(stdout()) > > ### Name: coerce > ### Title: Coercion to vector, matrix or array > ### Aliases: as.vector as.matrix as.array as.vector,SpatRaster-method > ### as.matrix,SpatRaster-method as.array,SpatRaster-method > ### as.array,SpatRasterDataset-method as.vector,SpatExtent-method > ### as.matrix,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=2, nrows=2) > values(r) <- 1:ncell(r) > > as.vector(r) [1] 1 2 3 4 > as.matrix(r) lyr.1 [1,] 1 [2,] 2 [3,] 3 [4,] 4 > as.matrix(r, wide=TRUE) [,1] [,2] [1,] 1 2 [2,] 3 4 > as.data.frame(r, xy=TRUE) x y lyr.1 1 -90 45 1 2 90 45 2 3 -90 -45 3 4 90 -45 4 > as.array(r) , , 1 [,1] [,2] [1,] 1 2 [2,] 3 4 > > as.vector(ext(r)) xmin xmax ymin ymax -180 180 -90 90 > as.matrix(ext(r)) min max [1,] -180 180 [2,] -90 90 > > > > cleanEx() > nameEx("collapse") > ### * collapse > > flush(stderr()); flush(stdout()) > > ### Name: tighten > ### Title: tighten SpatRaster or SpatRasterDataset objects > ### Aliases: tighten tighten,SpatRaster-method > ### tighten,SpatRasterDataset-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrow=5, ncol=9, vals=1:45) > x <- c(r, r*2, r*3) > x class : SpatRaster size : 5, 9, 3 (nrow, ncol, nlyr) resolution : 40, 36 (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 : 1, 2, 3 max values : 45, 90, 135 > tighten(x) class : SpatRaster size : 5, 9, 3 (nrow, ncol, nlyr) resolution : 40, 36 (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 : 1, 2, 3 max values : 45, 90, 135 > > > > cleanEx() > nameEx("colors") > ### * colors > > flush(stderr()); flush(stdout()) > > ### Name: colors > ### Title: Color table > ### Aliases: has.colors has.colors,SpatRaster-method coltab > ### coltab,SpatRaster-method coltab<- coltab<-,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=3, nrows=2, vals=1:6) > x <- c(r, r) > names(x) <- c("A", "B") > > coltb <- data.frame(value=1:6, col=rainbow(6, end=.9)) > coltb value col 1 1 #FF0000 2 2 #EBFF00 3 3 #00FF29 4 4 #00C2FF 5 5 #5200FF 6 6 #FF0099 > > plot(r) > > has.colors(r) [1] FALSE > coltab(r) <- coltb > plot(r) > has.colors(r) [1] TRUE > > tb <- coltab(r) > class(tb) [1] "list" > dim(tb[[1]]) [1] 6 5 > > coltab(x, layer="B") <- coltb > > > > cleanEx() > nameEx("combineGeoms") > ### * combineGeoms > > flush(stderr()); flush(stdout()) > > ### Name: combineGeoms > ### Title: Combine geometries > ### Aliases: combineGeoms combineGeoms,SpatVector,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > x1 <- vect("POLYGON ((0 0, 8 0, 8 9, 0 9, 0 0))") > x2 <- vect("POLYGON ((10 4, 12 4, 12 7, 11 7, 11 6, 10 6, 10 4))") > > y1 <- vect("POLYGON ((5 6, 15 6, 15 15, 5 15, 5 6))") > y2 <- vect("POLYGON ((8 2, 9 2, 9 3, 8 3, 8 2))") > y3 <- vect("POLYGON ((2 6, 3 6, 3 8, 2 8, 2 6))") > y4 <- vect("POLYGON ((2 12, 3 12, 3 13, 2 13, 2 12))") > > x <- rbind(x1, x2) > values(x) <- data.frame(xid=1:2) > crs(x) <- "+proj=utm +zone=1" > > y <- rbind(y1, y2, y3, y4) > values(y) <- data.frame(yid=letters[1:4]) > crs(y) <- "+proj=utm +zone=1" > > plot(rbind(x, y), border=c(rep("red",2), rep("blue", 4)), lwd=2) > text(x, "xid") > text(y, "yid") > > v <- combineGeoms(x, y) > plot(v, col=c("red", "blue")) > > v <- combineGeoms(x, y, boundary=FALSE, maxdist=1, minover=.05) > plot(v, col=rainbow(4)) > > > > cleanEx() > nameEx("compare-generics") > ### * compare-generics > > flush(stderr()); flush(stdout()) > > ### Name: Compare-methods > ### Title: Compare and logical methods > ### Aliases: Compare-methods Logic-methods logic compare > ### Compare,SpatRaster,SpatRaster-method > ### Compare,numeric,SpatRaster-method Compare,SpatRaster,numeric-method > ### Compare,SpatRaster,character-method Compare,SpatRaster,matrix-method > ### Compare,matrix,SpatRaster-method Logic,SpatRaster,SpatRaster-method > ### Logic,SpatRaster,numeric-method Logic,numeric,SpatRaster-method > ### Logic,SpatRaster,logical-method Logic,logical,SpatRaster-method > ### Compare,SpatExtent,SpatExtent-method is.na,SpatRaster-method > ### is.nan,SpatRaster-method !,SpatRaster-method > ### is.finite,SpatRaster-method is.infinite,SpatRaster-method > ### logic,SpatRaster-method compare,SpatRaster-method > ### Keywords: methods math spatial > > ### ** Examples > > r1 <- rast(ncols=10, nrows=10) > values(r1) <- runif(ncell(r1)) > r1[10:20] <- NA > r2 <- rast(r1) > values(r2) <- 1:ncell(r2) / ncell(r2) > > x <- is.na(r1) > !x class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : FALSE max value : TRUE > r1 == r2 class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : FALSE max value : FALSE > compare(r1, r2, "==") class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : FALSE max value : FALSE > compare(r1, r2, "==", TRUE) class : SpatRaster size : 10, 10, 1 (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 name : lyr.1 min value : TRUE max value : TRUE > > > > cleanEx() > nameEx("compareGeom") > ### * compareGeom > > flush(stderr()); flush(stdout()) > > ### Name: compareGeom > ### Title: Compare geometries > ### Aliases: compareGeom compareGeom,SpatRaster,SpatRaster-method > ### compareGeom,SpatRaster,SpatRasterCollection-method > ### compareGeom,SpatRaster,list-method > ### compareGeom,SpatRasterCollection,missing-method > ### compareGeom,SpatVector,SpatVector-method > ### compareGeom,SpatVector,missing-method > ### Keywords: spatial > > ### ** Examples > > r1 <- rast() > r2 <- rast() > r3 <- rast() > compareGeom(r1, r2, r3) [1] TRUE > nrow(r3) <- 10 > > > ## Not run: > ##D compareGeom(r1, r3) > ## End(Not run) > > > > cleanEx() > nameEx("concats") > ### * concats > > flush(stderr()); flush(stdout()) > > ### Name: concats > ### Title: Concatenate categorical rasters > ### Aliases: concats concats,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10) > values(r) <- sample(3, ncell(r), replace=TRUE) > levels(r) <- data.frame(id=1:3, cover=c("forest", "water", "urban")) > > rr <- rast(r) > values(rr) <- sample(1:3, ncell(rr), replace=TRUE) > levels(rr) <- data.frame(id=c(1:3), color=c("red", "green", "blue")) > > x <- concats(r, rr) > x class : SpatRaster size : 10, 10, 1 (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 categories : cover_color name : cover_color min value : forest_red max value : urban_blue > levels(x)[[1]] ID cover_color 1 0 forest_red 2 1 forest_green 3 2 forest_blue 4 3 water_red 5 4 water_green 6 5 water_blue 7 6 urban_red 8 7 urban_green 9 8 urban_blue > > > > cleanEx() > nameEx("contour") > ### * contour > > flush(stderr()); flush(stdout()) > > ### Name: contour > ### Title: Contour plot > ### Aliases: contour contour,SpatRaster-method as.contour > ### as.contour,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/elev.tif", package="terra")) > plot(r) > contour(r, add=TRUE) > > v <- as.contour(r) > plot(r) > lines(v) > > contour(r, filled=TRUE, nlevels=5) > > ## if you want a SpatVector with contour lines > template <- disagg(rast(r), 10) > rr <- resample(r, template) > rr <- floor(rr/100) * 100 > v <- as.polygons(rr) > plot(v, 1, col=terrain.colors(7)) > > ## to combine filled contours with contour lines (or other spatial data) > > br <- seq(100, 600, 100) > plot(r, breaks=br) > lines(as.contour(r, levels=br)) > > ## or > x <- as.polygons(classify(r, br)) > plot(x, "elevation") > > > > > cleanEx() > nameEx("convhull") > ### * convhull > > flush(stderr()); flush(stdout()) > > ### Name: hull > ### Title: Convex, concave, rectangular and circular hulls > ### Aliases: hull hull,SpatVector-method convHull > ### convHull,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > p <- vect(system.file("ex/lux.shp", package="terra")) > h <- hull(p) > > plot(p) > lines(h, col="orange") > > hh <- hull(p, "convex", by="NAME_1") > lines(hh, col="purple") > > pts <- centroids(p) > plot(pts, ext=ext(p)+0.1) > lines(hull(pts, type="convex"), col="darkgreen") > lines(hull(pts, type="rect"), col="blue") > lines(hull(pts, type="circle"), col="red") > > > > > cleanEx() > nameEx("costDist") > ### * costDist > > flush(stderr()); flush(stdout()) > > ### Name: costDist > ### Title: Cost-distance > ### Aliases: costDist costDist,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5, crs="+proj=utm +zone=1 +datum=WGS84", + xmin=0, xmax=5, ymin=0, ymax=5, vals=1) > r[13] <- 0 > d <- costDist(r) > plot(d) > text(d, digits=1) > > r <- rast(ncols=10, nrows=10, xmin=0, xmax=10, ymin=0, ymax=10, + vals=10, crs="+proj=utm +zone=1 +datum=WGS84") > r[5, 1] <- -10 > r[2:3, 1] <- r[1, 2:4] <- r[2, 5] <- 0 > r[3, 6] <- r[2, 7] <- r[1, 8:9] <- 0 > r[6, 6:10] <- NA > r[6:9, 6] <- NA > > d <- costDist(r, -10) > plot(d) > text(d, digits=1, cex=.8) > > > > cleanEx() > nameEx("cover") > ### * cover > > flush(stderr()); flush(stdout()) > > ### Name: cover > ### Title: Replace values with values from another object > ### Aliases: cover cover,SpatRaster,SpatRaster-method > ### cover,SpatRaster,missing-method cover,SpatVector,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r1 <- r2 <- rast(ncols=36, nrows=18) > values(r1) <- 1:ncell(r1) > values(r2) <- runif(ncell(r2)) > r2 <- classify(r2, cbind(-Inf, 0.5, NA)) > r3 <- cover(r2, r1) > > > p <- vect(system.file("ex/lux.shp", package="terra")) > e <- as.polygons(ext(6, 6.4, 49.75, 50)) > values(e) <- data.frame(y=10) > > cv <- cover(p, e) > plot(cv, col=rainbow(12)) > ci <- cover(p, e, identity=TRUE) > lines(e, lwd=3) > > plot(ci, col=rainbow(12)) > lines(e, lwd=3) > > > > > cleanEx() > nameEx("crds") > ### * crds > > flush(stderr()); flush(stdout()) > > ### Name: crds > ### Title: Get the coordinates of SpatVector geometries or SpatRaster cells > ### Aliases: crds crds,SpatVector-method crds,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > x1 <- rbind(c(-175,-20), c(-140,55), c(10, 0), c(-140,-60)) > x2 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45)) > x3 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55)) > x4 <- rbind(c(80,0), c(105,13), c(120,2), c(105,-13)) > z <- rbind(cbind(object=1, part=1, x1), cbind(object=2, part=1, x2), + cbind(object=3, part=1, x3), cbind(object=3, part=2, x4)) > colnames(z)[3:4] <- c('x', 'y') > z <- cbind(z, hole=0) > z[(z[, "object"]==3 & z[,"part"]==2), "hole"] <- 1 > > p <- vect(z, "polygons") > crds(p) x y [1,] -175 -20 [2,] -140 55 [3,] 10 0 [4,] -140 -60 [5,] -175 -20 [6,] -125 0 [7,] 0 60 [8,] 40 5 [9,] 15 -45 [10,] -125 0 [11,] -10 0 [12,] 140 60 [13,] 160 0 [14,] 140 -55 [15,] -10 0 [16,] 80 0 [17,] 105 13 [18,] 120 2 [19,] 105 -13 [20,] 80 0 > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > g <- crds(v) > head(g) x y [1,] 6.026519 50.17767 [2,] 6.031361 50.16563 [3,] 6.035646 50.16410 [4,] 6.042747 50.16157 [5,] 6.043894 50.16116 [6,] 6.048243 50.16008 > > > > > cleanEx() > nameEx("crop") > ### * crop > > flush(stderr()); flush(stdout()) > > ### Name: crop > ### Title: Cut out a geographic subset > ### Aliases: crop crop,SpatRaster-method crop,SpatRasterDataset-method > ### crop,SpatRasterCollection-method crop,SpatVector-method > ### crop,SpatGraticule-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(xmin=0, xmax=10, ymin=0, ymax=10, nrows=25, ncols=25) > values(r) <- 1:ncell(r) > e <- ext(-5, 5, -5, 5) > rc <- crop(r, e) > > # crop and mask > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > cm <- crop(r, v[9:12,], mask=TRUE) > plot(cm) > lines(v) > > # crop vector > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > e <- ext(6.15, 6.3, 49.7, 49.8) > x <- crop(v, e) > plot(x, "NAME_1") > > > > cleanEx() > nameEx("crosstab") > ### * crosstab > > flush(stderr()); flush(stdout()) > > ### Name: crosstab > ### Title: Cross-tabulate > ### Aliases: crosstab crosstab,SpatRaster,missing-method > ### Keywords: methods spatial > > ### ** Examples > > r <- s <- rast(nc=5, nr=5) > set.seed(1) > values(r) <- runif(ncell(r)) * 2 > values(s) <- runif(ncell(r)) * 3 > x <- c(r, s) > > crosstab(x) lyr.1.1 lyr.1 0 1 2 3 0 1 1 4 0 1 2 5 5 0 2 0 2 4 1 > > rs <- r/s > r[1:5] <- NA > s[20:25] <- NA > x <- c(r, s, rs) > crosstab(x, useNA=TRUE, long=TRUE) lyr.1 lyr.1 lyr.1 n 1 0 2 0 3 2 0 NA 0 1 3 0 NA 6 1 4 1 0 4 1 5 1 1 1 1 6 1 1 2 1 7 1 2 0 3 8 1 2 1 1 9 1 NA 0 1 10 1 NA 1 1 11 2 1 1 2 12 2 2 1 2 13 2 NA 1 2 14 NA 0 19 1 15 NA 1 0 2 16 NA 1 1 1 17 NA 3 1 1 > > > > cleanEx() > nameEx("crs") > ### * crs > > flush(stderr()); flush(stdout()) > > ### Name: crs > ### Title: Get or set a coordinate reference system > ### Aliases: crs crs<- crs,SpatExtent-method crs,SpatRaster-method > ### crs,SpatRasterDataset-method crs<-,SpatRaster-method > ### crs<-,SpatRaster,ANY-method crs,SpatVector-method > ### crs,SpatVectorProxy-method crs,SpatVectorCollection-method > ### crs<-,SpatVector-method crs<-,SpatVector,ANY-method crs,sf-method > ### crs,character-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > crs(r) [1] "GEOGCRS[\"WGS 84 (CRS84)\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"unknown\"],\n AREA[\"World\"],\n BBOX[-90,-180,90,180]],\n ID[\"OGC\",\"CRS84\"]]" > crs(r, describe=TRUE, proj=TRUE) name authority code area extent 1 WGS 84 (CRS84) OGC CRS84 World -180, 180, -90, 90 proj 1 +proj=longlat +datum=WGS84 +no_defs > > crs(r) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84" > crs(r) [1] "PROJCRS[\"unknown\",\n BASEGEOGCRS[\"unknown\",\n DATUM[\"Unknown based on WGS 84 ellipsoid\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",7030]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]]],\n CONVERSION[\"unknown\",\n METHOD[\"Lambert Conic Conformal (2SP)\",\n ID[\"EPSG\",9802]],\n PARAMETER[\"Latitude of false origin\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8821]],\n PARAMETER[\"Longitude of false origin\",-100,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8822]],\n PARAMETER[\"Latitude of 1st standard parallel\",48,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8823]],\n PARAMETER[\"Latitude of 2nd standard parallel\",33,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8824]],\n PARAMETER[\"Easting at false origin\",0,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8826]],\n PARAMETER[\"Northing at false origin\",0,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8827]]],\n CS[Cartesian,2],\n AXIS[\"(E)\",east,\n ORDER[1],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]],\n AXIS[\"(N)\",north,\n ORDER[2],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]]]" > > # You can use epsg codes > crs(r) <- "epsg:25831" > crs(r, describe=TRUE)$area [1] "Europe between 0°E and 6°E: Andorra; Belgium - onshore and offshore; Denmark - offshore; Germany - offshore; Jan Mayen - offshore; Norway including Svalbard - onshore and offshore; Spain - onshore and offshore" > > crs("epsg:25831", describe=TRUE) name authority code 1 ETRS89 / UTM zone 31N EPSG 25831 area 1 Europe between 0°E and 6°E: Andorra; Belgium - onshore and offshore; Denmark - offshore; Germany - offshore; Jan Mayen - offshore; Norway including Svalbard - onshore and offshore; Spain - onshore and offshore extent 1 0.00, 6.01, 37.00, 82.45 > > > > cleanEx() > nameEx("datatype") > ### * datatype > > flush(stderr()); flush(stdout()) > > ### Name: datatype > ### Title: Data type of a SpatRaster or SpatVector > ### Aliases: datatype datatype,SpatVector-method datatype,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > datatype(v) [1] "double" "string" "double" "string" "double" "double" > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > datatype(r) [1] "INT2S" > > # no data type > datatype(rast()) [1] "" > > > > cleanEx() > nameEx("deepcopy") > ### * deepcopy > > flush(stderr()); flush(stdout()) > > ### Name: deepcopy > ### Title: Deep copy > ### Aliases: deepcopy deepcopy,SpatRaster-method deepcopy,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10, nl=3) > x <- r > y <- deepcopy(r) > ext(r) SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > set.ext(x, c(0,10,0,10)) > ext(x) SpatExtent : 0, 10, 0, 10 (xmin, xmax, ymin, ymax) > ext(r) SpatExtent : 0, 10, 0, 10 (xmin, xmax, ymin, ymax) > ext(y) SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("densify") > ### * densify > > flush(stderr()); flush(stdout()) > > ### Name: densify > ### Title: Add additional nodes to lines or polygons > ### Aliases: densify densify,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(rbind(c(-120,-20), c(-80,5), c(-40,-60), c(-120,-20)), + type="polygons", crs="+proj=longlat") > vd <- densify(v, 200000) > > p <- project(v, "+proj=robin") > pd <- project(vd, "+proj=robin") > > # good > plot(pd, col="gray", border="red", lwd=10) > points(pd, col="gray") > > # bad > lines(p, col="blue", lwd=3) > points(p, col="blue", cex=2) > plot(p, col="blue", alpha=.1, add=TRUE) > legend("topright", c("good", "bad"), col=c("red", "blue"), lty=1, lwd=3) > > ## the other way around does not work > ## unless the original data was truly planar (e.g. derived from a map) > x <- densify(p, 250000) > y <- project(x, "+proj=longlat") > # bad > plot(y) > # good > lines(vd, col="red") > > > > cleanEx() > nameEx("density") > ### * density > > flush(stderr()); flush(stdout()) > > ### Name: density > ### Title: Density plot > ### Aliases: density density,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > logo <- rast(system.file("ex/logo.tif", package="terra")) > density(logo) > > > > cleanEx() > nameEx("depth") > ### * depth > > flush(stderr()); flush(stdout()) > > ### Name: depth > ### Title: depth of SpatRaster layers > ### Aliases: depth depth<- depth,SpatRaster-method > ### depth<-,SpatRaster-method depthName depthName<- > ### depthName,SpatRaster-method depthName<-,SpatRaster-method depthUnit > ### depthUnit<- depthUnit,SpatRaster-method depthUnit<-,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(nlyr=3) > > depth(s) <- c(0, pi/2, pi) > depth(s) [1] 0.000000 1.570796 3.141593 > > depthName(s) <- "angle" > depthUnit(s) <- "radians" > s class : SpatRaster size : 180, 360, 3 (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) depth : 0 to 3.14159265358979 (angle [radians]: 3 steps) > > > > cleanEx() > nameEx("describe") > ### * describe > > flush(stderr()); flush(stdout()) > > ### Name: describe > ### Title: describe > ### Aliases: describe describe,character-method describe,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > describe(f) [1] "Driver: GTiff/GeoTIFF" [2] "Files: /data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/elev.tif" [3] "Size is 95, 90" [4] "Coordinate System is:" [5] "GEOGCRS[\"WGS 84\"," [6] " ENSEMBLE[\"World Geodetic System 1984 ensemble\"," [7] " MEMBER[\"World Geodetic System 1984 (Transit)\"]," [8] " MEMBER[\"World Geodetic System 1984 (G730)\"]," [9] " MEMBER[\"World Geodetic System 1984 (G873)\"]," [10] " MEMBER[\"World Geodetic System 1984 (G1150)\"]," [11] " MEMBER[\"World Geodetic System 1984 (G1674)\"]," [12] " MEMBER[\"World Geodetic System 1984 (G1762)\"]," [13] " MEMBER[\"World Geodetic System 1984 (G2139)\"]," [14] " ELLIPSOID[\"WGS 84\",6378137,298.257223563," [15] " LENGTHUNIT[\"metre\",1]]," [16] " ENSEMBLEACCURACY[2.0]]," [17] " PRIMEM[\"Greenwich\",0," [18] " ANGLEUNIT[\"degree\",0.0174532925199433]]," [19] " CS[ellipsoidal,2]," [20] " AXIS[\"geodetic latitude (Lat)\",north," [21] " ORDER[1]," [22] " ANGLEUNIT[\"degree\",0.0174532925199433]]," [23] " AXIS[\"geodetic longitude (Lon)\",east," [24] " ORDER[2]," [25] " ANGLEUNIT[\"degree\",0.0174532925199433]]," [26] " USAGE[" [27] " SCOPE[\"Horizontal component of 3D system.\"]," [28] " AREA[\"World.\"]," [29] " BBOX[-90,-180,90,180]]," [30] " ID[\"EPSG\",4326]]" [31] "Data axis to CRS axis mapping: 2,1" [32] "Origin = (5.741666666666666,50.191666666666663)" [33] "Pixel Size = (0.008333333333333,-0.008333333333333)" [34] "Metadata:" [35] " AREA_OR_POINT=Area" [36] "Image Structure Metadata:" [37] " COMPRESSION=LZW" [38] " INTERLEAVE=BAND" [39] "Corner Coordinates:" [40] "Upper Left ( 5.7416667, 50.1916667) ( 5d44'30.00\"E, 50d11'30.00\"N)" [41] "Lower Left ( 5.7416667, 49.4416667) ( 5d44'30.00\"E, 49d26'30.00\"N)" [42] "Upper Right ( 6.5333333, 50.1916667) ( 6d32' 0.00\"E, 50d11'30.00\"N)" [43] "Lower Right ( 6.5333333, 49.4416667) ( 6d32' 0.00\"E, 49d26'30.00\"N)" [44] "Center ( 6.1375000, 49.8166667) ( 6d 8'15.00\"E, 49d49' 0.00\"N)" [45] "Band 1 Block=95x43 Type=Int16, ColorInterp=Gray" [46] " Description = elevation" [47] " Min=141.000 Max=547.000 " [48] " Minimum=141.000, Maximum=547.000, Mean=-9999.000, StdDev=-9999.000" [49] " NoData Value=-32768" [50] " Metadata:" [51] " STATISTICS_MAXIMUM=547" [52] " STATISTICS_MEAN=-9999" [53] " STATISTICS_MINIMUM=141" [54] " STATISTICS_STDDEV=-9999" > describe(f, meta=TRUE) [1] "AREA_OR_POINT=Area" > #g <- describe(f, options=c("json", "nomd", "proj4")) > #head(g) > > > > cleanEx() > nameEx("diff") > ### * diff > > flush(stderr()); flush(stdout()) > > ### Name: diff > ### Title: Lagged differences > ### Aliases: diff diff,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > d <- diff(s) > > > > cleanEx() > nameEx("dimensions") > ### * dimensions > > flush(stderr()); flush(stdout()) > > ### Name: dimensions > ### Title: Dimensions of a SpatRaster or SpatVector and related objects > ### Aliases: size length ncol nrow ncell nlyr nsrc dim res xres yres ncol<- > ### nrow<- nlyr<- res<- size,SpatRaster-method ncell,ANY-method > ### nrow,SpatRaster-method ncol,SpatRaster-method > ### nrow,SpatRasterDataset-method ncol,SpatRasterDataset-method > ### nrow,SpatRasterCollection-method ncol,SpatRasterCollection-method > ### nrow<-,SpatRaster,numeric-method ncol<-,SpatRaster,numeric-method > ### nlyr,SpatRasterDataset-method res<-,SpatRaster,numeric-method > ### nlyr,SpatRaster-method nlyr<-,SpatRaster,numeric-method > ### nsrc,SpatRaster-method ncell,SpatRaster-method > ### ncell,SpatRasterDataset-method length,SpatRasterDataset-method > ### length,SpatRasterCollection-method nlyr,SpatRasterCollection-method > ### dim,SpatRaster-method dim,SpatRasterDataset-method > ### dim,SpatRasterCollection-method dim<-,SpatRaster-method > ### dim,SpatVector-method dim,SpatVectorProxy-method > ### res,SpatRasterDataset-method res,SpatRaster-method > ### res<-,SpatRaster-method xres,SpatRaster-method yres,SpatRaster-method > ### nrow,SpatVector-method ncol,SpatVector-method > ### length,SpatVector-method length,SpatVectorCollection-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > ncol(r) [1] 360 > nrow(r) [1] 180 > nlyr(r) [1] 1 > dim(r) [1] 180 360 1 > nsrc(r) [1] 1 > ncell(r) [1] 64800 > > rr <- c(r,r) > nlyr(rr) [1] 2 > nsrc(rr) [1] 2 > ncell(rr) [1] 64800 > > nrow(r) <- 18 > ncol(r) <- 36 > # equivalent to > dim(r) <- c(18, 36) > > dim(r) [1] 18 36 1 > dim(r) <- c(10, 10, 5) > dim(r) [1] 10 10 5 > > xres(r) [1] 36 > yres(r) [1] 18 > res(r) [1] 36 18 > > res(r) <- 1/120 > # different xres and yres > res(r) <- c(1/120, 1/60) > > > > cleanEx() > nameEx("direction") > ### * direction > > flush(stderr()); flush(stdout()) > > ### Name: direction > ### Title: Direction > ### Aliases: direction direction,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncol=36,nrow=18, crs="+proj=merc") > values(r) <- NA > r[306] <- 1 > b <- direction(r, degrees=TRUE) > plot(b) > > crs(r) <- "+proj=longlat" > b <- direction(r) > plot(b) > > > > > cleanEx() > nameEx("disaggregate") > ### * disaggregate > > flush(stderr()); flush(stdout()) > > ### Name: disagg > ### Title: Disaggregate raster cells or vector geometries > ### Aliases: disagg disagg,SpatRaster-method disagg,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > rd <- disagg(r, fact=c(10, 2)) > ncol(rd) [1] 20 > nrow(rd) [1] 100 > values(r) <- 1:ncell(r) > rd <- disagg(r, fact=c(4, 2)) > > > > > cleanEx() > nameEx("distance") > ### * distance > > flush(stderr()); flush(stdout()) > > ### Name: distance > ### Title: Geographic distance > ### Aliases: distance distance,SpatRaster,missing-method > ### distance,SpatRaster,SpatVector-method distance,SpatRaster,sf-method > ### distance,SpatVector,ANY-method distance,SpatVector,SpatVector-method > ### distance,matrix,matrix-method distance,matrix,missing-method > ### distance,data.frame,data.frame-method > ### distance,data.frame,missing-method > ### Keywords: spatial > > ### ** Examples > > #lonlat > r <- rast(ncols=36, nrows=18, crs="+proj=longlat +datum=WGS84") > r[500] <- 1 > d <- distance(r, unit="km") > plot(d / 1000) > > #planar > rr <- rast(ncols=36, nrows=18, crs="+proj=utm +zone=1 +datum=WGS84") > rr[500] <- 1 > d <- distance(rr) > > rr[3:10, 3:10] <- 99 > e <- distance(rr, exclude=99) > > p1 <- vect(rbind(c(0,0), c(90,30), c(-90,-30)), crs="lonlat") > values(p1) <- data.frame(ID=LETTERS[1:3]) > dp <- distance(r, p1) > > d <- distance(p1) > d 1 2 2 10018754 3 10018754 20037508 > as.matrix(d) 1 2 3 1 0 10018754 10018754 2 10018754 0 20037508 3 10018754 20037508 0 > > p2 <- vect(rbind(c(30,-30), c(25,40), c(-9,-3)), crs="+proj=longlat +datum=WGS84") > values(p2) <- data.frame(ID=letters[1:3]) > dd <- distance(p1, p2, names=c("ID", "ID")) > dd a b c A 4609698 5124121 1055634 B 9219396 5900335 11053086 C 10818112 14137173 8984422 > pd <- distance(p1, p2, pairwise=TRUE) > pd [1] 4609698 5900335 8984422 > pd == diag(dd) [1] TRUE TRUE TRUE > > > # polygons, lines > crs <- "+proj=utm +zone=1" > p1 <- vect("POLYGON ((0 0, 8 0, 8 9, 0 9, 0 0))", crs=crs) > p2 <- vect("POLYGON ((5 6, 15 6, 15 15, 5 15, 5 6))", crs=crs) > p3 <- vect("POLYGON ((2 12, 3 12, 3 13, 2 13, 2 12))", crs=crs) > p <- rbind(p1, p2, p3) > L1 <- vect("LINESTRING(1 11, 4 6, 10 6)", crs=crs) > L2 <- vect("LINESTRING(8 14, 12 10)", crs=crs) > L3 <- vect("LINESTRING(1 8, 12 14)", crs=crs) > lns <- rbind(L1, L2, L3) > pts <- vect(cbind(c(7,10,10), c(3,5,6)), crs=crs) > > distance(p1,p3) [,1] [1,] 3 > distance(p) 1 2 2 0 3 3 2 > distance(p,pts) [,1] [,2] [,3] [1,] 0.000000 2.000000 2.000000 [2,] 3.000000 1.000000 0.000000 [3,] 9.848858 9.899495 9.219544 > distance(p,lns) [,1] [,2] [,3] [1,] 0.000000 3.535534 0.000000 [2,] 0.000000 0.000000 0.000000 [3,] 1.414214 5.099020 2.553878 > distance(pts,lns) [,1] [,2] [,3] [1,] 3 8.602325 7.262591 [2,] 1 5.385165 6.943356 [3,] 0 4.472136 6.065460 > > > > cleanEx() > nameEx("divide") > ### * divide > > flush(stderr()); flush(stdout()) > > ### Name: divide > ### Title: Subdivide a raster or polygons > ### Aliases: divide divide,SpatRaster-method divide,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- divide(r, 3) > # plot(r); lines(x) > > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > d <- divide(v, 3) > dv <- divide(v, list(h=.5)) > > > > cleanEx() > nameEx("dots") > ### * dots > > flush(stderr()); flush(stdout()) > > ### Name: dots > ### Title: Make a dot-density map > ### Aliases: dots dots,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v$population <- 1000*(1:12)^2 > plot(v, lwd=3, col="light gray", border="white") > d <- dots(v, "population", 1000, col="red", cex=.75) > lines(v) > d class : SpatVector geometry : points dimensions : 650, 7 (geometries, attributes) extent : 5.77034, 6.502824, 49.4565, 50.15081 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP population type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1000 1 Diekirch 2 Diekirch 218 3.254e+04 4000 1 Diekirch 2 Diekirch 218 3.254e+04 4000 > > > > cleanEx() > nameEx("elongate") > ### * elongate > > flush(stderr()); flush(stdout()) > > ### Name: elongate > ### Title: elongate lines > ### Aliases: elongate elongate,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(cbind(c(0,1,2), c(0,0,2)), "lines", crs="lonlat") > e <- elongate(v, 100000) > plot(e) > points(e) > geom(e) geom part x y hole [1,] 1 1 -0.8983153 0.000000 0 [2,] 1 1 1.0000000 0.000000 0 [3,] 1 1 2.4044661 2.807681 0 > > > > cleanEx() > nameEx("erase") > ### * erase > > flush(stderr()); flush(stdout()) > > ### Name: erase > ### Title: Erase parts of a SpatVector object > ### Aliases: erase erase,SpatVector,SpatVector-method > ### erase,SpatVector,missing-method erase,SpatVector,SpatExtent-method > ### erase,SpatGraticule,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > # polygons with polygons or extent > > e <- ext(5.6, 6, 49.55, 49.7) > x <- erase(v, e) > > p <- vect("POLYGON ((5.8 49.8, 6 49.9, 6.15 49.8, 6 49.6, 5.8 49.8))") > y <- erase(v, p) > > # lines with polygons > lns <- as.lines(rast(v, ncol=10, nrow=10))[12:22] > eln <- erase(lns, v) > plot(v) > lines(lns, col='blue', lwd=4, lty=3) > lines(eln, col='red', lwd=2) > > > ## self-erase > h <- convHull(v[-12], "NAME_1") > he <- erase(h) > plot(h, lwd=2, border="red", lty=2) > lines(he, col="gray", lwd=3) > > > > cleanEx() > nameEx("expanse") > ### * expanse > > flush(stderr()); flush(stdout()) > > ### Name: expanse > ### Title: Get the expanse (area) of individual polygons or for all > ### (summed) raster cells > ### Aliases: area area,SpatRaster-method area,SpatVector-method expanse > ### expanse,SpatRaster-method expanse,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > > ### SpatRaster > r <- rast(nrows=18, ncols=36) > v <- 1:ncell(r) > v[200:400] <- NA > values(r) <- v > > # summed area in km2 > expanse(r, unit="km") layer area 1 1 273986501 > > # all cells > expanse(rast(r), unit="km") layer area 1 1 510065622 > > r <- rast(ncols=90, nrows=45, ymin=-80, ymax=80) > m <- project(r, "+proj=merc") > > expanse(m, unit="km") layer area 1 1 498751903 > expanse(m, unit="km", transform=FALSE) layer area 1 1 1241591858 > > m2 <- c(m, m) > values(m2) <- cbind(c(1,2,NA,NA), c(11:14)) Warning: [setValues] values were recycled > expanse(m2, unit="km", byValue=TRUE, wide=TRUE) layer 1 2 11 12 13 14 1 1 10182145 10182145 0 0 0 0 3 2 0 0 10182145 10182145 10182145 10182145 > > > v <- vect(system.file("ex/lux.shp", package="terra")) > r <- rast(system.file("ex/elev.tif", package="terra")) > r <- round((r-50)/100) > levels(r) <- data.frame(id=1:5, name=c("forest", "water", "urban", "crops", "grass")) > expanse(r, byValue=TRUE) layer value area 1 1 forest 50778375 2 1 water 715780779 3 1 urban 1118216460 4 1 crops 617965652 5 1 grass 60868836 > > g <- rasterize(v, r, "NAME_1") > expanse(r, byValue=TRUE, zones=g, wide=TRUE) layer zone forest water urban crops grass 1 1 Diekirch 1110530 146734320 332045014 584531527 58106847 2 1 Grevenmacher 46327739 247619382 219364571 1670053 0 5 1 Luxembourg 0 315854211 555664543 25106417 0 > > > ### SpatVector > v <- vect(system.file("ex/lux.shp", package="terra")) > > a <- expanse(v) > a [1] 312283206 218674025 259454806 76200409 263174257 188282143 128991500 [8] 210354494 185630770 251322021 237113004 233329960 > sum(a) [1] 2564810595 > > > > cleanEx() > nameEx("ext") > ### * ext > > flush(stderr()); flush(stdout()) > > ### Name: ext > ### Title: Create, get or set a SpatExtent > ### Aliases: ext ext<- ext,SpatExtent-method ext,SpatRaster-method > ### ext,SpatVector-method ext,SpatVectorCollection-method > ### ext,SpatVectorProxy-method ext,SpatRasterDataset-method > ### ext,SpatRasterCollection-method ext,SpatRasterGraticule-method > ### ext<-,SpatRaster,SpatExtent-method ext<-,SpatRaster,numeric-method > ### ext,numeric-method ext,matrix-method ext,data.frame-method > ### ext,missing-method ext,Spatial-method ext,Raster-method > ### ext,Extent-method ext,sf-method ext,bbox-method > ### ext,PackedSpatExtent-method ext,SpatGraticule-method > ### Keywords: spatial > > ### ** Examples > > ext() SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > > r <- rast() > e <- ext(r) > as.vector(e) xmin xmax ymin ymax -180 180 -90 90 > as.character(e) [1] "ext(-180, 180, -90, 90)" > > ext(r) <- c(0, 2.5, 0, 1.5) > r class : SpatRaster size : 180, 360, 1 (nrow, ncol, nlyr) resolution : 0.006944444, 0.008333333 (x, y) extent : 0, 2.5, 0, 1.5 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) > er <- ext(r) > > round(er) SpatExtent : 0, 3, 0, 2 (xmin, xmax, ymin, ymax) > # go "in" > floor(er) SpatExtent : 0, 3, 0, 2 (xmin, xmax, ymin, ymax) > # go "out" > ceiling(er) SpatExtent : 0, 2, 0, 1 (xmin, xmax, ymin, ymax) > > ext(r) <- e > > > > > cleanEx() > nameEx("extend") > ### * extend > > flush(stderr()); flush(stdout()) > > ### Name: extend > ### Title: Extend > ### Aliases: extend extend,SpatRaster-method extend,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(xmin=-150, xmax=-120, ymin=30, ymax=60, ncols=36, nrows=18) > values(r) <- 1:ncell(r) > e <- ext(-180, -100, 40, 70) > re <- extend(r, e) > > # extend with a number of rows and columns (at each side) > re2 <- extend(r, c(2,10)) > > # SpatExtent > e <- ext(r) > e SpatExtent : -150, -120, 30, 60 (xmin, xmax, ymin, ymax) > extend(e, 10) SpatExtent : -160, -110, 20, 70 (xmin, xmax, ymin, ymax) > extend(e, c(10, -10, 0, 20)) SpatExtent : -160, -110, 30, 80 (xmin, xmax, ymin, ymax) > > > # add 10 columns / rows on all sides > e + 10 SpatExtent : -160, -110, 20, 70 (xmin, xmax, ymin, ymax) > # double extent > e * 2 SpatExtent : -165, -105, 15, 75 (xmin, xmax, ymin, ymax) > # increase extent by 25% > e * 1.25 SpatExtent : -153.75, -116.25, 26.25, 63.75 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("extract") > ### * extract > > flush(stderr()); flush(stdout()) > > ### Name: extract > ### Title: Extract values from a SpatRaster > ### Aliases: extract extract,SpatRaster,SpatVector-method > ### extract,SpatRaster,sf-method extract,SpatRaster,SpatExtent-method > ### extract,SpatRaster,matrix-method extract,SpatRaster,data.frame-method > ### extract,SpatRaster,numeric-method > ### extract,SpatVector,SpatVector-method extract,SpatVector,matrix-method > ### extract,SpatVector,data.frame-method > ### extract,SpatRasterCollection,ANY-method > ### extract,SpatRasterDataset,ANY-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5, xmin=0, xmax=5, ymin=0, ymax=5) > values(r) <- 1:25 > xy <- cbind(lon=c(0.5,2.5), lat=c(0.5,2.5)) > p <- vect(xy, crs="+proj=longlat +datum=WGS84") > > extract(r, xy) lyr.1 1 21 2 13 > extract(r, p) ID lyr.1 1 1 21 2 2 13 > > r[1,] lyr.1 1 1 2 2 3 3 4 4 5 5 > r[5] lyr.1 1 5 > r[,5] lyr.1 1 5 2 10 3 15 4 20 5 25 > > r[c(0:2, 99:101)] lyr.1 1 1 2 2 3 NA 4 NA 5 NA > > f <- system.file("ex/meuse.tif", package="terra") > r <- rast(f) > > xy <- cbind(179000, 330000) > xy <- rbind(xy-100, xy, xy+1000) > extract(r, xy) meuse 1 378 2 251 3 208 > > p <- vect(xy) > g <- geom(p) > g geom part x y hole [1,] 1 1 178900 329900 0 [2,] 2 1 179000 330000 0 [3,] 3 1 180000 331000 0 > > extract(r, p) ID meuse 1 1 378 2 2 251 3 3 208 > > x <- r + 10 > extract(x, p) ID meuse 1 1 388 2 2 261 3 3 218 > > i <- cellFromXY(r, xy) > x[i] meuse 1 388 2 261 3 218 > r[i] meuse 1 378 2 251 3 208 > > y <- c(x,x*2,x*3) > y[i] meuse meuse meuse 1 388 776 1164 2 261 522 783 3 218 436 654 > > ## extract with a polygon > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v <- v[1:2,] > > rf <- system.file("ex/elev.tif", package="terra") > x <- rast(rf) > extract(x, v, mean, na.rm=TRUE) ID elevation 1 1 467.1052 2 2 333.8629 > > z <- rast(v, resolution=.1, names="test") > values(z) <- 1:ncell(z) > e <- extract(z, v, ID=TRUE) > e ID test 1 1 2 2 1 3 3 1 6 4 1 7 5 1 8 6 2 13 7 2 17 8 2 18 9 2 19 > tapply(e[,2], e[,1], mean, na.rm=TRUE) 1 2 5.20 16.75 > > x <- c(z, z*2, z/3) > names(x) <- letters[1:3] > > e <- extract(x, v, ID=TRUE) > de <- data.frame(e) > aggregate(de[,2:4], de[,1,drop=FALSE], mean) ID a b c 1 1 5.20 10.4 1.733333 2 2 16.75 33.5 5.583333 > > > > cleanEx() > nameEx("extractAlong") > ### * extractAlong > > flush(stderr()); flush(stdout()) > > ### Name: extractAlong > ### Title: extract values along lines > ### Aliases: extractAlong > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=36, nrows=18, vals=1:(18*36)) > cds1 <- rbind(c(-50,0), c(0,60), c(40,5), c(15,-45), c(-10,-25)) > cds2 <- rbind(c(80,20), c(140,60), c(160,0), c(140,-55)) > lines <- vect(list(cds1, cds2), "lines") > > extractAlong(r, lines) ID lyr.1 1 1 338 2 1 302 3 1 266 4 1 267 5 1 231 6 1 232 7 1 196 8 1 197 9 1 161 10 1 162 11 1 126 12 1 127 13 1 163 14 1 164 15 1 200 16 1 201 17 1 237 18 1 273 19 1 274 20 1 310 21 1 310 22 1 346 23 1 382 24 1 381 25 1 417 26 1 453 27 1 452 28 1 488 29 1 488 30 1 487 31 1 451 32 1 450 33 1 414 34 2 279 35 2 243 36 2 244 37 2 208 38 2 209 39 2 174 40 2 175 41 2 139 42 2 140 43 2 141 44 2 177 45 2 213 46 2 214 47 2 250 48 2 286 49 2 322 50 2 358 51 2 394 52 2 430 53 2 429 54 2 465 55 2 501 56 2 537 > > > > cleanEx() > nameEx("extractRange") > ### * extractRange > > flush(stderr()); flush(stdout()) > > ### Name: extractRange > ### Title: Extract values for a range of layers from a SpatRaster > ### Aliases: extractRange extractRange,SpatRaster-method > ### extractRange,SpatRaster,ANY-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > xy <- data.frame(x=c(50,80), y=c(30, 60)) > extract(r, xy) ID red green blue 1 1 149 158 215 2 2 68 67 63 > extract(r, xy, layer=c("red", "green")) ID value 1 1 149 2 2 67 > > extractRange(r, xy, first=1:2, last=3:2) [[1]] red green blue 1 149 158 215 [[2]] green 2 67 > extractRange(r, xy, first=1:2, last=3:2, lyr_fun=sum) [1] 522 67 > > > > > cleanEx() > nameEx("factors") > ### * factors > > flush(stderr()); flush(stdout()) > > ### Name: factors > ### Title: Categorical rasters > ### Aliases: droplevels droplevels,SpatRaster-method levels > ### levels,SpatRaster-method levels<- levels<-,SpatRaster-method cats > ### cats,SpatRaster-method categories categories,SpatRaster-method > ### addCats addCats,SpatRaster-method simplifyLevels > ### simplifyLevels,SpatRaster-method combineLevels > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10) > values(r) <- sample(3, ncell(r), replace=TRUE) > is.factor(r) [1] FALSE > > cls <- data.frame(id=1:3, cover=c("forest", "water", "urban")) > levels(r) <- cls > is.factor(r) [1] TRUE > r class : SpatRaster size : 10, 10, 1 (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 categories : cover name : cover min value : forest max value : urban > > plot(r, col=c("green", "blue", "light gray")) > text(r, digits=3, cex=.75, halo=TRUE) > > levels(r) <- data.frame(id=1:3, cover=c("forest", "water", "forest")) > levels(simplifyLevels(r)) [[1]] id cover 1 1 forest 2 2 water > > > > # raster starts at 3 > x <- r + 2 > is.factor(x) [1] FALSE > > # Multiple categories > d <- data.frame(id=3:5, cover=cls[,2], letters=letters[1:3], value=10:12) > levels(x) <- d > x class : SpatRaster size : 10, 10, 1 (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 categories : cover, letters, value name : cover min value : forest max value : urban > > # get current index > activeCat(x) [1] 1 > # set index > activeCat(x) <- 3 > activeCat(x) [1] 3 > activeCat(x) <- "letters" > plot(x, col=c("green", "blue", "light gray")) > text(x, digits=3, cex=.75, halo=TRUE) > > r <- as.numeric(x) > r class : SpatRaster size : 10, 10, 1 (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 name : cover min value : 1 max value : 3 > > p <- as.polygons(x) > plot(p, "letters", col=c("green", "blue", "light gray")) > > > > cleanEx() > nameEx("fill") > ### * fill > > flush(stderr()); flush(stdout()) > > ### Name: fillHoles > ### Title: Remove or extract holes from polygons, or fill holes in raster > ### data > ### Aliases: fillHoles fillHoles,SpatVector-method > ### fillHoles,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > x <- rbind(c(50,0), c(140,60), c(160,0), c(140,-55)) > hole <- rbind(c(80,0), c(105,13), c(120,2), c(105,-13)) > > z <- rbind(cbind(object=1, part=1, x, hole=0), + cbind(object=1, part=1, hole, hole=1)) > colnames(z)[3:4] <- c('x', 'y') > p <- vect(z, "polygons", atts=data.frame(id=1), crs="local") > p class : SpatVector geometry : polygons dimensions : 1, 1 (geometries, attributes) extent : 50, 160, -55, 60 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) names : id type : values : 1 > > f <- fillHoles(p) > g <- fillHoles(p, inverse=TRUE) > > plot(p, lwd=16, border="gray", col="light yellow") > polys(f, border="blue", lwd=3, density=4, col="orange") > polys(g, col="white", lwd=3) > > > ## SpatRaster > v <- vect(c("POLYGON ((81.572 36.629, 98.508 9.624, 80 0, 99.902 -10.349, + 84.662 -34.709, 50 0, 81.572 36.629))", "POLYGON ((140 60, 160 0, + 140 -55, 84.662 -34.709, 99.902 -10.349, 105 -13, 120 2, 105 13, + 98.508 9.624, 81.572 36.629, 140 60))")) > v <- rbind(v, shift(p ,-120)) > v$ID <- 1:nrow(v) > r <- rasterize(v, rast(xmin=-80, crs="local"), "ID") > > f1 <- fillHoles(r) > f2 <- fillHoles(r, nearest=TRUE) > > > > cleanEx() > nameEx("fillTime") > ### * fillTime > > flush(stderr()); flush(stdout()) > > ### Name: fillTime > ### Title: Fill time gaps in a SpatRaster > ### Aliases: fillTime fillTime,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > s <- c(r, r) > time(s) <- as.Date("2001-01-01") + c(0:2, 5:7) > time(s) [1] "2001-01-01" "2001-01-02" "2001-01-03" "2001-01-06" "2001-01-07" [6] "2001-01-08" > ss <- fillTime(s) > time(ss) [1] "2001-01-01" "2001-01-02" "2001-01-03" "2001-01-04" "2001-01-05" [6] "2001-01-06" "2001-01-07" "2001-01-08" > > a <- approximate(ss) > > > > cleanEx() > nameEx("flip") > ### * flip > > flush(stderr()); flush(stdout()) > > ### Name: flip > ### Title: Flip or reverse a raster > ### Aliases: flip flip,SpatRaster-method flip,SpatVector-method rev > ### rev,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrow=18, ncol=36) > m <- matrix(1:ncell(r), nrow=18) > values(r) <- as.vector(t(m)) > rx <- flip(r, direction="h") > > values(r) <- as.vector(m) > ry <- flip(r, direction="v") > > v <- rev(r) > > > > cleanEx() > nameEx("flowAccumulation") > ### * flowAccumulation > > flush(stderr()); flush(stdout()) > > ### Name: flowAccumulation > ### Title: Flow accumulation > ### Aliases: flowAccumulation flowAccumulation,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > elev1 <- array(NA,c(9,9)) > elev2 <- elev1 > dx <- 1 > dy <- 1 > for (r in 1:nrow(elev1)) { + y <- (r-5)*dx + for (c in 1:ncol(elev1)) { + + x <- (c-5)*dy + elev1[r,c] <- 5*(x^2+y^2) + elev2[r,c] <- 10+5*(abs(x))-0.001*y + } + } > > > ## Elevation raster > elev1 <- rast(elev1) > elev2 <- rast(elev2) > > t(array(elev1[],rev(dim(elev1)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 160 125 100 85 80 85 100 125 160 [2,] 125 90 65 50 45 50 65 90 125 [3,] 100 65 40 25 20 25 40 65 100 [4,] 85 50 25 10 5 10 25 50 85 [5,] 80 45 20 5 0 5 20 45 80 [6,] 85 50 25 10 5 10 25 50 85 [7,] 100 65 40 25 20 25 40 65 100 [8,] 125 90 65 50 45 50 65 90 125 [9,] 160 125 100 85 80 85 100 125 160 > t(array(elev2[],rev(dim(elev2)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 30.004 25.004 20.004 15.004 10.004 15.004 20.004 25.004 30.004 [2,] 30.003 25.003 20.003 15.003 10.003 15.003 20.003 25.003 30.003 [3,] 30.002 25.002 20.002 15.002 10.002 15.002 20.002 25.002 30.002 [4,] 30.001 25.001 20.001 15.001 10.001 15.001 20.001 25.001 30.001 [5,] 30.000 25.000 20.000 15.000 10.000 15.000 20.000 25.000 30.000 [6,] 29.999 24.999 19.999 14.999 9.999 14.999 19.999 24.999 29.999 [7,] 29.998 24.998 19.998 14.998 9.998 14.998 19.998 24.998 29.998 [8,] 29.997 24.997 19.997 14.997 9.997 14.997 19.997 24.997 29.997 [9,] 29.996 24.996 19.996 14.996 9.996 14.996 19.996 24.996 29.996 > > plot(elev1) > plot(elev2) > > ## Flow direction raster > flowdir1<- terrain(elev1,v="flowdir") > flowdir2<- terrain(elev2,v="flowdir") > > t(array(flowdir1[],rev(dim(flowdir1)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 2 2 2 4 4 4 8 8 8 [2,] 2 2 2 4 4 4 8 8 8 [3,] 2 2 2 4 4 4 8 8 8 [4,] 1 1 1 2 4 8 16 16 16 [5,] 1 1 1 1 0 16 16 16 16 [6,] 1 1 1 128 64 32 16 16 16 [7,] 128 128 128 64 64 64 32 32 32 [8,] 128 128 128 64 64 64 32 32 32 [9,] 128 128 128 64 64 64 32 32 32 > t(array(flowdir2[],rev(dim(flowdir2)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 1 1 1 4 16 16 16 16 [2,] 1 1 1 1 4 16 16 16 16 [3,] 1 1 1 1 4 16 16 16 16 [4,] 1 1 1 1 4 16 16 16 16 [5,] 1 1 1 1 4 16 16 16 16 [6,] 1 1 1 1 4 16 16 16 16 [7,] 1 1 1 1 4 16 16 16 16 [8,] 1 1 1 1 4 16 16 16 16 [9,] 1 1 1 1 0 16 16 16 16 > > plot(flowdir1) > plot(flowdir2) > > ## > flow_acc1 <- flowAccumulation((flowdir1)) > flow_acc2 <- flowAccumulation((flowdir2)) > > weight <- elev1*0+10 > > flow_acc1w <- flowAccumulation(flowdir1,weight) > flow_acc2w <- flowAccumulation(flowdir2,weight) > > t(array(flow_acc1w[],rev(dim(flow_acc1w)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 10 10 10 10 10 10 10 10 10 [2,] 10 20 20 30 20 30 20 20 10 [3,] 10 20 30 60 30 60 30 20 10 [4,] 10 30 60 160 40 160 60 30 10 [5,] 10 20 30 40 810 40 30 20 10 [6,] 10 30 60 160 40 160 60 30 10 [7,] 10 20 30 60 30 60 30 20 10 [8,] 10 20 20 30 20 30 20 20 10 [9,] 10 10 10 10 10 10 10 10 10 > t(array(flow_acc2w[],rev(dim(flow_acc2w)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 10 20 30 40 90 40 30 20 10 [2,] 10 20 30 40 180 40 30 20 10 [3,] 10 20 30 40 270 40 30 20 10 [4,] 10 20 30 40 360 40 30 20 10 [5,] 10 20 30 40 450 40 30 20 10 [6,] 10 20 30 40 540 40 30 20 10 [7,] 10 20 30 40 630 40 30 20 10 [8,] 10 20 30 40 720 40 30 20 10 [9,] 10 20 30 40 810 40 30 20 10 > > plot(flow_acc1w) > plot(flow_acc2w) > > > ## Application with example elevation data > > elev <- rast(system.file('ex/elev.tif',package="terra")) > flowdir <- terrain(elev,"flowdir") > > weight <- cellSize(elev,unit="km") > flowacc_weight <- flowAccumulation(flowdir,weight) > flowacc <- flowAccumulation(flowdir) > > > > cleanEx() > nameEx("focal") > ### * focal > > flush(stderr()); flush(stdout()) > > ### Name: focal > ### Title: Focal values > ### Aliases: focal focal,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10, ext(0, 10, 0, 10)) > values(r) <- 1:ncell(r) > > f <- focal(r, w=3, fun=function(x, ...) quantile(x, c(.25, .5, .75), ...), na.rm=TRUE) > > f <- focal(r, w=3, fun="mean") > > # the following two statements are equivalent: > a <- focal(r, w=matrix(1/9, nc=3, nr=3)) > b <- focal(r, w=3, fun=mean, na.rm=FALSE) > > # but this is different > d <- focal(r, w=3, fun=mean, na.rm=TRUE) > > ## illustrating the effect of different > ## combinations of na.rm and na.policy > v <- vect(system.file("ex/lux.shp", package="terra")) > r <- rast(system.file("ex/elev.tif", package="terra")) > r[45:50, 45:50] <- NA > > # also try "mean" or "min" > f <- "sum" > # na.rm=FALSE > plot(focal(r, 5, f), fun=lines(v)) > > # na.rm=TRUE > plot(focal(r, 5, f, na.rm=TRUE), fun=lines(v)) > > # only change cells that are NA > plot(focal(r, 5, f, na.policy="only", na.rm=TRUE), fun=lines(v)) > > # do not change cells that are NA > plot(focal(r, 5, f, na.policy="omit", na.rm=TRUE), fun=lines(v)) > > # does not do anything > # focal(r, 5, f, na.policy="only", na.rm=FALSE) > > > > cleanEx() > nameEx("focal3D") > ### * focal3D > > flush(stderr()); flush(stdout()) > > ### Name: focal3D > ### Title: Three-dimensional focal values > ### Aliases: focal3D focal3D,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > x <- focal3D(r, c(5,5,3), na.rm=TRUE) > > a <- array(c(0,1,0,1,1,1,0,1,0, rep(1,9), 0,1,0,1,1,1,0,1,0), c(3,3,3)) > a[a==0] <- NA > z <- focal3D(r, a, na.rm=TRUE) > > > > cleanEx() > nameEx("focalCpp") > ### * focalCpp > > flush(stderr()); flush(stdout()) > > ### Name: focalCpp > ### Title: Compute focal values with an iterating C++ function > ### Aliases: focalCpp focalCpp,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > ## Not run: > ##D library(Rcpp) > ##D cppFunction( > ##D 'NumericVector sum_and_multiply(NumericVector x, double m, size_t ni, size_t nw) { > ##D NumericVector out(ni); > ##D // loop over cells > ##D size_t start = 0; > ##D for (size_t i=0; i ##D size_t end = start + nw; > ##D // compute something for a window > ##D double v = 0; > ##D // loop over the values of a window > ##D for (size_t j=start; j ##D v += x[j]; > ##D } > ##D out[i] = v * m; > ##D start = end; > ##D } > ##D return out; > ##D }' > ##D ) > ##D > ##D nr <- nc <- 10 > ##D r <- rast(ncols=nc, nrows=nr, ext= c(0, nc, 0, nr)) > ##D values(r) <- 1:ncell(r) > ##D > ##D raw <- focalCpp(r, w=3, fun=sum_and_multiply, fillvalue=0, m=10) > ##D > ##D # same as > ##D f1 <- focal(r, w=3, fun=sum, fillvalue=0) *10 > ##D all(values(f1) == values(raw)) > ##D > ##D # and as > ##D ffun <- function(x, m) { sum(x) * m } > ##D f2 <- focal(r, w=3, fun=ffun, fillvalue=0, m=10) > ##D > ##D > ##D # You can also use an R function with focalCpp but this > ##D # is not recommended > ##D > ##D R_sm_iter <- function(x, m, ni, nw) { > ##D out <- NULL > ##D for (i in 1:ni) { > ##D start <- (i-1) * nw + 1 > ##D out[i] <- sum(x[start:(start+nw-1)]) * m > ##D } > ##D out > ##D } > ##D > ##D fr <- focalCpp(r, w=3, fun=R_sm_iter, fillvalue=0, m=10) > ##D > ## End(Not run) > > > > cleanEx() > nameEx("focalMat") > ### * focalMat > > flush(stderr()); flush(stdout()) > > ### Name: focalMat > ### Title: Focal weights matrix > ### Aliases: focalMat > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=180, nrows=180, xmin=0) > focalMat(r, 2, "circle") [,1] [,2] [,3] [,4] [,5] [1,] 0.00000000 0.00000000 0.07692308 0.00000000 0.00000000 [2,] 0.00000000 0.07692308 0.07692308 0.07692308 0.00000000 [3,] 0.07692308 0.07692308 0.07692308 0.07692308 0.07692308 [4,] 0.00000000 0.07692308 0.07692308 0.07692308 0.00000000 [5,] 0.00000000 0.00000000 0.07692308 0.00000000 0.00000000 > > focalMat(r, c(2,3), "rect") [,1] [,2] [,3] [,4] [,5] [1,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [2,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [3,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [4,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [5,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [6,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 [7,] 0.02857143 0.02857143 0.02857143 0.02857143 0.02857143 > > # Gaussian filter for square cells > gf <- focalMat(r, 1, "Gauss") > > > > cleanEx() > nameEx("focalPairs") > ### * focalPairs > > flush(stderr()); flush(stdout()) > > ### Name: focalPairs > ### Title: Focal function across two layers > ### Aliases: focalCor focalCor,SpatRaster-method focalPairs > ### focalPairs,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > set.seed(0) > r[[1]] <- flip(r[[1]], "horizontal") > r[[2]] <- flip(r[[2]], "vertical") + init(rast(r,1), runif) > r[[3]] <- init(rast(r,1), runif) > > x <- focalPairs(r, w=5, "pearson", na.rm=TRUE) > plot(x) > > > # suppress warning "the standard deviation is zero" > suppressWarnings(x <- focalPairs(r, w=5, "pearson", use="complete.obs")) > > z <- focalPairs(r, w=9, function(x, y) mean(x) + mean(y)) > > > > cleanEx() > nameEx("focalReg") > ### * focalReg > > flush(stderr()); flush(stdout()) > > ### Name: focalReg > ### Title: Focal regression > ### Aliases: focalReg focalReg,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10, ext(0, 10, 0, 10)) > values(r) <- 1:ncell(r) > x <- c(r, init(r, runif) * r) > f <- focalReg(x, 3) > > > > cleanEx() > nameEx("focalValues") > ### * focalValues > > flush(stderr()); flush(stdout()) > > ### Name: focalValues > ### Title: Get focal values > ### Aliases: focalValues focalValues,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(ncol=4, nrow=4, crs="+proj=utm +zone=1 +datum=WGS84") > values(r) <- 1:ncell(r) > focalValues(r) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] NA NA NA NA 1 2 NA 5 6 [2,] NA NA NA 1 2 3 5 6 7 [3,] NA NA NA 2 3 4 6 7 8 [4,] NA NA NA 3 4 NA 7 8 NA [5,] NA 1 2 NA 5 6 NA 9 10 [6,] 1 2 3 5 6 7 9 10 11 [7,] 2 3 4 6 7 8 10 11 12 [8,] 3 4 NA 7 8 NA 11 12 NA [9,] NA 5 6 NA 9 10 NA 13 14 [10,] 5 6 7 9 10 11 13 14 15 [11,] 6 7 8 10 11 12 14 15 16 [12,] 7 8 NA 11 12 NA 15 16 NA [13,] NA 9 10 NA 13 14 NA NA NA [14,] 9 10 11 13 14 15 NA NA NA [15,] 10 11 12 14 15 16 NA NA NA [16,] 11 12 NA 15 16 NA NA NA NA > > > > cleanEx() > nameEx("forceCCW") > ### * forceCCW > > flush(stderr()); flush(stdout()) > > ### Name: forceCCW > ### Title: force counter-clockwise polygons > ### Aliases: forceCCW forceCCW,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > p <- vect("POLYGON ((2 45, 2 55, 18 55, 18 45, 2 45))") > pcc <- forceCCW(p) > geom(pcc, wkt=TRUE) [1] "POLYGON ((2 45, 18 45, 18 55, 2 55, 2 45))" > > > > cleanEx() > nameEx("freq") > ### * freq > > flush(stderr()); flush(stdout()) > > ### Name: freq > ### Title: Frequency table > ### Aliases: freq freq,SpatRaster-method > ### Keywords: spatial univar > > ### ** Examples > > r <- rast(nrows=10, ncols=10) > set.seed(2) > values(r) <- sample(5, ncell(r), replace=TRUE) > > freq(r) layer value count 1 1 1 27 2 1 2 15 3 1 3 17 4 1 4 17 5 1 5 24 > > x <- c(r, r/3) > freq(x, bylayer=FALSE) value count 1 0 27 2 1 76 3 2 39 4 3 17 5 4 17 6 5 24 > freq(x) layer value count 1 1 1 27 2 1 2 15 3 1 3 17 4 1 4 17 5 1 5 24 6 2 0 27 7 2 1 49 8 2 2 24 > > freq(x, digits=1) layer value count 1 1 1.0 27 2 1 2.0 15 3 1 3.0 17 4 1 4.0 17 5 1 5.0 24 6 2 0.3 27 7 2 0.7 15 8 2 1.0 17 9 2 1.3 17 10 2 1.7 24 > freq(x, digits=-1) layer value count 1 1 0 76 2 1 10 24 3 2 0 100 > > freq(x, value=5) layer value count 1 1 5 24 2 2 5 0 > > > > > cleanEx() > nameEx("gaps") > ### * gaps > > flush(stderr()); flush(stdout()) > > ### Name: gaps > ### Title: Find gaps between polygons > ### Aliases: gaps gaps,SpatVector-method gaps,SpatVector,SpatExtent-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > h <- convHull(v[-12], "NAME_1") > g <- gaps(h) > > > > cleanEx() > nameEx("gdal") > ### * gdal > > flush(stderr()); flush(stdout()) > > ### Name: gdal > ### Title: GDAL version, supported file formats, cache size, and PROJ > ### coordinate transformation control > ### Aliases: gdal libVersion gdalCache clearVSIcache getGDALconfig > ### setGDALconfig unloadGDALdrivers proj_ok projNetwork projPaths > ### Keywords: spatial > > ### ** Examples > > gdal() [1] "3.8.5" > gdal(2) > head(gdal(drivers=TRUE)) --1064531-- WARNING: unhandled amd64-linux syscall: 323 --1064531-- You may be able to write your own handler. --1064531-- Read the file README_MISSING_SYSCALL_OR_IOCTL. --1064531-- Nevertheless we consider this a bug. Please report --1064531-- it at http://valgrind.org/support/bug_reports.html. name raster vector can vsi long.name 1 AAIGrid TRUE FALSE read/write TRUE Arc/Info ASCII Grid 2 ACE2 TRUE FALSE read TRUE ACE2 3 ADRG TRUE FALSE read/write TRUE ARC Digitized Raster Graphics 4 AIG TRUE FALSE read TRUE Arc/Info Binary Grid 5 ARG TRUE FALSE read/write TRUE Azavea Raster Grid format 6 AVCBin FALSE TRUE read TRUE Arc/Info Binary Coverage > libVersion("all", TRUE) major minor sub gdal 3 8 5 proj 9 3 1 geos 3 12 2 > projNetwork() [1] NA > projPaths() [1] "/data/blackswan/ripley/.local/share/proj" [2] "/usr/share/proj" > projPaths(c("/custom/proj/path")) > > > > cleanEx() > nameEx("geometry") > ### * geometry > > flush(stderr()); flush(stdout()) > > ### Name: geom > ### Title: Get the geometry (coordinates) of a SpatVector > ### Aliases: geom geom,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > x1 <- rbind(c(-175,-20), c(-140,55), c(10, 0), c(-140,-60)) > x2 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45)) > x3 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55)) > x4 <- rbind(c(80,0), c(105,13), c(120,2), c(105,-13)) > z <- rbind(cbind(object=1, part=1, x1), cbind(object=2, part=1, x2), + cbind(object=3, part=1, x3), cbind(object=3, part=2, x4)) > colnames(z)[3:4] <- c('x', 'y') > z <- cbind(z, hole=0) > z[(z[, "object"]==3 & z[,"part"]==2), "hole"] <- 1 > > p <- vect(z, "polygons") > geom(p) geom part x y hole [1,] 1 1 -175 -20 0 [2,] 1 1 -140 55 0 [3,] 1 1 10 0 0 [4,] 1 1 -140 -60 0 [5,] 1 1 -175 -20 0 [6,] 2 1 -125 0 0 [7,] 2 1 0 60 0 [8,] 2 1 40 5 0 [9,] 2 1 15 -45 0 [10,] 2 1 -125 0 0 [11,] 3 1 -10 0 0 [12,] 3 1 140 60 0 [13,] 3 1 160 0 0 [14,] 3 1 140 -55 0 [15,] 3 1 -10 0 0 [16,] 3 1 80 0 1 [17,] 3 1 105 13 1 [18,] 3 1 120 2 1 [19,] 3 1 105 -13 1 [20,] 3 1 80 0 1 > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > g <- geom(v) > head(g) geom part x y hole [1,] 1 1 6.026519 50.17767 0 [2,] 1 1 6.031361 50.16563 0 [3,] 1 1 6.035646 50.16410 0 [4,] 1 1 6.042747 50.16157 0 [5,] 1 1 6.043894 50.16116 0 [6,] 1 1 6.048243 50.16008 0 > > w <- geom(v, wkt=TRUE) > substr(w, 1, 60) [1] "POLYGON ((6.026519 50.17767, 6.031361 50.165627, 6.035646 50" [2] "POLYGON ((6.178368 49.876823, 6.185479 49.870525, 6.189417 4" [3] "POLYGON ((5.881378 49.870148, 5.881672 49.868866, 5.886637 4" [4] "POLYGON ((6.131309 49.972565, 6.134291 49.972382, 6.139316 4" [5] "POLYGON ((5.977929 50.026016, 5.982312 50.022949, 5.981743 5" [6] "POLYGON ((6.385532 49.837029, 6.3886 49.833683, 6.390184 49." [7] "POLYGON ((6.316665 49.623375, 6.31835 49.623157, 6.320131 49" [8] "POLYGON ((6.425158 49.731644, 6.42657 49.73082, 6.427332 49." [9] "POLYGON ((5.998312 49.699924, 5.998632 49.698559, 5.998956 4" [10] "POLYGON ((6.039474 49.448261, 6.036906 49.448696, 6.036822 4" [11] "POLYGON ((6.155963 49.685047, 6.159284 49.685036, 6.161457 4" [12] "POLYGON ((6.067982 49.828465, 6.071922 49.825478, 6.073236 4" > > > > cleanEx() > nameEx("geomtype") > ### * geomtype > > flush(stderr()); flush(stdout()) > > ### Name: geomtype > ### Title: Geometry type of a SpatVector > ### Aliases: geomtype geomtype,SpatVector-method > ### geomtype,SpatVectorProxy-method geomtype,Spatial-method is.points > ### is.lines is.polygons is.points,SpatVector-method > ### is.lines,SpatVector-method is.polygons,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > geomtype(v) [1] "polygons" > is.polygons(v) [1] TRUE > is.lines(v) [1] FALSE > is.points(v) [1] FALSE > > names(v) [1] "ID_1" "NAME_1" "ID_2" "NAME_2" "AREA" "POP" > datatype(v) [1] "double" "string" "double" "string" "double" "double" > > > > cleanEx() > nameEx("global") > ### * global > > flush(stderr()); flush(stdout()) > > ### Name: global > ### Title: global statistics > ### Aliases: global global,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > global(r, "sum") sum lyr.1 5050 > global(r, "mean", na.rm=TRUE) mean lyr.1 50.5 > x <- c(r, r/10) > global(x, c("sum", "mean", "sd"), na.rm=TRUE) sum mean sd lyr.1 5050 50.50 29.011492 lyr.1.1 505 5.05 2.901149 > > global(x, function(i) min(i) / max(i)) global lyr.1 0.01 lyr.1.1 0.01 > > > > cleanEx() > nameEx("graticule") > ### * graticule > > flush(stderr()); flush(stdout()) > > ### Name: graticule > ### Title: Create a graticule > ### Aliases: graticule > ### Keywords: methods spatial > > ### ** Examples > > g <- graticule(60, 30, crs="+proj=robin") > g class : SpatGraticule lon : -180 -120 -60 0 60 120 180 lat : -90 -60 -30 0 30 60 90 coord. ref. : +proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs extent : -17005833, 17005833, -8625155, 8625155 (xmin, xmax, ymin, ymax) > > graticule(90, c(-90, -60, -23.5, 0, 23.5, 60, 90), crs="+proj=robin") class : SpatGraticule lon : -180 -90 0 90 180 lat : -90 -60 -23.5 0 23.5 60 90 coord. ref. : +proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs extent : -17005833, 17005833, -8625155, 8625155 (xmin, xmax, ymin, ymax) > > > > > cleanEx() > nameEx("grid") > ### * grid > > flush(stderr()); flush(stdout()) > > ### Name: add_grid > ### Title: add a grid to a map made with terra > ### Aliases: add_grid > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(system.file("ex/lux.shp", package="terra")) > plot(v) > add_grid() > > > > cleanEx() > nameEx("gridDist") > ### * gridDist > > flush(stderr()); flush(stdout()) > > ### Name: gridDist > ### Title: Distance on a grid > ### Aliases: gridDist gridDist,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > # global lon/lat raster > r <- rast(ncol=10,nrow=10, vals=1) > r[48] <- 0 > r[66:68] <- NA > d <- gridDist(r) > plot(d) > > > # planar > crs(r) <- "+proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs" > d <- gridDist(r) > plot(d) > > # distance to cells that are not NA > rr <- classify(r, cbind(1, NA)) > dd <- gridDist(rr, NA) > > > > > > cleanEx() > nameEx("halo") > ### * halo > > flush(stderr()); flush(stdout()) > > ### Name: halo > ### Title: Add halo-ed text to a plot > ### Aliases: halo > > ### ** Examples > > r <- rast(nrows=4, ncols=4) > values(r) <- 1:ncell(r) > plot(r, col="blue", legend=FALSE) > > text(-100, 20, "hello", cex=2) > halo(50, 20, "hello", cex=2) > > halo(0, -20, "world", font=3, hc="light blue", cex=2, hw=.2) > halo(0, 90, "world", font=2, cex=2, hw=.2, xpd=TRUE, pos=2) > halo(0, 90, "world", col="white", font=2, hc="blue", cex=2, hw=.2, xpd=TRUE, pos=4) > > > > cleanEx() > nameEx("headtail") > ### * headtail > > flush(stderr()); flush(stdout()) > > ### Name: headtail > ### Title: head and tail of a SpatRaster or SpatVector > ### Aliases: head head,SpatRaster-method head,SpatVector-method tail > ### tail,SpatRaster-method tail,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=25, ncols=25) > values(r) <- 1:ncell(r) > head(r) lyr.1 1 1 2 2 3 3 4 4 5 5 6 6 > tail(r) lyr.1 1 620 2 621 3 622 4 623 5 624 6 625 > > > > cleanEx() > nameEx("hist") > ### * hist > > flush(stderr()); flush(stdout()) > > ### Name: hist > ### Title: Histogram > ### Aliases: hist hist,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r1 <- r2 <- rast(nrows=50, ncols=50) > values(r1) <- runif(ncell(r1)) > values(r2) <- runif(ncell(r1)) > rs <- r1 + r2 > rp <- r1 * r2 > > opar <- par(no.readonly =TRUE) > par(mfrow=c(2,2)) > plot(rs, main='sum') > plot(rp, main='product') > hist(rs) > a <- hist(rp) > a $breaks [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 $counts [1] 848 490 319 255 188 165 102 71 48 14 $density [1] 3.392 1.960 1.276 1.020 0.752 0.660 0.408 0.284 0.192 0.056 $mids [1] 0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95 $xname [1] "lyr.1" $equidist [1] TRUE attr(,"class") [1] "histogram" > x <- c(rs, rp, sqrt(rs)) > hist(x) > par(opar) > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("identical") > ### * identical > > flush(stderr()); flush(stdout()) > > ### Name: identical > ### Title: Compare two SpatRaster, SpatVector or SpatExtent objects for > ### equality > ### Aliases: identical identical,SpatRaster,SpatRaster-method > ### identical,SpatVector,SpatVector-method > ### identical,SpatExtent,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > x <- sqrt(1:100) > mat <- matrix(x, 10, 10) > r1 <- rast(nrows=10, ncols=10, xmin=0, vals = x) > r2 <- rast(nrows=10, ncols=10, xmin=0, vals = t(mat)) > > identical(r1, r2) [1] TRUE > identical(r1, r1*1) [1] TRUE > identical(rast(r1), rast(r2)) [1] TRUE > > > > cleanEx() > nameEx("ifelse") > ### * ifelse > > flush(stderr()); flush(stdout()) > > ### Name: ifel > ### Title: ifelse for SpatRasters > ### Aliases: ifel ifel,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=5, ncols=5, xmin=0, xmax=1, ymin=0, ymax=1) > values(r) <- c(-10:0, NA, NA, NA, 0:10) > > x <- ifel(r > 1, 1, r) > # same as > a <- classify(r, cbind(1, Inf, 1)) > # or > b <- app(r, fun=function(i) {i[i > 1] <- 1; i}) > # or > d <- clamp(r, -Inf, 1) > # or (not recommended for large datasets) > e <- r > e[e>1] <- 1 > > ## other examples > f <- ifel(is.na(r), 100, r) > > z <- ifel(r > -2 & r < 2, 100, 0) > > # nested expressions > y <- ifel(r > 1, 1, ifel(r < -1, -1, r)) > > k <- ifel(r > 0, r+10, ifel(r < 0, r-10, 3)) > > > > cleanEx() > nameEx("image") > ### * image > > flush(stderr()); flush(stdout()) > > ### Name: image > ### Title: SpatRaster image method > ### Aliases: image image,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > image(r) > image(r, col=rainbow(24)) > > > > cleanEx() > nameEx("init") > ### * init > > flush(stderr()); flush(stdout()) > > ### Name: initialize > ### Title: Initialize a SpatRaster with values > ### Aliases: init init,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=5, xmin=0, xmax=10, ymin=0, ymax=5) > x <- init(r, fun="cell") > y <- init(r, fun=runif) > > # initialize with a single value > z <- init(r, fun=8) > > > > cleanEx() > nameEx("inplace") > ### * inplace > > flush(stderr()); flush(stdout()) > > ### Name: inplace > ### Title: Change values in-place > ### Aliases: set.ext set.ext,SpatRaster-method set.ext,SpatVector-method > ### set.values set.values,SpatRaster-method > ### set.values,SpatRasterDataset-method set.RGB set.RGB,SpatRaster-method > ### set.cats set.cats,SpatRaster-method set.names > ### set.names,SpatRaster-method set.names,SpatRasterDataset-method > ### set.names,SpatRasterCollection-method set.names,SpatVector-method > ### set.names,SpatVectorCollection-method set.crs > ### set.crs,SpatRaster-method set.crs,SpatVector-method set.window > ### set.window,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(ncols=5, nrows=5, nlyrs=3) > x <- s > names(s) [1] "lyr.1" "lyr.2" "lyr.3" > names(s) <- c("a", "b", "c") > names(s) [1] "a" "b" "c" > names(x) [1] "lyr.1" "lyr.2" "lyr.3" > > x <- s > set.names(s, c("e", "f", "g")) > names(s) [1] "e" "f" "g" > names(x) [1] "e" "f" "g" > > set.ext(x, c(0,180,0,90)) > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > > #values from file to memory > set.values(r) class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory varname : elev name : elevation min value : 141 max value : 547 > > # change values > set.values(r, 1:1000, 900) > > > > cleanEx() > nameEx("inset") > ### * inset > > flush(stderr()); flush(stdout()) > > ### Name: inset > ### Title: Make an inset map > ### Aliases: inset inset,SpatVector-method inset,SpatRaster-method inext > ### inext,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > x <- v[v$NAME_2 == "Diekirch", ] > > plot(x, density=10, col="blue") > inset(v) > > # more elaborate > plot(x, density=10, col="blue") > inset(v, col = "brown", border="lightgrey", perimeter=TRUE, + pper=list(col="orange", lwd=3, lty=2), + box=ext(x), pbox=list(col="blue", lwd=2)) > > cols <- rep("light grey", 12) > cols[2] <- "red" > e <- ext(c(6.2, 6.3, 49.9, 50)) > b <- ext(x)+0.02 > inset(v, e=e, col=cols, box=b) > > # with a SpatRaster > ff <- system.file("ex/elev.tif", package="terra") > r <- rast(ff) > r <- crop(r, ext(x) + .01) > plot(r, type="int", mar=c(2,2,2,2), plg=list(x="topright")) > lines(v, lwd=1.5) > lines(x, lwd=2.5) > inset(v, col=cols, loc="topleft", scale=0.15) > > # a more complex one > plot(r, plg=list(title="meter\n", shrink=.2, cex=.8)) > lines(v, lwd=4, col="white") > lines(v, lwd=1.5) > lines(x, lwd=2.5) > text(x, "NAME_2", cex=1.5, halo=TRUE) > sbar(6, c(6.04, 49.785), type="bar", below="km", label=c(0,3,6), cex=.8) > s <- inset(v, col=cols, box=b, scale=.2, loc="topright", background="light yellow", + pbox=list(lwd=2, lty=5, col="blue")) > > # note the returned inset SpatVector > s class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 6.255333, 6.321333, 49.9348, 49.99657 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > lines(s, col="orange") > > > > cleanEx() > nameEx("interpIDW") > ### * interpIDW > > flush(stderr()); flush(stdout()) > > ### Name: interpIDW > ### Title: Interpolate points using a moving window > ### Aliases: interpIDW interpIDW,SpatRaster,SpatVector-method > ### interpIDW,SpatRaster,matrix-method > ### Keywords: spatial > > ### ** Examples > > > r <- rast(ncol=100, nrow=100, crs="local", xmin=0, xmax=50, ymin=0, ymax=50) > set.seed(100) > x <- runif(25, 5, 45) > y <- runif(25, 5, 45) > z <- sample(25) > xyz <- cbind(x,y,z) > > x <- interpIDW(r, xyz, radius=5, power=1, smooth=1, maxPoints=5) > > > > > cleanEx() > nameEx("interpNear") > ### * interpNear > > flush(stderr()); flush(stdout()) > > ### Name: interpNear > ### Title: Nearest neighbor interpolation > ### Aliases: interpNear interpNear,SpatRaster,SpatVector-method > ### interpNear,SpatRaster,matrix-method > ### Keywords: spatial > > ### ** Examples > > > r <- rast(ncol=100, nrow=100, crs="local", xmin=0, xmax=50, ymin=0, ymax=50) > set.seed(100) > x <- runif(25, 5, 45) > y <- runif(25, 5, 45) > z <- sample(25) > xyz <- cbind(x,y,z) > > x <- interpNear(r, xyz, radius=5) > > p <- vect(data.frame(xyz), geom=c("x", "y")) > v <- voronoi(p) > > plot(x, col=rainbow(25)) > lines(v) > > # plot(v, col=rainbow(25)); points(p) > > > > cleanEx() > nameEx("interpolate") > ### * interpolate > > flush(stderr()); flush(stdout()) > > ### Name: interpolation > ### Title: Spatial interpolation > ### Aliases: interpolate interpolate,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > > r <- rast(system.file("ex/elev.tif", package="terra")) > ra <- aggregate(r, 10) > xy <- data.frame(xyFromCell(ra, 1:ncell(ra))) > v <- values(ra) > i <- !is.na(v) > xy <- xy[i,] > v <- v[i] > > ## Not run: > ##D library(fields) > ##D tps <- Tps(xy, v) > ##D p <- rast(r) > ##D > ##D # use model to predict values at all locations > ##D p <- interpolate(p, tps) > ##D p <- mask(p, r) > ##D plot(p) > ##D > ##D ### change "fun" from predict to fields::predictSE to get the TPS standard error > ##D ## need to use "rast(p)" to remove the values > ##D se <- interpolate(rast(p), tps, fun=predictSE) > ##D se <- mask(se, r) > ##D plot(se) > ##D > ##D ### another predictor variable, "e" > ##D e <- (init(r, "x") * init(r, "y")) / 100000000 > ##D names(e) <- "e" > ##D > ##D z <- as.matrix(extract(e, xy)[,-1]) > ##D > ##D ## add as another independent variable > ##D xyz <- cbind(xy, z) > ##D tps2 <- Tps(xyz, v) > ##D p2 <- interpolate(e, tps2, xyOnly=FALSE) > ##D > ##D ## as a linear covariate > ##D tps3 <- Tps(xy, v, Z=z) > ##D > ##D ## Z is a separate argument in Krig.predict, so we need a new function > ##D ## Internally (in interpolate) a matrix is formed of x, y, and elev (Z) > ##D > ##D pfun <- function(model, x, ...) { > ##D predict(model, x[,1:2], Z=x[,3], ...) > ##D } > ##D p3 <- interpolate(e, tps3, fun=pfun) > ##D > ##D > ##D #### gstat examples > ##D library(gstat) > ##D library(sp) > ##D data(meuse) > ##D > ##D ### inverse distance weighted (IDW) > ##D r <- rast(system.file("ex/meuse.tif", package="terra")) > ##D mg <- gstat(id = "zinc", formula = zinc~1, locations = ~x+y, data=meuse, > ##D nmax=7, set=list(idp = .5)) > ##D z <- interpolate(r, mg, debug.level=0, index=1) > ##D z <- mask(z, r) > ##D > ##D ## with a model built with an `sf` object you need to provide custom function > ##D > ##D library(sf) > ##D sfmeuse <- st_as_sf(meuse, coords = c("x", "y"), crs=crs(r)) > ##D mgsf <- gstat(id = "zinc", formula = zinc~1, data=sfmeuse, nmax=7, set=list(idp = .5)) > ##D > ##D interpolate_gstat <- function(model, x, crs, ...) { > ##D v <- st_as_sf(x, coords=c("x", "y"), crs=crs) > ##D p <- predict(model, v, ...) > ##D as.data.frame(p)[,1:2] > ##D } > ##D > ##D zsf <- interpolate(r, mgsf, debug.level=0, fun=interpolate_gstat, crs=crs(r), index=1) > ##D zsf <- mask(zsf, r) > ##D > ##D ### kriging > ##D > ##D ### ordinary kriging > ##D v <- variogram(log(zinc)~1, ~x+y, data=meuse) > ##D mv <- fit.variogram(v, vgm(1, "Sph", 300, 1)) > ##D gOK <- gstat(NULL, "log.zinc", log(zinc)~1, meuse, locations=~x+y, model=mv) > ##D OK <- interpolate(r, gOK, debug.level=0) > ##D > ##D ## universal kriging > ##D vu <- variogram(log(zinc)~elev, ~x+y, data=meuse) > ##D mu <- fit.variogram(vu, vgm(1, "Sph", 300, 1)) > ##D gUK <- gstat(NULL, "log.zinc", log(zinc)~elev, meuse, locations=~x+y, model=mu) > ##D names(r) <- "elev" > ##D UK <- interpolate(r, gUK, debug.level=0) > ##D > ##D ## co-kriging > ##D gCoK <- gstat(NULL, 'log.zinc', log(zinc)~1, meuse, locations=~x+y) > ##D gCoK <- gstat(gCoK, 'elev', elev~1, meuse, locations=~x+y) > ##D gCoK <- gstat(gCoK, 'cadmium', cadmium~1, meuse, locations=~x+y) > ##D gCoK <- gstat(gCoK, 'copper', copper~1, meuse, locations=~x+y) > ##D coV <- variogram(gCoK) > ##D plot(coV, type='b', main='Co-variogram') > ##D coV.fit <- fit.lmc(coV, gCoK, vgm(model='Sph', range=1000)) > ##D coV.fit > ##D plot(coV, coV.fit, main='Fitted Co-variogram') > ##D coK <- interpolate(r, coV.fit, debug.level=0) > ##D plot(coK) > ## End(Not run) > > > > cleanEx() > nameEx("intersect") > ### * intersect > > flush(stderr()); flush(stdout()) > > ### Name: intersect > ### Title: Intersection > ### Aliases: intersect intersect,SpatVector,SpatVector-method > ### intersect,SpatVector,SpatExtent-method > ### intersect,SpatExtent,SpatVector-method > ### intersect,SpatExtent,SpatExtent-method > ### intersect,SpatRaster,SpatRaster-method > ### intersect,SpatRaster,SpatExtent-method > ### intersect,SpatExtent,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > e1 <- ext(-10, 10, -20, 20) > e2 <- ext(0, 20, -40, 5) > intersect(e1, e2) SpatExtent : 0, 10, -20, 5 (xmin, xmax, ymin, ymax) > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > e <- ext(5.6, 6, 49.55, 49.7) > x <- intersect(v, e) > > p <- vect(c("POLYGON ((5.8 49.8, 6 49.9, 6.15 49.8, 6 49.6, 5.8 49.8))", + "POLYGON ((6.3 49.9, 6.2 49.7, 6.3 49.6, 6.5 49.8, 6.3 49.9))"), crs=crs(v)) > values(p) <- data.frame(pid=1:2, area=expanse(p)) > > y <- intersect(v, p) > > r <- s <- rast(ncol=5, nrow=5, xmin=1, xmax=5, ymin=1, ymax=5) > r[5:20] <- 5:20 > s[11:20] <- 11:20 > rs <- intersect(r, s) > > u <- shift(r, .8) > us <- intersect(u, s) > > > > cleanEx() > nameEx("is.bool") > ### * is.bool > > flush(stderr()); flush(stdout()) > > ### Name: is.bool > ### Title: Raster value types > ### Aliases: is.num is.num,SpatRaster-method is.bool > ### is.bool,SpatRaster-method as.bool as.bool,SpatRaster-method > ### isTRUE,SpatRaster-method isFALSE,SpatRaster-method > ### as.logical,SpatRaster-method is.int is.int,SpatRaster-method as.int > ### as.int,SpatRaster-method as.integer,SpatRaster-method is.factor > ### is.factor,SpatRaster-method as.factor as.factor,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=10, ncols=10, vals=1:100) > is.bool(r) [1] FALSE > z <- as.bool(r) > is.bool(z) [1] TRUE > > x <- r > 25 > is.bool(x) [1] TRUE > > rr <- r/2 > is.int(rr) [1] FALSE > is.int(round(rr)) [1] TRUE > > > > cleanEx() > nameEx("is.empty") > ### * is.empty > > flush(stderr()); flush(stdout()) > > ### Name: is.empty > ### Title: Check if a SpatExtent or SpatVector is empty > ### Aliases: is.empty is.empty,SpatVector-method is.empty,SpatExtent-method > ### Keywords: spatial > > ### ** Examples > > e <- ext(0,0,0,0) > is.valid(e) [1] TRUE > is.empty(e) [1] TRUE > > v <- vect() > is.valid(v) logical(0) > is.empty(v) [1] TRUE > > > > cleanEx() > nameEx("is.flipped") > ### * is.flipped > > flush(stderr()); flush(stdout()) > > ### Name: is.flipped > ### Title: Is a SpatRaster is flipped > ### Aliases: is.flipped is.flipped,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=10, ncols=10) > is.flipped(r) [1] FALSE > > > > cleanEx() > nameEx("is.lonlat") > ### * is.lonlat > > flush(stderr()); flush(stdout()) > > ### Name: is.lonlat > ### Title: Check for longitude/latitude crs > ### Aliases: is.lonlat is.lonlat,SpatRaster-method > ### is.lonlat,SpatVector-method is.lonlat,character-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > is.lonlat(r) [1] TRUE > is.lonlat(r, global=TRUE) [1] TRUE > > crs(r) <- "" > is.lonlat(r) Warning: [is.lonlat] unknown crs [1] NA > is.lonlat(r, perhaps=TRUE, warn=FALSE) [1] TRUE > > crs(r) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84" > is.lonlat(r) [1] FALSE > > > > cleanEx() > nameEx("is.rotated") > ### * is.rotated > > flush(stderr()); flush(stdout()) > > ### Name: is.rotated > ### Title: Check for rotation > ### Aliases: is.rotated is.rotated,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=10, ncols=10, vals=1:100) > is.rotated(r) [1] FALSE > > > > cleanEx() > nameEx("is.valid") > ### * is.valid > > flush(stderr()); flush(stdout()) > > ### Name: is.valid > ### Title: Check or fix polygon or extent validity > ### Aliases: is.valid is.valid,SpatVector-method is.valid,SpatExtent-method > ### makeValid makeValid,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > w <- vect("POLYGON ((0 -5, 10 0, 10 -10, 0 -5))") > is.valid(w) [1] TRUE > > w <- vect("POLYGON ((0 -5, 10 0, 10 -10, 4 -2, 0 -5))") > is.valid(w) [1] FALSE > is.valid(w, TRUE) valid reason 1 FALSE Self-intersection[4.54545454545455 -2.72727272727273] > v1 <- makeValid(w) > is.valid(v1) [1] TRUE > v2 <- makeValid(w, buffer=TRUE) > > plot(disagg(v1), col=c("light blue", "gray")) > # valid but incomplete > lines(v2, col="red", lwd=3) > > plot(w) > points(cbind(4.54, -2.72), cex=2, col="red") > > e <- ext(0, 1, 0, 1) > is.valid(e) [1] TRUE > > ee <- ext(0, 0, 0, 0) > is.valid(ee) [1] TRUE > > > > > cleanEx() > nameEx("k_means") > ### * k_means > > flush(stderr()); flush(stdout()) > > ### Name: k_means > ### Title: k_means > ### Aliases: k_means k_means,ANY-method k_means,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/logo.tif", package = "terra") > r <- rast(f) > km <- k_means(r, centers=5) > km class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : memory name : lyr1 min value : 1 max value : 5 > > > > cleanEx() > nameEx("lapp") > ### * lapp > > flush(stderr()); flush(stdout()) > > ### Name: lapp > ### Title: Apply a function to layers of a SpatRaster, or sub-datasets of a > ### SpatRasterDataset > ### Aliases: lapp lapp,SpatRaster-method lapp,SpatRasterDataset-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) + 1 > ss <- s[[2:1]] > > fvi <- function(x, y){ (x - y ) / (x + y) } > # test the function > data <- list(c(1:5,NA), 6:1) > do.call(fvi, data) [1] -0.7142857 -0.4285714 -0.1428571 0.1428571 0.4285714 NA > > x <- lapp(ss, fun=fvi ) > > # which is the same as supplying the layers to "fun" > # in some cases this will be much faster > y <- fvi(s[[2]], s[[1]]) > > f2 <- function(x, y, z){ (z - y + 1) / (x + y + 1) } > p1 <- lapp(s, fun=f2 ) > > p2 <- lapp(s[[1:2]], f2, z=200) > > # the usenames argument > > fvi2 <- function(red, green){ (red - green ) / (red + green) } > names(s) [1] "red" "green" "blue" > x1 <- lapp(s[[1:2]], fvi2, usenames=TRUE) > x2 <- lapp(s[[2:1]], fvi2, usenames=TRUE) > # x1 and x2 are the same, despite the change in the order of the layers > # x4 is also the same, but x3 is not > x3 <- lapp(s[[2:1]], fvi2, usenames=FALSE) > > # these fail because there are too many layers in s > # x4 <- lapp(s, fvi2, usenames=TRUE) > # x5 <- lapp(s, fvi2, usenames=FALSE) > > pairs(c(x1, x2, x3)) > > ## SpatRasterDataset > x <- sds(s, s[[1]]+50) > fun <- function(x, y) { x/y } > > # test "fun" > data <- list(matrix(1:9, ncol=3), matrix(9:1, ncol=3)) > do.call(fun, data) [,1] [,2] [,3] [1,] 0.1111111 0.6666667 2.333333 [2,] 0.2500000 1.0000000 4.000000 [3,] 0.4285714 1.5000000 9.000000 > > lapp(x, fun, recycle=TRUE) class : SpatRaster size : 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(s) : memory names : lyr.1, lyr.2, lyr.3 min values : 0.01960784, 0.01851852, 0.01694915 max values : 0.83660131, 0.88571429, 1.12359551 > > # the same, more concisely > z <- s / (s[[1]]+50) > > > > cleanEx() > nameEx("layerCor") > ### * layerCor > > flush(stderr()); flush(stdout()) > > ### Name: layerCor > ### Title: Correlation and (weighted) covariance > ### Aliases: layerCor layerCor,SpatRaster-method > > ### ** Examples > > b <- rast(system.file("ex/logo.tif", package="terra")) > layerCor(b, "cor") $correlation red green blue red 1.0000000 0.9980961 0.9501633 green 0.9980961 1.0000000 0.9658011 blue 0.9501633 0.9658011 1.0000000 $mean red green blue red NaN 182.2855 182.2855 green 185.3509 NaN 185.3509 blue 192.8046 192.8046 NaN $n red green blue red NaN 7777 7777 green 7777 NaN 7777 blue 7777 7777 NaN > > layerCor(b, "cov") $covariance red green blue red 5564.371 5443.405 4993.165 green 5443.405 5345.403 4974.478 blue 4993.165 4974.478 4962.942 $mean red green blue red 182.2855 182.2855 182.2855 green 185.3509 185.3509 185.3509 blue 192.8046 192.8046 192.8046 $n [,1] [,2] [,3] [1,] 7777 7777 7777 [2,] 7777 7777 7777 [3,] 7777 7777 7777 > > # weigh by column number > w <- init(b, fun="col") > layerCor(b, "weighted.cov", w=w) $weighted_covariance red green blue red 5670.750 5536.351 5009.851 green 5536.351 5427.161 4987.092 blue 5009.851 4987.092 4937.007 $weighted_mean red green blue red 177.5983 177.5983 177.5983 green 181.3521 181.3521 181.3521 blue 191.5236 191.5236 191.5236 > > # specify another function > layerCor(b, function(x, y) cor(x, y, method="spearman")) [,1] [,2] [,3] [1,] 1.0000000 0.9884613 0.9291036 [2,] 0.9884613 1.0000000 0.9425167 [3,] 0.9291036 0.9425167 1.0000000 > > > > cleanEx() > nameEx("legend") > ### * legend > > flush(stderr()); flush(stdout()) > > ### Name: add_legend > ### Title: add a custom legend > ### Aliases: add_legend > ### Keywords: methods spatial > > ### ** Examples > > v <- vect(system.file("ex/lux.shp", package="terra")) > plot(v) > points(centroids(v), col="red") > legend("topleft", legend = "centroids", pch = 20, xpd=NA, bg="white", col="red") > add_legend("topright", legend = "centroids", pch = 20, col="red") > > > > cleanEx() > nameEx("linearUnits") > ### * linearUnits > > flush(stderr()); flush(stdout()) > > ### Name: linearUnits > ### Title: Linear units of the coordinate reference system > ### Aliases: linearUnits linearUnits,SpatRaster-method > ### linearUnits,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > x <- rast() > crs(x) <- "" > linearUnits(x) [1] NaN > > crs(x) <- "+proj=longlat +datum=WGS84" > linearUnits(x) [1] 0 > > crs(x) <- "+proj=utm +zone=1 +units=cm" > linearUnits(x) [1] 0.01 > > crs(x) <- "+proj=utm +zone=1 +units=km" > linearUnits(x) [1] 1000 > > crs(x) <- "+proj=utm +zone=1 +units=us-ft" > linearUnits(x) [1] 0.3048006 > > > > cleanEx() > nameEx("lines") > ### * lines > > flush(stderr()); flush(stdout()) > > ### Name: lines > ### Title: Add points, lines, or polygons to a map > ### Aliases: points lines polys lines,SpatVector-method > ### lines,SpatRaster-method lines,SpatGraticule-method > ### lines,SpatExtent-method lines,sf-method points,SpatVector-method > ### points,SpatRaster-method points,SpatExtent-method points,sf-method > ### polys,SpatVector-method polys,SpatRaster-method > ### polys,SpatExtent-method polys,sf-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > r <- rast(v) > values(r) <- 1:ncell(r) > plot(r) > lines(v) > points(v) > > > > cleanEx() > nameEx("makeTiles") > ### * makeTiles > > flush(stderr()); flush(stdout()) > > ### Name: makeTiles > ### Title: Make tiles or get their extents > ### Aliases: getTileExtents getTileExtents,SpatRaster-method makeTiles > ### makeTiles,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=100, nrows=100) > values(r) <- 1:ncell(r) > x <- rast(ncols=2, nrows=2) > > getTileExtents(r, x) xmin xmax ymin ymax [1,] -180 0 0 90 [2,] 0 180 0 90 [3,] -180 0 -90 0 [4,] 0 180 -90 0 > getTileExtents(r, x, buffer=3) xmin xmax ymin ymax [1,] -190.8 10.8 -5.4 95.4 [2,] -10.8 190.8 -5.4 95.4 [3,] -190.8 10.8 -95.4 5.4 [4,] -10.8 190.8 -95.4 5.4 > > > filename <- paste0(tempfile(), "_.tif") > ff <- makeTiles(r, x, filename) > ff [1] "/tmp/RtmpvNGYQp/file103e5322d25af9_1.tif" [2] "/tmp/RtmpvNGYQp/file103e5322d25af9_2.tif" [3] "/tmp/RtmpvNGYQp/file103e5322d25af9_3.tif" [4] "/tmp/RtmpvNGYQp/file103e5322d25af9_4.tif" > > vrt(ff) class : SpatRaster size : 100, 100, 1 (nrow, ncol, nlyr) resolution : 3.6, 1.8 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : spat_103e531245305d_1064531_DvggOkjkTOQeAAI.vrt name : spat_103e531245305d_1064531_DvggOkjkTOQeAAI min value : 1 max value : 10000 > > > > cleanEx() > nameEx("map_extent") > ### * map_extent > > flush(stderr()); flush(stdout()) > > ### Name: map_extent > ### Title: Get the coordinates of the extent of a map > ### Aliases: map_extent > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(xmin=0, xmax=10, ymin=0, ymax=10, res=1, vals=1:100) > plot(r) > > map_extent() xmin xmax ymin ymax geo 0 10 0 10 TRUE > par()$usr [1] 8.915711e-16 1.000000e+01 -5.146492e-01 1.051465e+01 > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("mappal") > ### * mappal > > flush(stderr()); flush(stdout()) > > ### Name: map.pal > ### Title: color palettes for mapping > ### Aliases: map.pal > ### Keywords: spatial > > ### ** Examples > > map.pal("elevation", 10) [1] "#00BFBF" "#00E254" "#1CFF00" "#A9FF00" "#FFE200" "#FF9B00" "#E97F14" [8] "#C67F37" "#C39F7B" "#C8C8C8" > > r <- rast(system.file("ex/elev.tif", package="terra")) > plot(r, col=map.pal("elevation")) > > map.pal() [1] "aspect" "bcyr" "bgyr" "blues" "byg" [6] "byr" "curvature" "differences" "elevation" "grass" [11] "greens" "grey" "gyr" "haxby" "inferno" [16] "magma" "oranges" "plasma" "rainbow" "ramp" [21] "reds" "roygbiv" "rstcurv" "ryb" "ryg" [26] "sepia" "viridis" "water" "wave" > > > > cleanEx() > nameEx("mask") > ### * mask > > flush(stderr()); flush(stdout()) > > ### Name: mask > ### Title: Mask values in a SpatRaster or SpatVector > ### Aliases: mask mask,SpatRaster,SpatRaster-method > ### mask,SpatRaster,SpatVector-method mask,SpatRaster,SpatExtent-method > ### mask,SpatRaster,sf-method mask,SpatVector,SpatVector-method > ### mask,SpatVector,SpatExtent-method mask,SpatVector,sf-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > msk <- ifel(r < 400, NA, 1) > > m <- mask(r, msk) > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f)[1,] > > mv1 <- mask(r, v) > mv2 <- crop(r, v, mask=TRUE) > > > > cleanEx() > nameEx("match") > ### * match > > flush(stderr()); flush(stdout()) > > ### Name: match > ### Title: Value matching for SpatRasters > ### Aliases: match match,SpatRaster-method %in% %in%,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(nrows=10, ncols=10) > values(r) <- 1:100 > m <- match(r, c(5:10, 50:55)) > n <- r %in% c(5:10, 50:55) > > > > cleanEx() > nameEx("math-generics") > ### * math-generics > > flush(stderr()); flush(stdout()) > > ### Name: Math-methods > ### Title: General mathematical methods > ### Aliases: Math-methods Math2-methods Math,SpatRaster-method > ### Math2,SpatRaster-method Math,SpatExtent-method > ### Math2,SpatExtent-method Math2,SpatVector-method cumsum > ### cumsum,SpatRaster-method log log,SpatRaster-method round > ### round,SpatRaster-method round round,SpatVector-method sqrt > ### sqrt,SpatRaster-method math math,SpatRaster-method > ### Keywords: methods math spatial > > ### ** Examples > > r1 <- rast(ncols=10, nrows=10) > v <- runif(ncell(r1)) > v[10:20] <- NA > values(r1) <- v > r2 <- rast(r1) > values(r2) <- 1:ncell(r2) / ncell(r2) > r <- c(r1, r2) > > s <- sqrt(r) > # same as > math(r, "sqrt") class : SpatRaster size : 10, 10, 2 (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 min values : 0.1157166, 0.1 max values : 0.9801112, 1.0 > > round(s, 1) class : SpatRaster size : 10, 10, 2 (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 min values : 0.1, 0.1 max values : 1.0, 1.0 > > cumsum(r) class : SpatRaster size : 10, 10, 2 (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 min values : 0.01339033, 0.2516819 max values : 0.96061800, 1.8162692 > > > > cleanEx() > nameEx("mem") > ### * mem > > flush(stderr()); flush(stdout()) > > ### Name: mem > ### Title: Memory available and needed > ### Aliases: mem_info free_RAM > ### Keywords: spatial > > ### ** Examples > > mem_info(rast()) ------------------------ Memory (GB) ------------------------ check threshold : 1 (memmin) available : 362.66 allowed (50%) : 181.33 needed (n=1) : 0 ------------------------ proc in memory : TRUE nr chunks : 1 ------------------------ > > free_RAM() [1] 380274632 > > > > cleanEx() > nameEx("merge") > ### * merge > > flush(stderr()); flush(stdout()) > > ### Name: merge > ### Title: Merge SpatRasters, or merge a SpatVector with a data.frame > ### Aliases: merge merge,SpatRaster,SpatRaster-method > ### merge,SpatRasterCollection,missing-method > ### merge,SpatVector,SpatVector-method merge,SpatVector,data.frame-method > ### Keywords: methods spatial > > ### ** Examples > > x <- rast(xmin=-110, xmax=-80, ymin=40, ymax=70, res=1, vals=1) > y <- rast(xmin=-85, xmax=-55, ymax=60, ymin=30, res=1, vals=2) > z <- rast(xmin=-60, xmax=-30, ymax=50, ymin=20, res=1, vals=3) > > m1 <- merge(x, y, z) > m2 <- merge(z, y, x) > m3 <- merge(y, x, z) > # panel(c(m1, m2, m3)) > > # if you have many SpatRasters, it may be convenient > # to make a SpatRasterCollection > # s <- sprc(list(x, y, z)) > s <- sprc(x, y, z) > > sm1 <- merge(s, algo=1, first=FALSE) > sm2 <- merge(s, algo=2, first=FALSE) > #sm3 <- merge(s, algo=3, first=FALSE) > > ## SpatVector with data.frame > f <- system.file("ex/lux.shp", package="terra") > p <- vect(f) > dfr <- data.frame(District=p$NAME_1, Canton=p$NAME_2, Value=round(runif(length(p), 100, 1000))) > dfr <- dfr[1:5, ] > pm <- merge(p, dfr, all.x=TRUE, by.x=c('NAME_1', 'NAME_2'), by.y=c('District', 'Canton')) > pm class : SpatVector geometry : polygons dimensions : 12, 7 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 ID_1 ID_2 AREA POP Value type : values : Diekirch Clervaux 1 1 312 1.808e+04 339 Diekirch Diekirch 1 2 218 3.254e+04 435 Diekirch Redange 1 3 259 1.866e+04 616 > values(pm) NAME_1 NAME_2 ID_1 ID_2 AREA POP Value 1 Diekirch Clervaux 1 1 312 18081 339 2 Diekirch Diekirch 1 2 218 32543 435 3 Diekirch Redange 1 3 259 18664 616 4 Diekirch Vianden 1 4 76 5163 917 5 Diekirch Wiltz 1 5 263 16735 282 6 Grevenmacher Echternach 2 6 188 18899 NA 7 Grevenmacher Remich 2 7 129 22366 NA 8 Grevenmacher Grevenmacher 2 12 210 29828 NA 9 Luxembourg Capellen 3 8 185 48187 NA 10 Luxembourg Esch-sur-Alzette 3 9 251 176820 NA 11 Luxembourg Luxembourg 3 10 237 182607 NA 12 Luxembourg Mersch 3 11 233 32112 NA > > > > cleanEx() > nameEx("mergeTime") > ### * mergeTime > > flush(stderr()); flush(stdout()) > > ### Name: mergeTime > ### Title: merge SpatRasters by timelines to create a single timeseries > ### Aliases: mergeTime mergeTime,SpatRasterDataset-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > s1 <- c(r, r) > time(s1) <- as.Date("2001-01-01") + 0:5 > s1 <- s1/10 > time(s1) <- as.Date("2001-01-07") + 0:5 > s2 <- s1*10 > time(s2) <- as.Date("2001-01-05") + 0:5 > x <- sds(s1, s1, s2) > > m <- mergeTime(x, mean) > > > > cleanEx() > nameEx("metags") > ### * metags > > flush(stderr()); flush(stdout()) > > ### Name: metags > ### Title: Set or get metadata > ### Aliases: metags metags,SpatRaster-method > ### metags,SpatRasterDataset-method metags,SpatRasterCollection-method > ### metags<- metags<-,SpatRaster-method metags<-,SpatRasterDataset-method > ### metags<-,SpatRasterCollection-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncol=5, nrow=5) > m <- cbind(c("one", "two", "three"), c("ABC", "123", "hello")) > metags(r) <- m > metags(r) name value domain 1 one ABC 2 two 123 3 three hello > > metags(r) <- c("another_tag=another_value", "one more=this value") > metags(r) name value domain 1 one ABC 2 two 123 3 three hello 4 another_tag another_value 5 one more this value > > metags(r) <- cbind("test", "this", "mydomain") > metags(r) name value domain 1 one ABC 2 two 123 3 three hello 4 another_tag another_value 5 one more this value 6 test this mydomain > > metags(r, name="two") name value domain 2 two 123 > > # remove a tag > metags(r) <- cbind("one", "") > metags(r) <- "two=" > metags(r) name value domain 1 three hello 2 another_tag another_value 3 one more this value 4 test this mydomain > > # remove all tags > metags(r) <- NULL > metags(r) [1] name value domain <0 rows> (or 0-length row.names) > > > > cleanEx() > nameEx("minmax") > ### * minmax > > flush(stderr()); flush(stdout()) > > ### Name: extremes > ### Title: Get or compute the minimum and maximum cell values > ### Aliases: minmax hasMinMax setMinMax minmax,SpatRaster-method > ### hasMinMax,SpatRaster-method setMinMax,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(system.file("ex/elev.tif", package="terra")) > minmax(r) elevation min 141 max 547 > > > > cleanEx() > nameEx("modal") > ### * modal > > flush(stderr()); flush(stdout()) > > ### Name: modal > ### Title: modal value > ### Aliases: modal modal,SpatRaster-method > ### Keywords: univar math > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > r <- c(r/2, r, r*2) > m <- modal(r) > > > > cleanEx() > nameEx("mosaic") > ### * mosaic > > flush(stderr()); flush(stdout()) > > ### Name: mosaic > ### Title: mosaic SpatRasters > ### Aliases: mosaic mosaic,SpatRaster,SpatRaster-method > ### mosaic,SpatRasterCollection,missing-method > ### Keywords: methods spatial > > ### ** Examples > > > x <- rast(xmin=-110, xmax=-60, ymin=40, ymax=70, res=1, vals=1) > y <- rast(xmin=-95, xmax=-45, ymax=60, ymin=30, res=1, vals=2) > z <- rast(xmin=-80, xmax=-30, ymax=50, ymin=20, res=1, vals=3) > > m1 <- mosaic(x, y, z) > > m2 <- mosaic(z, y, x) > > # with many SpatRasters, make a SpatRasterCollection from a list > rlist <- list(x, y, z) > rsrc <- sprc(rlist) > > m <- mosaic(rsrc) > > > > cleanEx() > nameEx("na.omit") > ### * na.omit > > flush(stderr()); flush(stdout()) > > ### Name: na.omit > ### Title: Find and remove geometries that are NA > ### Aliases: na.omit na.omit,SpatVector-method is.na,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v$test <- c(1,2,NA) > nrow(v) [1] 12 > x <- na.omit(v, "test") > nrow(x) [1] 8 > > > > cleanEx() > nameEx("names") > ### * names > > flush(stderr()); flush(stdout()) > > ### Name: names > ### Title: Names of Spat* objects > ### Aliases: name name<- names names<- names,SpatRaster-method > ### names<-,SpatRaster-method names,SpatRasterDataset-method > ### names<-,SpatRasterDataset-method names,SpatRasterCollection-method > ### names<-,SpatRasterCollection-method names,SpatVector-method > ### names,SpatVectorCollection-method names,SpatVectorProxy-method > ### names<-,SpatVector-method names<-,SpatVectorCollection-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(ncols=5, nrows=5, nlyrs=3) > nlyr(s) [1] 3 > names(s) [1] "lyr.1" "lyr.2" "lyr.3" > names(s) <- c("a", "b", "c") > names(s) [1] "a" "b" "c" > > # SpatVector names > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > names(v) [1] "ID_1" "NAME_1" "ID_2" "NAME_2" "AREA" "POP" > names(v) <- paste0(substr(names(v), 1, 2), "_", 1:ncol(v)) > names(v) [1] "ID_1" "NA_2" "ID_3" "NA_4" "AR_5" "PO_6" > > > > cleanEx() > nameEx("nearby") > ### * nearby > > flush(stderr()); flush(stdout()) > > ### Name: nearest > ### Title: nearby geometries > ### Aliases: nearby nearby,SpatVector-method nearest > ### nearest,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > nearby(v, distance=12000) from to [1,] 2 4 [2,] 6 8 > > > > cleanEx() > nameEx("normalize.longitude") > ### * normalize.longitude > > flush(stderr()); flush(stdout()) > > ### Name: normalize.longitude > ### Title: normalize vector data that crosses the dateline > ### Aliases: normalize.longitude normalize.longitude,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > p <- vect("POLYGON ((120 10, 230 75, 230 -75, 120 10))") > normalize.longitude(p) class : SpatVector geometry : polygons dimensions : 1, 0 (geometries, attributes) extent : -180, 180, -75, 75 (xmin, xmax, ymin, ymax) coord. ref. : > > > > cleanEx() > nameEx("north") > ### * north > > flush(stderr()); flush(stdout()) > > ### Name: north > ### Title: North arrow > ### Aliases: north > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/meuse.tif", package="terra") > r <- rast(f) > plot(r) > north() > north(c(178550, 332500), d=250) > > ## Not run: > ##D f <- system.file("ex/elev.tif", package="terra") > ##D r <- rast(f) > ##D plot(r, type="interval") > ##D north(type=3, cex=.8) > ##D north(xy=c(6.7, 49.9), type=2, angle=45, label="NE") > ##D north(xy=c(6.6, 49.7), type=5, cex=1.25) > ##D north(xy=c(5.65, 49.6), type=9) > ##D north(d=.05, xy=c(5.65, 50), angle=180, label="S", lwd=2, col="blue") > ##D > ##D > ##D ## all arrows > ##D r <- rast(res=10) > ##D values(r) <- 1 > ##D plot(r, col="white", axes=FALSE, legend=FALSE, mar=c(0,0,0,0), reset=TRUE) > ##D for (i in 1:12) { > ##D x = -200+i*30 > ##D north(xy=cbind(x,30), type=i) > ##D text(x, -20, i, xpd=TRUE) > ##D } > ##D > ## End(Not run) > > > > > cleanEx() > nameEx("not.na") > ### * not.na > > flush(stderr()); flush(stdout()) > > ### Name: not.na > ### Title: is not NA > ### Aliases: not.na not.na,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5, vals=1, ext=c(0,1,0,1)) > r[10:20] <- NA > x <- not.na(r) > y <- not.na(r, falseNA=TRUE) > unique(values(c(x, y))) lyr.1 lyr.1 [1,] TRUE TRUE [2,] FALSE NA > > > > cleanEx() > nameEx("nseg") > ### * nseg > > flush(stderr()); flush(stdout()) > > ### Name: nseg > ### Title: Number of segments > ### Aliases: nseg nseg,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > nseg(v) [1] 330 441 308 165 363 249 195 296 297 442 538 359 > > > > cleanEx() > nameEx("origin") > ### * origin > > flush(stderr()); flush(stdout()) > > ### Name: origin > ### Title: Origin > ### Aliases: origin origin,SpatRaster-method origin<- > ### origin<-,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(xmin=-0.5, xmax = 9.5, ncols=10) > origin(r) [1] 0.5 0.0 > origin(r) <- c(0,0) > r class : SpatRaster size : 180, 10, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 10, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) > > > > cleanEx() > nameEx("pairs") > ### * pairs > > flush(stderr()); flush(stdout()) > > ### Name: pairs > ### Title: Pairs plot (matrix of scatterplots) > ### Aliases: pairs pairs,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <-rast(system.file("ex/elev.tif", package="terra")) > s <- c(r, 1/r, sqrt(r)) > names(s) <- c("elevation", "inverse", "sqrt") > pairs(s) > > # to make individual histograms: > hist(r) > # or scatter plots: > plot(s[[1]], s[[2]]) > > > > cleanEx() > nameEx("panel") > ### * panel > > flush(stderr()); flush(stdout()) > > ### Name: panel > ### Title: Map panel > ### Aliases: panel panel,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/elev.tif", package="terra")) > v <- vect(system.file("ex/lux.shp", package="terra")) > x <- c(r, r/2, r*2, r) > names(x) <- paste0("(", LETTERS[1:4], ")") > panel(x) > panel(x, fun=function() lines(v), loc.main="topright") > > > > cleanEx() > nameEx("patches") > ### * patches > > flush(stderr()); flush(stdout()) > > ### Name: patches > ### Title: Detect patches (clumps) of cells > ### Aliases: patches patches,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=18, ncols=36, xmin=0) > r[1:2, 5:8] <- 1 > r[5:8, 2:6] <- 1 > r[7:12, 22:36] <- 1 > r[15:16, 18:29] <- 1 > p <- patches(r) > > # zero as background instead of NA > r <- rast(nrows=10, ncols=10, xmin=0, vals=0) > r[3, 3] <- 10 > r[4, 4] <- 10 > r[5, 5:8] <- 12 > r[6, 6:9] <- 12 > > # treat zeros as NA > > p4 <- patches(r, zeroAsNA=TRUE) > p8 <- patches(r, 8, zeroAsNA=TRUE) > > ### patches for different values > p <- patches(r, values=TRUE) > > ### patch ID values are not guaranteed to be consecutive > r <- rast(nrows=5, ncols=10, xmin=0) > set.seed(0) > values(r)<- round(runif(ncell(r))*0.7) > rp <- patches(r, directions=8, zeroAsNA=TRUE) > plot(rp, type="classes"); text(rp) > > ## unless you set allowGaps=FALSE > rp <- patches(r, directions=8, zeroAsNA=TRUE, allowGaps=FALSE) > plot(rp, type="classes"); text(rp) > > ### use zonal to remove small patches > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- classify(r, cbind(-Inf, 400, NA)) > y <- patches(x) > # remove patches smaller than 100 ha > rz <- zonal(cellSize(y, unit="ha"), y, sum, as.raster=TRUE) > s <- ifel(rz < 250, NA, y) > > > > cleanEx() > nameEx("perim") > ### * perim > > flush(stderr()); flush(stdout()) > > ### Name: perim > ### Title: Perimeter or length > ### Aliases: perim perim,SpatVector-method perimeter > ### perimeter,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > perim(v) [1] 117100.12 93477.28 84502.45 44919.14 85032.61 74708.05 57991.42 [8] 81203.93 74443.82 95564.74 80618.76 70810.67 > > > > cleanEx() > nameEx("persp") > ### * persp > > flush(stderr()); flush(stdout()) > > ### Name: persp > ### Title: Perspective plot > ### Aliases: persp persp,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/elev.tif", package="terra")) > persp(r) > > > > cleanEx() > nameEx("pitfinder") > ### * pitfinder > > flush(stderr()); flush(stdout()) > > ### Name: pitfinder > ### Title: Pit Finder in a Flow Dir SpatRaster for Watershed Extraction > ### Aliases: pitfinder pitfinder,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > ## Creation of a Digital Elevation Model > > elev <- array(NA,c(9,9)) > dx <- 1 > dy <- 1 > for (r in 1:nrow(elev)) { + x <- (r-5)*dx + for (c in 1:ncol(elev)) { + + y <- (c-5)*dy + elev[r,c] <- 10+5*(x^2+y^2) + } + } > > elev <- cbind(elev,elev,elev,elev) > elev <- rbind(elev,elev,elev,elev) > elev <- rast(elev) > > ## Flow Directions > > flowdir<- terrain(elev,v="flowdir") > t(array(flowdir[],rev(dim(flowdir)[1:2]))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [2,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [3,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [4,] 1 1 1 2 4 8 16 16 16 1 1 1 2 [5,] 1 1 1 1 0 16 16 16 16 1 1 1 1 [6,] 1 1 1 128 64 32 16 16 16 1 1 1 128 [7,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [8,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [9,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [10,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [11,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [12,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [13,] 1 1 1 2 4 8 16 16 16 1 1 1 2 [14,] 1 1 1 1 0 16 16 16 16 1 1 1 1 [15,] 1 1 1 128 64 32 16 16 16 1 1 1 128 [16,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [17,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [18,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [19,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [20,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [21,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [22,] 1 1 1 2 4 8 16 16 16 1 1 1 2 [23,] 1 1 1 1 0 16 16 16 16 1 1 1 1 [24,] 1 1 1 128 64 32 16 16 16 1 1 1 128 [25,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [26,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [27,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [28,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [29,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [30,] 2 2 2 4 4 4 8 8 8 2 2 2 4 [31,] 1 1 1 2 4 8 16 16 16 1 1 1 2 [32,] 1 1 1 1 0 16 16 16 16 1 1 1 1 [33,] 1 1 1 128 64 32 16 16 16 1 1 1 128 [34,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [35,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [36,] 128 128 128 64 64 64 32 32 32 128 128 128 64 [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [1,] 4 4 8 8 8 2 2 2 4 4 4 8 [2,] 4 4 8 8 8 2 2 2 4 4 4 8 [3,] 4 4 8 8 8 2 2 2 4 4 4 8 [4,] 4 8 16 16 16 1 1 1 2 4 8 16 [5,] 0 16 16 16 16 1 1 1 1 0 16 16 [6,] 64 32 16 16 16 1 1 1 128 64 32 16 [7,] 64 64 32 32 32 128 128 128 64 64 64 32 [8,] 64 64 32 32 32 128 128 128 64 64 64 32 [9,] 64 64 32 32 32 128 128 128 64 64 64 32 [10,] 4 4 8 8 8 2 2 2 4 4 4 8 [11,] 4 4 8 8 8 2 2 2 4 4 4 8 [12,] 4 4 8 8 8 2 2 2 4 4 4 8 [13,] 4 8 16 16 16 1 1 1 2 4 8 16 [14,] 0 16 16 16 16 1 1 1 1 0 16 16 [15,] 64 32 16 16 16 1 1 1 128 64 32 16 [16,] 64 64 32 32 32 128 128 128 64 64 64 32 [17,] 64 64 32 32 32 128 128 128 64 64 64 32 [18,] 64 64 32 32 32 128 128 128 64 64 64 32 [19,] 4 4 8 8 8 2 2 2 4 4 4 8 [20,] 4 4 8 8 8 2 2 2 4 4 4 8 [21,] 4 4 8 8 8 2 2 2 4 4 4 8 [22,] 4 8 16 16 16 1 1 1 2 4 8 16 [23,] 0 16 16 16 16 1 1 1 1 0 16 16 [24,] 64 32 16 16 16 1 1 1 128 64 32 16 [25,] 64 64 32 32 32 128 128 128 64 64 64 32 [26,] 64 64 32 32 32 128 128 128 64 64 64 32 [27,] 64 64 32 32 32 128 128 128 64 64 64 32 [28,] 4 4 8 8 8 2 2 2 4 4 4 8 [29,] 4 4 8 8 8 2 2 2 4 4 4 8 [30,] 4 4 8 8 8 2 2 2 4 4 4 8 [31,] 4 8 16 16 16 1 1 1 2 4 8 16 [32,] 0 16 16 16 16 1 1 1 1 0 16 16 [33,] 64 32 16 16 16 1 1 1 128 64 32 16 [34,] 64 64 32 32 32 128 128 128 64 64 64 32 [35,] 64 64 32 32 32 128 128 128 64 64 64 32 [36,] 64 64 32 32 32 128 128 128 64 64 64 32 [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [1,] 8 8 2 2 2 4 4 4 8 8 8 [2,] 8 8 2 2 2 4 4 4 8 8 8 [3,] 8 8 2 2 2 4 4 4 8 8 8 [4,] 16 16 1 1 1 2 4 8 16 16 16 [5,] 16 16 1 1 1 1 0 16 16 16 16 [6,] 16 16 1 1 1 128 64 32 16 16 16 [7,] 32 32 128 128 128 64 64 64 32 32 32 [8,] 32 32 128 128 128 64 64 64 32 32 32 [9,] 32 32 128 128 128 64 64 64 32 32 32 [10,] 8 8 2 2 2 4 4 4 8 8 8 [11,] 8 8 2 2 2 4 4 4 8 8 8 [12,] 8 8 2 2 2 4 4 4 8 8 8 [13,] 16 16 1 1 1 2 4 8 16 16 16 [14,] 16 16 1 1 1 1 0 16 16 16 16 [15,] 16 16 1 1 1 128 64 32 16 16 16 [16,] 32 32 128 128 128 64 64 64 32 32 32 [17,] 32 32 128 128 128 64 64 64 32 32 32 [18,] 32 32 128 128 128 64 64 64 32 32 32 [19,] 8 8 2 2 2 4 4 4 8 8 8 [20,] 8 8 2 2 2 4 4 4 8 8 8 [21,] 8 8 2 2 2 4 4 4 8 8 8 [22,] 16 16 1 1 1 2 4 8 16 16 16 [23,] 16 16 1 1 1 1 0 16 16 16 16 [24,] 16 16 1 1 1 128 64 32 16 16 16 [25,] 32 32 128 128 128 64 64 64 32 32 32 [26,] 32 32 128 128 128 64 64 64 32 32 32 [27,] 32 32 128 128 128 64 64 64 32 32 32 [28,] 8 8 2 2 2 4 4 4 8 8 8 [29,] 8 8 2 2 2 4 4 4 8 8 8 [30,] 8 8 2 2 2 4 4 4 8 8 8 [31,] 16 16 1 1 1 2 4 8 16 16 16 [32,] 16 16 1 1 1 1 0 16 16 16 16 [33,] 16 16 1 1 1 128 64 32 16 16 16 [34,] 32 32 128 128 128 64 64 64 32 32 32 [35,] 32 32 128 128 128 64 64 64 32 32 32 [36,] 32 32 128 128 128 64 64 64 32 32 32 > > ## Pit Detect > > pits <- pitfinder(flowdir) > > ## Application with example DEM > > elev <- rast(system.file('ex/elev.tif',package="terra")) > flowdir <- terrain(elev,"flowdir") > > pits <- pitfinder(flowdir) > > > > > > > > > cleanEx() > nameEx("plet") > ### * plet > > flush(stderr()); flush(stdout()) > > ### Name: plet > ### Title: Plot with leaflet > ### Aliases: plet plet,missing-method plet,SpatRaster-method > ### plet,SpatRasterCollection-method plet,SpatVector-method > ### plet,SpatVectorCollection-method lines,leaflet-method > ### points,leaflet-method polys,leaflet-method > ### Keywords: methods spatial > > ### ** Examples > > ## Not run: > ##D if (require(leaflet) && (packageVersion("leaflet") > "2.1.1")) { > ##D > ##D v <- vect(system.file("ex/lux.shp", package="terra")) > ##D p <- spatSample(as.polygons(v, ext=T), 30) > ##D values(p) = data.frame(id=11:40, name=sample(letters, 30, replace=TRUE)) > ##D > ##D m <- plet(v, "NAME_1", tiles="", border="blue") > ##D m <- points(m, p, col="red", cex=2, popup=T) > ##D lines(m, v, lwd=1, col="white") > ##D > ##D plet(v, "NAME_1", split=TRUE, alpha=.2) |> > ##D points(p, col="white", border="red", cex=12, popup=TRUE, lwd=3, lty="1 4", > ##D clusterOptions = leaflet::markerClusterOptions()) > ##D > ##D s <- svc(v, p) > ##D names(s) <- c("the polys", "set of points") > ##D plet(s, col=c("red", "blue"), lwd=1) > ##D > ##D > ##D r <- rast(system.file("ex/elev.tif", package="terra")) > ##D plet(r, main="Hi\nthere", tiles=NULL) |> lines(v, lwd=1) > ##D > ##D plet(r, tiles="OpenTopoMap") |> lines(v, lwd=2, col="blue") > ##D > ##D x <- c(r, 50*classify(r, 5)) > ##D names(x) <- c("first", "second") > ##D > ##D # each their own legend > ##D plet(x, 1:2, collapse=FALSE) |> lines(v, lwd=2, col="blue", lty="5,5") > ##D > ##D # shared legend > ##D plet(x, 1:2, shared=TRUE, collapse=FALSE) |> lines(v, lwd=2, col="blue") > ##D > ##D } > ## End(Not run) > > > cleanEx() > nameEx("plot") > ### * plot > > flush(stderr()); flush(stdout()) > > ### Name: plot > ### Title: Make a map > ### Aliases: plot plot,SpatRaster,missing-method > ### plot,SpatRaster,numeric-method plot,SpatRaster,character-method > ### plot,SpatVector,missing-method plot,SpatVector,numeric-method > ### plot,SpatVector,data.frame-method plot,SpatVector,character-method > ### plot,SpatVectorProxy,missing-method > ### plot,SpatVectorCollection,missing-method > ### plot,SpatVectorCollection,numeric-method > ### Keywords: methods spatial > > ### ** Examples > > ## SpatRaster > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > plot(r) > > plot(r, type="interval") > > plot(r, plg=list(x=6.35, y = c(49.9, 50.1), title="Legend\nTitle", title.cex=0.9), + pax=list(side=1:4, retro=FALSE)) > north(cbind(5.8, 50.1)) > > d <- classify(r, c(100,200,300,400,500,600)) > plot(d) > > plot(d, type="interval", breaks=1:5) > plot(d, type="interval", breaks=c(1,4,5), plg=list(legend=c("1-4", "4-5"))) > plot(d, type="classes", xlim=c(5.6, 6.6), + plg=list(legend=c("Mr", "Xx", "As", "Zx", "Bb"), x="bottomleft")) > > > x <- trunc(r/200) > levels(x) <- data.frame(id=0:2, element=c("earth", "wind", "fire")) > plot(x, plg=list(x="topright"),mar=c(2,2,2,2)) > > oldpar <- par(no.readonly=TRUE) > > # two plots with the same legend > dev.new(width=6, height=4, noRStudioGD = TRUE) > par(mfrow=c(1,2)) > plot(r, range=c(50,600), mar=c(1,1,1,4)) > plot(r/2, range=c(50,600), mar=c(1,1,1,4)) > > # as we only need one legend (also see the "panel" method): > par(mfrow=c(1,2)) > plot(r, range=c(50,600), mar=c(2, 2, 2, 2), plg=list(size=0.9, cex=.8), + pax=list(side=1:2, cex.axis=.6), box=FALSE) > #text(182500, 335000, "Two maps, one plot", xpd=NA) > plot(r/2, range=c(50,600), mar=c(2, 2, 2, 2), legend=FALSE, + pax=list(side=c(1,4), cex.axis=.6), box=FALSE) > > par(oldpar) > > # multi-layer with RGB > s <- rast(system.file("ex/logo.tif", package="terra")) > s class : SpatRaster size : 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 > plot(s) > # remove RGB > plot(s*1) > # or use layers > plot(s, 1) > plot(s, 1:3) > > # fix legend by linking values and colors > > x = rast(nrows = 2, ncols = 2, vals=1) > y = rast(nrows = 2, ncols = 2, vals=c(1,2,2,1)) > cols = data.frame(id=1:2, col=c("red", "blue")) > plot(c(x,y), col=cols) > > r = rast(nrows=10, ncols=10, vals=1:100) > dr = data.frame(from=c(5,33,66,150), to=c(33, 66, 95,200), col=rainbow(4)) > plot(r, col=dr) > > ### SpatVector > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > plot(v) > > plot(v, "NAME_2", col=rainbow(12), border=c("gray", "blue"), lwd=3, zebra=TRUE) > > plot(v, 2, pax=list(side=1:2), plg=list(x=6.16, y=50.17, cex=.8), xlim=c(5.7, 6.7)) > > plot(v, 4, pax=list(side=1:2), plg=list(x=6.2, y=50.2, ncol=2), main="", box=FALSE) > > plot(v, 1, plg=list(x=5.8, y=49.37, horiz=TRUE, cex=1.1), main="", mar=c(5,2,0.5,0.5)) > > plot(v, density=1:12, angle=seq(18, 360, 20), col=rainbow(12)) > > > plot(v, "AREA", type="interval", breaks=3, mar=c(3.1, 3.1, 2.1, 3.1), + plg=list(x="topright"), main="") > > plot(v, "AREA", type="interval", breaks=c(0,200,250,350), + mar=c(2,2,2,2), xlim=c(5.7, 6.75), + plg=list(legend=c("<200", "200-250", ">250"), cex=1, bty="o", + x=6.3, y=50.15, box.lwd=2, bg="light yellow", title="My legend")) > > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("plotRGB") > ### * plotRGB > > flush(stderr()); flush(stdout()) > > ### Name: plotRGB > ### Title: Red-Green-Blue plot of a multi-layered SpatRaster > ### Aliases: plotRGB plotRGB,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > b <- rast(system.file("ex/logo.tif", package="terra")) > plotRGB(b) > plotRGB(b, mar=2) > plotRGB(b, 3, 2, 1) > > b[1000:2000] <- NA > plotRGB(b, 3, 2, 1, stretch="hist") > > > > cleanEx() > nameEx("plot_extent") > ### * plot_extent > > flush(stderr()); flush(stdout()) > > ### Name: plot_extent > ### Title: Plot a SpatExtent > ### Aliases: plot,SpatExtent,missing-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast() > plot(ext(r)) > > > > cleanEx() > nameEx("plot_graticule") > ### * plot_graticule > > flush(stderr()); flush(stdout()) > > ### Name: plot_graticule > ### Title: Plot a graticule > ### Aliases: plot,SpatGraticule,missing-method > ### lines,SpatGraticule,missing-method > ### Keywords: methods spatial > > ### ** Examples > > g <- graticule(60, 30, crs="+proj=robin") > > plot(g, background="azure", col="red", lty=2, box=TRUE) > plot(g, background="azure", col="light gray", lab.loc=c(1,2), + lab.lon=c(2,4,6), lab.lat=3:5, lty=3, retro=TRUE) > > > > cleanEx() > nameEx("prcomp") > ### * prcomp > > flush(stderr()); flush(stdout()) > > ### Name: prcomp > ### Title: SpatRaster PCA with prcomp > ### Aliases: prcomp prcomp,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/logo.tif", package = "terra") > r <- rast(f) > pca <- prcomp(r) > x <- predict(r, pca) > > # use "index" to get a subset of the components > p <- predict(r, pca, index=1:2) > > > > > cleanEx() > nameEx("predict") > ### * predict > > flush(stderr()); flush(stdout()) > > ### Name: predict > ### Title: Spatial model predictions > ### Aliases: predict predict,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > logo <- rast(system.file("ex/logo.tif", package="terra")) > names(logo) <- c("red", "green", "blue") > p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, + 66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, + 22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2) > > a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9, + 99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21, + 37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2) > > xy <- rbind(cbind(1, p), cbind(0, a)) > > # extract predictor values for points > e <- extract(logo, xy[,2:3]) > > # combine with response (excluding the ID column) > v <- data.frame(cbind(pa=xy[,1], e)) > > #build a model, here with glm > model <- glm(formula=pa~., data=v) > > #predict to a raster > r1 <- predict(logo, model) > > plot(r1) > points(p, bg='blue', pch=21) > points(a, bg='red', pch=21) > > # logistic regression > model <- glm(formula=pa~., data=v, family="binomial") Warning: glm.fit: algorithm did not converge Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred > r1log <- predict(logo, model, type="response") > > # to get the probability and standard error > r1se <- predict(logo, model, se.fit=TRUE) > > # or provide a custom predict function > > predfun <- function(model, data) { + v <- predict(model, data, se.fit=TRUE) + cbind(p=as.vector(v$fit), se=as.vector(v$se.fit)) + } > > r2 <- predict(logo, model, fun=predfun) > > ### principal components of a SpatRaster > pca <- prcomp(logo) > > # or use sampling if you have a large raster > # and cannot process all cell values > sr <- spatSample(logo, 100000, "regular") > pca <- prcomp(sr) > > x <- predict(logo, pca) > plot(x) > > ## parallelization > ## Not run: > ##D ## simple case with GLM > ##D model <- glm(formula=pa~., data=v) > ##D p <- predict(logo, model, cores=2) > ##D > ##D ## The above does not work with a model from a contributed > ##D ## package, as the package needs to be loaded in each core. > ##D ## Below are three approaches to deal with that > ##D > ##D library(randomForest) > ##D rfm <- randomForest(formula=pa~., data=v) > ##D > ##D ## approach 0 (not parallel) > ##D rp0 <- predict(logo, rfm) > ##D > ##D ## approach 1, use the "cpkgs" argument > ##D rp1 <- predict(logo, rfm, cores=2, cpkgs="randomForest") > ##D > ##D ## approach 2, write a custom predict function that loads the package > ##D rfun <- function(mod, dat, ...) { > ##D library(randomForest) > ##D predict(mod, dat, ...) > ##D } > ##D rp2 <- predict(logo, rfm, fun=rfun, cores=2) > ##D > ##D ## approach 3, write a parallelized custom predict function > ##D rfun <- function(mod, dat, ...) { > ##D ncls <- length(cls) > ##D nr <- nrow(dat) > ##D s <- split(dat, rep(1:ncls, each=ceiling(nr/ncls), length.out=nr)) > ##D unlist( parallel::clusterApply(cls, s, function(x, ...) predict(mod, x, ...)) ) > ##D } > ##D > ##D library(parallel) > ##D cls <- parallel::makeCluster(2) > ##D parallel::clusterExport(cls, c("rfm", "rfun", "randomForest")) > ##D rp3 <- predict(logo, rfm, fun=rfun) > ##D parallel::stopCluster(cls) > ##D > ##D plot(c(rp0, rp1, rp2, rp3)) > ##D > ##D > ##D ### with two output variables (probabilities for each class) > ##D v$pa <- as.factor(v$pa) > ##D rfm2 <- randomForest(formula=pa~., data=v) > ##D rfp <- predict(logo, rfm2, cores=2, type="prob", cpkgs="randomForest") > ## End(Not run) > > > > > cleanEx() > nameEx("princomp") > ### * princomp > > flush(stderr()); flush(stdout()) > > ### Name: princomp > ### Title: SpatRaster PCA with princomp > ### Aliases: princomp princomp,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/logo.tif", package = "terra") > r <- rast(f) > pca <- princomp(r) > x <- predict(r, pca) > > # use "index" to get a subset of the components > p <- predict(r, pca, index=1:2) > > ### use princomp directly > pca2 <- princomp(values(r), fix_sign = TRUE) > p2 <- predict(r, pca2) > > ### may need to use sampling with a large raster > ### here with prcomp instead of princomp > sr <- spatSample(r, 100000, "regular") > pca3 <- prcomp(sr) > p3 <- predict(r, pca3) > > > > cleanEx() > nameEx("project") > ### * project > > flush(stderr()); flush(stdout()) > > ### Name: project > ### Title: Change the coordinate reference system > ### Aliases: project project,SpatVector-method > ### project,SpatVectorCollection-method project,SpatRaster-method > ### project,SpatExtent-method project,matrix-method > ### Keywords: spatial > > ### ** Examples > > ## SpatRaster > a <- rast(ncols=40, nrows=40, xmin=-110, xmax=-90, ymin=40, ymax=60, + crs="+proj=longlat +datum=WGS84") > values(a) <- 1:ncell(a) > newcrs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84" > b <- rast(ncols=94, nrows=124, xmin=-944881, xmax=935118, ymin=4664377, ymax=7144377, crs=newcrs) > w <- project(a, b) > > > ## SpatVector > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > crs(v, proj=TRUE) [1] "+proj=longlat +datum=WGS84 +no_defs" > cat(crs(v), "\n") GEOGCRS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84",6378137,298.257223563, LENGTHUNIT["metre",1]]], PRIMEM["Greenwich",0, ANGLEUNIT["degree",0.0174532925199433]], CS[ellipsoidal,2], AXIS["geodetic latitude (Lat)",north, ORDER[1], ANGLEUNIT["degree",0.0174532925199433]], AXIS["geodetic longitude (Lon)",east, ORDER[2], ANGLEUNIT["degree",0.0174532925199433]], ID["EPSG",4326]] > > project(v, "+proj=moll") class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 437476.4, 497805.3, 5815524, 5892478 (xmin, xmax, ymin, ymax) coord. ref. : +proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > > project(v, "EPSG:2169") class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 49540.31, 105922, 57009.53, 138631.1 (xmin, xmax, ymin, ymax) coord. ref. : LUREF / Luxembourg TM (EPSG:2169) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > > > cleanEx() > nameEx("quantile") > ### * quantile > > flush(stderr()); flush(stdout()) > > ### Name: quantile > ### Title: Quantiles of spatial data > ### Aliases: quantile quantile,SpatRaster-method quantile,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > rr <- c(r/2, r, r*2) > qr <- quantile(rr) > qr class : SpatRaster size : 77, 101, 5 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : memory names : q0, q0.25, q0.5, q0.75, q1 min values : 0.0, 0.0, 0, 0, 0 max values : 127.5, 127.5, 255, 510, 510 > > ## Not run: > ##D # same but slower > ##D qa <- app(rr, quantile) > ## End(Not run) > > #quantile by layer instead of by cell > qg <- global(r, quantile) > > > > cleanEx() > nameEx("query") > ### * query > > flush(stderr()); flush(stdout()) > > ### Name: query > ### Title: Query a SpatVectorProxy object > ### Aliases: query query,SpatVectorProxy-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f, proxy=TRUE) > v class : SpatVectorProxy geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : > > x <- query(v, vars=c("ID_2", "NAME_2"), start=5, n=2) > x class : SpatVector geometry : polygons dimensions : 2, 2 (geometries, attributes) extent : 5.74414, 6.528252, 49.72324, 50.03632 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_2 NAME_2 type : values : 5 Wiltz 6 Echternach > > query(v, vars=c("ID_2", "NAME_1", "NAME_2"), where="NAME_1='Grevenmacher' AND ID_2 > 6") class : SpatVector geometry : polygons dimensions : 2, 3 (geometries, attributes) extent : 6.169137, 6.516485, 49.46498, 49.75016 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_2 NAME_1 NAME_2 type : values : 7 Grevenmacher Remich 12 Grevenmacher Grevenmacher > > ## with an extent > e <- ext(5.9, 6.3, 49.9, 50) > x <- query(v, extent=e) > > ## with polygons > p <- as.polygons(e) > x <- query(v, filter=p) > x class : SpatVector geometry : polygons dimensions : 4, 6 (geometries, attributes) extent : 5.74414, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 4 Vianden 76 5163 > > > > cleanEx() > nameEx("rangeFill") > ### * rangeFill > > flush(stderr()); flush(stdout()) > > ### Name: rangeFill > ### Title: Fill layers with a range > ### Aliases: rangeFill rangeFill,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > x <- y <- rast(ncol=2, nrow=2) > values(x) <- c(NA, 1:3) > values(y) <- c(NA, 4:6) > > r <- rangeFill(c(x, y), 8) > > > > cleanEx() > nameEx("rapp") > ### * rapp > > flush(stderr()); flush(stdout()) > > ### Name: rapp > ### Title: Range-apply > ### Aliases: rapp rapp,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=9, nrows=9) > values(r) <- 1:ncell(r) > s <- c(r, r, r, r, r, r) > s <- s * 1:6 > s[1:2] <- NA > start <- end <- rast(r) > start[] <- 1:3 > end[] <- 4:6 > a <- rapp(s, start, end, fun="mean") > b <- rapp(s, start, 2, fun="mean") > > # cumsum from start to nlyr(x). return all layers > r <- rapp(s, start, nlyr(s), cumsum, allyrs=TRUE, fill=0) > # return only the final value > rr <- rapp(s, start, nlyr(s), function(i) max(cumsum(i))) > > > > cleanEx() > nameEx("rast") > ### * rast > > flush(stderr()); flush(stdout()) > > ### Name: rast > ### Title: Create a SpatRaster > ### Aliases: rast rast,missing-method rast,character-method > ### rast,SpatRaster-method rast,SpatRasterDataset-method > ### rast,data.frame-method rast,matrix-method rast,array-method > ### rast,list-method rast,SpatExtent-method rast,SpatVector-method > ### rast,ANY-method rast,PackedSpatRaster-method rast,stars-method > ### rast,stars_proxy-method > ### Keywords: methods spatial > > ### ** Examples > > # Create a SpatRaster from scratch > x <- rast(nrows=108, ncols=21, xmin=0, xmax=10) > > # Create a SpatRaster from a file > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > > # A file with multiple layers. This one is special as the layers are RGB color channels > s <- rast(system.file("ex/logo.tif", package="terra")) > > # remove the color channels > #plot(s) > #RGB(s) <- NULL > #plot(s) > > # Create a skeleton with no associated cell values > rast(s) class : SpatRaster size : 77, 101, 3 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) > > # from a matrix > m <- matrix(1:25, nrow=5, ncol=5) > rm <- rast(m) > > # from a "xyz" data.frame > d <- as.data.frame(rm, xy=TRUE) > head(d) x y lyr.1 1 0.5 4.5 1 2 1.5 4.5 6 3 2.5 4.5 11 4 3.5 4.5 16 5 4.5 4.5 21 6 0.5 3.5 2 > rast(d, type="xyz") class : SpatRaster size : 5, 5, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 5, 0, 5 (xmin, xmax, ymin, ymax) coord. ref. : source(s) : memory name : lyr.1 min value : 1 max value : 25 > > > > > cleanEx() > nameEx("rasterize") > ### * rasterize > > flush(stderr()); flush(stdout()) > > ### Name: rasterize > ### Title: Rasterize vector data > ### Aliases: rasterize rasterize,SpatVector,SpatRaster-method > ### rasterize,sf,SpatRaster-method rasterize,matrix,SpatRaster-method > ### rasterize,data.frame,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(xmin=0, ncols=18, nrows=18) > > # generate points > set.seed(1) > p <- spatSample(r, 1000, xy=TRUE, replace=TRUE) > > # rasterize points as a matrix > x <- rasterize(p, r, fun=sum) > y <- rasterize(p, r, value=1:nrow(p), fun=max) > > # rasterize points as a SpatVector > pv <- vect(p) > xv <- rasterize(pv, r, fun=sum) > > # Polygons > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > r <- rast(v, ncols=75, nrows=100) > z <- rasterize(v, r, "NAME_2") > plot(z) > lines(v) > > > > cleanEx() > nameEx("rasterizeGeom") > ### * rasterizeGeom > > flush(stderr()); flush(stdout()) > > ### Name: rasterizeGeom > ### Title: Rasterize geometric properties of vector data > ### Aliases: rasterizeGeom rasterizeGeom,SpatVector,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > r <- rast(v, res=.1) > > # length of lines > lns <- as.lines(v) > x <- rasterizeGeom(lns, r, fun="length", "km") > > # count of points > set.seed(44) > pts <- spatSample(v, 100) > y <- rasterizeGeom(pts, r) > > # area of polygons > pols <- buffer(pts, 1000) > z <- rasterizeGeom(pols, r, fun="area") > > > > cleanEx() > nameEx("rasterizeWin") > ### * rasterizeWin > > flush(stderr()); flush(stdout()) > > ### Name: rasterizeWin > ### Title: Rasterize points with a moving window > ### Aliases: rasterizeWin rasterizeWin,SpatVector,SpatRaster-method > ### rasterizeWin,data.frame,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > r <- rast(ncol=100, nrow=100, crs="local", xmin=0, xmax=50, ymin=0, ymax=50) > set.seed(100) > x <- runif(50, 5, 45) > y <- runif(50, 5, 45) > z <- sample(50) > xyz <- data.frame(x,y,z) > > r <- rasterizeWin(xyz, r, fun="count", pars=5) > > rfuns <- c("count", "min", "max", "mean") > x <- lapply(rfuns, function(f) rasterizeWin(xyz, r, fun=f, pars=5)) > names(x) <- rfuns > x <- rast(x) > #plot(x) > > > > > cleanEx() > nameEx("rcl") > ### * rcl > > flush(stderr()); flush(stdout()) > > ### Name: rcl > ### Title: Combine row, column, and layer numbers > ### Aliases: rcl rcl,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > x <- rast(ncol=5, nrow=5, nlyr=2) > values(x) <- 1:size(x) > > rcl(x, 1, 2:3, 1:2) row col lyr [1,] 1 2 1 [2,] 1 3 1 [3,] 1 2 2 [4,] 1 3 2 > > i <- rcl(x, 1, list(1:2, 3:4), 1:2) > i row col lyr [1,] 1 1 1 [2,] 1 2 1 [3,] 1 3 2 [4,] 1 4 2 > > # get the values for these cells > x[i] [1] 1 2 28 29 > > > > cleanEx() > nameEx("regress") > ### * regress > > flush(stderr()); flush(stdout()) > > ### Name: regress > ### Title: Cell level regression > ### Aliases: regress regress,SpatRaster,SpatRaster-method > ### regress,SpatRaster,numeric-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > x <- regress(s, 1:nlyr(s)) > > > > cleanEx() > nameEx("relate") > ### * relate > > flush(stderr()); flush(stdout()) > > ### Name: relate > ### Title: Spatial relationships between geometries > ### Aliases: relate is.related relate,SpatVector,SpatVector-method > ### relate,SpatVector,missing-method relate,SpatVector,SpatExtent-method > ### relate,SpatExtent,SpatVector-method > ### relate,SpatExtent,SpatExtent-method > ### relate,SpatVector,SpatRaster-method > ### relate,SpatRaster,SpatVector-method > ### relate,SpatExtent,SpatRaster-method > ### relate,SpatRaster,SpatExtent-method > ### relate,SpatRaster,SpatRaster-method > ### is.related,SpatVector,SpatVector-method > ### is.related,SpatVector,SpatExtent-method > ### is.related,SpatExtent,SpatVector-method > ### is.related,SpatVector,SpatRaster-method > ### is.related,SpatRaster,SpatVector-method > ### is.related,SpatRaster,SpatRaster-method > ### is.related,SpatExtent,SpatRaster-method > ### is.related,SpatRaster,SpatExtent-method > ### Keywords: methods spatial > > ### ** Examples > > > # polygons > p1 <- vect("POLYGON ((0 0, 8 0, 8 9, 0 9, 0 0))") > p2 <- vect("POLYGON ((5 6, 15 6, 15 15, 5 15, 5 6))") > p3 <- vect("POLYGON ((8 2, 9 2, 9 3, 8 3, 8 2))") > p4 <- vect("POLYGON ((2 6, 3 6, 3 8, 2 8, 2 6))") > p5 <- vect("POLYGON ((2 12, 3 12, 3 13, 2 13, 2 12))") > p6 <- vect("POLYGON ((10 4, 12 4, 12 7, 11 7, 11 6, 10 6, 10 4))") > > p <- rbind(p1, p2, p3, p4, p5, p6) > plot(p, col=rainbow(6, alpha=.5)) > lines(p, lwd=2) > text(p) > > ## relate SpatVectors > relate(p1, p2, "intersects") [,1] [1,] TRUE > relate(p1, p3, "touches") [,1] [1,] TRUE > relate(p1, p5, "disjoint") [,1] [1,] TRUE > relate(rbind(p1, p2), p4, "disjoint") [,1] [1,] FALSE [2,] TRUE > > ## relate geometries within SpatVectors > # which are completely separated? > relate(p, relation="disjoint") [,1] [,2] [,3] [,4] [,5] [,6] [1,] FALSE FALSE FALSE FALSE TRUE TRUE [2,] FALSE FALSE TRUE TRUE TRUE FALSE [3,] FALSE TRUE FALSE TRUE TRUE TRUE [4,] FALSE TRUE TRUE FALSE TRUE TRUE [5,] TRUE TRUE TRUE TRUE FALSE TRUE [6,] TRUE FALSE TRUE TRUE TRUE FALSE > > # which touch (not overlap or within)? > relate(p, relation="touches") [,1] [,2] [,3] [,4] [,5] [,6] [1,] FALSE FALSE TRUE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE [3,] TRUE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE > # which overlap (not merely touch, and not within)? > relate(p, relation="overlaps") [,1] [,2] [,3] [,4] [,5] [,6] [1,] FALSE TRUE FALSE FALSE FALSE FALSE [2,] TRUE FALSE FALSE FALSE FALSE TRUE [3,] FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE TRUE FALSE FALSE FALSE FALSE > # which are within (not merely overlap)? > relate(p, relation="within") [,1] [,2] [,3] [,4] [,5] [,6] [1,] TRUE FALSE FALSE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE FALSE FALSE [3,] FALSE FALSE TRUE FALSE FALSE FALSE [4,] TRUE FALSE FALSE TRUE FALSE FALSE [5,] FALSE FALSE FALSE FALSE TRUE FALSE [6,] FALSE FALSE FALSE FALSE FALSE TRUE > > # do they touch or overlap or are within? > relate(p, relation="intersects") [,1] [,2] [,3] [,4] [,5] [,6] [1,] TRUE TRUE TRUE TRUE FALSE FALSE [2,] TRUE TRUE FALSE FALSE FALSE TRUE [3,] TRUE FALSE TRUE FALSE FALSE FALSE [4,] TRUE FALSE FALSE TRUE FALSE FALSE [5,] FALSE FALSE FALSE FALSE TRUE FALSE [6,] FALSE TRUE FALSE FALSE FALSE TRUE > > all(relate(p, relation="intersects") == + (relate(p, relation="overlaps") | + relate(p, relation="touches") | + relate(p, relation="within"))) [1] FALSE > > #for polygons, "coveredby" is "within" > relate(p, relation="coveredby") [,1] [,2] [,3] [,4] [,5] [,6] [1,] TRUE FALSE FALSE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE FALSE FALSE [3,] FALSE FALSE TRUE FALSE FALSE FALSE [4,] TRUE FALSE FALSE TRUE FALSE FALSE [5,] FALSE FALSE FALSE FALSE TRUE FALSE [6,] FALSE FALSE FALSE FALSE FALSE TRUE > > # polygons, lines, and points > > pp <- rbind(p1, p2) > L1 <- vect("LINESTRING(1 11, 4 6, 10 6)") > L2 <- vect("LINESTRING(8 14, 12 10)") > L3 <- vect("LINESTRING(1 8, 12 14)") > lns <- rbind(L1, L2, L3) > pts <- vect(cbind(c(7,10,10), c(3,5,6))) > > plot(pp, col=rainbow(2, alpha=.5)) > text(pp, paste0("POL", 1:2), halo=TRUE) > lines(pp, lwd=2) > lines(lns, col=rainbow(3), lwd=4) > text(lns, paste0("L", 1:3), halo=TRUE) > points(pts, cex=1.5) > text(pts, paste0("PT", 1:3), halo=TRUE, pos=4) > > > relate(lns, relation="crosses") [,1] [,2] [,3] [1,] FALSE FALSE TRUE [2,] FALSE FALSE TRUE [3,] TRUE TRUE FALSE > relate(lns, pp, relation="crosses") [,1] [,2] [1,] TRUE FALSE [2,] FALSE FALSE [3,] TRUE TRUE > relate(lns, pp, relation="touches") [,1] [,2] [1,] FALSE TRUE [2,] FALSE FALSE [3,] FALSE FALSE > relate(lns, pp, relation="intersects") [,1] [,2] [1,] TRUE TRUE [2,] FALSE TRUE [3,] TRUE TRUE > > relate(lns, pp, relation="within") [,1] [,2] [1,] FALSE FALSE [2,] FALSE TRUE [3,] FALSE FALSE > # polygons can contain lines or points, not the other way around > relate(lns, pp, relation="contains") [,1] [,2] [1,] FALSE FALSE [2,] FALSE FALSE [3,] FALSE FALSE > relate(pp, lns, relation="contains") [,1] [,2] [,3] [1,] FALSE FALSE FALSE [2,] FALSE TRUE FALSE > # points and lines can be covered by polygons > relate(lns, pp, relation="coveredby") [,1] [,2] [1,] FALSE FALSE [2,] FALSE TRUE [3,] FALSE FALSE > > relate(pts, pp, "within") [,1] [,2] [1,] TRUE FALSE [2,] FALSE FALSE [3,] FALSE FALSE > relate(pts, pp, "touches") [,1] [,2] [1,] FALSE FALSE [2,] FALSE FALSE [3,] FALSE TRUE > relate(pts, lns, "touches") [,1] [,2] [,3] [1,] FALSE FALSE FALSE [2,] FALSE FALSE FALSE [3,] TRUE FALSE FALSE > > > > > cleanEx() > nameEx("rep") > ### * rep > > flush(stderr()); flush(stdout()) > > ### Name: rep > ### Title: Replicate layers > ### Aliases: rep rep,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > x <- rep(s, 2) > nlyr(x) [1] 6 > names(x) [1] "red" "green" "blue" "red" "green" "blue" > x class : SpatRaster size : 77, 101, 6 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green, blue, red, green, blue min values : 0, 0, 0, 0, 0, 0 max values : 255, 255, 255, 255, 255, 255 > > > > cleanEx() > nameEx("replace_dollar") > ### * replace_dollar > > flush(stderr()); flush(stdout()) > > ### Name: replace_dollar > ### Title: Replace with $<- > ### Aliases: $<- $<-,SpatExtent-method $<-,SpatVector-method > ### $<-,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v$ID_1 <- LETTERS[1:12] > v$new <- sample(12) > values(v) ID_1 NAME_1 ID_2 NAME_2 AREA POP new 1 A Diekirch 1 Clervaux 312 18081 9 2 B Diekirch 2 Diekirch 218 32543 4 3 C Diekirch 3 Redange 259 18664 7 4 D Diekirch 4 Vianden 76 5163 1 5 E Diekirch 5 Wiltz 263 16735 2 6 F Grevenmacher 6 Echternach 188 18899 5 7 G Grevenmacher 7 Remich 129 22366 3 8 H Grevenmacher 12 Grevenmacher 210 29828 8 9 I Luxembourg 8 Capellen 185 48187 6 10 J Luxembourg 9 Esch-sur-Alzette 251 176820 11 11 K Luxembourg 10 Luxembourg 237 182607 12 12 L Luxembourg 11 Mersch 233 32112 10 > > > > cleanEx() > nameEx("replace_layers") > ### * replace_layers > > flush(stderr()); flush(stdout()) > > ### Name: replace_layers > ### Title: Replace layers or variables > ### Aliases: [[<- [[<-,SpatRaster,character-method > ### [[<-,SpatRaster,numeric-method [[<-,SpatVector,character-method > ### [[<-,SpatVector,numeric-method > ### Keywords: methods spatial > > ### ** Examples > > # raster > s <- rast(system.file("ex/logo.tif", package="terra")) > s[["red"]] <- mean(s) > s[[2]] <- sqrt(s[[1]]) > > # vector > v <- vect(system.file("ex/lux.shp", package="terra")) > v[["ID_1"]] <- 12:1 > > > > cleanEx() > nameEx("replace_values") > ### * replace_values > > flush(stderr()); flush(stdout()) > > ### Name: replace_values > ### Title: Replace values of a SpatRaster > ### Aliases: [<- [<-,SpatRaster,ANY,ANY,ANY-method > ### [<-,SpatRaster,ANY,ANY-method [<-,SpatExtent,numeric,missing-method > ### [<-,SpatVector,ANY,ANY-method [<-,SpatVector,ANY,missing-method > ### [<-,SpatVector,missing,ANY-method > ### Keywords: methods spatial > > ### ** Examples > > ## SpatRaster > r <- rast(ncols=5, nrows=5, xmin=0, xmax=5, ymin=0, ymax=5) > r[] <- 1:25 > r[1,] <- 5 > r[,2] <- 10 > r[r>10] <- NA > > ## SpatVector > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > v[2,2] <- "hello" > v[1,] <- v[10,] > v[,3] <- v[,1] > v[2, "NAME_2"] <- "terra" > head(v, 3) ID_1 NAME_1 ID_2 NAME_2 AREA POP 1 3 Luxembourg 3 Esch-sur-Alzette 251 176820 2 1 hello 1 terra 218 32543 3 1 Diekirch 1 Redange 259 18664 > > > > cleanEx() > nameEx("resample") > ### * resample > > flush(stderr()); flush(stdout()) > > ### Name: resample > ### Title: Transfer values of a SpatRaster to another one with a different > ### geometry > ### Aliases: resample resample,SpatRaster,SpatRaster-method > ### resample,SpatRaster,numeric-method > ### Keywords: spatial > > ### ** Examples > > > r <- rast(nrows=3, ncols=3, xmin=0, xmax=10, ymin=0, ymax=10) > values(r) <- 1:ncell(r) > s <- rast(nrows=25, ncols=30, xmin=1, xmax=11, ymin=-1, ymax=11) > x <- resample(r, s, method="bilinear") > > opar <- par(no.readonly =TRUE) > par(mfrow=c(1,2)) > plot(r) > plot(x) > par(opar) > > > > graphics::par(get("par.postscript", pos = 'CheckExEnv')) > cleanEx() > nameEx("rescale") > ### * rescale > > flush(stderr()); flush(stdout()) > > ### Name: rescale > ### Title: rescale > ### Aliases: rescale rescale,SpatRaster-method rescale,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > w <- rescale(v, 0.2) > plot(v) > lines(w, col="red") > > > > cleanEx() > nameEx("roll") > ### * roll > > flush(stderr()); flush(stdout()) > > ### Name: roll > ### Title: Rolling (moving) functions > ### Aliases: roll roll,SpatRaster-method roll,numeric-method > ### Keywords: spatial > > ### ** Examples > > ## numeric > roll(1:12, 3, mean) [1] NA 2 3 4 5 6 7 8 9 10 11 NA > roll(1:12, 3, mean, "to") [1] NA NA 2 3 4 5 6 7 8 9 10 11 > roll(1:12, 3, mean, circular=TRUE) [1] 5 2 3 4 5 6 7 8 9 10 11 8 > > ## SpatRaster > r <- rast(ncol=2, nrow=2, nlyr=10, vals=1) > r[1,2] <- 2 > r[2,2] <- 4 > > values(roll(r, n=3, "sum", "from", na.rm=FALSE)) lyr.1 lyr.2 lyr.3 lyr.4 lyr.5 lyr.6 lyr.7 lyr.8 lyr.9 lyr.10 [1,] 3 3 3 3 3 3 3 3 NaN NaN [2,] 6 6 6 6 6 6 6 6 NaN NaN [3,] 3 3 3 3 3 3 3 3 NaN NaN [4,] 12 12 12 12 12 12 12 12 NaN NaN > values(roll(r, n=3, "sum", "from", na.rm=TRUE)) lyr.1 lyr.2 lyr.3 lyr.4 lyr.5 lyr.6 lyr.7 lyr.8 lyr.9 lyr.10 [1,] 3 3 3 3 3 3 3 3 2 1 [2,] 6 6 6 6 6 6 6 6 4 2 [3,] 3 3 3 3 3 3 3 3 2 1 [4,] 12 12 12 12 12 12 12 12 8 4 > values(roll(r, n=3, "sum", "from", circular=TRUE)) lyr.1 lyr.2 lyr.3 lyr.4 lyr.5 lyr.6 lyr.7 lyr.8 lyr.9 lyr.10 [1,] 3 3 3 3 3 3 3 3 3 3 [2,] 6 6 6 6 6 6 6 6 6 6 [3,] 3 3 3 3 3 3 3 3 3 3 [4,] 12 12 12 12 12 12 12 12 12 12 > > values(roll(r, n=3, "sum", "to", na.rm=TRUE)) lyr.1 lyr.2 lyr.3 lyr.4 lyr.5 lyr.6 lyr.7 lyr.8 lyr.9 lyr.10 [1,] 1 2 3 3 3 3 3 3 3 3 [2,] 2 4 6 6 6 6 6 6 6 6 [3,] 1 2 3 3 3 3 3 3 3 3 [4,] 4 8 12 12 12 12 12 12 12 12 > > values(roll(r, n=3, "sum", "around", circular=TRUE)) lyr.1 lyr.2 lyr.3 lyr.4 lyr.5 lyr.6 lyr.7 lyr.8 lyr.9 lyr.10 [1,] 3 3 3 3 3 3 3 3 3 3 [2,] 6 6 6 6 6 6 6 6 6 6 [3,] 3 3 3 3 3 3 3 3 3 3 [4,] 12 12 12 12 12 12 12 12 12 12 > > > > cleanEx() > nameEx("rotate") > ### * rotate > > flush(stderr()); flush(stdout()) > > ### Name: rotate > ### Title: Rotate data along longitude > ### Aliases: rotate rotate,SpatRaster-method rotate,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > x <- rast(nrows=9, ncols=18, nl=3, xmin=0, xmax=360) > v <- rep(as.vector(t(matrix(1:ncell(x), nrow=9, ncol=18))), 3) > values(x) <- v > z <- rotate(x) > > ## Not run: > ##D #SpatVector > ##D p <- rbind(c(3847903, 1983584 ), c(3847903, 5801864), c(8301883, 5801864), c(8301883, 1983584 )) > ##D p <- vect(p, "polygons", crs="+init=EPSG:3347") > ##D d <- densify(p, 100000) > ##D g <- project(d, "+proj=longlat") > ##D > ##D x <- rotate(g, 50) > ##D plot(g) > ##D lines(x, col="red") > ## End(Not run) > > ## rotate countries to 0-360 longitude > #w <- geodata::world(path=".") > #x <- rotate(w, long=0, split=TRUE, left=FALSE) > > > > > cleanEx() > nameEx("rowSums") > ### * rowSums > > flush(stderr()); flush(stdout()) > > ### Name: rowSums > ### Title: row/col sums and means for SpatRaster > ### Aliases: rowSums rowSums,SpatRaster-method colSums > ### colSums,SpatRaster-method rowMeans rowMeans,SpatRaster-method > ### colMeans colMeans,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=2, nrows=5, nl=2, vals=1:20) > rowSums(r) lyr.1 lyr.2 [1,] 3 23 [2,] 7 27 [3,] 11 31 [4,] 15 35 [5,] 19 39 > colSums(r) lyr.1 lyr.2 [1,] 25 75 [2,] 30 80 > colMeans(r) lyr.1 lyr.2 [1,] 5 15 [2,] 6 16 > > > > cleanEx() > nameEx("same.crs") > ### * same.crs > > flush(stderr()); flush(stdout()) > > ### Name: same.crs > ### Title: Compare coordinate reference systems > ### Aliases: same.crs > ### Keywords: spatial > > ### ** Examples > > r <- rast() > same.crs(r, "+proj=longlat") [1] TRUE > > same.crs(r, "+proj=utm +zone=1") [1] FALSE > > > > cleanEx() > nameEx("sample") > ### * sample > > flush(stderr()); flush(stdout()) > > ### Name: spatSample > ### Title: Take a regular sample > ### Aliases: spatSample spatSample,SpatRaster-method > ### spatSample,SpatExtent-method spatSample,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > s <- spatSample(r, 10, as.raster=TRUE) > spatSample(r, 5) elevation 1 NA 2 NA 3 297 4 NA 5 NA > spatSample(r, 5, na.rm=TRUE) elevation 1 302 2 325 3 414 4 293 5 384 > spatSample(r, 5, "regular") elevation 1 479 2 NaN 3 NaN 4 419 5 290 6 306 7 281 8 286 9 NaN > > ## if you require cell numbers and/or coordinates > size <- 6 > spatSample(r, 6, "random", cells=TRUE, xy=TRUE, values=FALSE) cell x y [1,] 1229 6.479167 50.08750 [2,] 5273 6.137500 49.72917 [3,] 1989 6.479167 50.02083 [4,] 308 5.929167 50.16250 [5,] 4929 6.437500 49.76250 [6,] 2499 5.979167 49.97083 > > # regular, with values > spatSample(r, 6, "regular", cells=TRUE, xy=TRUE) cell x y elevation 1 7458 6.137500 49.53750 264 2 7505 6.529167 49.53750 NA 3 7411 5.745833 49.53750 NA 4 5368 6.137500 49.72083 289 5 5415 6.529167 49.72083 NA 6 5321 5.745833 49.72083 NA 7 3183 6.137500 49.91250 322 8 1093 6.137500 50.09583 NA > > # stratified > rr <- rast(ncol=10, nrow=10, names="stratum") > set.seed(1) > values(rr) <- round(runif(ncell(rr), 1, 3)) > spatSample(rr, 2, "stratified", xy=TRUE) x y stratum [1,] -162 -81 1 [2,] -54 45 1 [3,] -126 -27 2 [4,] 90 -81 2 [5,] -162 9 3 [6,] 54 27 3 > > s <- spatSample(rr, 5, "stratified", as.points=TRUE, each=FALSE) > plot(rr, plg=list(title="raster")) > plot(s, 1, add=TRUE, plg=list(x=185, y=1, title="points"), col=rainbow(5)) > > # spread > s <- spatSample(r, 10, "spread", as.points=TRUE) > plot(r); points(s) > > ## SpatExtent > e <- ext(r) > spatSample(e, 10, "random", lonlat=TRUE) x y [1,] 6.060745 49.45733 [2,] 6.389876 49.58025 [3,] 6.345150 50.02372 [4,] 5.901934 50.08592 [5,] 6.307978 49.52574 [6,] 6.234556 49.74633 [7,] 6.037959 50.11587 [8,] 5.892183 49.49884 [9,] 5.856140 49.48130 [10,] 6.134349 49.84738 > > ## SpatVector > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > # sample the geometries > i <- sample(v, 3) > > # sample points in geometries > p <- spatSample(v, 3) > > > > > cleanEx() > nameEx("sapp") > ### * sapp > > flush(stderr()); flush(stdout()) > > ### Name: sapp > ### Title: Apply a terra function that takes only a single layer and > ### returns a SpatRaster to all layers of a SpatRaster > ### Aliases: sapp sapp,SpatRaster-method sapp,SpatRasterDataset-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) + 1 > > #SpatRasterDataset > sd <- sds(s*2, s/2) > y <- sapp(sd, mean) > z <- sapp(sd, function(i) 2 * mean(i)) > > > > > cleanEx() > nameEx("sbar") > ### * sbar > > flush(stderr()); flush(stdout()) > > ### Name: sbar > ### Title: scale bar > ### Aliases: sbar > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/meuse.tif", package="terra") > r <- rast(f) > plot(r) > sbar() > sbar(1000, xy=c(178500, 333500), type="bar", divs=4, cex=.8) > sbar(1000, xy="bottomright", divs=3, cex=.8, ticks=TRUE) > > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > plot(r, type="interval") > sbar(20, c(6.2, 50.1), type="bar", cex=.8, divs=4) > sbar(15, c(6.3, 50), type="bar", below="km", label=c(0,7.5,15), cex=.8) > sbar(15, c(6.6, 49.8), cex=.8, label=c(0,"km",15)) > sbar(15, c(6.6, 49.7), cex=.8, label="15 kilometer", lwd=5) > sbar(15, c(6.6, 49.6), divs=4, cex=.8, below="km") > > > > cleanEx() > nameEx("scale") > ### * scale > > flush(stderr()); flush(stdout()) > > ### Name: scale > ### Title: Scale values > ### Aliases: scale scale,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > s <- scale(r) > > ## the equivalent, computed in steps > m <- global(r, "mean") > rr <- r - m[,1] > rms <- global(rr, "rms") > ss <- rr / rms[,1] > > > > > cleanEx() > nameEx("scale_linear") > ### * scale_linear > > flush(stderr()); flush(stdout()) > > ### Name: scale_linear > ### Title: Scale values linearly > ### Aliases: scale_linear scale_linear,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(system.file("ex/logo.tif", package="terra")) > s1 <- scale_linear(r) > s2 <- scale_linear(r, 1, 10) > > > > cleanEx() > nameEx("scatter") > ### * scatter > > flush(stderr()); flush(stdout()) > > ### Name: scatterplot > ### Title: Scatterplot of two SpatRaster layers > ### Aliases: plot,SpatRaster,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > plot(s[[1]], s[[2]]) > plot(s, sqrt(s[[3:1]])) > > > > cleanEx() > nameEx("scoff") > ### * scoff > > flush(stderr()); flush(stdout()) > > ### Name: scoff > ### Title: Scale (gain) and offset > ### Aliases: scoff scoff<- scoff,SpatRaster-method > ### scoff<-,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(system.file("ex/elev.tif", package="terra")) > minmax(r) elevation min 141 max 547 > scoff(r) scale offset [1,] 1 0 > r[4603] elevation 1 279 > > scoff(r) <- cbind(10, 5) > minmax(r) elevation min 1415 max 5475 > scoff(r) scale offset [1,] 10 5 > r[4603] elevation 1 2795 > > > > cleanEx() > nameEx("sds") > ### * sds > > flush(stderr()); flush(stdout()) > > ### Name: sds > ### Title: Create a SpatRasterDataset > ### Aliases: sds sds,missing-method sds,character-method sds,list-method > ### sds,array-method sds,SpatRaster-method sds,stars-method > ### sds,stars_proxy-method [<-,SpatRasterDataset,numeric,missing-method > ### Keywords: methods spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > x <- sds(s, s/2) > names(x) <- c("first", "second") > x class : SpatRasterDataset subdatasets : 2 dimensions : 77, 101 (nrow, ncol) nlyr : 3, 3 resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : logo.tif, memory names : first, second > length(x) [1] 2 > > # extract the second SpatRaster > x[2] class : SpatRaster size : 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(s) : memory varname : logo names : red, green, blue min values : 0.0, 0.0, 0.0 max values : 127.5, 127.5, 127.5 > > a <- array(1:9, c(3,3,3,3)) > sds(a) class : SpatRasterDataset subdatasets : 3 dimensions : 3, 3 (nrow, ncol) nlyr : 3, 3, 3 resolution : 1, 1 (x, y) extent : 0, 3, 0, 3 (xmin, xmax, ymin, ymax) coord. ref. : source(s) : memory > > > > cleanEx() > nameEx("segregate") > ### * segregate > > flush(stderr()); flush(stdout()) > > ### Name: segregate > ### Title: segregate > ### Aliases: segregate segregate,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=5, ncols=5) > values(r) <- rep(c(1:4, NA), each=5) > b <- segregate(r) > bb <- segregate(r, keep=TRUE, other=NA) > > > > cleanEx() > nameEx("select") > ### * select > > flush(stderr()); flush(stdout()) > > ### Name: sel > ### Title: Spatial selection > ### Aliases: sel sel,SpatRaster-method sel,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > ## Not run: > ##D # select a subset of a SpatRaster > ##D r <- rast(nrows=10, ncols=10) > ##D values(r) <- 1:ncell(r) > ##D plot(r) > ##D s <- sel(r) # now click on the map twice > ##D > ##D # plot the selection on a new canvas: > ##D x11() > ##D plot(s) > ##D > ##D # vector > ##D f <- system.file("ex/lux.shp", package="terra") > ##D v <- vect(f) > ##D plot(v) > ##D x <- sel(v) # now click on the map twice > ##D x > ## End(Not run) > > > > cleanEx() > nameEx("selectHigh") > ### * selectHigh > > flush(stderr()); flush(stdout()) > > ### Name: selectHighest > ### Title: select cells with high or low values > ### Aliases: selectHighest selectHighest,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- selectHighest(r, 1000) > y <- selectHighest(r, 1000, TRUE) > > m <- merge(y-1, x) > levels(m) <- data.frame(id=0:1, elevation=c("low", "high")) > plot(m) > > > > cleanEx() > nameEx("selectRange") > ### * selectRange > > flush(stderr()); flush(stdout()) > > ### Name: selectRange > ### Title: Select the values of a range of layers, as specified by cell > ### values in another SpatRaster > ### Aliases: selectRange selectRange,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1 > s <- c(r, r+2, r+5) > s <- c(s, s) > set.seed(1) > values(r) <- sample(3, ncell(r), replace=TRUE) > x <- selectRange(s, r) > > x <- selectRange(s, r, 3) > > > > cleanEx() > nameEx("serialize") > ### * serialize > > flush(stderr()); flush(stdout()) > > ### Name: serialize > ### Title: saveRDS and serialize for SpatVector and SpatRaster* > ### Aliases: serialize saveRDS unserialize readRDS > ### serialize,SpatExtent-method serialize,SpatVector-method > ### serialize,SpatRaster-method serialize,SpatRasterDataset-method > ### serialize,SpatRasterCollection-method unserialize,ANY-method > ### saveRDS,SpatExtent-method saveRDS,SpatVector-method > ### saveRDS,SpatRaster-method saveRDS,SpatRasterCollection-method > ### saveRDS,SpatRasterDataset-method readRDS,character-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > p <- serialize(v, NULL) > head(p) [1] 58 0a 00 00 00 03 > x <- unserialize(p) > x class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > > > cleanEx() > nameEx("setValues") > ### * setValues > > flush(stderr()); flush(stdout()) > > ### Name: setValues > ### Title: Set the values of raster cells or of geometry attributes > ### Aliases: values<- values<-,SpatRaster,ANY-method setValues > ### setValues,SpatRaster-method setValues,SpatRaster,ANY-method > ### values<-,SpatVector,data.frame-method > ### values<-,SpatVector,matrix-method values<-,SpatVector,ANY-method > ### values<-,SpatVector,NULL-method setValues,SpatVector-method > ### setValues,SpatVector,ANY-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- setValues(r, 1:ncell(r)) > x class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory varname : elev name : elevation min value : 1 max value : 8550 > values(x) <- runif(ncell(x)) > x class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory varname : elev name : elevation min value : 0.0001064336 max value : 0.9999305937 > head(x) elevation 1 0.2655087 2 0.3721239 3 0.5728534 4 0.9082078 5 0.2016819 6 0.8983897 > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > values(v) <- data.frame(ID=1:12, name=letters[1:12]) > head(v) ID name 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 f > > > > cleanEx() > nameEx("shade") > ### * shade > > flush(stderr()); flush(stdout()) > > ### Name: shade > ### Title: Hill shading > ### Aliases: shade > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > # disaggregating because the resolution of this raster is a bit low > # you generally should not do that with your own data > r <- disagg(r, 10, method="bilinear") > > slope <- terrain(r, "slope", unit="radians") > aspect <- terrain(r, "aspect", unit="radians") > hill <- shade(slope, aspect, 40, 270) > plot(hill, col=grey(0:100/100), legend=FALSE, mar=c(2,2,1,4)) > plot(r, col=rainbow(25, alpha=0.35), add=TRUE) > > # A better hill shade may be achieved by combining > # different angles and directions. For example > hh <- shade(slope, aspect, angle=30, direction=c(225, 270, 315, 360)) > h1 <- Reduce(mean, hh) > h2 <- mean(hh) > > > > cleanEx() > nameEx("sharedPaths") > ### * sharedPaths > > flush(stderr()); flush(stdout()) > > ### Name: sharedPaths > ### Title: Shared paths > ### Aliases: sharedPaths sharedPaths,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > plot(v, col="light gray") > text(v, halo=TRUE) > > x <- sharedPaths(v) > lines(x, col="red", lwd=2) > text(x, col="blue", halo=TRUE, cex=0.8) > head(x) id1 id2 1 1 2 2 1 4 3 1 5 4 2 3 5 2 4 6 2 5 > > z <- sharedPaths(v[3,], v[12,]) > > > > > cleanEx() > nameEx("shift") > ### * shift > > flush(stderr()); flush(stdout()) > > ### Name: shift > ### Title: Shift > ### Aliases: shift shift,SpatRaster-method shift,SpatExtent-method > ### shift,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(xmin=0, xmax=1, ymin=0, ymax=1) > r <- shift(r, dx=1, dy=-1) > > e <- ext(r) > shift(e, 5, 5) SpatExtent : 6, 7, 4, 5 (xmin, xmax, ymin, ymax) > > > > cleanEx() > nameEx("sieve") > ### * sieve > > flush(stderr()); flush(stdout()) > > ### Name: sieve > ### Title: Sieve filter > ### Aliases: sieve sieve,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=18, ncols=18, xmin=0, vals=0, crs="local") > r[2, 5] <- 1 > r[5:8, 2:3] <- 2 > r[7:12, 10:15] <- 3 > r[15:16, 15:18] <- 4 > freq(r, bylayer=FALSE) value count 1 0 271 2 1 1 3 2 8 4 3 36 5 4 8 > > x <- sieve(r, 8) > y <- sieve(r, 9) > > > > cleanEx() > nameEx("simplify") > ### * simplify > > flush(stderr()); flush(stdout()) > > ### Name: simplifyGeom > ### Title: simplifyGeom geometries > ### Aliases: simplifyGeom simplifyGeom,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > w <- simplifyGeom(v, .02, makeValid=FALSE) > e <- erase(w) > g <- gaps(e) > plot(e, lwd=5, border="light gray") > polys(g, col="red", border="red") > > > > cleanEx() > nameEx("sort") > ### * sort > > flush(stderr()); flush(stdout()) > > ### Name: sort > ### Title: Sort a SpatRaster or SpatVector > ### Aliases: sort sort,SpatRaster-method sort,SpatVector-method > ### sort,data.frame-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > r <- c(r, r/2, r*2) > sort(r) class : SpatRaster size : 90, 95, 3 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory varname : elev names : elevation, elevation, elevation min values : 70.5, 141, 282 max values : 273.5, 547, 1094 > > ord <- sort(r, order=TRUE) > # these two are the same > ord[[1]] class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory varname : elev name : elevation min value : 0 max value : 1 > which.min(r) class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory name : which.min min value : 2 max value : 2 > > > > cleanEx() > nameEx("sources") > ### * sources > > flush(stderr()); flush(stdout()) > > ### Name: sources > ### Title: Data sources of a SpatRaster > ### Aliases: sources sources,SpatRaster-method > ### sources,SpatRasterCollection-method sources,SpatRasterDataset-method > ### sources,SpatVector-method sources,SpatVectorProxy-method hasValues > ### hasValues,SpatRaster-method inMemory inMemory,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > s <- rast(r) > values(s) <- 1:ncell(s) > rs <- c(r,r,s,r) > sources(rs) [1] "/data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/elev.tif" [2] "/data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/elev.tif" [3] "" [4] "/data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/elev.tif" > hasValues(r) [1] TRUE > x <- rast() > hasValues(x) [1] FALSE > > > > cleanEx() > nameEx("spin") > ### * spin > > flush(stderr()); flush(stdout()) > > ### Name: spin > ### Title: spin a SpatVector > ### Aliases: spin spin,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > w <- spin(v, 180) > plot(v) > lines(w, col="red") > > # lower-right corner as center > e <- as.vector(ext(v)) > x <- spin(v, 45, e[1], e[3]) > > > > cleanEx() > nameEx("split") > ### * split > > flush(stderr()); flush(stdout()) > > ### Name: split > ### Title: Split a SpatRaster or SpatVector > ### Aliases: split split,SpatRaster,ANY-method split,SpatVector,ANY-method > ### split,SpatVector,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > > ## split layers > s <- rast(system.file("ex/logo.tif", package="terra")) > y <- split(s, c(1,2,1)) > sds(y) class : SpatRasterDataset subdatasets : 2 dimensions : 77, 101 (nrow, ncol) nlyr : 2, 1 resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source(s) : logo.tif > > ## split attributes > v <- vect(system.file("ex/lux.shp", package="terra")) > x <- split(v, "NAME_1") > > ## split geometries > v <- v[1:5,] > line <- vect(matrix(c(5.79, 6.22, 5.75, 6.1, 5.8, + 50.14, 50.05, 49.88, 49.85, 49.71), ncol=2), "line") > s <- split(v, line) > > > > cleanEx() > nameEx("sprc") > ### * sprc > > flush(stderr()); flush(stdout()) > > ### Name: sprc > ### Title: Create a SpatRasterCollection > ### Aliases: sprc sprc,missing-method sprc,list-method > ### sprc,SpatRaster-method sprc,character-method > ### Keywords: methods spatial > > ### ** Examples > > x <- rast(xmin=-110, xmax=-50, ymin=40, ymax=70, ncols=60, nrows=30) > y <- rast(xmin=-80, xmax=-20, ymax=60, ymin=30) > res(y) <- res(x) > values(x) <- 1:ncell(x) > values(y) <- 1:ncell(y) > > z <- sprc(x, y) > z class : SpatRasterCollection length : 2 nrow : 30, 30 ncol : 60, 60 nlyr : 1, 1 extent : -110, -20, 30, 70 (xmin, xmax, ymin, ymax) crs (first) : lon/lat WGS 84 (CRS84) (OGC:CRS84) > > > > cleanEx() > nameEx("stretch") > ### * stretch > > flush(stderr()); flush(stdout()) > > ### Name: stretch > ### Title: Stretch > ### Aliases: stretch stretch,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nc=10, nr=10) > values(r) <- rep(1:25, 4) > rs <- stretch(r) > s <- c(r, r*2) > sr <- stretch(s) > > > > cleanEx() > nameEx("subset") > ### * subset > > flush(stderr()); flush(stdout()) > > ### Name: subset > ### Title: Subset a SpatRaster or a SpatVector > ### Aliases: subset subset,SpatRaster-method subset,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > ### SpatRaster > s <- rast(system.file("ex/logo.tif", package="terra")) > subset(s, 2:3) class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : green, blue min values : 0, 0 max values : 255, 255 > subset(s, c(3,2,3,1)) class : SpatRaster size : 77, 101, 4 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : blue, green, blue, red min values : 0, 0, 0, 0 max values : 255, 255, 255, 255 > > #equivalent to > s[[ c(3,2,3,1) ]] class : SpatRaster size : 77, 101, 4 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : blue, green, blue, red min values : 0, 0, 0, 0 max values : 255, 255, 255, 255 > > s[[c("red", "green")]] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > s$red class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif name : red min value : 0 max value : 255 > > # expression based (partial) matching of names with single brackets > s["re"] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > s["^re"] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif name : red min value : 0 max value : 255 > > # not with double brackets > # s[["re"]] > > > ### SpatVector > > v <- vect(system.file("ex/lux.shp", package="terra")) > > subset(v, v$NAME_1 == "Diekirch", c("NAME_1", "NAME_2")) class : SpatVector geometry : polygons dimensions : 5, 2 (geometries, attributes) extent : 5.74414, 6.315773, 49.69933, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 type : values : Diekirch Clervaux Diekirch Diekirch Diekirch Redange > > subset(v, NAME_1 == "Diekirch", c(NAME_1, NAME_2), NSE=TRUE) class : SpatVector geometry : polygons dimensions : 5, 2 (geometries, attributes) extent : 5.74414, 6.315773, 49.69933, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 type : values : Diekirch Clervaux Diekirch Diekirch Diekirch Redange > > # or like this > v[2:3,] class : SpatVector geometry : polygons dimensions : 2, 6 (geometries, attributes) extent : 5.746118, 6.315773, 49.69933, 49.98745 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > v[1:2, 2:3] class : SpatVector geometry : polygons dimensions : 2, 2 (geometries, attributes) extent : 5.826232, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 ID_2 type : values : Diekirch 1 Diekirch 2 > v[1:2, c("NAME_1", "NAME_2")] class : SpatVector geometry : polygons dimensions : 2, 2 (geometries, attributes) extent : 5.826232, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 type : values : Diekirch Clervaux Diekirch Diekirch > > # or by location, i.e. by spatial overlap with another object > poly <- as.polygons(ext(6, 6.4, 49.75, 50)) > subset(v, poly) class : SpatVector geometry : polygons dimensions : 8, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.58699, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > v[poly] class : SpatVector geometry : polygons dimensions : 8, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.58699, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > > > cleanEx() > nameEx("subset_dollar") > ### * subset_dollar > > flush(stderr()); flush(stdout()) > > ### Name: subset_dollar > ### Title: Subset a SpatRaster or a SpatVector > ### Aliases: $ $,SpatRaster-method $,SpatRasterDataset-method > ### $,SpatVector-method $,SpatExtent-method $,SpatVectorCollection-method > ### Keywords: spatial > > ### ** Examples > > ### SpatRaster > s <- rast(system.file("ex/logo.tif", package="terra")) > subset(s, 2:3) class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : green, blue min values : 0, 0 max values : 255, 255 > subset(s, c(3,2,3,1)) class : SpatRaster size : 77, 101, 4 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : blue, green, blue, red min values : 0, 0, 0, 0 max values : 255, 255, 255, 255 > #equivalent to > s[[ c(3,2,3,1) ]] class : SpatRaster size : 77, 101, 4 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : blue, green, blue, red min values : 0, 0, 0, 0 max values : 255, 255, 255, 255 > > s[[c("red", "green")]] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > s$red class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif name : red min value : 0 max value : 255 > > # expression based (partial) matching of names with single brackets > s["re"] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > s["^re"] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif name : red min value : 0 max value : 255 > > # not with double brackets > # s[["re"]] > > > ### SpatVector > > v <- vect(system.file("ex/lux.shp", package="terra")) > v[2:3,] class : SpatVector geometry : polygons dimensions : 2, 6 (geometries, attributes) extent : 5.746118, 6.315773, 49.69933, 49.98745 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > v[1:2, 2:3] class : SpatVector geometry : polygons dimensions : 2, 2 (geometries, attributes) extent : 5.826232, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 ID_2 type : values : Diekirch 1 Diekirch 2 > > subset(v, v$NAME_1 == "Diekirch", c("NAME_1", "NAME_2")) class : SpatVector geometry : polygons dimensions : 5, 2 (geometries, attributes) extent : 5.74414, 6.315773, 49.69933, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 type : values : Diekirch Clervaux Diekirch Diekirch Diekirch Redange > > subset(v, NAME_1 == "Diekirch", c(NAME_1, NAME_2), NSE=TRUE) class : SpatVector geometry : polygons dimensions : 5, 2 (geometries, attributes) extent : 5.74414, 6.315773, 49.69933, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 NAME_2 type : values : Diekirch Clervaux Diekirch Diekirch Diekirch Redange > > > > cleanEx() > nameEx("subset_double") > ### * subset_double > > flush(stderr()); flush(stdout()) > > ### Name: subset_double > ### Title: Subset a SpatRaster or a SpatVector > ### Aliases: [[ [[,SpatRaster,numeric,missing-method > ### [[,SpatRaster,logical,missing-method > ### [[,SpatRaster,character,missing-method > ### [[,SpatRaster,ANY,missing-method [[,SpatRasterDataset,ANY,ANY-method > ### [[,SpatVector,numeric,missing-method > ### [[,SpatVector,logical,missing-method > ### [[,SpatVector,character,missing-method > ### [[,SpatVectorCollection,ANY,missing-method > ### Keywords: spatial > > ### ** Examples > > ### SpatRaster > s <- rast(system.file("ex/logo.tif", package="terra")) > s[[ 1:2 ]] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > > s[[c("red", "green")]] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > > # expression based (partial) matching of names with single brackets > s["re"] class : SpatRaster size : 77, 101, 2 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif names : red, green min values : 0, 0 max values : 255, 255 > s["^re"] class : SpatRaster size : 77, 101, 1 (nrow, ncol, nlyr) resolution : 1, 1 (x, y) extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax) coord. ref. : Cartesian (Meter) source : logo.tif name : red min value : 0 max value : 255 > > # does not with double brackets > # s[["re"]] > > > ### SpatVector > > v <- vect(system.file("ex/lux.shp", package="terra")) > v[[2:3]] NAME_1 ID_2 1 Diekirch 1 2 Diekirch 2 3 Diekirch 3 4 Diekirch 4 5 Diekirch 5 6 Grevenmacher 6 7 Grevenmacher 7 8 Grevenmacher 12 9 Luxembourg 8 10 Luxembourg 9 11 Luxembourg 10 12 Luxembourg 11 > > # to keep the geometry use > v[,2:3] class : SpatVector geometry : polygons dimensions : 12, 2 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 ID_2 type : values : Diekirch 1 Diekirch 2 Diekirch 3 > > > > cleanEx() > nameEx("subset_single") > ### * subset_single > > flush(stderr()); flush(stdout()) > > ### Name: subset_single > ### Title: Extract values from a SpatRaster, SpatVector or SpatExtent > ### Aliases: [ [,SpatExtent,missing,missing-method > ### [,SpatExtent,numeric,missing-method [,SpatRaster,ANY,ANY,ANY-method > ### [,SpatRaster,ANY,ANY-method > ### [,SpatRasterDataset,logical,missing-method > ### [,SpatRasterDataset,character,missing-method > ### [,SpatRasterDataset,numeric,missing-method > ### [,SpatRasterDataset,numeric,numeric-method > ### [,SpatRasterDataset,numeric,logical-method > ### [,SpatRasterDataset,missing,numeric-method > ### [,SpatRasterDataset,missing,logical-method > ### [,SpatRasterCollection,numeric,missing-method > ### [,SpatVector,missing,missing-method > ### [,SpatVector,missing,numeric-method > ### [,SpatVector,missing,character-method > ### [,SpatVector,missing,logical-method > ### [,SpatVector,character,missing-method > ### [,SpatVector,numeric,missing-method > ### [,SpatVector,numeric,numeric-method > ### [,SpatVector,numeric,character-method > ### [,SpatVector,numeric,logical-method > ### [,SpatVector,logical,missing-method > ### [,SpatVector,logical,character-method > ### [,SpatVector,logical,numeric-method > ### [,SpatVector,logical,logical-method > ### [,SpatVector,data.frame,missing-method > ### [,SpatVector,data.frame,ANY-method [,SpatVector,matrix,missing-method > ### [,SpatVector,SpatVector,missing-method > ### [,SpatVector,SpatExtent,missing-method > ### [,SpatRaster,SpatVector,missing-method > ### [,SpatRaster,missing,missing-method > ### [,SpatRaster,numeric,missing-method > ### [,SpatRaster,missing,numeric-method > ### [,SpatRaster,numeric,numeric-method > ### [,SpatRaster,data.frame,missing-method > ### [,SpatRaster,matrix,missing-method > ### [,SpatRaster,SpatRaster,missing-method > ### [,SpatRaster,SpatExtent,missing-method > ### Keywords: spatial > > ### ** Examples > > ### SpatRaster > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > r[3638] elevation 1 473 > rowColFromCell(r, 2638) [,1] [,2] [1,] 28 73 > r[39, 28] elevation 1 473 > x <- r[39:40, 28:29, drop=FALSE] > as.matrix(x, wide=TRUE) [,1] [,2] [1,] 473 429 [2,] 430 436 > > ### SpatVector > > v <- vect(system.file("ex/lux.shp", package="terra")) > v[2:3,] class : SpatVector geometry : polygons dimensions : 2, 6 (geometries, attributes) extent : 5.746118, 6.315773, 49.69933, 49.98745 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > v[1:2, 2:3] class : SpatVector geometry : polygons dimensions : 2, 2 (geometries, attributes) extent : 5.826232, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : NAME_1 ID_2 type : values : Diekirch 1 Diekirch 2 > v[1:2, 2:3, drop=TRUE] NAME_1 ID_2 1 Diekirch 1 2 Diekirch 2 > > > > cleanEx() > nameEx("subst") > ### * subst > > flush(stderr()); flush(stdout()) > > ### Name: subst > ### Title: replace cell values > ### Aliases: subst subst,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5, xmin=0, xmax=1, ymin=0, ymax=1, crs="") > r <- init(r, 1:6) > x <- subst(r, 3, 7) > x <- subst(r, 2:3, NA) ==1064531== Invalid read of size 8 ==1064531== at 0x18B9F116: SpatRaster::lookup_apply(std::vector >, std::vector >, bool, double, SpatOptions&) (packages/tests-vg/terra/src/raster_methods.cpp:79) ==1064531== by 0x18B9F567: SpatRaster::lookup_classify(std::vector >, std::vector >, bool, double, SpatOptions&) (packages/tests-vg/terra/src/raster_methods.cpp:141) ==1064531== by 0x18B9F648: SpatRaster::lookup_subst(std::vector >, std::vector >, bool, double, SpatOptions&) (packages/tests-vg/terra/src/raster_methods.cpp:145) ==1064531== by 0x18A8B03C: operator() (R-devel/site-library/Rcpp/include/Rcpp/Module.h:385) ==1064531== by 0x18A8B03C: SEXPREC* Rcpp::internal::call_impl >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::{lambda(std::vector >, std::vector >, bool, double, SpatOptions&)#1}, SpatRaster, std::vector >, std::vector >, bool, double, SpatOptions&, 0, 1, 2, 3, 4, (void*)0>(Rcpp::CppMethodImplN >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::{lambda(std::vector >, std::vector >, bool, double, SpatOptions&)#1} const&, SEXPREC**, Rcpp::internal::type_pack >, std::vector >, bool, double, SpatOptions&>, Rcpp::traits::index_sequence<0, 1, 2, 3, 4>) [clone .isra.0] (R-devel/site-library/Rcpp/include/Rcpp/internal/call.h:23) ==1064531== by 0x18A8B174: call >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::, std::vector, bool, double, SpatOptions&)>, SpatRaster, std::vector >, std::vector >, bool, double, SpatOptions&> (R-devel/site-library/Rcpp/include/Rcpp/internal/call.h:47) ==1064531== by 0x18A8B174: Rcpp::CppMethodImplN >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**) (R-devel/site-library/Rcpp/include/Rcpp/Module.h:387) ==1064531== by 0x189F67F9: Rcpp::class_::invoke_notvoid(SEXPREC*, SEXPREC*, SEXPREC**, int) (R-devel/site-library/Rcpp/include/Rcpp/module/class.h:241) ==1064531== by 0x177EA00F: CppMethod__invoke_notvoid(SEXPREC*) (/tmp/Rtmp8qmSGX/R.INSTALLa4b8c26e7caba/Rcpp/src/module.cpp:220) ==1064531== by 0x4A62ED: do_External (svn/R-devel/src/main/dotcode.c:576) ==1064531== by 0x4F2997: Rf_eval (svn/R-devel/src/main/eval.c:1260) ==1064531== by 0x4F5C44: do_begin (svn/R-devel/src/main/eval.c:2992) ==1064531== by 0x4F27B2: Rf_eval (svn/R-devel/src/main/eval.c:1232) ==1064531== by 0x4F41CD: R_execClosure (svn/R-devel/src/main/eval.c:2389) ==1064531== Address 0x17728de8 is 0 bytes after a block of size 8 alloc'd ==1064531== at 0x4843FEC: operator new(unsigned long) (/builddir/build/BUILD/valgrind-3.24.0/coregrind/m_replacemalloc/vg_replace_malloc.c:487) ==1064531== by 0x18B9F477: allocate (/usr/include/c++/14/bits/new_allocator.h:151) ==1064531== by 0x18B9F477: allocate (/usr/include/c++/14/bits/alloc_traits.h:478) ==1064531== by 0x18B9F477: _M_allocate (/usr/include/c++/14/bits/stl_vector.h:380) ==1064531== by 0x18B9F477: _M_create_storage (/usr/include/c++/14/bits/stl_vector.h:398) ==1064531== by 0x18B9F477: _Vector_base (/usr/include/c++/14/bits/stl_vector.h:334) ==1064531== by 0x18B9F477: vector (/usr/include/c++/14/bits/stl_vector.h:603) ==1064531== by 0x18B9F477: SpatRaster::lookup_classify(std::vector >, std::vector >, bool, double, SpatOptions&) (packages/tests-vg/terra/src/raster_methods.cpp:141) ==1064531== by 0x18B9F648: SpatRaster::lookup_subst(std::vector >, std::vector >, bool, double, SpatOptions&) (packages/tests-vg/terra/src/raster_methods.cpp:145) ==1064531== by 0x18A8B03C: operator() (R-devel/site-library/Rcpp/include/Rcpp/Module.h:385) ==1064531== by 0x18A8B03C: SEXPREC* Rcpp::internal::call_impl >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::{lambda(std::vector >, std::vector >, bool, double, SpatOptions&)#1}, SpatRaster, std::vector >, std::vector >, bool, double, SpatOptions&, 0, 1, 2, 3, 4, (void*)0>(Rcpp::CppMethodImplN >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::{lambda(std::vector >, std::vector >, bool, double, SpatOptions&)#1} const&, SEXPREC**, Rcpp::internal::type_pack >, std::vector >, bool, double, SpatOptions&>, Rcpp::traits::index_sequence<0, 1, 2, 3, 4>) [clone .isra.0] (R-devel/site-library/Rcpp/include/Rcpp/internal/call.h:23) ==1064531== by 0x18A8B174: call >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**)::, std::vector, bool, double, SpatOptions&)>, SpatRaster, std::vector >, std::vector >, bool, double, SpatOptions&> (R-devel/site-library/Rcpp/include/Rcpp/internal/call.h:47) ==1064531== by 0x18A8B174: Rcpp::CppMethodImplN >, std::vector >, bool, double, SpatOptions&>::operator()(SpatRaster*, SEXPREC**) (R-devel/site-library/Rcpp/include/Rcpp/Module.h:387) ==1064531== by 0x189F67F9: Rcpp::class_::invoke_notvoid(SEXPREC*, SEXPREC*, SEXPREC**, int) (R-devel/site-library/Rcpp/include/Rcpp/module/class.h:241) ==1064531== by 0x177EA00F: CppMethod__invoke_notvoid(SEXPREC*) (/tmp/Rtmp8qmSGX/R.INSTALLa4b8c26e7caba/Rcpp/src/module.cpp:220) ==1064531== by 0x4A62ED: do_External (svn/R-devel/src/main/dotcode.c:576) ==1064531== by 0x4F2997: Rf_eval (svn/R-devel/src/main/eval.c:1260) ==1064531== by 0x4F5C44: do_begin (svn/R-devel/src/main/eval.c:2992) ==1064531== by 0x4F27B2: Rf_eval (svn/R-devel/src/main/eval.c:1232) ==1064531== by 0x4F41CD: R_execClosure (svn/R-devel/src/main/eval.c:2389) ==1064531== > x <- subst(x, NA, 10) > > # multiple output layers > z <- subst(r, 2:3, cbind(20,30)) > > # multiple input layers > rr <- c(r, r+1, r+2) > m <- rbind(c(1:3), c(3:5)) > zz <- subst(rr, m, c(100, 200)) > > > > cleanEx() > nameEx("summarize-generics") > ### * summarize-generics > > flush(stderr()); flush(stdout()) > > ### Name: summarize > ### Title: Summarize > ### Aliases: Summary-methods median mean min max which.min which.max any > ### all nany prod range sum anyNA allNA noNA countNA > ### mean,SpatRaster-method mean,SpatVector-method mean,SpatExtent-method > ### median,SpatRaster-method median,SpatVector-method > ### min,SpatRaster-method max,SpatRaster-method range,SpatRaster-method > ### which.min,SpatRaster-method which.max,SpatRaster-method > ### any,SpatRaster-method all,SpatRaster-method nany,SpatRaster-method > ### nany,ANY-method prod,SpatRaster-method range,SpatRaster-method > ### sum,SpatRaster-method anyNA,SpatRaster-method allNA,SpatRaster-method > ### noNA,SpatRaster-method countNA,SpatRaster-method stdev > ### stdev,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10, nlyrs=3) > values(r) <- runif(ncell(r) * nlyr(r)) > > x <- mean(r) > # note how this returns one layer > x <- sum(c(r, r[[2]]), 5) > > # and this returns three layers > y <- sum(r, r[[2]], 5) > > max(r) class : SpatRaster size : 10, 10, 1 (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 name : max min value : 0.1808664 max value : 0.9926841 > > ## when adding a number, do you want 1 layer or all layers? > # 1 layer > max(r, 0.5) class : SpatRaster size : 10, 10, 1 (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 name : max min value : 0.5000000 max value : 0.9926841 > > # all layers > max(r, 0.5, par=TRUE) class : SpatRaster size : 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.2, lyr.3 min values : 0.5000000, 0.5000000, 0.5000000 max values : 0.9919061, 0.9926841, 0.9815635 > > y <- stdev(r) > # not the same as > yy <- app(r, sd) > > z <- stdev(r, r*2) > > x <- mean(r, filename=paste0(tempfile(), ".tif")) > > > v <- values(r) > set.seed(3) > v[sample(length(v), 50)] <- NA > values(r) <- v > is.na(r) class : SpatRaster size : 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.2, lyr.3 min values : FALSE, FALSE, FALSE max values : TRUE, TRUE, TRUE > anyNA(r) class : SpatRaster size : 10, 10, 1 (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 name : lyr1 min value : FALSE max value : TRUE > allNA(r) class : SpatRaster size : 10, 10, 1 (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 name : lyr1 min value : FALSE max value : TRUE > countNA(r) class : SpatRaster size : 10, 10, 1 (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 name : lyr1 min value : 0 max value : 3 > countNA(r, 2) class : SpatRaster size : 10, 10, 1 (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 name : lyr1 min value : FALSE max value : TRUE > > > > cleanEx() > nameEx("summary") > ### * summary > > flush(stderr()); flush(stdout()) > > ### Name: summary > ### Title: summary > ### Aliases: summary summary,SpatRaster-method summary,SpatVector-method > ### Summary,SpatExtent-method Summary,SpatRaster-method > ### Summary,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > set.seed(0) > r <- rast(nrows=10, ncols=10, nlyrs=3) > values(r) <- runif(nlyr(r)*ncell(r)) > summary(r) lyr.1 lyr.2 lyr.3 Min. :0.01339 Min. :0.01308 Min. :0.02779 1st Qu.:0.32308 1st Qu.:0.28440 1st Qu.:0.18947 Median :0.48781 Median :0.51860 Median :0.37810 Mean :0.52076 Mean :0.51569 Mean :0.43403 3rd Qu.:0.77171 3rd Qu.:0.72570 3rd Qu.:0.63596 Max. :0.99191 Max. :0.99268 Max. :0.98156 > > > > cleanEx() > nameEx("surfArea") > ### * surfArea > > flush(stderr()); flush(stdout()) > > ### Name: surfArea > ### Title: Compute surface area from elevation data > ### Aliases: surfArea surfArea,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > v <- rast(volcano, crs="local") > x <- terra::surfArea(v) > > > > cleanEx() > nameEx("svc") > ### * svc > > flush(stderr()); flush(stdout()) > > ### Name: svc > ### Title: Create a SpatVectorCollection > ### Aliases: svc svc,missing-method svc,character-method svc,list-method > ### svc,sf-method svc,SpatVector-method > ### [<-,SpatVectorCollection,numeric,missing-method > ### [,SpatVectorCollection,numeric,missing-method > ### [[,SpatVectorCollection,numeric,missing-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > x <- svc() > x <- svc(v, v[1:3,], as.lines(v[3:5,]), as.points(v)) > length(x) [1] 4 > x class : SpatVectorCollection length : 4 geometry : polygons (12) polygons (3) lines (3) points (3983) crs (first) : lon/lat WGS 84 (EPSG:4326) names : , , , > > # extract > x[3] class : SpatVector geometry : lines dimensions : 3, 6 (geometries, attributes) extent : 5.74414, 6.239243, 49.69933, 50.03632 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 3 Redange 259 1.866e+04 1 Diekirch 4 Vianden 76 5163 1 Diekirch 5 Wiltz 263 1.674e+04 > > # replace > x[2] <- as.lines(v[1,]) > > > > > > cleanEx() > nameEx("symdif") > ### * symdif > > flush(stderr()); flush(stdout()) > > ### Name: symdif > ### Title: Symmetrical difference > ### Aliases: symdif symdif,SpatVector,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > p <- vect(system.file("ex/lux.shp", package="terra")) > b <- as.polygons(ext(6, 6.4, 49.75, 50)) > #sd <- symdif(p, b) > #plot(sd, col=rainbow(12)) > > > > cleanEx() > nameEx("tapp") > ### * tapp > > flush(stderr()); flush(stdout()) > > ### Name: tapp > ### Title: Apply a function to subsets of layers of a SpatRaster > ### Aliases: tapp tapp,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > s <- c(r, r, r, r, r, r) > s <- s * 1:6 > b1 <- tapp(s, index=c(1,1,1,2,2,2), fun=sum) > b1 class : SpatRaster size : 10, 10, 2 (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 : X1, X2 min values : 6, 15 max values : 600, 1500 > b2 <- tapp(s, c(1,2,3,1,2,3), fun=sum) > b2 class : SpatRaster size : 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 : X1, X2, X3 min values : 5, 7, 9 max values : 500, 700, 900 > > > > cleanEx() > nameEx("terraOptions") > ### * terraOptions > > flush(stderr()); flush(stdout()) > > ### Name: options > ### Title: Options > ### Aliases: terraOptions > ### Keywords: classes spatial > > ### ** Examples > > terraOptions() memfrac : 0.5 tolerance : 0.1 verbose : FALSE todisk : FALSE tempdir : /tmp/RtmpvNGYQp datatype : FLT4S memmin : 1 progress : 3 > terraOptions(memfrac=0.5, tempdir = "c:/temp") Warning: [options] you cannot set the tempdir to a path that does not exist > terraOptions(progress=10) > terraOptions() memfrac : 0.5 tolerance : 0.1 verbose : FALSE todisk : FALSE tempdir : /tmp/RtmpvNGYQp datatype : FLT4S memmin : 1 progress : 10 > > > > cleanEx() > nameEx("terrain") > ### * terrain > > flush(stderr()); flush(stdout()) > > ### Name: terrain > ### Title: terrain characteristics > ### Aliases: terrain terrain,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- terrain(r, "slope") > > > > cleanEx() > nameEx("text") > ### * text > > flush(stderr()); flush(stdout()) > > ### Name: text > ### Title: Add labels to a map > ### Aliases: text text,SpatRaster-method text,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(nrows=4, ncols=4) > values(r) <- 1:ncell(r) > > plot(r) > text(r) > > set.seed(123) > text(r, jitter = 2, col = "red", halo = TRUE) > > plot(r) > text(r, halo=TRUE, hc="blue", col="white", hw=0.2) > > plot(r, col=rainbow(16)) > text(r, col=c("black", "white"), vfont=c("sans serif", "bold"), cex=2) > > > > cleanEx() > nameEx("thresh") > ### * thresh > > flush(stderr()); flush(stdout()) > > ### Name: thresh > ### Title: Thresholding > ### Aliases: thresh thresh,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > s <- rast(system.file("ex/logo.tif", package="terra")) > thresh(s, "mean", as.raster=FALSE) red green blue 182.2855 185.3509 192.8046 > thresh(s, "mean", combine=TRUE, as.raster=FALSE) [1] 186.8136 > > > plot(thresh(s, "otsu")) > > > > > cleanEx() > nameEx("time") > ### * time > > flush(stderr()); flush(stdout()) > > ### Name: time > ### Title: time of SpatRaster layers > ### Aliases: time time,SpatRaster-method time,SpatRasterDataset-method > ### has.time has.time,SpatRaster-method has.time,SpatRasterDataset-method > ### time<- time<-,SpatRaster-method time<-,SpatRasterDataset-method > ### timeInfo timeInfo,SpatRaster-method timeInfo,SpatRasterDataset-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > > # Date" > d <- as.Date("2001-05-04") + 0:2 > time(s) <- d > time(s) [1] "2001-05-04" "2001-05-05" "2001-05-06" > > # POSIX (date/time with a resolution of seconds) > time(s) <- as.POSIXlt(d) > time(s) [1] "2001-05-04 UTC" "2001-05-05 UTC" "2001-05-06 UTC" > > # with time zone > time(s) <- as.POSIXlt(Sys.time(), "America/New_York") + 0:2 > time(s) [1] "2026-03-08 19:24:17 EDT" "2026-03-08 19:24:18 EDT" [3] "2026-03-08 19:24:19 EDT" > timeInfo(s) time step zone 1 TRUE seconds America/New_York > > # years > time(s, tstep="years") <- 2000 + 0:2 > s class : SpatRaster size : 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 time (years): 2000 to 2002 (3 steps) > > time(s, tstep="months") <- 1:3 > s class : SpatRaster size : 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 time (mnts) : Jan to Mar (3 steps) > > > > cleanEx() > nameEx("tmpFile") > ### * tmpFile > > flush(stderr()); flush(stdout()) > > ### Name: tmpFiles > ### Title: Temporary files > ### Aliases: tmpFiles > ### Keywords: spatial > > ### ** Examples > > tmpFiles() [1] "/tmp/RtmpvNGYQp/spat_103e531245305d_1064531_DvggOkjkTOQeAAI.vrt" > > > > cleanEx() > nameEx("toMemory") > ### * toMemory > > flush(stderr()); flush(stdout()) > > ### Name: toMemory > ### Title: Read all cell values into memory > ### Aliases: toMemory toMemory,SpatRaster-method > ### toMemory,SpatRasterDataset-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > sources(r) [1] "/data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/elev.tif" > inMemory(r) [1] FALSE > x <- toMemory(r) > inMemory(x) [1] TRUE > > > > cleanEx() > nameEx("topology") > ### * topology > > flush(stderr()); flush(stdout()) > > ### Name: topology > ### Title: Vector topology methods > ### Aliases: topology makeNodes mergeLines removeDupNodes emptyGeoms snap > ### makeNodes,SpatVector-method mergeLines,SpatVector-method > ### snap,SpatVector-method removeDupNodes,SpatVector-method > ### emptyGeoms,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > p1 <- as.polygons(ext(0,1,0,1)) > p2 <- as.polygons(ext(1.1,2,0,1)) > > p <- rbind(p1, p2) > > y <- snap(p, tol=.15) > plot(p, lwd=3, col="light gray") > lines(y, col="red", lwd=2) > > > > cleanEx() > nameEx("transpose") > ### * transpose > > flush(stderr()); flush(stdout()) > > ### Name: transpose > ### Title: Transpose > ### Aliases: t t,SpatRaster-method t,SpatVector-method trans > ### trans,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(nrows=18, ncols=36) > values(r) <- 1:ncell(r) > tr1 <- t(r) > tr2 <- trans(r) > ttr <- trans(tr2) > > > > cleanEx() > nameEx("trim") > ### * trim > > flush(stderr()); flush(stdout()) > > ### Name: trim > ### Title: Trim a SpatRaster > ### Aliases: trim trim,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10, xmin=0,xmax=10,ymin=0,ymax=10) > v <- rep(NA, ncell(r)) > v[c(12,34,69)] <- 1:3 > values(r) <- v > s <- trim(r) > > > > cleanEx() > nameEx("union") > ### * union > > flush(stderr()); flush(stdout()) > > ### Name: union > ### Title: Union SpatVector or SpatExtent objects > ### Aliases: union union,SpatExtent,SpatExtent-method > ### union,SpatVector,SpatVector-method union,SpatVector,SpatExtent-method > ### union,SpatVector,missing-method > ### Keywords: methods spatial > > ### ** Examples > > e1 <- ext(-10, 10, -20, 20) > e2 <- ext(0, 20, -40, 5) > union(e1, e2) SpatExtent : -10, 20, -40, 20 (xmin, xmax, ymin, ymax) > > #SpatVector > v <- vect(system.file("ex/lux.shp", package="terra")) > v <- v[,3:4] > p <- vect(c("POLYGON ((5.8 49.8, 6 49.9, 6.15 49.8, 6 49.65, 5.8 49.8))", + "POLYGON ((6.3 49.9, 6.2 49.7, 6.3 49.6, 6.5 49.8, 6.3 49.9))"), crs=crs(v)) > values(p) <- data.frame(pid=1:2, value=expanse(p)) > u <- union(v, p) > plot(u, "pid") > > b <- buffer(v, 1000) > > u <- union(b) > u$sum <- rowSums(as.data.frame(u)) > plot(u, "sum") > > > > cleanEx() > nameEx("unique") > ### * unique > > flush(stderr()); flush(stdout()) > > ### Name: unique > ### Title: Unique values > ### Aliases: unique unique,SpatRaster-method unique,SpatRaster,ANY-method > ### unique,SpatVector-method unique,SpatVector,ANY-method > ### Keywords: spatial > > ### ** Examples > > r <- rast(ncols=5, nrows=5) > values(r) <- rep(1:5, each=5) > unique(r) lyr.1 1 1 2 2 3 3 4 4 5 5 > s <- c(r, round(r/3)) > unique(s) lyr.1 lyr.1 1 1 0 2 2 1 3 3 1 4 4 1 5 5 2 > unique(s,TRUE) [[1]] [1] 1 2 3 4 5 [[2]] [1] 0 1 2 > > unique(s, as.raster=TRUE) class : SpatRaster size : 5, 5, 1 (nrow, ncol, nlyr) resolution : 72, 36 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) source(s) : memory categories : label, lyr.1, lyr.1.1 name : label min value : 1_0 max value : 5_2 > > > v <- vect(cbind(x=c(1:5,1:5), y=c(5:1,5:1)), + crs="+proj=utm +zone=1 +datum=WGS84") > nrow(v) [1] 10 > u <- unique(v) > nrow(u) [1] 5 > > values(v) <- c(1:5, 1:3, 5:4) > unique(v) class : SpatVector geometry : points dimensions : 7, 1 (geometries, attributes) extent : 1, 5, 1, 5 (xmin, xmax, ymin, ymax) coord. ref. : +proj=utm +zone=1 +datum=WGS84 +units=m +no_defs names : value type : values : 1 2 3 > > > > cleanEx() > nameEx("units") > ### * units > > flush(stderr()); flush(stdout()) > > ### Name: units > ### Title: units of SpatRaster or SpatRasterDataSet > ### Aliases: units units<- units,SpatRaster-method > ### units<-,SpatRaster-method units,SpatRasterDataset-method > ### units<-,SpatRasterDataset-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > > units(s) <- c("m/s", "kg", "ha") > units(s) [1] "m/s" "kg" "ha" > s class : SpatRaster size : 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 unit : m/s, kg, ha > > units(s) <- "kg" > units(s) [1] "kg" "kg" "kg" > > > > cleanEx() > nameEx("update") > ### * update > > flush(stderr()); flush(stdout()) > > ### Name: update > ### Title: Change values in a file > ### Aliases: update update,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > fname <- paste0(tempfile(), ".tif") > x <- writeRaster(s, fname) > ext(x) <- ext(x) + 1 > crs(x) <- "+proj=utm +zone=1" > > update(x, crs=TRUE, extent=TRUE) > > rast(fname) class : SpatRaster size : 77, 101, 3 (nrow, ncol, nlyr) resolution : 1.019802, 1.025974 (x, y) extent : -1, 102, -1, 78 (xmin, xmax, ymin, ymax) coord. ref. : WGS 84 / UTM zone 1N (EPSG:32601) source : file103e532e3f65de.tif colors RGB : 1, 2, 3 names : red, green, blue min values : 0, 0, 0 max values : 254, 254, 254 > > > > cleanEx() > nameEx("values") > ### * values > > flush(stderr()); flush(stdout()) > > ### Name: values > ### Title: Cell values and geometry attributes > ### Aliases: values values,SpatRaster-method values,SpatVector-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > r class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : elev.tif name : elevation min value : 141 max value : 547 > x <- values(r) > x[3650:3655, ] [1] 349 287 334 319 271 228 > r[3650:3655] elevation 1 349 2 287 3 334 4 319 5 271 6 228 > > > ff <- system.file("ex/lux.shp", package="terra") > v <- vect(ff) > y <- values(v) > head(y) ID_1 NAME_1 ID_2 NAME_2 AREA POP 1 1 Diekirch 1 Clervaux 312 18081 2 1 Diekirch 2 Diekirch 218 32543 3 1 Diekirch 3 Redange 259 18664 4 1 Diekirch 4 Vianden 76 5163 5 1 Diekirch 5 Wiltz 263 16735 6 2 Grevenmacher 6 Echternach 188 18899 > > > > cleanEx() > nameEx("varnames") > ### * varnames > > flush(stderr()); flush(stdout()) > > ### Name: varnames > ### Title: variable and long variable names > ### Aliases: varnames varnames<- longnames longnames<- > ### varnames,SpatRaster-method varnames<-,SpatRaster-method > ### longnames,SpatRaster-method longnames<-,SpatRaster-method > ### varnames,SpatRasterDataset-method varnames<-,SpatRasterDataset-method > ### longnames,SpatRasterDataset-method > ### longnames<-,SpatRasterDataset-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(ncols=5, nrows=5, nlyrs=3) > names(s) <- c("a", "b", "c") > x <- sds(s, s) > varnames(x) <- c("one", "two") > x class : SpatRasterDataset subdatasets : 2 dimensions : 5, 5 (nrow, ncol) nlyr : 3, 3 resolution : 72, 36 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) source(s) : memory names : one, two > > > > cleanEx() > nameEx("vect") > ### * vect > > flush(stderr()); flush(stdout()) > > ### Name: vect > ### Title: Create SpatVector objects > ### Aliases: vect vect,character-method vect,list-method > ### vect,SpatVector-method vect,SpatVectorCollection-method > ### vect,SpatExtent-method vect,data.frame-method vect,matrix-method > ### vect,missing-method vect,Spatial-method vect,sf-method > ### vect,sfc-method vect,XY-method vect,PackedSpatVector-method > ### vect,SpatGraticule-method > ### Keywords: methods spatial > > ### ** Examples > > ### SpatVector from file > f <- system.file("ex/lux.shp", package="terra") > f [1] "/data/blackswan/ripley/R/packages/tests-vg/terra.Rcheck/terra/ex/lux.shp" > v <- vect(f) > v class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) source : lux.shp coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > ## subsetting (large) files > ## with attribute query > v <- vect(f, query="SELECT NAME_1, NAME_2, ID_2 FROM lux WHERE ID_2 < 4") > > ## with an extent > e <- ext(5.9, 6.3, 49.9, 50) > v <- vect(f, extent=e) > > ## with polygons > p <- as.polygons(e) > v <- vect(f, filter=p) > > > ### SpatVector from a geom matrix > x1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60)) > x2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55)) > x3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45)) > hole <- rbind(c(80,0), c(105,13), c(120,2), c(105,-13)) > z <- rbind(cbind(object=1, part=1, x1, hole=0), cbind(object=2, part=1, x3, hole=0), + cbind(object=3, part=1, x2, hole=0), cbind(object=3, part=1, hole, hole=1)) > colnames(z)[3:4] <- c('x', 'y') > > p <- vect(z, "polygons") > p class : SpatVector geometry : polygons dimensions : 3, 0 (geometries, attributes) extent : -180, 160, -60, 60 (xmin, xmax, ymin, ymax) coord. ref. : > > z[z[, "hole"]==1, "object"] <- 4 > lns <- vect(z[,1:4], "lines") > plot(p) > lines(lns, col="red", lwd=2) > > ### from wkt > v <- vect("POLYGON ((0 -5, 10 0, 10 -10, 0 -5))") > > wkt <- c("MULTIPOLYGON ( ((40 40, 20 45, 45 30, 40 40)), + ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))", + "POLYGON ((0 -5, 10 0, 10 -10, 0 -5))") > w <- vect(wkt) > > # combine two SpatVectors > vw <- rbind(w, v) > > # add a data.frame > d <- data.frame(id=1:2, name=c("a", "b")) > values(w) <- d > > # add data.frame on creation, here from a geom matrix > g <- geom(w) > d <- data.frame(id=1:2, name=c("a", "b")) > m <- vect(g, "polygons", atts=d, crs="+proj=longlat +datum=WGS84") > > ### SpatVector from a data.frame > d$wkt <- wkt > x <- vect(d, geom="wkt") > > d$wkt <- NULL > d$lon <- c(0,10) > d$lat <- c(0,10) > x <- vect(d, geom=c("lon", "lat")) > > # SpatVector to sf > #sf::st_as_sf(x) > > > > cleanEx() > nameEx("viewshed") > ### * viewshed > > flush(stderr()); flush(stdout()) > > ### Name: viewshed > ### Title: Compute a viewshed > ### Aliases: viewshed viewshed,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > x <- project(r, "EPSG:2169") > p <- cbind(70300, 96982) > v <- viewshed(x, p, 0, 0, 0.85714) > > > > cleanEx() > nameEx("voronoi") > ### * voronoi > > flush(stderr()); flush(stdout()) > > ### Name: voronoi > ### Title: Voronoi diagram and Delaunay triangles > ### Aliases: delaunay voronoi voronoi,SpatVector-method > ### delaunay,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > wkt <- c("MULTIPOLYGON ( ((40 40, 20 45, 45 30, 40 40)), + ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))", + "POLYGON ((0 -5, 10 0, 10 -10, 0 -5))") > x <- vect(wkt) > v <- voronoi(x) > v class : SpatVector geometry : polygons dimensions : 14, 0 (geometries, attributes) extent : -55, 100, -65, 100 (xmin, xmax, ymin, ymax) coord. ref. : > > d <- delaunay(x) > d class : SpatVector geometry : polygons dimensions : 18, 0 (geometries, attributes) extent : 0, 45, -10, 45 (xmin, xmax, ymin, ymax) coord. ref. : > > plot(v, lwd=2, col=rainbow(15)) > lines(x, col="gray", lwd=2) > points(x) > > > > cleanEx() > nameEx("vrt") > ### * vrt > > flush(stderr()); flush(stdout()) > > ### Name: vrt > ### Title: Virtual Raster Dataset > ### Aliases: vrt vrt,character-method vrt,SpatRasterCollection-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=100, nrows=100) > values(r) <- 1:ncell(r) > x <- rast(ncols=2, nrows=2) > filename <- paste0(tempfile(), "_.tif") > ff <- makeTiles(r, x, filename) > ff [1] "/tmp/RtmpvNGYQp/file103e5325a38d8f_1.tif" [2] "/tmp/RtmpvNGYQp/file103e5325a38d8f_2.tif" [3] "/tmp/RtmpvNGYQp/file103e5325a38d8f_3.tif" [4] "/tmp/RtmpvNGYQp/file103e5325a38d8f_4.tif" > > #vrtfile <- paste0(tempfile(), ".vrt") > #v <- vrt(ff, vrtfile) > > > ## output in lower resolution > #vrtfile <- paste0(tempfile(), ".vrt") > #v <- vrt(ff, vrtfile, options = c("-tr", 5, 5)) > #head(readLines(vrtfile)) > #v > > > > cleanEx() > nameEx("watershed") > ### * watershed > > flush(stderr()); flush(stdout()) > > ### Name: watershed > ### Title: Catchment delineation > ### Aliases: watershed watershed,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > elev <- rast(system.file('ex/elev_vinschgau.tif', package="terra")) > flowdir <- terrain(elev, "flowdir") > ## pour point at Naturns > pp <- cbind(653358.3, 5168222) > w <- watershed(flowdir, pp) > > > > cleanEx() > nameEx("weighted.mean") > ### * weighted.mean > > flush(stderr()); flush(stdout()) > > ### Name: weighted.mean > ### Title: Weighted mean of layers > ### Aliases: weighted.mean weighted.mean,SpatRaster,numeric-method > ### weighted.mean,SpatRaster,SpatRaster-method > > ### ** Examples > > b <- rast(system.file("ex/logo.tif", package="terra")) > > # give least weight to first layer, most to last layer > wm1 <- weighted.mean(b, w=1:3) > > # spatially varying weights > # weigh by column number > w1 <- init(b, "col") > > # weigh by row number > w2 <- init(b, "row") > w <- c(w1, w2, w2) > > wm2 <- weighted.mean(b, w=w) > > > > cleanEx() > nameEx("where") > ### * where > > flush(stderr()); flush(stdout()) > > ### Name: where > ### Title: Where are the cells with the min or max values? > ### Aliases: where.min where.min,SpatRaster-method where.max > ### where.max,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > where.min(r) layer cell value [1,] 1 7770 141 [2,] 1 8055 141 > > > > cleanEx() > nameEx("which") > ### * which > > flush(stderr()); flush(stdout()) > > ### Name: which.lyr > ### Title: Which cells are TRUE? > ### Aliases: which.lyr which.lyr,SpatRaster-method > ### Keywords: spatial > > ### ** Examples > > s <- rast(system.file("ex/logo.tif", package="terra")) > x <- which.lyr(s > 100) > > > > cleanEx() > nameEx("width") > ### * width > > flush(stderr()); flush(stdout()) > > ### Name: width > ### Title: SpatVector geometric properties > ### Aliases: width clearance width,SpatVector-method > ### clearance,SpatVector-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > > width(v) [1] 25511.501 20333.498 19019.103 9255.223 20141.189 14308.518 15475.916 [8] 18111.001 17435.502 15750.096 18936.122 15692.522 > clearance(v) [1] 24.349993 50.070894 31.878312 51.390103 13.517168 15.960117 2.871879 [8] 17.584083 9.023613 4.239727 8.231322 60.918397 > > width_lines <- width(v, as.lines=TRUE) > plot(v) > lines(width_lines, col="blue") > > > > cleanEx() > nameEx("window") > ### * window > > flush(stderr()); flush(stdout()) > > ### Name: window > ### Title: Set a window > ### Aliases: window window<- window,SpatRaster-method > ### window<-,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > global(r, "mean", na.rm=TRUE) mean elevation 348.3366 > e <- ext(c(5.9, 6,49.95, 50)) > > window(r) <- e > global(r, "mean", na.rm=TRUE) mean elevation 396.4167 > r class : SpatRaster size : 6, 12, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) window : 5.9, 6, 49.95, 50 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : elev.tif name : elevation min value : >141 max value : 547< > > x <- rast(f) > xe <- crop(x, e) > global(xe, "mean", na.rm=TRUE) mean elevation 396.4167 > > b <- c(xe, r) > window(b) [1] FALSE TRUE > b class : SpatRaster size : 6, 12, 2 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent (win): 5.9, 6, 49.95, 50 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) sources : memory elev.tif varnames : elev elev names : elevation, elevation min values : 323, >141 max values : 492, 547< > > window(r) <- NULL > r class : SpatRaster size : 90, 95, 1 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : elev.tif name : elevation min value : 141 max value : 547 > > > > cleanEx() > nameEx("wrap") > ### * wrap > > flush(stderr()); flush(stdout()) > > ### Name: wrap > ### Title: wrap and unwrap > ### Aliases: wrap unwrap wrap,SpatExtent-method wrap,SpatVector-method > ### wrap,SpatRaster-method wrap,SpatRasterDataset-method > ### wrap,SpatRasterCollection-method unwrap,ANY-method > ### unwrap,PackedSpatExtent-method unwrap,PackedSpatRaster-method > ### unwrap,PackedSpatRasterDC-method unwrap,PackedSpatVector-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > p <- wrap(v) > p [1] "This is a PackedSpatVector object. Use 'terra::unwrap()' to unpack it" > vv <- vect(p) > vv class : SpatVector geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : values : 1 Diekirch 1 Clervaux 312 1.808e+04 1 Diekirch 2 Diekirch 218 3.254e+04 1 Diekirch 3 Redange 259 1.866e+04 > > > > cleanEx() > nameEx("wrapCache") > ### * wrapCache > > flush(stderr()); flush(stdout()) > > ### Name: wrapCache > ### Title: SpatRaster wrap with caching options > ### Aliases: wrapCache wrapCache,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > > x <- wrapCache(r, path=tempdir()) > x [1] "This is a PackedSpatRaster object. Use 'terra::unwrap()' to unpack it" > > > > > cleanEx() > nameEx("writeCDF") > ### * writeCDF > > flush(stderr()); flush(stdout()) > > ### Name: writeCDF > ### Title: Write raster data to a NetCDF file > ### Aliases: writeCDF writeCDF,SpatRasterDataset-method > ### writeCDF,SpatRaster-method > ### Keywords: spatial methods > > ### ** Examples > > > f <- system.file("ex/elev.tif", package="terra") > r <- rast(f) > fname <- paste0(tempfile(), ".nc") > rr <- writeCDF(r, fname, overwrite=TRUE, varname="alt", + longname="elevation in m above sea level", unit="m") > > a <- rast(ncols=5, nrows=5, nl=50) > values(a) <- 1:prod(dim(a)) > time(a) <- as.Date("2020-12-31") + 1:nlyr(a) > aa <- writeCDF(a, fname, overwrite=TRUE, varname="power", + longname="my nice data", unit="U/Pa") > > b <- sqrt(a) > s <- sds(a, b) > names(s) <- c("temp", "prec") > longnames(s) <- c("temperature (C)", "precipitation (mm)") > units(s) <- c("°C", "mm") > ss <- writeCDF(s, fname, overwrite=TRUE) > > # four dimensional > r1 <- rast(nrow=5, ncol=5, vals=1:100, nlyr=4) > depth(r1) <- c(0, 2, 0, 2) > time(r1) <- c(as.Date("2012-12-12") + c(1,1,2,2)) > depthName(r1) <- "angle" > > r2 <- rast(nrow=5, ncol=5, vals=1:150, nlyr=6) > depth(r2) <- c(10, 10, 20, 20, 30, 30) > time(r2) <- c(as.Date("2012-12-12") + c(1:2, 1:2, 1:2)) > depthName(r2) <- "height" > depthUnit(r2) <- "cm" > > s <- sds(r1, r2) > names(s) <- c("TH", "DBZH") > units(s) <- c("-", "Pa") > x <- writeCDF(s, filename = fname, overwrite=TRUE) > x[1] class : SpatRaster size : 5, 5, 4 (nrow, ncol, nlyr) resolution : 72, 36 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) source : file103e5342246144.nc:TH varname : TH names : TH_angle=0_1, TH_angle=2_1, TH_angle=0_2, TH_angle=2_2 unit : - depth : 0 to 2 (angle: 2 steps) time (days) : 2012-12-13 to 2012-12-14 (2 steps) > time(x[1]) [1] "2012-12-13" "2012-12-13" "2012-12-14" "2012-12-14" > depth(x[1]) [1] 0 2 0 2 > > x[2] class : SpatRaster size : 5, 5, 6 (nrow, ncol, nlyr) resolution : 72, 36 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) source : file103e5342246144.nc:DBZH varname : DBZH names : DBZH_~=10_1, DBZH_~=10_2, DBZH_~=20_1, DBZH_~=20_2, DBZH_~=30_1, DBZH_~=30_2 unit : Pa depth : 10 to 30 (height [cm]: 3 steps) time (days) : 2012-12-13 to 2012-12-14 (2 steps) > time(x[2]) [1] "2012-12-13" "2012-12-14" "2012-12-13" "2012-12-14" "2012-12-13" [6] "2012-12-14" > depth(x[2]) [1] 10 10 20 20 30 30 > > # for CRAN > file.remove(fname) [1] TRUE > > > > cleanEx() > nameEx("writeRaster") > ### * writeRaster > > flush(stderr()); flush(stdout()) > > ### Name: writeRaster > ### Title: Write raster data to a file > ### Aliases: writeRaster,SpatRaster,character-method writeRaster > ### Keywords: spatial methods > > ### ** Examples > > r <- rast(nrows=5, ncols=5, vals=1:25) > > # create a temporary filename for the example > f <- file.path(tempdir(), "test.tif") > > writeRaster(r, f, overwrite=TRUE) > > writeRaster(r, f, overwrite=TRUE, gdal=c("COMPRESS=NONE", "TFW=YES"), datatype='INT1U') > > ## Or with a wopt argument: > > writeRaster(r, f, overwrite=TRUE, wopt= list(gdal=c("COMPRESS=NONE"), datatype='INT1U')) > > ## remove the file > unlink(f) > > > > cleanEx() > nameEx("writeVector") > ### * writeVector > > flush(stderr()); flush(stdout()) > > ### Name: writeVector > ### Title: Write SpatVector data to a file > ### Aliases: writeVector,SpatVector,character-method writeVector > ### Keywords: spatial methods > > ### ** Examples > > v <- vect(cbind(1:5,1:5)) > crs(v) <- "+proj=longlat +datum=WGS84" > v$id <- 1:length(v) > v$name <- letters[1:length(v)] > tmpf1 <- paste0(tempfile(), ".gpkg") > writeVector(v, tmpf1, overwrite=TRUE) > x <- vect(tmpf1) > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f) > tmpf2 <- paste0(tempfile(), ".gpkg") > writeVector(v, tmpf2, overwrite=TRUE) > y <- vect(tmpf2) > > > > cleanEx() > nameEx("xapp") > ### * xapp > > flush(stderr()); flush(stdout()) > > ### Name: xapp > ### Title: Apply a function to the cells of two SpatRasters > ### Aliases: xapp xapp,SpatRaster,SpatRaster-method > ### Keywords: methods spatial > > ### ** Examples > > r <- rast(ncols=10, nrows=10, nlyr=5) > set.seed(1) > r <- init(r, runif) > s <- init(r, runif) > x <- xapp(r, s, fun=cor) > > > > cleanEx() > nameEx("xmin") > ### * xmin > > flush(stderr()); flush(stdout()) > > ### Name: xmin > ### Title: Get or set single values of an extent > ### Aliases: xmin xmax ymin ymax xmin<- xmax<- ymin<- ymax<- > ### xmin,SpatExtent-method xmin,SpatRaster-method xmin,SpatVector-method > ### xmax,SpatExtent-method xmax,SpatRaster-method xmax,SpatVector-method > ### ymin,SpatExtent-method ymin,SpatRaster-method ymin,SpatVector-method > ### ymax,SpatExtent-method ymax,SpatRaster-method ymax,SpatVector-method > ### xmin<-,SpatExtent,numeric-method xmin<-,SpatRaster,numeric-method > ### xmax<-,SpatExtent,numeric-method xmax<-,SpatRaster,numeric-method > ### ymin<-,SpatExtent,numeric-method ymin<-,SpatRaster,numeric-method > ### ymax<-,SpatExtent,numeric-method ymax<-,SpatRaster,numeric-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > ext(r) SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) > ext(c(0, 20, 0, 20)) SpatExtent : 0, 20, 0, 20 (xmin, xmax, ymin, ymax) > > xmin(r) [1] -180 > xmin(r) <- 0 > xmin(r) [1] 0 > > > > cleanEx() > nameEx("xyCellFrom") > ### * xyCellFrom > > flush(stderr()); flush(stdout()) > > ### Name: xyRowColCell > ### Title: Coordinates from a row, column or cell number and vice versa > ### Aliases: xFromCol xFromCol,SpatRaster,numeric-method > ### xFromCol,SpatRaster,missing-method yFromRow > ### yFromRow,SpatRaster,numeric-method yFromRow,SpatRaster,missing-method > ### xyFromCell xyFromCell,SpatRaster,numeric-method xFromCell > ### xFromCell,SpatRaster,numeric-method yFromCell > ### yFromCell,SpatRaster,numeric-method cellFromRowCol > ### cellFromRowCol,SpatRaster,numeric,numeric-method > ### cellFromRowColCombine > ### cellFromRowColCombine,SpatRaster,numeric,numeric-method rowColCombine > ### rowColCombine,SpatRaster,numeric,numeric-method colFromX > ### colFromX,SpatRaster,numeric-method rowFromY > ### rowFromY,SpatRaster,numeric-method cellFromXY > ### cellFromXY,SpatRaster,matrix-method > ### cellFromXY,SpatRaster,data.frame-method rowFromCell > ### rowFromCell,SpatRaster,numeric-method colFromCell > ### colFromCell,SpatRaster,numeric-method rowColFromCell > ### rowColFromCell,SpatRaster,numeric-method > ### Keywords: spatial > > ### ** Examples > > r <- rast() > > xFromCol(r, c(1, 120, 180)) [1] -179.5 -60.5 -0.5 > yFromRow(r, 90) [1] 0.5 > xyFromCell(r, 10000) x y [1,] 99.5 62.5 > xyFromCell(r, c(0, 1, 32581, ncell(r), ncell(r)+1)) x y [1,] NaN NaN [2,] -179.5 89.5 [3,] 0.5 -0.5 [4,] 179.5 -89.5 [5,] NaN NaN > > cellFromRowCol(r, 5, 5) [1] 1445 > cellFromRowCol(r, 1:2, 1:2) [1] 1 362 > cellFromRowCol(r, 1, 1:3) [1] 1 2 3 > > # all combinations > cellFromRowColCombine(r, 1:2, 1:2) [1] 1 2 361 362 > > colFromX(r, 10) [1] 191 > rowFromY(r, 10) [1] 81 > xy <- cbind(lon=c(10,5), lat=c(15, 88)) > cellFromXY(r, xy) [1] 27191 906 > > # if no row/col specified all are returned > range(xFromCol(r)) [1] -179.5 179.5 > length(yFromRow(r)) [1] 180 > > > > cleanEx() > nameEx("zonal") > ### * zonal > > flush(stderr()); flush(stdout()) > > ### Name: zonal > ### Title: Zonal statistics > ### Aliases: zonal zonal,SpatRaster,SpatRaster-method > ### zonal,SpatRaster,SpatVector-method zonal,SpatVector,SpatVector-method > ### Keywords: spatial > > ### ** Examples > > > ### SpatRaster, SpatRaster > r <- rast(ncols=10, nrows=10) > values(r) <- 1:ncell(r) > z <- rast(r) > values(z) <- rep(c(1:2, NA, 3:4), each=20) > names(z) <- "zone" > zonal(r, z, "sum", na.rm=TRUE) zone lyr.1 1 1 210 2 2 610 3 3 1410 4 4 1810 > > # with weights > w <- init(r, "col") > zonal(r, z, w=w, "mean", na.rm=TRUE) zone lyr.1 1 1 12 2 2 32 3 3 72 4 4 92 > > # multiple layers > r <- rast(system.file("ex/logo.tif", package = "terra")) > # zonal layer > z <- rast(r, 1) > names(z) <- "zone" > values(z) <- rep(c(1:2, NA, c(3:4)), each=ncell(r)/5, length.out=ncell(r)) > > zonal(r, z, "mean", na.rm = TRUE) zone red green blue 1 1 197.9486 198.0103 193.5556 2 2 173.2219 176.7717 185.2585 3 3 168.2952 172.6232 184.6939 4 4 193.5859 197.0019 206.5717 > > # raster of zonal values > zr <- zonal(r, z, "mean", na.rm = TRUE, as.raster=TRUE) > > > ### SpatRaster, SpatVector > x <- rast(ncol=2,nrow=2, vals=1:4, xmin=0, xmax=1, ymin=0, ymax=1, crs="+proj=utm +zone=1") > p <- as.polygons(x) > pp <- shift(p, .2) > r <- disagg(x, 4) > > zonal(r, p) lyr.1 1 1 2 2 3 3 4 4 > zonal(r, p, sum) lyr.1 1 16 2 32 3 48 4 64 > zonal(x, pp, exact=TRUE) lyr.1 1 1.4 2 2.0 3 3.4 4 4.0 > zonal(c(x, x*10), pp, w=x) lyr.1 lyr.1 1 1 10 2 2 20 3 3 30 4 4 40 > > > ### SpatVector, SpatVector > > f <- system.file("ex/lux.shp", package="terra") > v <- vect(f)[,c(2,4)] > > p <- spatSample(v, 100) > values(p) <- data.frame(b2=1:100, ssep1=100:1) > > zonal(p, v, mean) zone b2 ssep1 1 1 70.21429 30.78571 2 2 62.50000 38.50000 3 3 37.50000 63.50000 4 4 57.50000 43.50000 5 5 34.57143 66.42857 6 6 55.42857 45.57143 7 7 59.75000 41.25000 8 8 49.25000 51.75000 9 9 41.16667 59.83333 10 10 47.60000 53.40000 11 11 42.62500 58.37500 12 12 42.80000 58.20000 > > > > ### *