Skip to content

Commit 99f120e

Browse files
Merge pull request #1162 from M3nin0/feature/netrc-handling
2 parents d36dfa2 + 78cac6b commit 99f120e

6 files changed

+69
-23
lines changed

R/api_check.R

+48
Original file line numberDiff line numberDiff line change
@@ -2398,3 +2398,51 @@
23982398
.check_set_caller(".check_linkage_method")
23992399
.check_that(linkage %in% .conf("dendro_linkage"))
24002400
}
2401+
#' @title Check netrc file
2402+
#' @description
2403+
#' Check if netrc file exists and if its content is correct
2404+
#' @param attributes Attributes required from the netrc file
2405+
#' @return Called for side effects
2406+
#' @keywords internal
2407+
#' @noRd
2408+
.check_netrc_gdal <- function(attributes) {
2409+
.check_set_caller(".check_netrc_gdal")
2410+
# define if the current GDAL version is reading netrc from env variable
2411+
is_gdal_reading_netrc <- .gdal_version() >= "3.7.0"
2412+
# define from where `netrc` file must be loaded
2413+
# case 1 - gdal environment variable (requires GDAL >= 3.7.0)
2414+
netrc_from_var <- ifelse(
2415+
is_gdal_reading_netrc,
2416+
Sys.getenv("GDAL_HTTP_NETRC_FILE", unset = NA),
2417+
NA
2418+
)
2419+
# case 2 - netrc file stored in user home directory
2420+
netrc_from_home <- ifelse(
2421+
.Platform[["OS.type"]] == "windows",
2422+
.conf("gdal_netrc_file_path_win"),
2423+
.conf("gdal_netrc_file_path")
2424+
)
2425+
# define which netrc file will be used
2426+
netrc_file <- ifelse(
2427+
is.na(netrc_from_var),
2428+
netrc_from_home,
2429+
netrc_from_var
2430+
)
2431+
# if the env variable is used, then, warning users not to set the GDAL
2432+
# variable using `Sys.setenv`
2433+
if (!is.na(netrc_from_var)) {
2434+
warning(.conf("messages", ".check_netrc_gdal_var"))
2435+
}
2436+
# check if file exist
2437+
.check_that(file.exists(netrc_file))
2438+
# load netrc content
2439+
netrc_content <- readLines(netrc_file)
2440+
# check netrc file content
2441+
.check_that(
2442+
any(
2443+
purrr::map_lgl(netrc_content, function(x) {
2444+
stringr::str_detect(x, attributes)
2445+
})
2446+
)
2447+
)
2448+
}

R/api_gdal.R

+7
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,10 @@
346346
)
347347
return(invisible(out_file))
348348
}
349+
#' @title Get GDAL Version
350+
#' @noRd
351+
#' @returns GDAL Version
352+
.gdal_version <- function() {
353+
sf_versions <- sf::sf_extSoftVersion()
354+
sf_versions[["GDAL"]]
355+
}

R/api_source_hls.R

+5-19
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
tiles = NULL) {
1818
# set caller to show in errors
1919
.check_set_caller(".source_items_new_hls_cube")
20-
# NASA EarthData requires a login/password combination
21-
if (.Platform[["OS.type"]] == "windows")
22-
netrc_path <- .conf("HLS_NETRC_FILE_PATH_WIN")
23-
else
24-
netrc_path <- .conf("HLS_NETRC_FILE_PATH")
25-
.check_that(file.exists(netrc_path))
20+
# check netrc file
21+
suppressWarnings(.check_netrc_gdal(attributes = .conf("HLS_ACCESS_URL")))
2622
# convert tiles to a valid STAC query
2723
if (!is.null(tiles)) {
2824
roi <- .s2_mgrs_to_roi(tiles)
@@ -81,18 +77,8 @@
8177
#' @return Called for side effects
8278
.source_configure_access.hls_cube <- function(source, collection = NULL) {
8379
.check_set_caller(".source_configure_access_hls_cube")
84-
# NASA EarthData requires a login/password combination
85-
if (.Platform[["OS.type"]] == "windows")
86-
netrc_file <- .conf("HLS_NETRC_FILE_PATH_WIN")
87-
else
88-
netrc_file <- .conf("HLS_NETRC_FILE_PATH")
89-
# does the file exist
90-
if (file.exists(netrc_file)) {
91-
conf_hls <- utils::read.delim(netrc_file)
92-
if (!(.conf("HLS_ACCESS_URL") %in% names(conf_hls)))
93-
stop(.conf("messages", ".source_configure_access_hls_cube"))
94-
} else {
95-
stop(.conf("messages", ".source_configure_access_hls_cube"))
96-
}
80+
# check netrc file
81+
.check_netrc_gdal(attributes = .conf("HLS_ACCESS_URL"))
82+
# done!
9783
return(invisible(source))
9884
}

inst/extdata/config_internals.yml

+5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ gdal_creation_options: [ "COMPRESS=LZW", "PREDICTOR=2",
194194
gdalcubes_options: ["COMPRESS=LZW", "PREDICTOR=2",
195195
"BIGTIFF=YES", "BLOCKXSIZE=512",
196196
"BLOCKYSIZE=512"]
197+
198+
# GDAL netrc file
199+
gdal_netrc_file_path: "~/.netrc"
200+
gdal_netrc_file_path_win: "%HOME%\\_netrc"
201+
197202
# gdalcubes parameters
198203
gdalcubes_chunk_size: [1, 2048, 2048]
199204
gdalcubes_type_format: "int16"

inst/extdata/config_messages.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
.check_file_missing: "file does not exist:"
3333
.check_file_writable: "cannot write file to disk"
3434
.check_find_class: "cube is not properly configured"
35+
.check_netrc_gdal: "invalid configuration file\nTo learn more, see instructions in Chapter 4 of the online book"
36+
.check_netrc_gdal_var: "netrc environment variable configuration detected (GDAL_HTTP_NETRC_FILE)\n please, make sure it is available to all R sessions. To learn more, see the instructions in Chapter 4 of the online book"
3537
.check_gpu_memory: "when using GPU: gpu_memory must be informed"
3638
.check_gpu_memory_size: "not enough GPU memory - should have at least 2GB memory free to use"
3739
.check_is_class_cube: "data should be a classified cube - check 'cube' parameter"
@@ -253,7 +255,6 @@
253255
.source_collection_tile_check: "invalid tiles for collection"
254256
.source_configure_access: "unable to access collection - service is unavailable"
255257
.source_configure_access_aws_cube: "a valid AWS_SECRET_ACCESS_KEY is required to access this collection\n if you have one, include it as an enviromental variable"
256-
.source_configure_access_hls_cube: "missing HLS access configuration\n see instructions in Chapter 4 of on-line book"
257258
.source_configure_access_usgs_cube: "a valid AWS_SECRET_ACCESS_KEY is required to access USGS collection\n if you have one, include it as an enviromental variable"
258259
.source_collection_access_test: "enable to access data from the collection"
259260
.source_collection_access_var_set: "invalid access vars for collection"
@@ -265,7 +266,7 @@
265266
.source_items_cube_stac_cube: "error when opening files"
266267
.source_items_fid_stac_cube: "invalid feature id value"
267268
.source_items_new: "search returned no items - check selection parameters."
268-
.source_items_new_hls_cube: "could not find .netrc file\n please configure your access to NASA EarthData"
269+
.source_items_new_hls_cube: "search returned no items - check selection parameters."
269270
.source_items_new_cdse_cube: "search returned no items - check selection parameters."
270271
.source_items_new_mpc_cube_landsat_c2_l2: "when retrieving Landsat collection in MPC searching by tiles is not allowed, use roi"
271272
.source_items_new_mpc_s1_grd: "invalid orbit parameter for MPC S1 GRD collection"

inst/extdata/sources/config_source_hls.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# The parameters enable access to the cloud collections
33

44
HLS_ACCESS_URL: "machine.urs.earthdata.nasa.gov"
5-
HLS_NETRC_FILE_PATH: "~/.netrc"
6-
HLS_NETRC_FILE_PATH_WIN: "%HOME%\\_netrc"
5+
76
sources:
87
HLS :
98
s3_class : ["hls_cube", "stac_cube", "eo_cube",

0 commit comments

Comments
 (0)