rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
541 stars 89 forks source link

Passing gdal config options to rast #608

Closed ebolch closed 2 years ago

ebolch commented 2 years ago

I'm trying to use rast to access NASA Earthdata cloud optimized geotiffs. A NASA Earthdata account and a netrc file with login and password are required(Earthdata netrc setup script).

I've been trying the following, but seem to be getting an authentication error:

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, opts=c("GDAL_HTTP_UNSAFESSL=YES",
                                      "GDAL_HTTP_COOKIEFILE=.rcookies",
                                      "GDAL_HTTP_COOKIEJAR=.rcookies",
                                      "GDAL_DISABLE_READDIR_ON_OPEN=EMPTY_DIR",
                                      "CPL_VSIL_CURL_ALLOWED_EXTENSIONS=TIF"))

I am able to do this successfully with the raster and rgdal packages:

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)

Am I just passing on the gdal config options incorrectly? Any help would be appreciated.

rhijmans commented 2 years ago

Thank you. The GDAL configuration options were not exposed by terra. On linux you could set them with rgdal, as GDAL is a shared library, but on windows each R package has its own copy. I now get:

library(terra)
#terra 1.5.26

## options not set
getGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE"))
# GDAL_HTTP_UNSAFESSL GDAL_HTTP_COOKIEFILE 
#                  ""                   "" 

## set options as "option=value"
setGDALconfig(c("GDAL_HTTP_UNSAFESSL=YES", 
         "GDAL_HTTP_COOKIEFILE=.rcookies",
          "GDAL_HTTP_COOKIEJAR=.rcookies", 
          "GDAL_DISABLE_READDIR_ON_OPEN=EMPTY_DIR",
          "CPL_VSIL_CURL_ALLOWED_EXTENSIONS=TIF"))

## options have been set
getGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE", "GDAL_HTTP_COOKIEJAR"))
# GDAL_HTTP_UNSAFESSL GDAL_HTTP_COOKIEFILE  GDAL_HTTP_COOKIEJAR 
#               "YES"          ".rcookies"          ".rcookies" 

## reset to default
setGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE", "GDAL_HTTP_COOKIEJAR"),  c("", "", ""))

getGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE", "GDAL_HTTP_COOKIEJAR"))
# GDAL_HTTP_UNSAFESSL GDAL_HTTP_COOKIEFILE  GDAL_HTTP_COOKIEJAR 
#                  ""                   ""                   "" 

## set options as "options, values"
setGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE"),  c("YES", ".rcookies"))

getGDALconfig(c("GDAL_HTTP_UNSAFESSL", "GDAL_HTTP_COOKIEFILE", "GDAL_HTTP_COOKIEJAR"))
# GDAL_HTTP_UNSAFESSL GDAL_HTTP_COOKIEFILE  GDAL_HTTP_COOKIEJAR 
#               "YES"          ".rcookies"                   ""