-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_stac_collections.R
49 lines (45 loc) · 1.77 KB
/
get_stac_collections.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
#' Display all data retrievable via the STAC API
#'
#' \code{get_stac_collections} displays a description of all data provided
#' via the Spatial Temporal Asset Catalog (STAC) REST Interface on the
#' geo-information platform of the Swiss Confederation
#' (\url{https://data.geo.admin.ch/api/stac/v0.9/}).
#'
#' @importFrom httr GET content
#' @importFrom tibble tibble
#' @importFrom purrr map_chr map
#' @importFrom tidyr unnest_wider
#' @importFrom magrittr "%>%"
#'
#' @param api link to query endpoint listing all available datasets.
#'
#' @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 a set of metadata (name, id, description, spatial and temporal extent)
#' about all available geospatial datasets.
#'
#' @examples
#' # Show all available datasets of the STAC API
#' get_stac_collections()
#'
#' @export
#'
get_stac_collections <- function(api = "https://data.geo.admin.ch/api/stac/v0.9/collections") {
# Fetch
res <- httr::GET(api)
cnt <- httr::content(res)
# Extract
dt <- tibble::tibble(
title = purrr::map_chr(cnt[["collections"]], "title"),
id = purrr::map_chr(cnt[["collections"]], "id"),
description = purrr::map_chr(cnt[["collections"]], "description"),
udated = purrr::map_chr(cnt[["collections"]], "updated"),
extent_spatial = purrr::map(purrr::map(purrr::map(cnt[["collections"]], "extent"), "spatial"), unlist),
extent_temporal = purrr::map(purrr::map(purrr::map(cnt[["collections"]], "extent"), "temporal"), unlist),
license = purrr::map_chr(cnt[["collections"]], "license")
) %>%
tidyr::unnest_wider(extent_spatial) %>%
tidyr::unnest_wider(extent_temporal)
return(dt)
}