
Function to extract point values directly from a SpatRaster
Source:R/utils-spatial.R
get_rastervalue.RdThis function simply extracts the values from a provided
SpatRaster, SpatRasterDataset or SpatRasterCollection object. For
points where or NA values were extracted a small buffer is applied to try and
obtain the remaining values.
Arguments
- coords
A
data.frame,matrixorsfobject.- env
A
SpatRasterobject with the provided predictors.- ngb_fill
logicalon whether cells should be interpolated from neighbouring values.- rm.na
logicalparameter which - if set - removes all rows with a missing data point (NA) from the result.
Value
A data.frame with the extracted covariate data from each provided
data point.
Details
It is essentially a wrapper for terra::extract.
Examples
# Dummy raster:
r <- terra::rast(nrows = 10, ncols = 10, res = 0.05, xmin = -1.5, xmax = 1.5,
ymin = -1.5, ymax = 1.5, vals = rnorm(3600,mean = .5,sd = .1))
# (dummy points)
pp <- terra::spatSample(r,20,as.points = TRUE) |> sf::st_as_sf()
# Extract values
vals <- get_rastervalue(pp, r)
head(vals)
#> ID lyr.1 x y
#> 1 1 0.5154511 1.475 -1.275
#> 2 2 0.3948053 1.275 1.175
#> 3 3 0.5363174 -1.025 0.225
#> 4 4 0.3373516 0.625 -1.275
#> 5 5 0.5024094 0.225 -0.775
#> 6 6 0.4530912 1.025 1.275