From 640add3c510cec835a6cbd7b7d1921147675a0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BB=C3=B3=C5=82tak?= Date: Tue, 18 Dec 2018 12:08:54 +0100 Subject: [PATCH] S2_download() can now be interrupted. --- .travis.yml | 1 + DESCRIPTION | 2 +- NEWS.md | 3 ++- R/S2_download.R | 51 ++++++++++++++++++++++++++++--------------------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7d837d..5aac4ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ addons: apt: packages: - libgdal-dev + - libudunits2-dev after_success: - Rscript -e 'covr::coveralls(function_exclusions = c("\\.onLoad"))' diff --git a/DESCRIPTION b/DESCRIPTION index c447a41..1982a72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "sebastian.boeck@boku.ac.at", role = c("aut", "cre")), person("Mateusz", "Zoltak", email = "mateusz.zoltak@boku.ac.at", role = c("ctb")) diff --git a/NEWS.md b/NEWS.md index 7067f7f..2b42f9b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/S2_download.R b/R/S2_download.R index e6f540a..1c6ee8a 100644 --- a/R/S2_download.R +++ b/R/S2_download.R @@ -17,7 +17,7 @@ #' @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' @@ -25,7 +25,7 @@ #' 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', @@ -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( @@ -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) } @@ -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))