Skip to content

Commit

Permalink
Merge branch 'ee_as_rast'
Browse files Browse the repository at this point in the history
  • Loading branch information
csaybar committed Aug 18, 2023
2 parents 7871aad + 3a47248 commit 21d09c3
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 61 deletions.
68 changes: 37 additions & 31 deletions .github/db_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@
import json
import re


def dataset_upgrade():
earth_engine_list = "https://raw.githubusercontent.com/samapriya/Earth-Engine-Datasets-List/master/gee_catalog.json"

with request.urlopen(earth_engine_list) as url:
ee_datasets = json.loads(url.read().decode())

FeatureCollection = OrderedDict()
ImageCollection = OrderedDict()
Image = OrderedDict()

for ee_dataset in ee_datasets:
key_name = re.sub("/", "_", ee_dataset["id"])
if (ee_dataset["type"] == "table") or (ee_dataset["type"] == "table_collection"):
FeatureCollection[key_name] = ee_dataset["id"]
elif ee_dataset["type"] == "image":
Image[key_name] = ee_dataset["id"]
elif ee_dataset["type"] == "image_collection":
ImageCollection[key_name] = ee_dataset["id"]
else:
raise Exception("We expected a type value: image, image_collection or table.")

eeDataset = OrderedDict(
FeatureCollection = FeatureCollection,
ImageCollection = ImageCollection,
Image = Image
)

with open('inst/dataset.json', 'w') as json_file:
json.dump(eeDataset, json_file)

return True
earth_engine_list = "https://raw.githubusercontent.com/samapriya/Earth-Engine-Datasets-List/master/gee_catalog.json"

with request.urlopen(earth_engine_list) as url:
ee_datasets = json.loads(url.read().decode())

FeatureCollection = OrderedDict()
ImageCollection = OrderedDict()
Image = OrderedDict()

for ee_dataset in ee_datasets:
key_name = re.sub("/", "_", ee_dataset["id"])
if (ee_dataset["type"] == "table") or (
ee_dataset["type"] == "table_collection"
):
FeatureCollection[key_name] = ee_dataset["id"]
elif ee_dataset["type"] == "image":
Image[key_name] = ee_dataset["id"]
elif ee_dataset["type"] == "image_collection":
ImageCollection[key_name] = ee_dataset["id"]
else:
raise Exception(
"We expected a type value: image, image_collection or table."
)

eeDataset = OrderedDict(
FeatureCollection=FeatureCollection,
ImageCollection=ImageCollection,
Image=Image,
)

with open("inst/dataset.json", "w") as json_file:
json.dump(eeDataset, json_file)

return True


if __name__ == "__main__":
dataset_upgrade()
dataset_upgrade()
8 changes: 4 additions & 4 deletions .github/ee_up.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import ee

words = [
"#' Earth Engine API version",
"#' Earth Engine API version",
"#'",
"#'",
"#' @family session management functions",
"#' @return Character. Earth Engine Python API version used to build rgee.",
"#' @export",
"ee_version <- function() {",
" '%s'" % (ee.__version__),
"}"
"}",
]

with open("R/ee_version.R", "w") as file:
file.write("\n".join(words))
file.write("\n".join(words))
5 changes: 3 additions & 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.1.6.9999
Version: 1.1.7
Authors@R:
c(person(given = "Cesar",
family = "Aybar",
Expand Down Expand Up @@ -97,7 +97,7 @@ Depends:
R (>= 3.3.0)
Imports:
methods,
reticulate (>= 1.24),
reticulate (>= 1.27),
rstudioapi (>= 0.7),
leaflet (>= 2.0.2),
magrittr,
Expand All @@ -123,6 +123,7 @@ Suggests:
spelling,
testthat,
future,
terra,
covr,
knitr,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ importFrom(reticulate,virtualenv_remove)
importFrom(stats,na.omit)
importFrom(utils,browseURL)
importFrom(utils,download.file)
importFrom(utils,help)
importFrom(utils,menu)
importFrom(utils,packageVersion)
importFrom(utils,read.csv)
Expand Down
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

# rgee 1.1.6.9999
# rgee 1.1.7

- Add Python Path to ee_Initialize.
- Fix a bug in ee_image_info strsplit(code, ":") : object 'band_metadata' not found. Thanks @TianyaImpression.
- Fix a bug in VScode studio when getOption("rgee.print.option") is "simple".
- ee_as_raster (raster) is deprecated in favour to ee_as_rast (terra).
- `via` argument in ee_as_stars, and ee_as_rast have a new option called "getDownloadURL". via="getDownloadURL" is the new default. getDownloadURL permit download small images x800%
faster.
- `via` argument in ee_as_stars, and ee_as_rast have a new option called "getDownloadURL".


# rgee 1.1.6
Expand Down
4 changes: 2 additions & 2 deletions R/ee_help.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @param eeobject Earth Engine Object to print documentation.
#' @param browser Logical. Display documentation in the browser.
#' @importFrom reticulate py_function_docs
#' @importFrom utils tail
#' @importFrom utils tail help
#' @family helper functions
#' @return No return value, called for displaying Earth Engine documentation.
#' @examples
Expand Down Expand Up @@ -620,5 +620,5 @@ rgeeExtracheckmans <- function(fun_name, type="image") {
} else if (type=="ic") {
checkmanfiles <- sprintf("%s%s", "ee_ImageCollection_", fun_name)
}
help(checkmanfiles, package = "rgeeExtra", verbose = FALSE)
utils::help(checkmanfiles, package = "rgeeExtra", verbose = FALSE)
}
44 changes: 32 additions & 12 deletions R/ee_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
ee_as_stars <- function(image,
region = NULL,
dsn = NULL,
via = "getDownloadURL",
via = "drive",
container = "rgee_backup",
scale = NULL,
maxPixels = 1e9,
Expand Down Expand Up @@ -408,7 +408,7 @@ ee_as_stars <- function(image,
ee_as_rast <- function(image,
region = NULL,
dsn = NULL,
via = "getDownloadURL",
via = "drive",
container = "rgee_backup",
scale = NULL,
maxPixels = 1e9,
Expand All @@ -432,16 +432,36 @@ ee_as_rast <- function(image,
dsn <- tempfile(fileext = ".tif")
}

rast_obj <- ee_image_local_getDownloadURL(
image = image,
dsn = dsn,
quiet = quiet,
scale = scale,
grid_batch = grid_batch,
export = "terra",
format = "GEO_TIFF",
geometry = region
)
# Download data
counter <- 1
while(TRUE) {
rast_obj <- tryCatch(
expr = {
ee_image_local_getDownloadURL(
image = image,
dsn = dsn,
quiet = quiet,
scale = scale,
grid_batch = grid_batch,
export = "terra",
format = "GEO_TIFF",
geometry = region
)
}, error = function(e) {
Sys.sleep(1)
if (counter > 4) {
stop(e)
}
counter <- counter + 1
FALSE
}
)

if (inherits(rast_obj, "SpatRaster")) {
break
}
}

return(rast_obj)
}

Expand Down
4 changes: 2 additions & 2 deletions R/ee_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @return An R object
#' @export
ee_utils_py_to_r <- function(x) {
p_r <- suppressWarnings(try(py_to_r(x), silent = TRUE))
p_r <- suppressWarnings(try(reticulate::py_to_r(x), silent = TRUE))
if (class(p_r) %in% 'try-error') {
return(x)
} else {
Expand Down Expand Up @@ -453,7 +453,7 @@ get_signature <- function (sigs) {
py_value_str <- ifelse(
is.character(sigs[[k]]),
paste0("'", sigs[[k]], "'"),
as.character(r_to_py(eval(sigs[[k]]))))
as.character(reticulate::r_to_py(eval(sigs[[k]]))))
paste0(k, "=", py_value_str)
}
})
Expand Down
2 changes: 1 addition & 1 deletion R/ee_version.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#' @return Character. Earth Engine Python API version used to build rgee.
#' @export
ee_version <- function() {
'0.1.355'
'0.1.364'
}
4 changes: 4 additions & 0 deletions R/utils-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ ee_connect_to_py <- function(path, n = 5) {
"does not have the Python package \"earthengine-api\" installed. Do you restarted/terminated",
"your R session after install miniconda or run ee_install()?",
"If this is not the case, try:",
"> ee_install_upgrade(): Install the latest Earth Engine Python version.",
"> reticulate::use_python(): Refresh your R session and manually set the Python environment with all rgee dependencies.",
"> ee_install(): To create and set a Python environment with all rgee dependencies.",
"> ee_install_set_pyenv(): To set a specific Python environment."
Expand Down Expand Up @@ -411,6 +412,9 @@ ee_message_05 <- function() {
ee_message_06 <- function(gcs_credentials, ee_user) {
cat("\r", crayon::green(cli::symbol[["tick"]]), crayon::blue("Earth Engine account:"),
crayon::green(crayon::bold(ee_user)), "\n")
py_ee_config <- reticulate::py_config()
cat("\r", crayon::green(cli::symbol[["tick"]]), crayon::blue("Python Path:"),
crayon::green(crayon::bold(py_ee_config[["python"]])), "\n")
cat(cli::rule(), "\n")
if (!is.na(gcs_credentials[["message"]])) {
message(gcs_credentials[["message"]])
Expand Down
4 changes: 2 additions & 2 deletions inst/python/ee_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def create_codes(*nonce_keys):
table = {}
for key in nonce_keys:
table[key] = _base64param(os.urandom(32))
if key.endswith('_verifier'):
if key.endswith("_verifier"):
# Generate a challenge that the server will use to ensure that requests
# only work with our verifiers. https://tools.ietf.org/html/rfc7636
pkce_challenge = _base64param(hashlib.sha256(table[key]).digest())
table[key.replace('_verifier', '_challenge')] = pkce_challenge
table[key.replace("_verifier", "_challenge")] = pkce_challenge
return {k: v.decode() for k, v in table.items()}


Expand Down
2 changes: 1 addition & 1 deletion man/ee_as_rast.Rd

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

2 changes: 1 addition & 1 deletion man/ee_as_stars.Rd

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

0 comments on commit 21d09c3

Please sign in to comment.