From 7d122cfb61860870b3bedbb290e0f2f5cf8c4f10 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Thu, 8 Feb 2024 15:22:42 +0100 Subject: [PATCH] feat: handle iho spatial layer - fix #30 --- DESCRIPTION | 1 + R/filenames.R | 8 +++++++ R/gets.R | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 21e842b..4c0c69c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,6 +41,7 @@ Imports: jsonlite, lubridate, rlang, + sf, tidyr, utils, vroom diff --git a/R/filenames.R b/R/filenames.R index 182da3d..5fd4efb 100644 --- a/R/filenames.R +++ b/R/filenames.R @@ -35,3 +35,11 @@ cpr_south_filename <- function() "FORCIS_cpr_south_" #' @noRd sediment_trap_filename <- function() "FORCIS_trap_" + + + +#' Sediment Traps file name +#' +#' @noRd + +iho_oceans_filename <- function() "iho_oceans_boundaries" diff --git a/R/gets.R b/R/gets.R index 4e6357f..29fb0b0 100644 --- a/R/gets.R +++ b/R/gets.R @@ -528,6 +528,72 @@ get_sediment_trap_data <- function(path = ".", version = NULL, +#' Download and read IHO spatial layer +#' +#' Used in the `spatial_filter_*()` functions +#' +#' @noRd + +get_iho_data <- function(path = ".", version = NULL, + check_for_update = TRUE, overwrite = FALSE, + timeout = 60) { + + ## Check args ---- + + is_character(path) + check_zen_version(version) + + + ## Check/set version ---- + + version <- set_zen_version(version, ask = check_for_update) + + + ## Build outputs directory ---- + + path <- file.path(path, "forcis-db", paste0("version-", version)) + + if (!dir.exists(path)) { + dir.create(path, recursive = TRUE) + } + + + ## Download file (if required) ---- + + file_name <- list.files(path, pattern = iho_oceans_filename()) + + if (!length(file_name)) { + + forcis_meta <- zen_get_version_info(version = version) + forcis_files <- forcis_meta$"files" + + pos <- grep(iho_oceans_filename(), forcis_files$"key") + + if (length(pos) == 1) { + + download_file(url = forcis_files[pos, "links"]$"self", + path = path, + file = forcis_files[pos, "key"], + overwrite = overwrite, + timeout = timeout) + + } else { + + stop("Unable to download the 'IHO' dataset", call. = FALSE) + } + } + + + ## Read data ---- + + file_name <- list.files(path, pattern = iho_oceans_filename()) + + data <- readRDS(file.path(path, file_name)) + sf::st_as_sf(data) +} + + + #' Download a csv file #' #' @param file a `character` of length 1. The name of the csv to download.