How do I use the terra R package with cloud optimized geotiffs requiring authentication?

Viewed 137

I am currently using the raster package to access NASA Earthdata cloud optimized geotiffs, but want to switch to the terra package. When I try to use terra, I get an error. I think this is because I'm not passing along the necessary GDAL config Options.

When using the raster package I do the following:

library(raster) 

library(rgdal) 

rgdal::setCPLConfigOption(ConfigOption = "GDAL_HTTP_UNSAFESSL", value = "YES") 

rgdal::setCPLConfigOption(ConfigOption = "GDAL_HTTP_COOKIEFILE", value = ".rcookies") 

rgdal::setCPLConfigOption(ConfigOption = "GDAL_HTTP_COOKIEJAR", value = ".rcookies") 

rgdal::setCPLConfigOption(ConfigOption = "GDAL_DISABLE_READDIR_ON_OPEN", value = "EMPTY_DIR") rgdal::setCPLConfigOption(ConfigOption = "CPL_VSIL_CURL_ALLOWED_EXTENSIONS", value = "TIF") 

url <- "/vsicurl/https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T10SEJ.2021214T184919.v2.0/HLS.S30.T10SEJ.2021214T184919.v2.0.B8A.tif" 

r <- raster(url) 

A NASA Earthdata account, a netrc file with login and password, and those GDAL config options are necessary for this to work.

NASA Earthdata Login/Signup: https://urs.earthdata.nasa.gov/

Rscript to create netrc file: https://git.earthdata.nasa.gov/projects/LPDUR/repos/hls_tutorial_r/browse/Scripts/earthdata_netrc_setup.R

If I want to use the terra package to do the same, how do I pass along the necessary GDAL config options? Or is there another problem?

Example using terra:

library(terra) 

url <- "/vsicurl/https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T10SEJ.2021214T184919.v2.0/HLS.S30.T10SEJ.2021214T184919.v2.0.B8A.tif" 

r <- rast(url) 
1 Answers

You can do

library(terra) 
setGDALconfig("GDAL_HTTP_UNSAFESSL", "YES")
setGDALconfig("GDAL_HTTP_COOKIEFILE", ".rcookies") 
setGDALconfig("GDAL_DISABLE_READDIR_ON_OPEN", "EMPTY_DIR")
setGDALconfig("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", "TIF") 

url <- "/vsicurl/https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSS30.020/HLS.S30.T10SEJ.2021214T184919.v2.0/HLS.S30.T10SEJ.2021214T184919.v2.0.B8A.tif" 

r <- rast(url)
Related