R/example.R

Defines functions download_it get_example_data

Documented in get_example_data

## get all the files, though avoid re-downloading
download_it <- function(x, path = "geotiff") {
  file <- file.path(path, basename(x))
  if (!file.exists(file)) {
    curl::curl_download(x, file)
    return(TRUE)
  }
  FALSE
}

#' Built-in example
#'
#' Download 18 example files in GeoTIFF format (total 12 Mb).
#'
#' @return data frame of file names and their start date
#' @export
#' @importFrom tibble tibble
#' @importFrom utils askYesNo
#' @importFrom purrr map_lgl
#' @examples
#' \dontrun{
#' files <- get_example_data()
#' }
get_example_data <- function() {
  appdir <- getOption("xytract.appdir")
  cache <- appdir$cache()
  ok <- TRUE
#  if (interactive())  ok <- utils::askYesNo(sprintf("Download files to %s", cache))

  if (!ok) {
    stop("no files downloaded")
  }
  dirtarget <- file.path(cache, "geotiff")
  dir.create(dirtarget, showWarnings = FALSE, recursive = TRUE)
  ## TRUE if we downloaded it, FALSE if it was already there
  test <- unlist(lapply(dat$fn, download_it, path = dirtarget))
 ## now build a file-db
 tibble::tibble(fullname = list.files(dirtarget, full.names = TRUE, pattern = "tif$"),
                date = as.POSIXct(strptime(basename(fullname), "N_%Y%m%d", tz = "UTC")))

}
mdsumner/xytract documentation built on May 21, 2019, 12:21 p.m.