Skip to content

Commit

Permalink
feat: use iho as an internal dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 16, 2024
1 parent 20f53b5 commit 6a13790
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 104 deletions.
16 changes: 3 additions & 13 deletions R/filter_by_ocean.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,12 @@ filter_by_ocean <- function(data, ocean) {


data_sf <- data_to_sf(data)


## Read IHO layer ----

iho <- read_iho_data(check_for_update = FALSE)
iho <- iho[iho$"NAME" %in% ocean, ]


## Project spatial objects into Robinson system ----

iho <- sf::st_transform(iho, sf::st_crs(crs_robinson()))



## Spatial filter ----

inter <- suppressWarnings(sf::st_intersects(data_sf, iho, sparse = FALSE))
inter <- suppressWarnings(sf::st_intersects(data_sf, iho_boundaries,
sparse = FALSE))

data[which(apply(inter, 1, any)), ]
}
2 changes: 1 addition & 1 deletion R/get_ocean_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

get_ocean_names <- function() {

read_iho_data(check_for_update = FALSE)$"NAME"
iho_boundaries$"NAME"
}
77 changes: 0 additions & 77 deletions R/internals.R
Original file line number Diff line number Diff line change
@@ -1,72 +1,3 @@
#' Download and read IHO spatial layer
#'
#' @noRd

read_iho_data <- function(path = ".",
version = options()$"forcis_version",
check_for_update = options()$"check_for_update",
overwrite = FALSE, timeout = 60) {

## Check args ----

check_if_character(path)
check_version(version)


## Check/set version ----

if (is.null(check_for_update)) {
check_for_update <- TRUE
}

version <- set_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 <- 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
#'
#' @noRd
Expand Down Expand Up @@ -159,14 +90,6 @@ sediment_trap_filename <- function() "FORCIS_trap_"



#' Sediment Traps file name
#'
#' @noRd

iho_oceans_filename <- function() "iho_oceans_boundaries"



#' Vector of device types available in FORCIS database
#'
#' @noRd
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
13 changes: 0 additions & 13 deletions tests/testthat/test-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,6 @@ test_that("Test sediment_trap_filename() for success", {



## iho_oceans_filename() ----

test_that("Test iho_oceans_filename() for success", {

x <- iho_oceans_filename()

expect_equal(x, "iho_oceans_boundaries")
expect_equal(class(x), "character")
expect_equal(length(x), 1L)
})



## get_required_columns() ----

test_that("Test get_required_columns() for success", {
Expand Down

0 comments on commit 6a13790

Please sign in to comment.