Skip to content

Commit

Permalink
Merge pull request #86 from r-spatial/rgeev.1.0.3
Browse files Browse the repository at this point in the history
rgee v.1.0.3
  • Loading branch information
csaybar committed Aug 4, 2020
2 parents 2c89e24 + 1a0c0ce commit 0d6c1ba
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 56 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rgee
Title: R Bindings for Calling the 'Earth Engine' API
Version: 1.0.2
Version: 1.0.3
Authors@R:
c(person(given = "Cesar",
family = "Aybar",
Expand Down Expand Up @@ -89,7 +89,6 @@ Imports:
methods,
reticulate (>= 1.15),
magrittr,
getPass,
crayon,
cli
Suggests:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ importFrom(crayon,green)
importFrom(crayon,red)
importFrom(crayon,white)
importFrom(crayon,yellow)
importFrom(getPass,getPass)
importFrom(magrittr,"%>%")
importFrom(methods,as)
importFrom(methods,is)
Expand Down
85 changes: 67 additions & 18 deletions R/ee_Initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#' @param gcs Logical (optional). If TRUE, the Google Cloud Storage
#' credential will be cached in the path \code{~/.config/earthengine/}.
#'
#' @param display Logical. If TRUE display the earthengine authentification URL.
#'
#' @param quiet Logical. Suppress info messages.
#'
#' @importFrom utils read.table browseURL write.table packageVersion
#' @importFrom reticulate import_from_path import install_miniconda py_available
#' @importFrom getPass getPass
#' @importFrom cli symbol rule
#' @importFrom crayon blue green black red bold white
#'
Expand Down Expand Up @@ -56,6 +57,7 @@
ee_Initialize <- function(email = NULL,
drive = FALSE,
gcs = FALSE,
display = FALSE,
quiet = FALSE) {
# Message for new user
init_rgee_message <- ee_search_init_message()
Expand Down Expand Up @@ -104,7 +106,16 @@ ee_Initialize <- function(email = NULL,

# get the path of earth engine credentials
ee_current_version <- system.file("python/ee_utils.py", package = "rgee")
ee_utils <- ee_source_python(ee_current_version)
ee_utils <- try(ee_source_python(ee_current_version), silent = TRUE)
# counter added to prevent problems with reticulate
con_reticulate_counter <- 0
while (any(class(ee_utils) %in% "try-error") & con_reticulate_counter < 3) {
ee_utils <- try(ee_source_python(ee_current_version), silent = TRUE)
con_reticulate_counter <- con_reticulate_counter + 1
if (con_reticulate_counter == 2) {
stop("reticulate refuse to connect with rgee")
}
}
earthengine_version <- ee_utils_py_to_r(ee_utils$ee_getversion())

if (!quiet) {
Expand Down Expand Up @@ -161,6 +172,12 @@ ee_Initialize <- function(email = NULL,
gcs_credentials <- list(path = NA, message = NA)

if (drive) {
if (!requireNamespace("googledrive", quietly = TRUE)) {
stop("The googledrive package is not installed. Try",
' install.packages("googledrive")',
call. = FALSE
)
}
if (!quiet) {
cat(
"",
Expand All @@ -181,6 +198,12 @@ ee_Initialize <- function(email = NULL,
}

if (gcs) {
if (!requireNamespace("googleCloudStorageR", quietly = TRUE)) {
stop("The googleCloudStorageR package is not installed. Try",
' install.packages("googleCloudStorageR")',
call. = FALSE
)
}
if (!quiet) {
cat(
"",
Expand Down Expand Up @@ -222,7 +245,8 @@ ee_Initialize <- function(email = NULL,
blue("Initializing Google Earth Engine:")
)
}
ee_create_credentials_earthengine(email_clean)

ee_create_credentials_earthengine(email_clean, display = display)
ee$Initialize()

if (!quiet) {
Expand Down Expand Up @@ -292,7 +316,7 @@ ee_Initialize <- function(email = NULL,
#' where they can be automatically refreshed, as necessary.
#' }
#' @noRd
ee_create_credentials_earthengine <- function(email_clean) {
ee_create_credentials_earthengine <- function(email_clean, display) {
oauth_func_path <- system.file("python/ee_utils.py", package = "rgee")
utils_py <- ee_source_python(oauth_func_path)

Expand All @@ -317,15 +341,31 @@ ee_create_credentials_earthengine <- function(email_clean) {
code_verifier <- oauth_codes[[1]]
code_challenge <- oauth_codes[[2]]
earthengine_auth <- ee$oauth$get_authorization_url(code_challenge)
browseURL(earthengine_auth)
auth_code <- getPass("Enter Earth Engine Authentication: ")
token <- ee$oauth$request_token(auth_code, code_verifier)
credential <- sprintf('{"refresh_token":"%s"}', token)
write(credential, main_ee_credential)
write(credential, user_ee_credential)
# Display URL?
if (display) {
message("\n", earthengine_auth)
}
ee_save_eecredentials(
url = earthengine_auth,
code_verifier = code_verifier,
main_ee_credential = main_ee_credential,
user_ee_credential = user_ee_credential
)
invisible(TRUE)
}
}

#' Save EE credentials
#' @noRd
ee_save_eecredentials <- function(url, code_verifier, main_ee_credential, user_ee_credential) {
browseURL(url)
auth_code <- readline("Enter Earth Engine Authentication: ")
token <- ee$oauth$request_token(auth_code, code_verifier)
credential <- sprintf('{"refresh_token":"%s"}', token)
write(credential, main_ee_credential)
write(credential, user_ee_credential)
}

#' Create credentials - Google Drive
#' @noRd
ee_create_credentials_drive <- function(email) {
Expand Down Expand Up @@ -452,7 +492,7 @@ ee_create_credentials_gcs <- function(email) {
ee_users <- function(quiet = FALSE) {
#space among columns
wsc <- " "
title <- c('user', ' EE', ' GD', ' GCS')
title <- c('user', ' EE', ' GCS', ' GD')

oauth_func_path <- system.file("python/ee_utils.py", package = "rgee")
utils_py <- ee_source_python(oauth_func_path)
Expand Down Expand Up @@ -532,6 +572,10 @@ ee_user_info <- function(quiet = FALSE) {

# google drive
gd <- user_session_list[grepl("@gmail.com", user_session_list)]
if (length(gd) == 0) {
gd <- "NOT FOUND"
}

if (!quiet) {
cat(blue$bold('\nGoogle Drive Credentials:'))
cat("\n - ", basename(gd))
Expand All @@ -540,20 +584,25 @@ ee_user_info <- function(quiet = FALSE) {

# google cloud storage
gcs <- user_session_list[grepl(".json", user_session_list)]
if (length(gcs) == 0) {
gcs <- "NOT FOUND"
}

if (!quiet) {
cat(blue$bold('\nGoogle Cloud Storage Credentials:'))
cat("\n - ",basename(gcs))
cat("\n", rule(), "\n")
}
ee_user <- ee_exist_credentials()

if (isFALSE(grepl(email_drive, ee_user$email)) & ee_user$email != "ndef") {
message(
"\nNOTE: Google Drive credential does not match with your Google",
" Earth Engine credentials. All functions which depend on Google",
" Drive will not work (e.g. ee_image_to_drive)."
)
}
# if (isFALSE(grepl(email_drive, ee_user$email)) & ee_user$email != "ndef") {
# message(
# "\nNOTE: Google Drive credential does not match with your Google",
# " Earth Engine credentials. All functions which depend on Google",
# " Drive will not work (e.g. ee_image_to_drive)."
# )
# }

ee_check_python_packages(quiet = TRUE)
invisible(TRUE)
}
Expand Down
70 changes: 37 additions & 33 deletions R/ee_help.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,44 @@ ee_help <- function(eeobject, browser = FALSE) {
ee_functions <- eequery_scope[search_funnames]
fun_name <- paste0("ee$",gsub("\\.","$",tail(ee_functions,1)))
} else {
wrap_lhs <- function(x) gsub("rgee", "", ee_get_lhs())
fun_name <- wrap_lhs(eeobject)
if (length(fun_name) == 0) {
fun_name <- deparse(substitute(eeobject))
}
if (is.character(eeobject)) {
fun_name <- eeobject
} else {
wrap_lhs <- function(x) gsub("rgee", "", ee_get_lhs())
fun_name <- wrap_lhs(eeobject)
if (length(fun_name) == 0) {
fun_name <- deparse(substitute(eeobject))
}

if (is.null(eequery_scope)) {
components <- strsplit(fun_name, "\\$")[[1]]
topic <- components[[length(components)]]
source <- paste(components[1:(length(components) - 1)],
collapse = "$")
# The name is a base function?
is_a_basefunction <- tryCatch(
expr = {eval(parse(text = sprintf("base::%s", fun_name))); TRUE},
error = function(e) FALSE
)
if (isTRUE(is_a_basefunction)) {
stop(
"'", fun_name, "' is not subsettable. Are you using a ",
"function name that matches the names of the R base",
" library?. If 'base::", fun_name, "' exists ee_help will not work."
if (is.null(eequery_scope)) {
components <- strsplit(fun_name, "\\$")[[1]]
topic <- components[[length(components)]]
source <- paste(components[1:(length(components) - 1)],
collapse = "$")
# The name is a base function?
is_a_basefunction <- tryCatch(
expr = {eval(parse(text = sprintf("base::%s", fun_name))); TRUE},
error = function(e) FALSE
)
}
if (topic == source) {
fun_name <- topic
} else {
# Remove just the last parenthesis
extract_parenthesis_text <- gregexpr("(?=\\().*?(?<=\\))",
topic,
perl = TRUE)
parenthesis_text <- regmatches(topic, extract_parenthesis_text)[[1]]
to_display <- gsub(parenthesis_text, "", topic, fixed = TRUE)
to_display <- gsub("\\(|\\)", "", to_display)
fun_name <- paste(source,to_display,sep = "$")
if (isTRUE(is_a_basefunction)) {
stop(
"'", fun_name, "' is not subsettable. Are you using a ",
"function name that matches the names of the R base",
" library?. If 'base::", fun_name, "' exists ee_help will not work."
)
}
if (topic == source) {
fun_name <- topic
} else {
# Remove just the last parenthesis
extract_parenthesis_text <- gregexpr("(?=\\().*?(?<=\\))",
topic,
perl = TRUE)
parenthesis_text <- regmatches(topic, extract_parenthesis_text)[[1]]
to_display <- gsub(parenthesis_text, "", topic, fixed = TRUE)
to_display <- gsub("\\(|\\)", "", to_display)
fun_name <- paste(source,to_display,sep = "$")
}
}
}
}
Expand Down Expand Up @@ -116,7 +120,7 @@ ee_help <- function(eeobject, browser = FALSE) {
close(fileConn)
browseURL(temp_file)
}
invisible(TRUE)
invisible(temp_file)
}


Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<a href="https://colab.research.google.com/github/r-spatial/rgee/blob/examples/rgee_colab.ipynb"><img align="left" src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab" title="Open and Execute in Google Colaboratory"></a>
[![R build status](https://github.com/r-spatial/rgee/workflows/R-CMD-check/badge.svg)](https://github.com/r-spatial/rgee/actions)
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
Expand Down Expand Up @@ -110,7 +111,11 @@ Install the `rgee` package from GitHub is quite simple, you just have to run in
remotes::install_github("r-spatial/rgee")
```

**`rgee` depends on [sf](https://github.com/r-spatial/sf)**. Therefore, is necessary to install its external libraries, follow the installation steps specified [here](https://github.com/r-spatial/sf#installing).
**`rgee` depends on [sf](https://github.com/r-spatial/sf)**. Therefore, is necessary to install its external libraries, follow the installation steps specified [here](https://github.com/r-spatial/sf#installing). If you are using a Debian-based operating system, you probably need to install **virtualenv** as well. If you are using a Debian-based operating system, you probably need to install virtualenv as well.

```
sudo pip3 install virtualenv
```

#### Docker image

Expand Down
10 changes: 9 additions & 1 deletion man/ee_Initialize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d6c1ba

Please sign in to comment.