1\name{extract}
2
3\docType{methods}
4
5\alias{extract}
6\alias{extract,Raster,vector-method}
7\alias{extract,Raster,matrix-method}
8\alias{extract,Raster,data.frame-method}
9\alias{extract,Raster,SpatialPoints-method}
10\alias{extract,Raster,SpatialLines-method}
11\alias{extract,Raster,SpatialPolygons-method}
12\alias{extract,Raster,sf-method}
13\alias{extract,Raster,Extent-method}
14\alias{extract,SpatialPolygons,SpatialPoints-method}
15\alias{extract,SpatialPolygons,data.frame-method}
16\alias{extract,SpatialPolygons,matrix-method}
17
18\title{Extract values from Raster objects}
19
20\description{
21Extract 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.
22
23If \code{y} represents points, \code{extract} returns the values of a Raster* object for the cells in which a set of points fall.
24If \code{y} represents lines, the \code{extract} method returns the values of the cells of a Raster* object that are touched by a line. If \code{y} represents polygons, the \code{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 \code{weights} option for considering partly covered cells; and argument \code{small} for getting values for small polygons).
25
26It is also possible to extract values for point locations from SpatialPolygons.
27}
28
29\usage{
30\S4method{extract}{Raster,matrix}(x, y, method='simple', buffer=NULL, small=FALSE, cellnumbers=FALSE,
31   fun=NULL, na.rm=TRUE, layer, nl, df=FALSE, factors=FALSE, ...)
32
33\S4method{extract}{Raster,SpatialLines}(x, y, fun=NULL, na.rm=FALSE, cellnumbers=FALSE, df=FALSE, layer,
34   nl, factors=FALSE, along=FALSE, sp=FALSE, ...)
35
36\S4method{extract}{Raster,SpatialPolygons}(x, y, fun=NULL, na.rm=FALSE, exact=FALSE, weights=FALSE,
37   normalizeWeights=TRUE, cellnumbers=FALSE, small=TRUE, df=FALSE, layer, nl,
38   factors=FALSE, sp=FALSE, ...)
39
40
41\S4method{extract}{SpatialPolygons,SpatialPoints}(x, y, ...)
42}
43
44\arguments{
45\item{x}{Raster* object}
46
47\item{y}{points represented by a two-column matrix or data.frame, or \code{\link{SpatialPoints}*}; \code{\link{SpatialPolygons}*}; \code{\link[sp]{SpatialLines}}; \code{sf} spatial vector objects; \code{\link{Extent}}; or a numeric vector representing cell numbers}
48
49\item{method}{character. \code{'simple'} or \code{'bilinear'}. If \code{'simple'} values for the cell a point falls in are returned. If \code{'bilinear'} the returned values are interpolated from the values of the four nearest raster cells.}
50
51\item{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).}
52
53\item{small}{logical. If \code{TRUE} and \code{y} represents points and a \code{buffer} argument is used, the function always return a number, also when the buffer does not include the center of a single cell. The value of the cell in which the point falls is returned if no cell center is within the buffer. If \code{y} represents polygons, a value is also returned for relatively small polygons (e.g. those smaller than a single cell of the Raster* object), or polygons with an odd shape, for which otherwise no values are returned because they do not cover any raster cell centers. In some cases, you could alternatively use the centroids of such polygons, for example using \code{extract(x, coordinates(y))} or \code{extract(x, coordinates(y), method='bilinear')}.}
54
55\item{fun}{function to summarize the values (e.g. \code{mean}). The function should take a single numeric vector as argument and return a single value (e.g. mean, min or max), and accept a \code{na.rm} argument. Thus, standard R functions not including an na.rm argument must be wrapped as in this example: fun=function(x,...)length(x). If \code{y} represents points, \code{fun} is only used when a buffer is used (and hence multiple values per spatial feature would otherwise be returned).}
56
57\item{na.rm}{logical. Only useful when an argument \code{fun} is supplied. If \code{na.rm=TRUE} (the default value), NA values are removed before fun is applied. This argument may be ignored if the function used has a \code{...} argument and ignores an additional \code{na.rm} argument}
58
59\item{cellnumbers}{logical. If \code{cellnumbers=TRUE}, cell-numbers will also be returned (if no \code{fun} argument is  supplied, and when extracting values with points, if \code{buffer} is \code{NULL})}
60
61\item{df}{logical. If \code{df=TRUE}, results will be returned as a data.frame. The first column is a sequential ID, the other column(s) are the extracted values}
62
63\item{exact}{logical. If \code{TRUE} the fraction of each cell that is (partly) covered by the polygon is extracted, not only the cells of which the centers are covered. This option is particularly useful if the polygons are small relative to the cells size of the Raster* object}
64
65\item{weights}{logical. If \code{TRUE} the fraction of a cell that is covered is returned or used by \code{fun}. These can be used as weights can be used for averaging; see examples. If \code{exact} is \code{FALSE}, this is the approximate fraction of each cell that is covered by the polygon, rounded to 1/100 }
66
67\item{normalizeWeights}{logical. If \code{TRUE}, weights are normalized such that they add up to one for each polygon}
68
69\item{factors}{logical. If \code{TRUE}, factor values are returned, else their integer representation is returned}
70
71\item{layer}{integer. First layer for which you want values (if \code{x} is a multilayer object)}
72
73\item{nl}{ integer. Number of layers for which you want values (if \code{x} is a multilayer object)}
74
75\item{along}{ boolean. Should returned values be ordered to go along the lines?}
76
77\item{sp}{ boolean. Should the extracted values be added to the data.frame of the Spatial* object \code{y}? This only applies if \code{y} is a Spatial* object and, for SpatialLines and SpatialPolygons, if \code{fun} is not NULL. In this case the returned value is the expanded Spatial object}
78
79
80\item{...}{additional arguments (none implemented)}
81}
82
83\value{
84A vector for RasterLayer objects, and a matrix for RasterStack or RasterBrick objects. A list (or a data.frame if \code{df=TRUE}) if \code{y} is a SpatialPolygons* or SpatialLines* object or if a \code{buffer} argument is used (but not a \code{fun} argument). If \code{sp=TRUE} and  \code{y} is a Spatial* object and  \code{fun} is not NULL a Spatial* object is returned. The order of the returned values corresponds to the order of object \code{y}. If \code{df=TRUE}, this is also indicated in the first variable ('ID').
85}
86
87\seealso{ \code{\link{getValues}, \link{getValuesFocal}}  }
88
89\examples{
90r <- raster(ncol=36, nrow=18, vals=1:(18*36))
91
92###############################
93# extract values by cell number
94###############################
95extract(r, c(1:2, 10, 100))
96s <- stack(r, sqrt(r), r/r)
97extract(s, c(1, 10, 100), layer=2, n=2)
98
99###############################
100# extract values with points
101###############################
102xy <- cbind(-50, seq(-80, 80, by=20))
103extract(r, xy)
104
105sp <- SpatialPoints(xy)
106extract(r, sp, method='bilinear')
107
108# examples with a buffer
109extract(r, xy[1:3,], buffer=1000000)
110extract(r, xy[1:3,], buffer=1000000, fun=mean)
111
112## illustrating the varying size of a buffer (expressed in meters)
113## on a longitude/latitude raster
114 z <- extract(r, xy, buffer=1000000)
115 s <- raster(r)
116 for (i in 1:length(z)) { s[z[[i]]] <- i }
117
118## compare with raster that is not longitude/latitude
119 crs(r) <- "+proj=utm +zone=17"
120 xy[,1] <- 50
121 z <- extract(r, xy, buffer=8)
122 for (i in 1:length(z)) { s[z[[i]]] <- i }
123 plot(s)
124# library(maptools)
125# data(wrld_simpl)
126# plot(wrld_simpl, add=TRUE)
127
128###############################
129# extract values with lines
130###############################
131r <- raster(ncol=36, nrow=18, vals=1:(18*36))
132cds1 <- rbind(c(-50,0), c(0,60), c(40,5), c(15,-45), c(-10,-25))
133cds2 <- rbind(c(80,20), c(140,60), c(160,0), c(140,-55))
134lines <- spLines(cds1, cds2)
135
136extract(r, lines)
137
138###############################
139# extract values with polygons
140###############################
141cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
142cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
143polys <- spPolygons(cds1, cds2)
144
145v <- extract(r, polys)
146# mean for each polygon
147unlist(lapply(v, function(x) if (!is.null(x)) mean(x, na.rm=TRUE) else NA ))
148
149# v <- extract(r, polys, cellnumbers=TRUE)
150
151# weighted mean
152# v <- extract(r, polys, weights=TRUE, fun=mean)
153# equivalent to:
154# v <- extract(r, polys, weights=TRUE)
155# sapply(v, function(x) if (!is.null(x)) {sum(apply(x, 1, prod)) / sum(x[,2])} else NA)
156
157
158###############################
159# extract values with an extent
160###############################
161e <- extent(150,170,-60,-40)
162extract(r, e)
163#plot(r)
164#plot(e, add=T)
165}
166
167\keyword{methods}
168\keyword{spatial}
169
170