Learn R Programming

terra (version 0.5-2)

extract: Extract values from a SpatRaster

Description

Extract values from a SpatRaster for the locations of a SpatVector

Usage

# S4 method for SpatRaster,SpatVector
extract(x, y, fun=NULL, ..., method="simple", drop=FALSE)
# S4 method for SpatRaster,matrix
extract(x, y, ...)

Arguments

x

SpatRaster

y

SpatVector (for points, lines, polygons), or 2-column matrix (x, y; for points)

fun

character. function to summarize the data. Currently ignored

...

additional arguments. None implemented

method

character. method for extracting values with points. The default is "simple", the alternative is "bilinear"

drop

boolean. If TRUE, the output is simplified

Value

data.frame

See Also

values

Examples

Run this code
# NOT RUN {
r <- rast(ncol=5, nrow=5, xmin=0, xmax=5, ymin=0, ymax=5)
values(r) <- 1:25
xy <- rbind(c(0.5,0.5), c(2.5,2.5))
p <- vect(xy)

extract(r, xy)
extract(r, p)

r[1,]
r[5]
r[,5]

r[c(0:2, 99:101)]

f <- system.file("exdata/test.tif", package="terra")
r <- rast(f)

xy <- cbind(179000, 330000)
xy <- rbind(xy-100, xy, xy+1000)
extract(r, xy)

p <- vect(xy)
g <- geom(p)
g

extract(r, p)

x <- r + 10
extract(x, p)

i <- cellFromXY(r, xy)
x[i]
r[i]

y <- c(x,x*2,x*3)
y[i]

# extract with a polygon
f <- system.file("exdata/lux.shp", package="terra")
v <- vect(f)
z <- rast(v)
values(z) <- 1:100
e <- extract(z, v)
e[1:2]
rapply(e, mean)

x <- c(z, z*2, z/3)
e <- extract(x, v)
matrix(rapply(e, mean), ncol=nlyr(x), byrow=TRUE)
# }

Run the code above in your browser using DataLab