Extract values from a ee$Image or ImageCollection at the locations of a geometry object. You can use ee$Geometry$*, ee$Feature, ee$FeatureCollection and sf objects. This function mimicking how extract currently works.

ee_extract(x, y, fun = ee$Reducer$mean(), scale = 1000, sf = FALSE, ...)

Arguments

x

ee$Image or ee$ImageCollection with a single band.

y

ee$Geometry$*, ee$Feature, ee$FeatureCollection or sf objects.

fun

ee$Reducer object. Function to summarize the values. The function must take a single numeric value as an argument and return a single value. See details.

scale

A nominal scale in meters of the Image projection to work in. By default 1000.

sf

Logical. Should the extracted values be added to the data.frame of the sf object y?

...

reduceRegions additional parameters. See ee_help(ee$Image$reduceRegions) for more details.

Value

A data.frame or an sf object depending on the sf argument. The columns receive their name from the image metadata property RGEE_NAME. If it is not defined ee_extract use the band name (ee$Image$name) if x is an ee$Image and the system:index property if x is an ee$ImageCollection.

Details

The reducer functions that return one value are:

  • allNonZero: Returns a Reducer that returns 1 if all of its inputs are non-zero, 0 otherwise.

  • anyNonZero: Returns a Reducer that returns 1 if any of its inputs are non-zero, 0 otherwise.

  • bitwiseAnd: Returns a Reducer that computes the bitwise-and summation of its inputs.

  • bitwiseOr: Returns a Reducer that computes the bitwise-or summation of its inputs.

  • count: Returns a Reducer that computes the number of non-null inputs.

  • first: Returns a Reducer that returns the first of its inputs.

  • firstNonNull: Returns a Reducer that returns the first of its non-null inputs.

  • kurtosis: Returns a Reducer that Computes the kurtosis of its inputs.

  • last: Returns a Reducer that returns the last of its inputs.

  • lastNonNull: Returns a Reducer that returns the last of its non-null inputs.

  • max: Creates a reducer that outputs the maximum value of its (first) input. If numInputs is greater than one, also outputs the corresponding values of the additional inputs.

  • mean: Returns a Reducer that computes the (weighted) arithmetic mean of its inputs.

  • median: Create a reducer that will compute the median of the inputs. For small numbers of inputs (up to maxRaw) the median will be computed directly; for larger numbers of inputs the median will be derived from a histogram.

  • min: Creates a reducer that outputs the minimum value of its (first) input. If numInputs is greater than one, also outputs additional inputs.

  • mode: Create a reducer that will compute the mode of the inputs. For small numbers of inputs (up to maxRaw) the mode will be computed directly; for larger numbers of inputs the mode will be derived from a histogram.

  • product: Returns a Reducer that computes the product of its inputs.

  • sampleStdDev: Returns a Reducer that computes the sample standard deviation of its inputs.

  • sampleVariance: Returns a Reducer that computes the sample variance of its inputs.

  • stdDev: Returns a Reducer that computes the standard deviation of its inputs.

  • sum: Returns a Reducer that computes the (weighted) sum of its inputs.

  • variance: Returns a Reducer that computes the variance of its inputs.

Examples

if (FALSE) {
library(rgee)
library(sf)

ee_Initialize()

# Define a Image or ImageCollection: Terraclimate
terraclimate <- ee$ImageCollection("IDAHO_EPSCOR/TERRACLIMATE")$
  filterDate("2001-01-01", "2002-01-01")$
  map(function(x){
    date <- ee$Date(x$get("system:time_start"))$format('YYYY_MM_dd')
    name <- ee$String$cat("Terraclimate_pp_", date)
    x$select("pr")$reproject("EPSG:4326")$set("RGEE_NAME", name)
  })

# Define a geometry
nc <- st_read(
  dsn = system.file("shape/nc.shp", package = "sf"),
  stringsAsFactors = FALSE,
  quiet = TRUE
)

# Extract values
ee_nc_rain <- ee_extract(
  x = terraclimate,
  y = nc,
  scale = 250,
  fun = ee$Reducer$mean(),
  sf = TRUE
)

# Spatial plot
plot(
  ee_nc_rain["Terraclimate_pp_2001_01_01"],
  main = "2001 Jan Precipitation - Terraclimate",
  reset = FALSE
)
}