Learn R Programming

rgee (version 0.2.0)

ee_download_gcs: Move EE results from Google Cloud Storage to a local directory

Description

Move results of an EE task saved in Google Cloud Storage to a local directory.

Usage

ee_download_gcs(task, filename, overwrite = FALSE,
  GCS_AUTH_FILE = getOption("rgee.gcs.auth"), quiet = TRUE)

Arguments

task

List generated after finished correctly a EE task. See details.

filename

Output filename.

overwrite

A boolean indicating whether "filename" should be overwritten.

GCS_AUTH_FILE

Authentication json file you have downloaded from your Google Cloud Project

quiet

Logical. Suppress info message

Details

The best way to use rgee::ee_download_gcs is save the Google Cloud Project JSON file into ee_get_earthengine_path() with the name GCS_AUTH_FILE.json. It is necessary in order to attain that rgee can read the credentials automatically.

The task argument needs "COMPLETED" task state to work, since the parameters necessaries to locate the file into google cloud storage are obtained from ee$batch$Export$*$toCloudStorage(...)$start()$status().

Examples

Run this code
# NOT RUN {
library(rgee)
library(stars)
library(sf)

ee_reattach()  # reattach ee as a reserved word
ee_Initialize()
ee_user_info()

# Communal Reserve Amarakaeri - Peru
xmin <- -71.132591318
xmax <- -70.953664315
ymin <- -12.892451233
ymax <- -12.731116372
x_mean <- (xmin + xmax) / 2
y_mean <- (ymin + ymax) / 2

ROI <- c(xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax, xmin, ymin)
ROI_polygon <- matrix(ROI, ncol = 2, byrow = TRUE) %>%
  list() %>%
  st_polygon() %>%
  st_sfc() %>%
  st_set_crs(4326)
ee_geom <- sf_as_ee(ROI_polygon)

# Get the mean annual NDVI for 2011
cloudMaskL457 <- function(image) {
  qa <- image$select("pixel_qa")
  cloud <- qa$bitwiseAnd(32L)$
    And(qa$bitwiseAnd(128L))$
    Or(qa$bitwiseAnd(8L))
  mask2 <- image$mask()$reduce(ee$Reducer$min())
  image <- image$updateMask(cloud$Not())$updateMask(mask2)
  image$normalizedDifference(list("B4", "B3"))
}

ic_l5 <- ee$ImageCollection("LANDSAT/LT05/C01/T1_SR")$
  filterBounds(ee_geom)$
  filterDate("2011-01-01", "2011-12-31")$
  map(cloudMaskL457)
mean_l5 <- ic_l5$mean()$rename("NDVI")
mean_l5 <- mean_l5$reproject(crs = "EPSG:4326", scale = 500)
mean_l5_Amarakaeri <- mean_l5$clip(ee_geom)

# Download a EE Image
task_img <- ee$batch$Export$image$toCloudStorage(
  image = mean_l5_Amarakaeri,
  bucket = "rgee_dev",
  fileFormat = "GEO_TIFF",
  fileNamePrefix = "my_image"
)
task_img$start()
ee_monitoring(task_img)
img <- ee_download_gcs(task_img)
plot(img)

# Download a EE FeatureCollection
amk_fc <- ee$FeatureCollection(
  list(ee$Feature(ee_geom, list(name = "Amarakaeri")))
)

task_vector <- ee$batch$Export$table$toCloudStorage(
  collection = amk_fc,
  bucket = "rgee_dev",
  fileFormat = "SHP",
  fileNamePrefix = "geom_Amarakaeri"
)
task_vector$start()
ee_monitoring(task_vector) # optional
amk_geom <- ee_download_gcs(task = task_vector)
plot(amk_geom$geometry, border = "red", lwd = 10)
# }

Run the code above in your browser using DataLab