Skip to contents

Extract SDP raster data at a set of locations.

Usage

sdp_extract_data(
  raster,
  locations,
  date_start = NULL,
  date_end = NULL,
  years = NULL,
  catalog_id = NULL,
  url_template = NULL,
  bind = TRUE,
  return_type = "SpatVector",
  method = "bilinear",
  sum_fun = "mean",
  verbose = TRUE,
  ...
)

Arguments

raster

class SpatRaster. A raster dataset (class terra::SpatRaster) to extract data from.

locations

A vector dataset (class terra::SpatVector or sf::sf) containing points, lines, or polygons at which to sample the raster data.

date_start

class Date. If the raster dataset is a daily or monthly time-series, the minimum date of extracted data.

date_end

class Date. If the raster dataset is a daily or monthly time-series, the maximum date of extracted data.

years

numeric. If the raster dataset is an annual time-series, the years of data requested.

catalog_id

character. Alternative method of specifying which dataset to sample. NOT IMPLEMENTED YET.

url_template

character. Alternative method of specifying whic dataset to sample. NOT IMPLEMENTED YET.

bind

logical. Should the extracted data be bound to the inputs? If not, a dataset is returned with the ID field in common with input data.

return_type

character. Class of the output. If return_type = 'SpatVector', retains geometry (as class terra::SpatVector). If return_type = 'sf' then also retains geometry as a Simple Features object (class sf::sf). If return_type = 'DataFrame' returns an ordinary data frame.

method

Method for extracting values ("simple" or "bilinear"). With "simple" values for the cell a point falls in are returned. With "bilinear" the returned values are interpolated from the values of the four nearest raster cells. Ignored if locations represent lines or polygons.

sum_fun

character or function. Function to use to summarize raster cells that overlap input features. Ignored if extracting by point. If NULL, and locations represent lines or polygons, the function returns all cell values.

verbose

logical. Should the function print messages about the process?

...

other arguments to pass along to terra::Extract()

Value

a data.frame or SpatVector with extracted data. Each layer in the raster dataset is a column in the returned data.

Examples


## Loads a raster.
sdp_rast <- sdp_get_raster("R4D004",date_start=as.Date("2021-11-02"),date_end=as.Date("2021-11-03"))
#> [1] "Returning daily dataset with 2 layers..."

## Sampling locations.
location_pts <- data.frame(SiteName=c("Roaring Judy","Gothic","Galena Lake"),
                          Lat=c(38.716995,38.958446,39.021644),
                          Lon=c(-106.853186,-106.988934,-107.072569))
location_sv <- terra::vect(location_pts,geom=c("Lon","Lat"),crs="EPSG:4327")

## Extract data for sampling locations.
sdp_extr_sv <- sdp_extract_data(sdp_rast,location_sv,return_spatvector=TRUE)
#> [1] "Re-projecting locations to coordinate system of the raster."
#> [1] "Extracting data at 3 locations for 2 raster layers."
#> [1] "Extraction complete."
sdp_extr_sv
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 3, 4  (geometries, attributes)
#>  extent      : 320579.1, 338885.9, 4287002, 4321222  (xmin, xmax, ymin, ymax)
#>  coord. ref. : WGS 84 / UTM zone 13N (EPSG:32613) 
#>  names       :     SiteName    ID 2021-11-02 2021-11-03
#>  type        :        <chr> <num>      <num>      <num>
#>  values      : Roaring Judy     1      7.938      9.894
#>                      Gothic     2      3.788      7.493
#>                 Galena Lake     3     0.1095      1.442

## Can also return a data frame.
sdp_extr_df <- sdp_extract_data(sdp_rast,location_sv,return_spatvector=FALSE)
#> [1] "Re-projecting locations to coordinate system of the raster."
#> [1] "Extracting data at 3 locations for 2 raster layers."
#> [1] "Extraction complete."
sdp_extr_df
#>  class       : SpatVector 
#>  geometry    : points 
#>  dimensions  : 3, 4  (geometries, attributes)
#>  extent      : 320579.1, 338885.9, 4287002, 4321222  (xmin, xmax, ymin, ymax)
#>  coord. ref. : WGS 84 / UTM zone 13N (EPSG:32613) 
#>  names       :     SiteName    ID 2021-11-02 2021-11-03
#>  type        :        <chr> <num>      <num>      <num>
#>  values      : Roaring Judy     1      7.938      9.894
#>                      Gothic     2      3.788      7.493
#>                 Galena Lake     3     0.1095      1.442