Skip to content

Commit

Permalink
feat: implement, doc & test function to dl basins
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed May 2, 2024
1 parent 64f0c6e commit fc833f2
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(get_drainage_basins_layer)
export(get_marine_ecoregions_layer)
export(ggmap_marine)
export(ggmap_terrestrial)
Expand Down
122 changes: 122 additions & 0 deletions R/get_drainage_basins_layer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#' Download World drainage basins spatial layer
#'
#' @description
#' Downloads the spatial layer of drainage basins of the World available on the
#' Figshare website
#' (<https://figshare.com/collections/A_global_database_on_freshwater_fish_species_occurrences_in_drainage_basins/3739145>).
#'
#' @param path a `character` of length 1. The path to the folder to save the
#' spatial layer. Note that a subdirectory `basins/` will be created.
#'
#' @param overwrite a `logical`. If `TRUE` it will override the existing files.
#' Default is `FALSE`.
#'
#' @param timeout an `integer`. The timeout for downloading files.
#' Default is `60`. This number can be increased for slow Internet connection.
#'
#' @param ... other arguments to pass to `download.file()` (e.g. `headers`).
#'
#' @return No return value. Files will be written in `path`.
#'
#' @export
#'
#' @references
#' Tedesco P _et al._ (2017) A global database on freshwater fish species
#' occurrence in drainage basins. Scientific Data, 4, 170141.
#' <https://doi.org/10.1038/sdata.2017.141>.
#'
#' @examples
#' \dontrun{
#' ## Download drainage basins spatial layer ----
#' get_drainage_basins_layer(path = "data")
#' }

get_drainage_basins_layer <- function(path = ".", overwrite = FALSE,
timeout = 60, ...) {


## Check path ----

error_if_null(path)
error_if_na(path)
error_if_not_character(path)
error_if_not_length_of(path, 1)


## Check other args ----

error_if_null(overwrite)
error_if_na(overwrite)
error_if_not_logical(overwrite)
error_if_not_length_of(overwrite, 1)

error_if_null(timeout)
error_if_na(timeout)
error_if_not_numeric(timeout)
error_if_not_length_of(timeout, 1)
error_if_not_strictly_positive(timeout)


## Create path if required ----

if (!dir.exists(path)) {
dir.create(path, recursive = TRUE)
}


## Check if layer is already locally available ----

if (!("Basin042017_3119.shp" %in% list.files(file.path(path, "basins")))) {


## Change timeout for slow connection ----

user_opts <- options()
on.exit(options(user_opts))
options(timeout = max(timeout, getOption("timeout")))


## URL parameters ----

baseurl <- "https://figshare.com/ndownloader/files/8964583/"
zipname <- "datatoFigshare.zip"


## Download ZIP file ----

utils::download.file(url = paste0(baseurl, zipname),
destfile = file.path(path, zipname),
mode = "wb",
quiet = TRUE, ...)


## Extract files in ZIP ----

utils::unzip(zipfile = file.path(path, zipname),
exdir = file.path(path, "basins"))


## Delete ZIP file ----

invisible(file.remove(file.path(path, zipname)))


## Delete optional files ----

fls_to_del <- list.files(file.path(path, "basins"), pattern = "\\.csv$")

for (fls in fls_to_del) {
invisible(file.remove(file.path(path, "basins", fls)))
}

message("Drainage basins spatial layer has been successfully saved in '",
file.path(path, "basins"), "/'")

} else {

message("Drainage basins spatial layer is already available in '",
file.path(path, "basins"), "/'")
}

invisible(NULL)
}
39 changes: 39 additions & 0 deletions man/get_drainage_basins_layer.Rd

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

151 changes: 151 additions & 0 deletions tests/testthat/test-get_drainage_basins_layer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
## Test get_drainage_basins_layer() ----

### Tests for errors ----

test_that("Test get_drainage_basins_layer() for error", {

expect_error(get_drainage_basins_layer(path = NULL),
"Argument 'path' cannot be `NULL`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(path = NA),
"Argument 'path' cannot contain `NA`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(path = 1),
"Argument 'path' must be a `character`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(path = c("data", "outputs")),
"Argument 'path' must be of length 1",
fixed = TRUE)

expect_error(get_drainage_basins_layer(overwrite = NULL),
"Argument 'overwrite' cannot be `NULL`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(overwrite = NA),
"Argument 'overwrite' cannot contain `NA`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(overwrite = 2),
"Argument 'overwrite' must be a `logical`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(overwrite = "2"),
"Argument 'overwrite' must be a `logical`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(overwrite = c(TRUE, TRUE)),
"Argument 'overwrite' must be of length 1",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = NULL),
"Argument 'timeout' cannot be `NULL`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = NA),
"Argument 'timeout' cannot contain `NA`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = c(1, 2)),
"Argument 'timeout' must be of length 1",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = "2"),
"Argument 'timeout' must be a `numeric`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = FALSE),
"Argument 'timeout' must be a `numeric`",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = 0),
"Argument 'timeout' must be strictly positive",
fixed = TRUE)

expect_error(get_drainage_basins_layer(timeout = -60),
"Argument 'timeout' must be strictly positive",
fixed = TRUE)
})


### Tests for success ----

#### Path exists ----

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

skip_on_cran()

create_tempdir()
dir.create("data")

expect_message(
get_drainage_basins_layer(
path = "data",
headers = c("User-Agent" = paste0("Mozilla/5.0 (Windows NT 10.0; Win64; ",
"x64; rv:125.0) Gecko/20100101 ",
"Firefox/125.0"))),
"Drainage basins spatial layer has been successfully saved in 'data/basins/'",
fixed = TRUE)

expect_false(file.exists(file.path("data", "datatoFigshare.zip")))

expect_true(dir.exists(file.path("data", "basins")))
expect_true(file.exists(file.path("data", "basins", "Basin042017_3119.shp")))

expect_message(
get_drainage_basins_layer(
path = "data",
headers = c("User-Agent" = paste0("Mozilla/5.0 (Windows NT 10.0; Win64; ",
"x64; rv:125.0) Gecko/20100101 ",
"Firefox/125.0"))),
"Drainage basins spatial layer is already available in 'data/basins/'",
fixed = TRUE)

expect_false(file.exists(file.path("data", "datatoFigshare.zip")))

expect_true(dir.exists(file.path("data", "basins")))
expect_true(file.exists(file.path("data", "basins", "Basin042017_3119.shp")))
})


#### Path doesn't exist ----

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

skip_on_cran()

create_tempdir()

expect_message(
get_drainage_basins_layer(
path = "data",
headers = c("User-Agent" = paste0("Mozilla/5.0 (Windows NT 10.0; Win64; ",
"x64; rv:125.0) Gecko/20100101 ",
"Firefox/125.0"))),
"Drainage basins spatial layer has been successfully saved in 'data/basins/'",
fixed = TRUE)

expect_false(file.exists(file.path("data", "datatoFigshare.zip")))

expect_true(dir.exists(file.path("data")))
expect_true(dir.exists(file.path("data", "basins")))
expect_true(file.exists(file.path("data", "basins", "Basin042017_3119.shp")))


expect_message(
get_drainage_basins_layer(
path = "data",
headers = c("User-Agent" = paste0("Mozilla/5.0 (Windows NT 10.0; Win64; ",
"x64; rv:125.0) Gecko/20100101 ",
"Firefox/125.0"))),
"Drainage basins spatial layer is already available in 'data/basins/'",
fixed = TRUE)

expect_false(file.exists(file.path("data", "datatoFigshare.zip")))

expect_true(dir.exists(file.path("data", "basins")))
expect_true(file.exists(file.path("data", "basins", "Basin042017_3119.shp")))
})

0 comments on commit fc833f2

Please sign in to comment.