44
44
# ' between 100 Mb and 500Mb report a message but
45
45
# ' continue. Between 500Mb and 3000Mb requires
46
46
# ' 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}.
48
52
# ' @param ... Extra arguments to pass to \code{httr::GET} via a named vector,
49
53
# ' \code{config}. See
50
54
# ' \code{\link{get_aws_terrain}} for more details.
76
80
# ' max=sf::st_bbox(lake)$ymax))
77
81
# '
78
82
# ' 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 )
80
84
# ' x <- get_elev_raster(lake, src = "gl3", expand = 5000)
81
85
# ' x <- get_elev_raster(lake_buff, z = 10, clip = "locations")
82
86
# ' }
@@ -92,6 +96,7 @@ get_elev_raster <- function(locations, z, prj = NULL,
92
96
return (NULL )
93
97
}
94
98
99
+ tmp_dir <- normalizePath(tmp_dir , mustWork = TRUE )
95
100
src <- match.arg(src )
96
101
clip <- match.arg(clip )
97
102
@@ -124,9 +129,11 @@ get_elev_raster <- function(locations, z, prj = NULL,
124
129
125
130
# Pass of locations to APIs to get data as raster
126
131
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 , ... )
128
134
} 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 , ... )
130
137
}
131
138
sources <- attr(raster_elev , " sources" )
132
139
if (is.null(sources )){sources <- src }
@@ -181,6 +188,10 @@ get_elev_raster <- function(locations, z, prj = NULL,
181
188
# ' @param ncpu Number of CPU's to use when downloading aws tiles.
182
189
# ' @param serial Logical to determine if API should be hit in serial or in
183
190
# ' 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}.
184
195
# ' @param ... Extra configuration parameters to be passed to httr::GET. Common
185
196
# ' usage is to adjust timeout. This is done as
186
197
# ' \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,
229
240
clear = FALSE ,
230
241
width = 60
231
242
))
243
+
232
244
progressr :: with_progress({
233
245
if (serial ){
234
246
@@ -255,7 +267,7 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
255
267
dem_list <- furrr :: future_map(urls ,
256
268
function (x ){
257
269
p()
258
- tmpfile <- tempfile(tempdir = tmp_dir , fileext = " .tif" )
270
+ tmpfile <- tempfile(tmpdir = tmp_dir , fileext = " .tif" )
259
271
resp <- httr :: GET(x ,
260
272
httr :: user_agent(" elevatr R package (https://github.com/jhollist/elevatr)" ),
261
273
httr :: write_disk(tmpfile ,overwrite = TRUE ), ... )
@@ -270,7 +282,7 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
270
282
}
271
283
})
272
284
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 )
274
286
sources <- unlist(lapply(dem_list , function (x ) attr(x , " source" )))
275
287
if (! is.null(sources )){
276
288
sources <- trimws(unlist(strsplit(sources , " ," )))
@@ -295,6 +307,10 @@ get_aws_terrain <- function(locations, z, prj, expand=NULL,
295
307
# ' @param method the method for resampling/reprojecting. Default is 'bilinear'.
296
308
# ' Options can be found [here](https://gdal.org/programs/gdalwarp.html#cmdoption-gdalwarp-r)
297
309
# ' @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}.
298
314
# ' @export
299
315
# ' @keywords internal
300
316
@@ -303,7 +319,7 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
303
319
304
320
message(paste(" Mosaicing & Projecting" ))
305
321
306
- destfile <- tempfile(tempdir = temp_dir , fileext = " .tif" )
322
+ destfile <- tempfile(tmpdir = tmp_dir , fileext = " .tif" )
307
323
files <- unlist(raster_list )
308
324
309
325
if (is.null(target_prj )){
@@ -318,7 +334,7 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
318
334
)
319
335
# Using two steps now as gdal with one step introduced NA's along seams
320
336
# Slower but more accurate!
321
- destfile2 <- tempfile(tempdir = temp_dir , fileext = " .tif" )
337
+ destfile2 <- tempfile(tmpdir = tmp_dir , fileext = " .tif" )
322
338
sf :: gdal_utils(util = " warp" ,
323
339
source = destfile ,
324
340
destination = destfile2 ,
@@ -347,6 +363,10 @@ merge_rasters <- function(raster_list, target_prj, method = "bilinear",
347
363
# ' argument is required for a \code{data.frame} of locations.
348
364
# ' @param expand A numeric value of a distance, in map units, used to expand the
349
365
# ' 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}.
350
370
# ' @param ... Extra configuration parameters to be passed to httr::GET. Common
351
371
# ' usage is to adjust timeout. This is done as
352
372
# ' \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(),
362
382
# Expand (if needed) and re-project bbx to ll_geo
363
383
bbx <- proj_expand(locations ,prj ,expand )
364
384
365
- tmpfile <- tempfile(tempdir = temp_dir )
385
+ tmpfile <- tempfile(tmpdir = tmp_dir )
366
386
base_url <- " https://portal.opentopography.org/API/globaldem?demtype="
367
387
data_set <- switch (src ,
368
388
gl3 = " SRTMGL3" ,
@@ -391,7 +411,7 @@ get_opentopo <- function(locations, src, prj, expand=NULL, tmp_dir = tempdir(),
391
411
if (httr :: http_type(resp ) != " application/octet-stream" ) {
392
412
stop(" API did not return octet-stream as expected" , call. = FALSE )
393
413
}
394
- dem <- merge_rasters(tmpfile , target_prj = prj )
414
+ dem <- merge_rasters(tmpfile , target_prj = prj , tmp_dir = tmp_dir )
395
415
dem
396
416
}
397
417
0 commit comments