Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean #9

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions R/create_rgee_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
#' @export
#' @importFrom reticulate condaenv_exists
#' @importFrom kwb.python conda_py_install
create_ad4gd_env <- function(force = FALSE, debug = FALSE) {
create_ad4gd_env <- function(force = FALSE, debug = FALSE)
{
if (!reticulate::condaenv_exists("ad4gd") || force) {

if(!reticulate::condaenv_exists("ad4gd") | force) {
kwb.python::conda_py_install(env_name = "ad4gd",
pkgs = list(conda = c("python=3.12.2",
"numpy"),
py = "earthengine-api==0.1.370"))
} else {
if(debug) {
message(paste0("Conda environment 'ad4gd' already exists. Use ",
"'force' = TRUE, to reinstall if required"))
}
kwb.python::conda_py_install(env_name = "ad4gd", pkgs = list(
conda = c("python=3.12.2", "numpy"),
py = "earthengine-api==0.1.370"
))

} else if (debug) {

message(
"Conda environment 'ad4gd' already exists. ",
"Use 'force' = TRUE to reinstall, if required."
)
}
}

20 changes: 10 additions & 10 deletions R/flatten_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
#' @importFrom dplyr bind_rows
#' @importFrom tidyr unnest
#' @importFrom sf st_polygon st_sfc st_set_crs
flatten_results <- function(sat_data_list,
cols_unnest = "satellite_data_metadata") {

if(sum(c("satellite_data", "satellite_metadata") %in% cols_unnest) == 2) {
names_sep <- "."
} else {
names_sep <- NULL
}
flatten_results <- function(
sat_data_list,
cols_unnest = "satellite_data_metadata"
)
{
satellite_columns <- c("satellite_data", "satellite_metadata")

sat_data_list %>%
dplyr::bind_rows() %>%
tidyr::unnest(tidyselect::all_of(cols_unnest),
names_sep = names_sep)
tidyr::unnest(
tidyselect::all_of(cols_unnest),
names_sep = if (all(satellite_columns %in% cols_unnest)) "." # else NULL
)
}
76 changes: 35 additions & 41 deletions R/get_metadata_gee.R
Original file line number Diff line number Diff line change
@@ -1,78 +1,72 @@
#' Helper function: convert to list
#'
#' @param coords column with satellite metadata coordinates (system:footprint`$coordinates)
#'
#' @param coords column with satellite metadata coordinates
#' (system:footprint`$coordinates)
#' @return list of coordinates
#' @keywords internal
#'
convert_to_list <- function(coords) {

tmp_mat <- lapply(seq_along(coords),
FUN = function(idx) {

t(as.matrix(coords[[idx]]))
})

list(do.call(rbind, tmp_mat))
convert_to_list <- function(coords)
{
lapply(coords, function(x) t(as.matrix(x))) %>%
do.call(what = rbind) %>%
list()
}


#' Google Earth Engine: get metadata for collection
#'
#' @param collection collection
#'
#' @return tibble with metadata. In addition also the complex column
#' "geometry_meta_org" is simplified and stored in the new column "geometry_meta_cleaned"
#' "geometry_meta_org" is simplified and stored in the new column
#' "geometry_meta_cleaned"
#' @export
#' @importFrom tibble as_tibble tibble
#' @importFrom dplyr bind_cols bind_rows
#' @importFrom kwb.utils removeColumns
#' @importFrom tidyr nest
#' @importFrom tidyselect matches all_of
gee_get_metadata <- function(collection) {

nImages <- collection$size()$getInfo()
#' @importFrom tidyselect matches
gee_get_metadata <- function(collection)
{
metadata_list <- collection$size()$getInfo() %>%
collection$toList() %>%
collection$fromImages()

collectionList <- collection$toList(nImages)

metadata_list <- collection$fromImages(collectionList)
metadata_list <- metadata_list$getInfo()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metadata_list$getInfo()$features da metadata_list <- metadata_list$getInfo() gelöscht wurde

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Steht das nicht in Zeile 33?



metadata_features_df <- lapply(seq_along(metadata_list$features), function(idx) {
tibble::as_tibble(metadata_list$features[[idx]]$properties)
}) %>% dplyr::bind_rows() %>%
metadata_features_df <- metadata_list$features %>%
lapply(function(x) tibble::as_tibble(x$properties)) %>%
dplyr::bind_rows() %>%
tidyr::nest(geometry_meta_org = tidyselect::matches("system:footprint"))

geometries <- metadata_features_df$geometry_meta_org

coords <- lapply(seq_len(nrow(metadata_features_df)), function(idx) {
convert_to_list(metadata_features_df$geometry_meta_org[[idx]]$`system:footprint`$coordinates) %>%
coords <- lapply(seq_len(nrow(metadata_features_df)), function(i) {
geometries[[i]]$`system:footprint`$coordinates %>%
convert_to_list() %>%
sf::st_polygon() %>%
sf::st_sfc() %>%
sf::st_set_crs(value = 4326)
})

metadata_features_df$geometry_meta_cleaned <- coords

metadata_ids <- tibble::tibble(
id = sapply(seq_along(metadata_list$features), function(idx) metadata_list$features[[idx]]$id)
)

dplyr::bind_cols(metadata_ids, metadata_features_df) %>%
tidyr::separate(col = "id",
into = c("provider_name", "provider_collection", "id_short"),
sep = "/",
remove = FALSE) %>%
tibble::tibble(
id = sapply(metadata_list$features, function(x) x$id)
) %>%
dplyr::bind_cols(metadata_features_df) %>%
tidyr::separate(
col = "id",
into = c("provider_name", "provider_collection", "id_short"),
sep = "/",
remove = FALSE
) %>%
tidyr::separate(
col = "id_short",
into = c("datetime_start",
"datetime_end",
"tile_id"),
into = c("datetime_start", "datetime_end", "tile_id"),
sep = "_"
) %>%
dplyr::mutate(
datetime_start = lubridate::ymd_hms(datetime_start),
datetime_end = lubridate::ymd_hms(datetime_end)
) %>%
dplyr::select(- tidyselect::all_of(c("provider_name","provider_collection")))

kwb.utils::removeColumns(c("provider_name", "provider_collection"))
}
Loading