extract,GRaster,GVector-method {fasterRaster} | R Documentation |
Extract values from a GRaster at locations in a points GVector
Description
extract()
obtains the values of a GRaster
or GVector
associated with the locations of a set of points. The output depends on the input:
-
Case #1:
x
is a numeric or integerGRaster
andy
is a pointsGVector
: Returns values of cells that have points. Ifxy
isTRUE
, also returns the coordinates of the points. -
Case #2:
x
is a categorical (factor)GRaster
andy
is a pointsGVector
: Same as case #1, but ifcats
isTRUE
, returns category labels of cells that have points. Ifxy
isTRUE
, also returns the coordinates of the points. -
Case #3:
x
is a categoricalGRaster
andy
is a lines or polygonsGVector
: Returns a summary (e.g., mean, standard deviation, etc.) of all cells that overlap the line(s) or polygon(s). -
Case #4:
x
is aGVector
andy
is a pointsGVector
: Returns the data table row associated each point. Ifxy
isTRUE
, also returns the coordinates of the points. Note that whenever a pointsGVector
is allowed fory
, adata.frame
,data.table
,matrix
, ornumeric
values representing points can be used instead.
Usage
## S4 method for signature 'GRaster,GVector'
extract(
x,
y,
fun = "mean",
prob = 0.5,
overlap = TRUE,
xy = FALSE,
cats = TRUE,
verbose = FALSE,
...
)
## S4 method for signature 'GRaster,data.frame'
extract(x, y, xy = FALSE, cats = TRUE)
## S4 method for signature 'GRaster,data.table'
extract(x, y, xy = FALSE, cats = TRUE)
## S4 method for signature 'GRaster,matrix'
extract(x, y, xy = FALSE, cats = TRUE)
## S4 method for signature 'GRaster,numeric'
extract(x, y, xy = FALSE, cats = TRUE)
## S4 method for signature 'GVector,GVector'
extract(x, y, xy = FALSE, verbose = TRUE)
## S4 method for signature 'GVector,data.frame'
extract(x, y, xy = FALSE, verbose = TRUE)
## S4 method for signature 'GVector,data.table'
extract(x, y, xy = FALSE, verbose = TRUE)
## S4 method for signature 'GVector,matrix'
extract(x, y, xy = FALSE, verbose = TRUE)
## S4 method for signature 'GVector,numeric'
extract(x, y, xy = FALSE)
Arguments
x |
A |
y |
A |
fun |
Character vector: Name(s) of function(s) to apply to values. This is used when
|
prob |
Numeric in the range from 0 to 1: Quantile which to calculate. The value of |
overlap |
Logical: If |
xy |
Logical: If |
cats |
Logical (extracting from a raster): If |
verbose |
Logical: If |
... |
Arguments to pass to |
Value
A data.frame
or data.table
.
See Also
terra::extract()
, and modules r.what
and v.what
in GRASS
Examples
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Example data: elevation raster and points vector
madElev <- fastData("madElev") # raster
madCover <- fastData("madCover") # categorical raster
madDypsis <- fastData("madDypsis") # points vector
madRivers <- fastData("madRivers") # lines vector
madCoast4 <- fastData("madCoast4") # polygons vector
# Convert to fasterRaster formats:
elev <- fast(madElev) # raster
cover <- fast(madCover) # categorical raster
dypsis <- fast(madDypsis) # points vector
rivers <- fast(madRivers) # lines vector
coast <- fast(madCoast4) # polygons vector
# Get values of elevation at points where Dypsis species are located:
extract(elev, dypsis, xy = TRUE)
# Extract from categorical raster at points:
categories <- extract(cover, dypsis)
categoryValues <- extract(cover, dypsis, cats = FALSE)
categories
categoryValues
# Extract and summarize values on a raster across polygons:
extract(elev, coast, fun = c("sum", "mean", "countNonNA"), overlap = FALSE)
# Extract and summarize values on a raster across lines:
extract(elev, rivers, fun = c("sum", "mean", "countNonNA"), overlap = FALSE)
# Extract from a polygons vector at a points vector:
polysFromPoints <- extract(coast, dypsis, xy = TRUE)
head(polysFromPoints) # first 3 are outside polygons vector, next 3 are inside
}