Description Usage Arguments Value See Also Examples
Extract values from a Raster* object at the locations of spatial vector data. There are methods for points, lines, and polygons (classes from 'sp' or 'sf'), for a matrix or data.frame of points. You can also use cell numbers and Extent (rectangle) objects to extract values.
If y
represents points, extract
returns the values of a Raster* object for the cells in which a set of points fall.
If y
represents lines, the extract
method returns the values of the cells of a Raster* object that are touched by a line. If y
represents polygons, the extract
method returns the values of the cells of a Raster* object that are covered by a polygon. A cell is covered if its center is inside the polygon (but see the weights
option for considering partly covered cells; and argument small
for getting values for small polygons).
It is also possible to extract values for point locations from SpatialPolygons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ## S4 method for signature 'Raster,matrix'
extract(x, y, method='simple', buffer=NULL, small=FALSE, cellnumbers=FALSE,
fun=NULL, na.rm=TRUE, layer, nl, df=FALSE, factors=FALSE, ...)
## S4 method for signature 'Raster,SpatialLines'
extract(x, y, fun=NULL, na.rm=FALSE, cellnumbers=FALSE, df=FALSE, layer,
nl, factors=FALSE, along=FALSE, sp=FALSE, ...)
## S4 method for signature 'Raster,SpatialPolygons'
extract(x, y, fun=NULL, na.rm=FALSE, weights=FALSE,
normalizeWeights=TRUE, cellnumbers=FALSE, small=TRUE, df=FALSE, layer, nl,
factors=FALSE, sp=FALSE, ...)
## S4 method for signature 'SpatialPolygons,SpatialPoints'
extract(x, y, ...)
|
x |
Raster* object |
y |
points represented by a two-column matrix or data.frame, or |
method |
character. |
buffer |
numeric. The radius of a buffer around each point from which to extract cell values. If the distance between the sampling point and the center of a cell is less than or equal to the buffer, the cell is included. The buffer can be specified as a single value, or as a vector of the length of the number of points. If the data are not projected (latitude/longitude), the unit should be meters. Otherwise it should be in map-units (typically also meters). |
small |
logical. If |
fun |
function to summarize the values (e.g. |
na.rm |
logical. Only useful when an argument |
cellnumbers |
logical. If |
df |
logical. If |
weights |
logical. If |
normalizeWeights |
logical. If |
factors |
logical. If |
layer |
integer. First layer for which you want values (if |
nl |
integer. Number of layers for which you want values (if |
along |
boolean. Should returned values be ordered to go along the lines? |
sp |
boolean. Should the extracted values be added to the data.frame of the Spatial* object |
... |
additional arguments (none implemented) |
A vector for RasterLayer objects, and a matrix for RasterStack or RasterBrick objects. A list (or a data.frame if df=TRUE
) if y
is a SpatialPolygons* or SpatialLines* object or if a buffer
argument is used (but not a fun
argument). If sp=TRUE
and y
is a Spatial* object and fun
is not NULL a Spatial* object is returned. The order of the returned values corresponds to the order of object y
. If df=TRUE
, this is also indicated in the first variable ('ID').
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | r <- raster(ncol=36, nrow=18, vals=1:(18*36))
###############################
# extract values by cell number
###############################
extract(r, c(1:2, 10, 100))
s <- stack(r, sqrt(r), r/r)
extract(s, c(1, 10, 100), layer=2, n=2)
###############################
# extract values with points
###############################
xy <- cbind(-50, seq(-80, 80, by=20))
extract(r, xy)
sp <- SpatialPoints(xy)
extract(r, sp, method='bilinear')
# examples with a buffer
extract(r, xy[1:3,], buffer=1000000)
extract(r, xy[1:3,], buffer=1000000, fun=mean)
## illustrating the varying size of a buffer (expressed in meters)
## on a longitude/latitude raster
z <- extract(r, xy, buffer=1000000)
s <- raster(r)
for (i in 1:length(z)) { s[z[[i]]] <- i }
## compare with raster that is not longitude/latitude
crs(r) <- "+proj=utm +zone=17"
xy[,1] <- 50
z <- extract(r, xy, buffer=8)
for (i in 1:length(z)) { s[z[[i]]] <- i }
plot(s)
# library(maptools)
# data(wrld_simpl)
# plot(wrld_simpl, add=TRUE)
###############################
# extract values with lines
###############################
r <- raster(ncol=36, nrow=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 <- spLines(cds1, cds2)
extract(r, lines)
###############################
# extract values with polygons
###############################
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- spPolygons(cds1, cds2)
v <- extract(r, polys)
# mean for each polygon
unlist(lapply(v, function(x) if (!is.null(x)) mean(x, na.rm=TRUE) else NA ))
# v <- extract(r, polys, cellnumbers=TRUE)
# weighted mean
# v <- extract(r, polys, weights=TRUE, fun=mean)
# equivalent to:
# v <- extract(r, polys, weights=TRUE)
# sapply(v, function(x) if (!is.null(x)) {sum(apply(x, 1, prod)) / sum(x[,2])} else NA)
###############################
# extract values with an extent
###############################
e <- extent(150,170,-60,-40)
extract(r, e)
#plot(r)
#plot(e, add=T)
|
Loading required package: sp
[1] 1 2 10 100
layer.2 layer.3
[1,] 1.000000 1
[2,] 3.162278 1
[3,] 10.000000 1
[1] 626 554 482 410 338 266 194 122 50
[1] 607.5 535.5 463.5 391.5 319.5 247.5 175.5 103.5 31.5
[[1]]
[1] 586 587 588 589 590 591 592 593 620 621 622 623 624 625 626 627 628 629 630
[20] 631
[[2]]
[1] 517 518 552 553 554 555
[[3]]
[1] 445 446 481 482
[1] 611.1 541.5 463.5
[[1]]
[1] 126 127 161 162 163 164 196 197 200 201 231 232 237 266 267 273 274 302 310
[20] 311 338 346 381 382 414 417 450 451 452 453 487 488
[[2]]
[1] 139 140 141 174 175 177 208 209 210 213 243 244 249 250 279 286 322 358 359
[20] 394 429 430 465 501 537
[1] 387.8158 329.3913
[1] 502 503 538 539
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.