Skip to content

Commit 83ae7f5

Browse files
committed
Exposed ncpu and set default to 2 or 1 (depending on available cpus). closes #100
1 parent 041d88e commit 83ae7f5

10 files changed

+46
-24
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ Imports:
4040
License: MIT + file LICENSE
4141
Encoding: UTF-8
4242
LazyData: true
43-
RoxygenNote: 7.2.3
4443
Suggests:
4544
testthat,
4645
knitr,
4746
rmarkdown,
4847
formatR,
4948
progress
5049
VignetteBuilder: knitr
50+
RoxygenNote: 7.3.2

NEWS.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
elevatr 0.99.0 (2024-0x-xx)
1+
elevatr 1.0.0 (2024-0x-xx)
22
=============
33
# API Changes
44
- add argument for specifying temp directory for download files. Allows users to specify a specific location. (Thanks, @andrew-caudillo: https://github.com/jhollist/elevatr/issues/95)
5-
5+
- exposed ncpu argument so user can control. Defaults to 2 if more than 2 cores available. Thanks to @courtiol for finding this issue and the suggestion!
66

77
elevatr 0.99.0 (2023-09-11)
88
=============

R/get_elev_point.R

+15-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#' of points (e.g. > 500). The "aws" source may be quicker in these
2323
#' cases provided the points are in a similar geographic area. The
2424
#' "aws" source downloads a DEM using \code{get_elev_raster} and then
25-
#' extracts the elevation for each point.
25+
#' extracts the elevation for each point.
26+
#' @param ncpu Number of CPU's to use when downloading aws tiles. Defaults to 2
27+
#' if more than two available, 1 otherwise.
2628
#' @param overwrite A logical indicating that existing \code{elevation} and
2729
#' \code{elev_units} columns should be overwritten. Default is
2830
#' FALSE and \code{get_elev_point} will error if these columns
@@ -73,7 +75,8 @@
7375
#' mts_elev
7476
#' }
7577
#'
76-
get_elev_point <- function(locations, prj = NULL, src = c("epqs", "aws"),
78+
get_elev_point <- function(locations, prj = NULL, src = c("epqs", "aws"),
79+
ncpu = ifelse(future::availableCores() > 2, 2, 1),
7780
overwrite = FALSE, ...){
7881

7982
# First Check for internet
@@ -98,13 +101,13 @@ get_elev_point <- function(locations, prj = NULL, src = c("epqs", "aws"),
98101

99102
# Pass of reprojected to epqs or aws to get data as spatialpointsdataframe
100103
if (src == "epqs"){
101-
locations_prj <- get_epqs(locations, ...)
104+
locations_prj <- get_epqs(locations, ncpu = ncpu, ...)
102105
units <- locations_prj[[2]]
103106
locations_prj <- locations_prj[[1]]
104107
}
105108

106109
if(src == "aws"){
107-
locations_prj <- get_aws_points(locations, verbose = FALSE, ...)
110+
locations_prj <- get_aws_points(locations, ncpu = ncpu, verbose = FALSE, ...)
108111
units <- locations_prj[[2]]
109112
locations_prj <- locations_prj[[1]]
110113
}
@@ -149,6 +152,8 @@ get_elev_point <- function(locations, prj = NULL, src = c("epqs", "aws"),
149152
#' the second column is Latitude.
150153
#' @param units Character string of either meters or feet. Conversions for
151154
#' 'epqs' are handled by the API itself.
155+
#' @param ncpu Number of CPU's to use when downloading aws tiles. Defaults to 2
156+
#' if more than two available, 1 otherwise.
152157
#' @param ncpu Number of CPU's to use when downloading epqs data.
153158
#' @param serial Logical to determine if API should be hit in serial or in
154159
#' parallel. TRUE will use purrr, FALSE will use furrr.
@@ -159,7 +164,7 @@ get_elev_point <- function(locations, prj = NULL, src = c("epqs", "aws"),
159164
#' @importFrom purrr map_dbl
160165
#' @keywords internal
161166
get_epqs <- function(locations, units = c("meters","feet"),
162-
ncpu = future::availableCores() - 1,
167+
ncpu = ifelse(future::availableCores() > 2, 2, 1),
163168
serial = NULL){
164169

165170
ll_prj <- "EPSG:4326"
@@ -310,16 +315,19 @@ get_epqs <- function(locations, units = c("meters","feet"),
310315
#' @param units Character string of either meters or feet. Conversions for
311316
#' 'aws' are handled in R as the AWS terrain tiles are served in
312317
#' meters.
318+
#' @param ncpu Number of CPU's to use when downloading aws tiles. Defaults to 2
319+
#' if more than two available, 1 otherwise.
313320
#' @param verbose Report back messages.
314321
#' @param ... Arguments to be passed to \code{get_elev_raster}
315322
#' @return a list with a SpatialPointsDataFrame or sf POINT or MULTIPOINT object with
316323
#' elevation added to the data slot and a character of the elevation units
317324
#' @export
318325
#' @keywords internal
319-
get_aws_points <- function(locations, z = 5, units = c("meters", "feet"),
326+
get_aws_points <- function(locations, z = 5, units = c("meters", "feet"),
327+
ncpu = ifelse(future::availableCores() > 2, 2, 1),
320328
verbose = TRUE, ...){
321329
units <- match.arg(units)
322-
dem <- get_elev_raster(locations, z, verbose = verbose, ...)
330+
dem <- get_elev_raster(locations, z, ncpu = ncpu, verbose = verbose, ...)
323331
dem <- methods::as(dem, "SpatRaster")
324332
elevation <- units::set_units(terra::extract(dem, locations)[,2], "m")
325333
if(units == "feet"){

R/get_elev_raster.R

+10-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
4949
#' temporary location. Alternatively, the user may supply an
5050
#' existing path for these raster files. New folders are not
51-
#' created by \code{get_elev_raster}.
51+
#' created by \code{get_elev_raster}.
52+
#' @param ncpu Number of CPU's to use when downloading aws tiles. Defaults to 2
53+
#' if more than two available, 1 otherwise.
5254
#' @param ... Extra arguments to pass to \code{httr::GET} via a named vector,
5355
#' \code{config}. See
5456
#' \code{\link{get_aws_terrain}} for more details.
@@ -89,7 +91,9 @@ get_elev_raster <- function(locations, z, prj = NULL,
8991
src = c("aws", "gl3", "gl1", "alos", "srtm15plus"),
9092
expand = NULL, clip = c("tile", "bbox", "locations"),
9193
verbose = TRUE, neg_to_na = FALSE,
92-
override_size_check = FALSE, tmp_dir = tempdir(), ...){
94+
override_size_check = FALSE, tmp_dir = tempdir(),
95+
ncpu = ifelse(future::availableCores() > 2, 2, 1),
96+
...){
9397
# First Check for internet
9498
if(!curl::has_internet()) {
9599
message("Please connect to the internet and try again.")
@@ -130,7 +134,7 @@ get_elev_raster <- function(locations, z, prj = NULL,
130134
# Pass of locations to APIs to get data as raster
131135
if(src == "aws") {
132136
raster_elev <- get_aws_terrain(locations, z, prj = prj, expand = expand,
133-
tmp_dir = tmp_dir, ...)
137+
tmp_dir = tmp_dir, ncpu = ncpu, ...)
134138
} else if(src %in% c("gl3", "gl1", "alos", "srtm15plus")){
135139
raster_elev <- get_opentopo(locations, src, prj = prj, expand = expand,
136140
tmp_dir = tmp_dir, ...)
@@ -185,7 +189,8 @@ get_elev_raster <- function(locations, z, prj = NULL,
185189
#' bounding box that is used to fetch the terrain tiles. This can
186190
#' be used for features that fall close to the edge of a tile and
187191
#' additional area around the feature is desired. Default is NULL.
188-
#' @param ncpu Number of CPU's to use when downloading aws tiles.
192+
#' @param ncpu Number of CPU's to use when downloading aws tiles. Defaults to 2
193+
#' if more than two available, 1 otherwise.
189194
#' @param serial Logical to determine if API should be hit in serial or in
190195
#' parallel. TRUE will use purrr, FALSE will use furrr.
191196
#' @param tmp_dir The location to store downloaded raster files. Defaults to a
@@ -202,7 +207,7 @@ get_elev_raster <- function(locations, z, prj = NULL,
202207
#' @keywords internal
203208

204209
get_aws_terrain <- function(locations, z, prj, expand=NULL,
205-
ncpu = future::availableCores() - 1,
210+
ncpu = ifelse(future::availableCores() > 2, 2, 1),
206211
serial = NULL, tmp_dir = tempdir(), ...){
207212
# Expand (if needed) and re-project bbx to dd
208213

man/get_aws_points.Rd

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

man/get_aws_terrain.Rd

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

man/get_elev_point.Rd

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

man/get_elev_raster.Rd

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

man/get_epqs.Rd

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

vignettes/introduction_to_elevatr.Rmd

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
---
2-
title: "Accessing elevation data in R with the elevatr package, version 1"
2+
title: "Introduction to elevatr"
33
author: "Jeffrey W. Hollister"
44
date: '`r Sys.Date()`'
5-
output:
6-
html_document:
7-
theme: readable
8-
toc: yes
9-
toc_float: yes
5+
output: rmarkdown::html_vignette
106
vignette: >
117
%\VignetteIndexEntry{Introduction to elevatr}
128
%\VignetteEncoding{UTF-8}

0 commit comments

Comments
 (0)