r-spatial / rgee

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

Inconsistency with names using ee_imagecollection_to_local() #254

Closed bmaitner closed 2 years ago

bmaitner commented 2 years ago

At submit an issue, please attached the following information of your rgee session:

library(rgee)

# Initialize the Earth Engine module.
ee_Initialize()

# Print metadata for a DEM dataset.
print(ee$Image('USGS/SRTMGL1_003')$getInfo())

Attach your Python (reticulate) configuration:

library(reticulate)
py_config()

Description

When running ee_imagecollection_to_local(), the default filename format differs depending on the number of images in the collection. For example, when downloading several MODIS NDVI layers, the filenames will be of the format "YYYY_MM_DD.tif". However, when there is a single image in the collection, the filename will be ".tif". It would be great if the filenames were handled consistently.

What I Did

csaybar commented 2 years ago

Hi, @bmaitner can you share with us a reproducible example. Thanks :)

bmaitner commented 2 years ago

Sure thing, apologies for that, I should have thought ahead and included one. See below :)

library(rgee)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

ee_Initialize()
#> ── rgee 1.1.2 ─────────────────────────────────────── earthengine-api 0.1.305 ── 
#>  ✓ user: not_defined
#>  ✓ Initializing Google Earth Engine: ✓ Initializing Google Earth Engine:  DONE!
#>  ✓ Earth Engine account: users/maitner 
#> ────────────────────────────────────────────────────────────────────────────────

#Create two temporary directories
  dir.create("temp_1")
  dir.create("temp_2")

# Load the image collection (omitting the cleaning for the sake of the example)
  modis_ndvi <- ee$ImageCollection("MODIS/006/MOD13A1") #500 m
  modis_ndvi <- modis_ndvi$select("NDVI")

# Specify a regon
  domain <- ee$Geometry$Rectangle(16.3449768409, -34.8191663551, 32.830120477, -22.0913127581)

#Example 1: Downloading 2 layers
  info <- modis_ndvi$getInfo()
  dates <- unlist(lapply(info$features,FUN = function(x){x$properties$`system:index`}))

  modis_ndvi_ex1 <- modis_ndvi$filterDate(start = paste(as_date(dates[31]),sep = ""),
                                              opt_end = paste(as_date(dates[33]),sep = "") )

  ee_imagecollection_to_local(ic = modis_ndvi_ex1,
                              region = domain,
                              dsn = "temp_1/")
#> ───────────────────────────────────── Downloading ImageCollection - via drive ──
#> Registered S3 method overwritten by 'geojsonsf':
#>   method        from   
#>   print.geojson geojson
#> - region parameters
#>  sfg      : POLYGON ((16.34498 -34.8191 .... .09131, 16.34498 -34.81917)) 
#>  CRS      : GEOGCRS["WGS 84",
#>     DATUM["World Geodetic System 1984",
#>         ELLIPSOID["WGS 84",6378137,298.257223563, ..... 
#>  geodesic : TRUE 
#>  evenOdd  : TRUE 
#> 
#> Downloading: temp_1/2001_06_10.tif
#> 
#> NOTE: Google Drive credentials were not loaded. Running ee_Initialize(user = 'ndef', drive = TRUE) to fix.
#> File downloaded:
#> • '2001_06_10_2022_04_18_22_33_54.tif' <id: 172ENf2Yu1PC_hI9R5TOr_fBd4DHVgtQ9>
#> Saved locally as:
#> • 'temp_1/2001_06_10.tif'
#> 
#> Downloading: temp_1/2001_06_26.tif
#> File downloaded:
#> • '2001_06_26_2022_04_18_22_39_14.tif' <id: 1Z4_z8yYwMjdIF-ok2KLuqV_85sx6RSt3>
#> Saved locally as:
#> • 'temp_1/2001_06_26.tif'
#> 
#>  ────────────────────────────────────────────────────────────────────────────────
#> [[1]]
#> [[1]]$dsn
#> [1] "/home/rstudio/emma_envdata/scratch_code/temp_1/2001_06_10.tif"
#> 
#> [[1]]$metadata
#> [[1]]$metadata$ee_id
#> [1] "V2MB6HGAEIL4ECLRBM2NQ2C5"
#> 
#> [[1]]$metadata$drive_name
#> [1] "2001_06_10_2022_04_18_22_33_54.tif"
#> 
#> [[1]]$metadata$drive_id
#> <drive_id[1]>
#> [1] 172ENf2Yu1PC_hI9R5TOr_fBd4DHVgtQ9
#> 
#> [[1]]$metadata$drive_download_link
#> [1] "https://drive.google.com/uc?id=172ENf2Yu1PC_hI9R5TOr_fBd4DHVgtQ9&export=download"
#> 
#> 
#> 
#> [[2]]
#> [[2]]$dsn
#> [1] "/home/rstudio/emma_envdata/scratch_code/temp_1/2001_06_26.tif"
#> 
#> [[2]]$metadata
#> [[2]]$metadata$ee_id
#> [1] "4V7GMAJOMGWYJAYI67UKTOEM"
#> 
#> [[2]]$metadata$drive_name
#> [1] "2001_06_26_2022_04_18_22_39_14.tif"
#> 
#> [[2]]$metadata$drive_id
#> <drive_id[1]>
#> [1] 1Z4_z8yYwMjdIF-ok2KLuqV_85sx6RSt3
#> 
#> [[2]]$metadata$drive_download_link
#> [1] "https://drive.google.com/uc?id=1Z4_z8yYwMjdIF-ok2KLuqV_85sx6RSt3&export=download"

#Example 2: Downloading 1 layers
  modis_ndvi_ex2 <- modis_ndvi$filterDate(start = paste(as_date(dates[31]),sep = ""),
                                          opt_end = paste(as_date(dates[32]),sep = "") )

  ee_imagecollection_to_local(ic = modis_ndvi_ex2,
                              region = domain,
                              dsn = "temp_2/")
#> ───────────────────────────────────── Downloading ImageCollection - via drive ──- region parameters
#>  sfg      : POLYGON ((16.34498 -34.8191 .... .09131, 16.34498 -34.81917)) 
#>  CRS      : GEOGCRS["WGS 84",
#>     DATUM["World Geodetic System 1984",
#>         ELLIPSOID["WGS 84",6378137,298.257223563, ..... 
#>  geodesic : TRUE 
#>  evenOdd  : TRUE 
#> 
#> Downloading: temp_2/.tif
#> File downloaded:
#> • '_2022_04_18_22_44_14.tif' <id: 1K_Ndbq8-pzrZfHNNlvJgije_yVQJJ6MD>
#> Saved locally as:
#> • 'temp_2/.tif'
#> 
#>  ────────────────────────────────────────────────────────────────────────────────
#> [[1]]
#> [[1]]$dsn
#> [1] "/home/rstudio/emma_envdata/scratch_code/temp_2/.tif"
#> 
#> [[1]]$metadata
#> [[1]]$metadata$ee_id
#> [1] "DOECKJEDYV3ZGDN36LLKGSVK"
#> 
#> [[1]]$metadata$drive_name
#> [1] "_2022_04_18_22_44_14.tif"
#> 
#> [[1]]$metadata$drive_id
#> <drive_id[1]>
#> [1] 1K_Ndbq8-pzrZfHNNlvJgije_yVQJJ6MD
#> 
#> [[1]]$metadata$drive_download_link
#> [1] "https://drive.google.com/uc?id=1K_Ndbq8-pzrZfHNNlvJgije_yVQJJ6MD&export=download"

#Check the file names
  list.files("temp_1/",all.files = TRUE,pattern = ".tif") #2 files: "2001_06_10.tif" "2001_06_26.tif"
#> [1] "2001_06_10.tif" "2001_06_26.tif"
  list.files("temp_2/",all.files = TRUE,pattern = ".tif") #1 file: ".tif"
#> [1] ".tif"

Created on 2022-04-18 by the reprex package (v2.0.1)

csaybar commented 2 years ago

Hi @bmaitner sorry for the late reply,

I added a message to prevent this issue.

image