Skip to content

Commit

Permalink
working in rgee v.1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
csaybar committed Aug 5, 2020
1 parent 0d6c1ba commit b6dae6a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion 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.3
Version: 1.0.4
Authors@R:
c(person(given = "Cesar",
family = "Aybar",
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ vignette: >
%\VignetteIndexEntry{NEWS}
%\VignetteEncoding{UTF-8}
---
# rgee 1.0.4
- Fix a bug that makes that `ee_as_sf` only supports `GeoJSON` format.
- If `dsn` is not specified in `ee_as_sf`, it will create a temporary shapefile (in \tmp dir).
- Fix a bug in `ee_imagecollection_to_local` (#87 Thanks @cedlfc44)

# rgee 1.0.3
- getPass library removed from `ee_Initialize`.
- New argument `display` in `ee_Initialize` to return the authentication URI. Useful for `rgee` colab users.
- Changes in some diagnostic messages to make possible to use `rgee` in colab.
- `ee_help` returns a HTML file rather than TRUE. It also now supports characters (e.g. `ee_help("ee$Image")`).
- Fix a strange bug when `ee_Initialize` tries to connect to reticulate the first time.
- Fix small bugs in `ee_user_info` and `ee_users`

# rgee 1.0.2
- Earth Engine Python API updated to 0.1.229.
- Fix a bug in `ee_Initialize`, that does not permit users to use `ee_createAssetHome` to define their *Earth Engine Assets home root folder*
Expand Down
11 changes: 6 additions & 5 deletions R/ee_Initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ ee_Initialize <- function(email = NULL,
}

# Root folder exist?
ee_user_assetroot <- try(ee$data$getAssetRoots()[[1]])
ee_user_assetroot <- ee$data$getAssetRoots()
assetroot_exist <- length(ee_user_assetroot) == 0
# if ee_asset_home (list) length is zero
if (length(ee_user_assetroot) == 0 | class(ee_user_assetroot) == "try-error") {
if (assetroot_exist) {
root_text <- paste(
"Earth Engine Assets home root folder does not exist for the current user.",
"Please enter your desired root folder name below. Take into consideration",
Expand All @@ -275,10 +276,10 @@ ee_Initialize <- function(email = NULL,
)
message(root_text)
ee_createAssetHome()
ee_user_assetroot <- ee$data$getAssetRoots()[[1]]
ee_user_assetroot <- ee$data$getAssetRoots()
}

ee_user <- ee_remove_project_chr(ee_user_assetroot$id)
ee_user_assetroot_id <- ee_user_assetroot[[1]]$id
ee_user <- ee_remove_project_chr(ee_user_assetroot_id)

options(rgee.ee_user = ee_user)
ee_sessioninfo(
Expand Down
68 changes: 60 additions & 8 deletions R/ee_as_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param x Earth Engine table (ee$FeatureCollection) to be converted into a sf
#' object.
#' @param dsn Character. Output filename; in case \code{dsn} is missing
#' \code{ee_as_sf} will create a temporary file.
#' \code{ee_as_sf} will create a shapefile file in tmp() directory.
#' @param crs Integer or character. coordinate reference system
#' for the EE table. If is NULL, \code{ee_as_sf} will take the CRS of
#' the first element.
Expand Down Expand Up @@ -101,7 +101,7 @@ ee_as_sf <- function(x,
sp_eeobjects <- ee_get_spatial_objects('Table')

if (missing(dsn)) {
dsn <- paste0(tempfile(),".geojson")
dsn <- paste0(tempfile(),".shp")
}

if (!any(class(x) %in% sp_eeobjects)) {
Expand Down Expand Up @@ -195,12 +195,21 @@ ee_as_sf <- function(x,
file_name <- paste0(table_id, "_", time_format)

# table to drive
table_format <- ee_get_table_format(dsn)
if (is.na(table_format)) {
stop(
'sf_as_ee(..., via = \"drive\"), only support the ',
'following output format: "CSV", "GeoJSON", "KML", "KMZ", "SHP"',
'. Use ee_table_to_drive and ee_drive_to_local to save in a TFRecord format.'
)
}

table_task <- ee_table_to_drive(
collection = x_fc,
description = ee_description,
folder = container,
fileNamePrefix = file_name,
fileFormat = "GeoJSON",
fileFormat = table_format,
selectors = selectors
)

Expand All @@ -227,7 +236,12 @@ ee_as_sf <- function(x,
overwrite = overwrite,
consider = 'all'
)
local_sf <- sf::read_sf(dsn, quiet = TRUE)

if (table_format == "CSV") {
return(read.csv(dsn, stringsAsFactors = FALSE))
} else {
local_sf <- sf::read_sf(dsn, quiet = TRUE)
}
} else if (via == 'gcs') {
# Creating name for temporal file; just for either drive or gcs
time_format <- format(Sys.time(), "%Y-%m-%d-%H:%M:%S")
Expand All @@ -245,13 +259,22 @@ ee_as_sf <- function(x,

file_name <- paste0(table_id, "_", time_format)

# table to drive
# table to gcs
table_format <- ee_get_table_format(dsn)
if (is.na(table_format)) {
stop(
'sf_as_ee(..., via = \"gcs\"), only support the ',
'following output format: "CSV", "GeoJSON", "KML", "KMZ", "SHP"',
'. Use ee_table_to_drive and ee_drive_to_local to save in a TFRecord format.'
)
}

table_task <- ee_table_to_gcs(
collection = x_fc,
description = ee_description,
bucket = container,
fileNamePrefix = file_name,
fileFormat = "GeoJSON",
fileFormat = table_format,
selectors = selectors
)

Expand All @@ -271,7 +294,11 @@ ee_as_sf <- function(x,
stop(table_task$status()$error_message)
}
ee_gcs_to_local(task = table_task,dsn = dsn, overwrite = overwrite)
local_sf <- sf::read_sf(dsn, quiet = TRUE)
if (table_format == "CSV") {
return(read.csv(dsn, stringsAsFactors = FALSE))
} else {
local_sf <- sf::read_sf(dsn, quiet = TRUE)
}
} else {
stop("via argument invalid.")
}
Expand Down Expand Up @@ -313,7 +340,32 @@ ee_fc_to_sf_getInfo <- function(x_fc, dsn, maxFeatures, overwrite = TRUE) {
if (missing(dsn)) {
x_sf
} else {
sf::write_sf(x_sf, dsn, delete_dsn = overwrite, quiet = TRUE)
suppressWarnings(
sf::write_sf(x_sf, dsn, delete_dsn = overwrite, quiet = TRUE)
)
x_sf
}
}

#' Sync sf and ee drivers
#' @noRd
ee_get_table_format <- function(dsn) {
table_format <- tolower(sub(".*([.*])", "\\1", basename(dsn)))
if (length(table_format) != 1) {
stop("dns must be a single-length character")
}

if (table_format == ".shp") {
"SHP"
} else if (table_format == ".geojson") {
"GeoJSON"
} else if (table_format == ".kml") {
"KML"
} else if (table_format == ".kmz") {
"KMZ"
} else if (table_format == ".csv") {
"CSV"
} else {
NA
}
}
4 changes: 2 additions & 2 deletions R/ee_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ ee_image_local <- function(image,
is_geodesic <- region$geodesic()$getInfo()
#is evenodd?
query_params <- unlist(jsonlite::parse_json(region$serialize())$scope)
is_evenodd <- as.logical(
is_evenodd <- all(as.logical(
query_params[grepl("evenOdd", names(query_params))]
)
))
if (length(is_evenodd) == 0 | is.null(is_evenodd)) {
is_evenodd <- TRUE
}
Expand Down
6 changes: 3 additions & 3 deletions R/ee_imagecollection.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ ee_geometry_message <- function(region, quiet = FALSE) {
is_geodesic <- region$geodesic()$getInfo()
#is evenodd?
query_params <- unlist(jsonlite::parse_json(region$serialize())$scope)
is_evenodd <- as.logical(
is_evenodd <- all(as.logical(
query_params[grepl("evenOdd", names(query_params))]
)
))
if (length(is_evenodd) == 0 | is.null(is_evenodd)) {
is_evenodd <- TRUE
}
Expand All @@ -194,7 +194,7 @@ ee_geometry_message <- function(region, quiet = FALSE) {
'- region parameters\n',
'WKT :', sf::st_as_text(sf_region), "\n",
'CRS :', region_crs, "\n",
'geodesic :', is_geodesic, "\n",
'geodesic :', ee_utils_py_to_r(is_geodesic), "\n",
'evenOdd :', is_evenodd, "\n"
)
}
Expand Down
2 changes: 1 addition & 1 deletion man/ee_as_sf.Rd

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

0 comments on commit b6dae6a

Please sign in to comment.