-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bluegreen-labs/patch-hwsd-v2
Patch hwsd v2
- Loading branch information
Showing
18 changed files
with
681 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
^CONDUCT\.md$ | ||
^NEWS\.md$ | ||
^docs$ | ||
^data-raw$ | ||
^analysis$ | ||
^pkgdown$ | ||
^LICENSE$ | ||
^.*-logo\..*$ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(ws_download) | ||
export(ws_get) | ||
export(ws_subset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#' Download HWSD v2.0 data | ||
#' | ||
#' Downloads both the database and gridded HWSD v2.0 data products | ||
#' to a desired output path for subsetting. | ||
#' | ||
#' When an existing path is used which is not the temporary directory | ||
#' an environmental variable WS_PATH will be set. This variable will | ||
#' override the default temporary directory if it exists. This allows | ||
#' the gridded data to be stored elsewhere and be forgotten (while using the | ||
#' {hwsdr} package for HWSD v2.0). | ||
#' | ||
#' Should you delete the gridded file, than it can be downloaded again but | ||
#' the environmental variable should be set again using the new_path = TRUE | ||
#' flag. | ||
#' | ||
#' @param ws_path the path / directory where to store the HWSD v2.0 database | ||
#' @param new_path update the path, i.e. dowload data to a new local | ||
#' location (logical, default FALSE) | ||
#' | ||
#' @return current data path | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' \dontrun{ | ||
#' | ||
#' # Download the gridded soil map of | ||
#' # HWSD v2.0 to the temporary directory | ||
#' ws_download() | ||
#' | ||
#' # download the same data to a specific | ||
#' # directory (which should exist) | ||
#' ws_download( | ||
#' ws_path = "~/my_path" | ||
#' ) | ||
#' | ||
#' # download the same data to a specific | ||
#' # directory (which should exist) and | ||
#' # update the environmental variable | ||
#' ws_download( | ||
#' ws_path = "~/my_path", | ||
#' new_path = TRUE | ||
#' ) | ||
#' } | ||
|
||
ws_download <- function( | ||
ws_path = file.path(tempdir(), "ws_db"), | ||
new_path = FALSE | ||
) { | ||
|
||
# check if environmental variable is set | ||
# if so use dir as overriding location | ||
if(Sys.getenv("WS_PATH") != "" & !new_path ) { | ||
ws_path <- Sys.getenv("WS_PATH") | ||
} | ||
|
||
if (ws_path == file.path(tempdir(), "ws_db")) { | ||
|
||
message( | ||
sprintf( | ||
"Creating temporary HWSD v2.0 files at:\n %s", ws_path ) | ||
) | ||
|
||
# create storage path | ||
if (!dir.exists(ws_path)) { | ||
dir.create(ws_path, recursive = TRUE) | ||
} | ||
|
||
} else { | ||
|
||
if (!dir.exists(ws_path)) { | ||
stop( | ||
"Database directory does not exist, please create the directory first!" | ||
) | ||
} else { | ||
|
||
if(Sys.getenv("WS_PATH") == "" | new_path) { | ||
Sys.setenv("WS_PATH" = ws_path) | ||
} | ||
|
||
if (file.exists(file.path(ws_path, "HWSD2.bil"))) { | ||
message("Grid file exists, skipping download!") | ||
return(ws_path) | ||
} | ||
} | ||
} | ||
|
||
# grab urls | ||
urls <- server(version = "2.0") | ||
|
||
# download zipped gridded data | ||
httr::GET( | ||
urls$grid, | ||
httr::write_disk( | ||
file.path(ws_path, "hwsd2_raster.zip"), | ||
overwrite = TRUE | ||
) | ||
) | ||
|
||
utils::unzip( | ||
file.path(ws_path, "hwsd2_raster.zip"), | ||
exdir = ws_path | ||
) | ||
|
||
# clean up zip files | ||
status <- file.remove( | ||
c( | ||
file.path(ws_path, "hwsd2_raster.zip") | ||
) | ||
) | ||
|
||
message( | ||
sprintf( | ||
"Saving HWSD v2.0 files in at:\n %s", ws_path ) | ||
) | ||
|
||
# exit statement | ||
message("Downloaded HWSD v2.0 files") | ||
return(ws_path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.