From f8b8eff6233396dac9029f94335c8140dd04b9e0 Mon Sep 17 00:00:00 2001 From: williamstravis Date: Fri, 3 Apr 2020 08:51:48 -0600 Subject: [PATCH 1/2] Fixed a path issue in get_shapefile --- R/shapefile.R | 32 ++++++++++++++++++++------------ tests/testthat/test-cstdata.R | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/R/shapefile.R b/R/shapefile.R index 4fb63e1..59aa0f6 100644 --- a/R/shapefile.R +++ b/R/shapefile.R @@ -1,4 +1,8 @@ get_aoi <- function(park, shp_path, area_name, local_dir) { + + # Use either a park or a shape file path + if (missing(park)) park <- NA + # Choose which to area of interest to return. if (!is.na(park)) { aoi <- get_park_boundaries(park, local_dir = local_dir) @@ -12,32 +16,36 @@ get_shapefile <- function(shp_path, shp_name = NA, local_dir = tempdir()) { if (is.na(shp_name)) shp_name <- tools::file_path_sans_ext(basename(shp_path)) - # Create a path within chosen directory for this shapefile - local_dir <- file.path(local_dir, "shapefiles") - shp_folder <- file.path(local_dir, shp_name) - # Check if this is a url or local path if ( grepl("www.|http:|https:", shp_path) ) { + + # Create a local path within chosen local directory for this shapefile + local_dir <- file.path(local_dir, "shapefiles") + shp_folder <- file.path(local_dir, shp_name) if ( !httr::http_error(shp_path) ) { - expected_file <- list.files(shp_folder, - full.names = TRUE, + expected_file <- list.files(shp_folder, full.names = TRUE, pattern = "\\.shp$") if (length(expected_file) == 0) { - - # download shapefile if it doesn't exist + + # Download shapefile if it doesn't exist dir.create(shp_folder, recursive = TRUE, showWarnings = FALSE) zip_path <- file.path(shp_folder, "temp.zip") utils::download.file(url = shp_path, destfile = zip_path, mode = "wb") utils::unzip(zip_path, exdir = shp_folder) file.remove(zip_path) } + + # Now get whatever the shapefile is path from the folder + path <- list.files(shp_folder, pattern = "\\.shp$", full.names = TRUE) } + } else { + + # Just use the given shapefile path + path <- shp_path } - # Get the shapefile path from the folder - path <- list.files(shp_folder, pattern = "\\.shp$", full.names = TRUE) - - tryCatch({ # readOGR can read the URL! We could simplify this + # Now read locally + tryCatch({ aoi <- rgdal::readOGR(path, verbose = FALSE) }, error = function(e) { stop(paste0("Cannot read ", shp_path)) diff --git a/tests/testthat/test-cstdata.R b/tests/testthat/test-cstdata.R index 3de9c9f..8e5f746 100644 --- a/tests/testthat/test-cstdata.R +++ b/tests/testthat/test-cstdata.R @@ -20,7 +20,7 @@ test_that("Providing a shapefile but no area name raises an error", { }) test_that("A full run of cstdata completes and saves an expected file", { - local_dir <- "." + local_dir <- "~/data" file_refs <- cstdata(park = "Acadia National Park", years = c(2004, 2005), models = "bcc-csm1-1", From 66bbfdcb820110001f35057485d578625dbe8113 Mon Sep 17 00:00:00 2001 From: williamstravis Date: Fri, 3 Apr 2020 11:16:31 -0600 Subject: [PATCH 2/2] Changed a local_dir path in test cstdata --- tests/testthat/test-cstdata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-cstdata.R b/tests/testthat/test-cstdata.R index 8e5f746..3de9c9f 100644 --- a/tests/testthat/test-cstdata.R +++ b/tests/testthat/test-cstdata.R @@ -20,7 +20,7 @@ test_that("Providing a shapefile but no area name raises an error", { }) test_that("A full run of cstdata completes and saves an expected file", { - local_dir <- "~/data" + local_dir <- "." file_refs <- cstdata(park = "Acadia National Park", years = c(2004, 2005), models = "bcc-csm1-1",