Skip to content

Commit

Permalink
S2_download() can now be interrupted.
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Dec 18, 2018
1 parent 541f9b5 commit 640add3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ addons:
apt:
packages:
- libgdal-dev
- libudunits2-dev

after_success:
- Rscript -e 'covr::coveralls(function_exclusions = c("\\.onLoad"))'
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sentinel2
Title: Tools to access Sentinel-2 data pre-processed by IVFL, BOKU Vienna
Version: 0.3.0
Version: 0.3.1
Authors@R: c(
person("Sebastian", "Boeck", email = "[email protected]", role = c("aut", "cre")),
person("Mateusz", "Zoltak", email = "[email protected]", role = c("ctb"))
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 0.3.0 (2018-12-18)
# 0.3.1 (2018-12-18)

* It is now possible to abort the `S2_download()` function.
* A `spatial` parameter of the `S2_query_granule()` and `S2_query_image()` is now
a string and allows to choose between the `sp` package and the `sf` package spatial
object representation.
Expand Down
51 changes: 29 additions & 22 deletions R/S2_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
#' @examples
#' \dontrun{
#' # find, download and unzip a full granule
#' granules <- S2_query_granule(
#' granules = S2_query_granule(
#' utm = '33UXP',
#' dateMin = '2016-06-01',
#' dateMax = '2016-06-30'
#' )
#' S2_download(granules$url, granules$date)
#'
#' # find and download a bunch of images
#' images <- S2_query_image(
#' images = S2_query_image(
#' utm = '33UXP',
#' dateMin = '2016-06-01',
#' dateMax = '2016-06-30',
Expand All @@ -43,7 +43,7 @@
#' )
#' }

S2_download <- function(url, destfile, zip = TRUE, skipExisting = TRUE, progressBar = TRUE, ...){
S2_download = function(url, destfile, zip = TRUE, skipExisting = TRUE, progressBar = TRUE, ...){
url = as.character(url)
destfile = as.character(destfile)
stopifnot(
Expand All @@ -54,26 +54,26 @@ S2_download <- function(url, destfile, zip = TRUE, skipExisting = TRUE, progress
length(url) == length(destfile)
)
filter = !is.na(url)
url <- url[filter]
destfile <- destfile[filter]
url = url[filter]
destfile = destfile[filter]
stopifnot(all(!is.na(destfile)))

addParam = list(...)
if (zip) {
addParam[['format']] <- "application/zip"
addParam[['format']] = "application/zip"
}
if (length(addParam) > 0) {
stopifnot(
all(names(addParam) != ''),
length(unique(names(addParam))) == length(addParam)
)
addParamNames <- sapply(names(addParam), utils::URLencode)
addParamValues <- sapply(as.character(addParam), utils::URLencode)
addParam <- paste0(addParamNames, '=', addParamValues, collapse = '&')
url <- paste0(url, '?', addParam)
addParamNames = sapply(names(addParam), utils::URLencode)
addParamValues = sapply(as.character(addParam), utils::URLencode)
addParam = paste0(addParamNames, '=', addParamValues, collapse = '&')
url = paste0(url, '?', addParam)
}

success <- rep(FALSE, length(url))
success = rep(FALSE, length(url))
if (progressBar) {
pb = txtProgressBar(0, length(url), style = 3)
}
Expand All @@ -86,19 +86,26 @@ S2_download <- function(url, destfile, zip = TRUE, skipExisting = TRUE, progress
setTxtProgressBar(pb, i)
}

try({
curl::curl_download(url = url[i], destfile = destfile[i], quiet = TRUE)
tryCatch(
{
curl::curl_download(url = url[i], destfile = destfile[i], quiet = TRUE)

signature = readBin(destfile[i], 'raw', 4)
if (all(signature == as.raw(c(80L, 75L, 3L, 4L))) & zip) {
destfile[i] = sub('[.]zip$', '', destfile[i])
zipfile = paste0(destfile[i], '.zip')
file.rename(destfile[i], zipfile)
utils::unzip(zipfile = zipfile, exdir = destfile[i])
}
signature = readBin(destfile[i], 'raw', 4)
if (all(signature == as.raw(c(80L, 75L, 3L, 4L))) & zip) {
destfile[i] = sub('[.]zip$', '', destfile[i])
zipfile = paste0(destfile[i], '.zip')
file.rename(destfile[i], zipfile)
utils::unzip(zipfile = zipfile, exdir = destfile[i])
}

success[i] = TRUE
})
success[i] = TRUE
},
warning = function(w) {
if (all(w$message == 'Operation was aborted by an application callback')) {
break
}
}
)
}

return(invisible(success))
Expand Down

0 comments on commit 640add3

Please sign in to comment.