-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_geodata.R
83 lines (68 loc) · 2.69 KB
/
download_geodata.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#' Download datasets
#'
#' \code{download_geodata} downloads datasets from the geo-information platform of
#' the Swiss Confederation (\url{https://data.geo.admin.ch/}).
#'
#' @importFrom utils browseURL download.file
#' @importFrom stringr str_replace_all
#' @importFrom purrr walk2
#'
#' @param dataset character string containing the name of an available dataset.
#' Use \code{get_available_geodata} to find the corresponding names.
#' @param path the directory to download files to.
#' @param unzip if \code{TRUE}, the downloaded folder is unzipped and the zip folder is
#' deleted.
#' @param available_geodata result of a previous call with \code{get_available_geodata}.
#' Optional argument.
#'
#' @details The acquisition and use of data or services is free of charge,
#' subject to the provisions on fair use (see \url{https://www.geo.admin.ch/terms-of-use}).
#'
#' @examples
#' \dontrun{
#' # Download data on building lines for motorways
#' download_geodata("ch.astra.baulinien-nationalstrassen")
#' }
#'
#' @export
#'
download_geodata <- function(dataset, path, unzip = TRUE, available_geodata) {
# Download available datasets
if (missing(available_geodata)) available_geodata <- get_available_geodata(include_links = T)
if (missing(path)) path = "."
# Search matching datasets
res <- search_geodata(dataset, include_links = T, available_geodata = available_geodata)
# Search, check and clarify
res <- search_geodata(dataset, include_links = T, available_geodata = available_geodata)
if (nrow(res) == 0) {
message("No matching dataset found.\n")
return(invisible())
}
if (nrow(res) > 1) {
choice <- utils::menu(
choices = c(res[["name"]], "No."),
title = "The entered dataset name is ambiguous. Would you like to view the metadata of any of the following datasets?"
)
} else {
choice <- 1
}
# Prep Data
if (choice == nrow(res) + 2) return(invisible(res))
if (choice == nrow(res) + 1) res <- res[is.na(res[["STAC_API"]]),]
if (choice <= nrow(res)) res <- res[choice,]
# 'Non-trivial' cases
if (nrow(res) == 1 && !is.na(res[["STAC_API"]])) {
message("Please download the data with via the STAC API (see get_stac_assets()) or via your browser.\nThe corresponding web page opens automatically in a few seconds.")
Sys.sleep(3)
utils::browseURL(stringr::str_replace_all(res[["download"]], "\\/de\\/", "/en/"))
return(invisible(res))
}
# Download
dl <- res[["download"]]
p0 <- paste0(path, "/", res[["name"]])
p1 <- paste0(p0, ".zip")
purrr::walk2(dl, p1, utils::download.file, mode = "wb")
if (unzip) purrr::walk2(p1, p0, function(x, y) unzip(x, exdir = y))
if (unzip) file.remove(p1)
return(invisible(res))
}