EVOLUTION-MANAGER
Edit File: extract.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Extract values from Raster objects</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for extract {raster}"><tr><td>extract {raster}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Extract values from Raster objects</h2> <h3>Description</h3> <p>Extract values from a Raster* object at the locations of other spatial data. You can use coordinates (points), lines, polygons or an Extent (rectangle) object. You can also use cell numbers to extract values. </p> <p>If <code>y</code> represents points, <code>extract</code> returns the values of a Raster* object for the cells in which a set of points fall. If <code>y</code> represents lines, the <code>extract</code> method returns the values of the cells of a Raster* object that are touched by a line. If <code>y</code> represents polygons, the <code>extract</code> 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</code> option for considering partly covered cells; and argument <code>small</code> for getting values for small polygons). </p> <p>It is also possible to extract values for point locations from SpatialPolygons. </p> <h3>Usage</h3> <pre> ## 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, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>Raster* object</p> </td></tr> <tr valign="top"><td><code>y</code></td> <td> <p>points represented by a two-column matrix or data.frame, or <code><a href="../../sp/html/SpatialPoints.html">SpatialPoints</a>*</code>; <code><a href="../../sp/html/SpatialPolygons.html">SpatialPolygons</a>*</code>; <code><a href="../../sp/html/SpatialLines.html">SpatialLines</a></code>; <code><a href="Extent-class.html">Extent</a></code>; or a numeric vector representing cell numbers</p> </td></tr> <tr valign="top"><td><code>method</code></td> <td> <p>character. <code>'simple'</code> or <code>'bilinear'</code>. If <code>'simple'</code> values for the cell a point falls in are returned. If <code>'bilinear'</code> the returned values are interpolated from the values of the four nearest raster cells.</p> </td></tr> <tr valign="top"><td><code>buffer</code></td> <td> <p>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).</p> </td></tr> <tr valign="top"><td><code>small</code></td> <td> <p>logical. If <code>TRUE</code> and <code>y</code> represents points and a <code>buffer</code> 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</code> 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))</code> or <code>extract(x, coordinates(y), method='bilinear')</code>.</p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>function to summarize the values (e.g. <code>mean</code>). 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</code> 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</code> represents points, <code>fun</code> is only used when a buffer is used (and hence multiple values per spatial feature would otherwise be returned).</p> </td></tr> <tr valign="top"><td><code>na.rm</code></td> <td> <p>logical. Only useful when an argument <code>fun</code> is supplied. If <code>na.rm=TRUE</code> (the default value), NA values are removed before fun is applied. This argument may be ignored if the function used has a <code>...</code> argument and ignores an additional <code>na.rm</code> argument</p> </td></tr> <tr valign="top"><td><code>cellnumbers</code></td> <td> <p>logical. If <code>cellnumbers=TRUE</code>, cell-numbers will also be returned (if no <code>fun</code> argument is supplied, and when extracting values with points, if <code>buffer</code> is <code>NULL</code>)</p> </td></tr> <tr valign="top"><td><code>df</code></td> <td> <p>logical. If <code>df=TRUE</code>, results will be returned as a data.frame. The first column is a sequential ID, the other column(s) are the extracted values</p> </td></tr> <tr valign="top"><td><code>weights</code></td> <td> <p>logical. If <code>TRUE</code> and <code>normalizeWeights=FALSE</code>, the function returns, for each polygon, a matrix with the cell values and the approximate fraction of each cell that is covered by the polygon(rounded to 1/100). If <code>TRUE</code> and <code>normalizeWeights=TRUE</code> the weights are normalized such that they add up to one. The weights can be used for averaging; see examples. This option can be useful (but slow) if the polygons are small relative to the cells size of the Raster* object</p> </td></tr> <tr valign="top"><td><code>normalizeWeights</code></td> <td> <p>logical. If <code>TRUE</code>, weights are normalized such that they add up to one for each polygon</p> </td></tr> <tr valign="top"><td><code>factors</code></td> <td> <p>logical. If <code>TRUE</code>, factor values are returned, else their integer representation is returned</p> </td></tr> <tr valign="top"><td><code>layer</code></td> <td> <p>integer. First layer for which you want values (if <code>x</code> is a multilayer object)</p> </td></tr> <tr valign="top"><td><code>nl</code></td> <td> <p> integer. Number of layers for which you want values (if <code>x</code> is a multilayer object)</p> </td></tr> <tr valign="top"><td><code>along</code></td> <td> <p> boolean. Should returned values be ordered to go along the lines?</p> </td></tr> <tr valign="top"><td><code>sp</code></td> <td> <p> boolean. Should the extracted values be added to the data.frame of the Spatial* object <code>y</code>? This only applies if <code>y</code> is a Spatial* object and, for SpatialLines and SpatialPolygons, if <code>fun</code> is not NULL. In this case the returned value is the expanded Spatial object</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>additional arguments (none implemented)</p> </td></tr> </table> <h3>Value</h3> <p>A vector for RasterLayer objects, and a matrix for RasterStack or RasterBrick objects. A list (or a data.frame if <code>df=TRUE</code>) if <code>y</code> is a SpatialPolygons* or SpatialLines* object or if a <code>buffer</code> argument is used (but not a <code>fun</code> argument). If <code>sp=TRUE</code> and <code>y</code> is a Spatial* object and <code>fun</code> is not NULL a Spatial* object is returned. The order of the returned values corresponds to the order of object <code>y</code>. If <code>df=TRUE</code>, this is also indicated in the first variable ('ID'). </p> <h3>See Also</h3> <p><code><a href="getValues.html">getValues</a>, <a href="getValuesFocal.html">getValuesFocal</a></code> </p> <h3>Examples</h3> <pre> 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) </pre> <hr /><div style="text-align: center;">[Package <em>raster</em> version 3.3-13 <a href="00Index.html">Index</a>]</div> </body></html>