Skip to content

Commit 041d88e

Browse files
committed
update to include user specified location for downloaded/temporary raster tiles. closes #95
1 parent aa5c84c commit 041d88e

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

R/get_elev_raster.R

+30-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
#' between 100 Mb and 500Mb report a message but
4545
#' continue. Between 500Mb and 3000Mb requires
4646
#' interaction and greater than 3000Mb fails. These
47-
#' can be overriden with this argument set to TRUE.
47+
#' can be overriden with this argument set to TRUE.
48+
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
49+
#' temporary location. Alternatively, the user may supply an
50+
#' existing path for these raster files. New folders are not
51+
#' created by \code{get_elev_raster}.
4852
#' @param ... Extra arguments to pass to \code{httr::GET} via a named vector,
4953
#' \code{config}. See
5054
#' \code{\link{get_aws_terrain}} for more details.
@@ -76,7 +80,7 @@
7680
#' max=sf::st_bbox(lake)$ymax))
7781
#'
7882
#' x <- get_elev_raster(locations = loc_df, prj = st_crs(lake) , z=10)
79-
#' x <- get_elev_raster(lake, z = 12)
83+
#' x <- get_elev_raster(lake, z = 14)
8084
#' x <- get_elev_raster(lake, src = "gl3", expand = 5000)
8185
#' x <- get_elev_raster(lake_buff, z = 10, clip = "locations")
8286
#' }
@@ -92,6 +96,7 @@ get_elev_raster <- function(locations, z, prj = NULL,
9296
return(NULL)
9397
}
9498

99+
tmp_dir <- normalizePath(tmp_dir, mustWork = TRUE)
95100
src <- match.arg(src)
96101
clip <- match.arg(clip)
97102

@@ -124,9 +129,11 @@ get_elev_raster <- function(locations, z, prj = NULL,
124129

125130
# Pass of locations to APIs to get data as raster
126131
if(src == "aws") {
127-
raster_elev <- get_aws_terrain(locations, z, prj = prj, expand = expand, ...)
132+
raster_elev <- get_aws_terrain(locations, z, prj = prj, expand = expand,
133+
tmp_dir = tmp_dir, ...)
128134
} else if(src %in% c("gl3", "gl1", "alos", "srtm15plus")){
129-
raster_elev <- get_opentopo(locations, src, prj = prj, expand = expand, ...)
135+
raster_elev <- get_opentopo(locations, src, prj = prj, expand = expand,
136+
tmp_dir = tmp_dir, ...)
130137
}
131138
sources <- attr(raster_elev, "sources")
132139
if(is.null(sources)){sources <- src}
@@ -181,6 +188,10 @@ get_elev_raster <- function(locations, z, prj = NULL,
181188
#' @param ncpu Number of CPU's to use when downloading aws tiles.
182189
#' @param serial Logical to determine if API should be hit in serial or in
183190
#' parallel. TRUE will use purrr, FALSE will use furrr.
191+
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
192+
#' temporary location. Alternatively, the user may supply an
193+
#' existing path for these raster files. New folders are not
194+
#' created by \code{get_elev_raster}.
184195
#' @param ... Extra configuration parameters to be passed to httr::GET. Common
185196
#' usage is to adjust timeout. This is done as
186197
#' \code{config=timeout(x)} where \code{x} is a numeric value in
@@ -229,6 +240,7 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
229240
clear = FALSE,
230241
width= 60
231242
))
243+
232244
progressr::with_progress({
233245
if(serial){
234246

@@ -255,7 +267,7 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
255267
dem_list <- furrr::future_map(urls,
256268
function(x){
257269
p()
258-
tmpfile <- tempfile(tempdir = tmp_dir, fileext = ".tif")
270+
tmpfile <- tempfile(tmpdir = tmp_dir, fileext = ".tif")
259271
resp <- httr::GET(x,
260272
httr::user_agent("elevatr R package (https://github.com/jhollist/elevatr)"),
261273
httr::write_disk(tmpfile,overwrite=TRUE), ...)
@@ -270,7 +282,7 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
270282
}
271283
})
272284

273-
merged_elevation_grid <- merge_rasters(dem_list, target_prj = prj)
285+
merged_elevation_grid <- merge_rasters(dem_list, target_prj = prj, tmp_dir = tmp_dir)
274286
sources <- unlist(lapply(dem_list, function(x) attr(x, "source")))
275287
if(!is.null(sources)){
276288
sources <- trimws(unlist(strsplit(sources, ",")))
@@ -295,6 +307,10 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
295307
#' @param method the method for resampling/reprojecting. Default is 'bilinear'.
296308
#' Options can be found [here](https://gdal.org/programs/gdalwarp.html#cmdoption-gdalwarp-r)
297309
#' @param returnRaster if TRUE, return a raster object (default), else, return the file path to the object
310+
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
311+
#' temporary location. Alternatively, the user may supply an
312+
#' existing path for these raster files. New folders are not
313+
#' created by \code{get_elev_raster}.
298314
#' @export
299315
#' @keywords internal
300316

@@ -303,7 +319,7 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
303319

304320
message(paste("Mosaicing & Projecting"))
305321

306-
destfile <- tempfile(tempdir = temp_dir, fileext = ".tif")
322+
destfile <- tempfile(tmpdir = tmp_dir, fileext = ".tif")
307323
files <- unlist(raster_list)
308324

309325
if(is.null(target_prj)){
@@ -318,7 +334,7 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
318334
)
319335
# Using two steps now as gdal with one step introduced NA's along seams
320336
# Slower but more accurate!
321-
destfile2 <- tempfile(tempdir = temp_dir, fileext = ".tif")
337+
destfile2 <- tempfile(tmpdir = tmp_dir, fileext = ".tif")
322338
sf::gdal_utils(util = "warp",
323339
source = destfile,
324340
destination = destfile2,
@@ -347,6 +363,10 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
347363
#' argument is required for a \code{data.frame} of locations.
348364
#' @param expand A numeric value of a distance, in map units, used to expand the
349365
#' bounding box that is used to fetch the SRTM data.
366+
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
367+
#' temporary location. Alternatively, the user may supply an
368+
#' existing path for these raster files. New folders are not
369+
#' created by \code{get_elev_raster}.
350370
#' @param ... Extra configuration parameters to be passed to httr::GET. Common
351371
#' usage is to adjust timeout. This is done as
352372
#' \code{config=timeout(x)} where \code{x} is a numeric value in
@@ -362,7 +382,7 @@ get_opentopo <- function(locations, src, prj, expand=NULL, tmp_dir = tempdir(),
362382
# Expand (if needed) and re-project bbx to ll_geo
363383
bbx <- proj_expand(locations,prj,expand)
364384

365-
tmpfile <- tempfile(tempdir = temp_dir)
385+
tmpfile <- tempfile(tmpdir = tmp_dir)
366386
base_url <- "https://portal.opentopography.org/API/globaldem?demtype="
367387
data_set <- switch(src,
368388
gl3 = "SRTMGL3",
@@ -391,7 +411,7 @@ get_opentopo <- function(locations, src, prj, expand=NULL, tmp_dir = tempdir(),
391411
if (httr::http_type(resp) != "application/octet-stream") {
392412
stop("API did not return octet-stream as expected", call. = FALSE)
393413
}
394-
dem <- merge_rasters(tmpfile, target_prj = prj)
414+
dem <- merge_rasters(tmpfile, target_prj = prj, tmp_dir = tmp_dir)
395415
dem
396416
}
397417

man/get_aws_terrain.Rd

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_elev_raster.Rd

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_opentopo.Rd

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/merge_rasters.Rd

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)