-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_geodata.R
64 lines (55 loc) · 2.34 KB
/
search_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
#' Search available datasets for pattern matches
#'
#' \code{search_geodata} searches for matches to argument \code{pattern} within
#' the names of all available datasets on the geo-information platform of
#' the Swiss Confederation (\url{https://data.geo.admin.ch/}).
#'
#' @importFrom dplyr pull filter bind_rows distinct
#' @importFrom stringr str_replace_all str_detect
#' @importFrom magrittr "%>%"
#' @importFrom purrr map_lgl
#'
#' @param pattern character string containing a regular expression to be matched in
#' the dataset names on the geo-information platform.
#' @param fuzzy_support if \code{TRUE}, approximate matches are included.
#' @param include_links if \code{TRUE}, the links to available resources are included
#' in the results.
#' @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}).
#'
#' @return A tibble with matching datasets and their retrieval function. The links
#' to available resources are included if \code{include_links = TRUE}.
#'
#' @examples
#' # Show all available datasets for the search term 'Agglomerations'
#' search_geodata(pattern = "Agglomeration")
#'
#' # Show all available datasets of the Federal Office for the Environment (BAFU)
#' search_geodata(pattern = "BAFU")
#'
#' @export
#'
search_geodata <- function(pattern, fuzzy_support = FALSE, include_links = FALSE, available_geodata) {
# Download available datasets
if (missing(available_geodata)) available_geodata <- get_available_geodata(include_links = include_links)
# Return all available datasets if no query is specified
if (missing(pattern)) return(available_geodata)
# Adjust Umlauts
pattern <- pattern %>%
tolower() %>%
stringr::str_replace_all("\u00e4", "ae") %>%
stringr::str_replace_all("\u00f6", "oe") %>%
stringr::str_replace_all("\u00fc", "ue")
# String matching
res <- dplyr::filter(available_geodata, stringr::str_detect(name, pattern))
# Fuzzy string matching
if (fuzzy_support) {
res2 <- dplyr::filter(available_geodata, purrr::map_lgl(name, function(x) agrepl(pattern, x)))
res <- dplyr::bind_rows(res, res2) %>% dplyr::distinct()
}
# Return result
return(res)
}