r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
677 stars 146 forks source link

Is it possible to pull return multiple statistics columns for the same image? #174

Closed ifeanyi588 closed 2 years ago

ifeanyi588 commented 3 years ago

At the moment, I have a function that is able to compute any statistic based on the an image. Here is the function below:

gee_datapull <- function(email = "ifeanyi.edochie@gmail.com",
                         gee_boundary = "users/ifeanyiedochie/gadm36_CMR_0",
                         gee_polygons = "users/ifeanyiedochie/cmr_polypop_boundary",
                         gee_dataname = "NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG",
                         gee_datestart = "2018-01-01",
                         gee_dateend = "2018-01-31",
                         gee_band = "avg_rad",
                         scale = 100,
                         gee_desc = "nighttimelight_cmr",
                         gee_stat = "mean",
                         gdrive_folder = "/SAEplus",
                         ldrive_dsn = "data/cmr_nighttimelight",
                         gee_crs = 'EPSG:4326'){

ee_users()

options(gargle_oauth_email = email)
  ee_Initialize(email = email)

  agebs <- ee$FeatureCollection(gee_polygons)
  agebs_boundary <- ee$FeatureCollection(gee_boundary)

  s5p_collect <- ee$ImageCollection(gee_dataname)$
    filterBounds(agebs_boundary)$
    filterDate(gee_datestart, gee_dateend)$
    select(gee_band)

  if(gee_stat == "mean"){
    s5p_mean <- s5p_collect$mean()

    s5p_agebs <- s5p_mean$reduceRegions(
      collection =  agebs,
      reducer = ee$Reducer$mean(),
      scale = scale,
      crs = gee_crs
    )
  } else if(gee_stat == "sum"){
    s5p_sum <- s5p_collect$sum()

    s5p_agebs <- s5p_sum$reduceRegions(
      collection = agebs,
      reducer = ee$Reducer$sum(),
      scale = scale,
      crs = gee_crs
    )
  } else if(gee_stat == "stdDev"){
    s5p_stddev <- s5p_collect$stdDev()

    s5p_agebs <- s5p_stddev$reduceRegions(
      collection = agebs,
      reducer = ee$Reducer$stdDev(),
      scale = scale,
      crs = gee_crs
    )
  } else if(gee_stat == "median"){
    s5p_median <- s5p_collect$median()

    s5p_agebs <- s5p_median$reduceRegions(
      collection = agebs,
      reducer = ee$Reducer$median(),
      scale = scale,
      crs = gee_crs
    )
  } else if(gee_stat == "minMax") {
    s5p_minmax <- s5p_collect$minMax()

    s5p_agebs <- s5p_minmax$reduceRegions(
      collection = agebs,
      reducer = ee$Reducer$minMax(),
      scale = scale,
      crs = gee_crs
    )

  }  else {return("gee_stat must be specified as sum or mean, please specify accordingly")}

  task <- ee_table_to_drive(
    collection = s5p_agebs,
    description = gee_desc,
    folder = gdrive_folder,
    fileFormat = "SHP"
  )

  task$start()

  ee_monitoring(task)

  return(ee_drive_to_local(task = task, dsn = ldrive_dsn))

}

However, this will only estimate one statistic at a time. How could I write something that will compute multiple statistics and put it in the same dataframe object?

Thank you for your help

csaybar commented 3 years ago

You can use ee$Reducer$combine