Skip to content

Commit

Permalink
0.1.2
Browse files Browse the repository at this point in the history
Updated version
  • Loading branch information
zumbov2 committed Nov 21, 2022
1 parent 1354a84 commit c5adc08
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ Package: swissgd
Type: Package
Title: Interface to the Geo-Information Platform of the Swiss
Confederation
Version: 0.1.1
Version: 0.1.2
Authors@R: person("David", "Zumbach", , "[email protected]", c("aut", "cre"))
Description: Search and download data from the geo-information platform of the
Swiss Confederation (see <data.geo.admin.ch>) and query data from the
Spatial Temporal Asset Catalog (STAC) API (see <https://data.geo.admin.ch/api/stac/v0.9/>).
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
BugReports: https://github.com/zumbov2/swissgd/issues
URL:
https://www.geo.admin.ch/en/geo-services/geo-services/terms-of-use.html
RoxygenNote: 7.1.1
RoxygenNote: 7.2.2
Imports: dplyr, httr, magrittr, purrr, rvest, stringr, tibble, tidyr,
xml2, sf
sf
NeedsCompilation: no
Packaged: 2021-06-08 08:21:31 UTC
Packaged: 2022-11-21 00:08:03 UTC; david
Author: David Zumbach [aut, cre]
Maintainer: David Zumbach <[email protected]>
12 changes: 6 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ export(get_stac_collections)
export(search_geodata)
export(show_metadata)
export(show_preview)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,lag)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(dplyr,ungroup)
importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,modify_url)
Expand All @@ -29,6 +26,7 @@ importFrom(purrr,map_chr)
importFrom(purrr,map_int)
importFrom(purrr,map_lgl)
importFrom(purrr,walk2)
importFrom(rvest,html_attr)
importFrom(rvest,html_nodes)
importFrom(rvest,html_text)
importFrom(sf,st_as_sf)
Expand All @@ -38,9 +36,11 @@ importFrom(stringr,str_detect)
importFrom(stringr,str_replace_all)
importFrom(stringr,str_split)
importFrom(tibble,tibble)
importFrom(tidyr,complete)
importFrom(tidyr,fill)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,unnest_wider)
importFrom(utils,browseURL)
importFrom(utils,download.file)
importFrom(utils,menu)
importFrom(xml2,xml_attrs)
importFrom(utils,tail)
4 changes: 2 additions & 2 deletions R/download_geodata.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#'
#' @examples
#' \dontrun{
#' # Show all available records for the search term Agglomerations
#' download_geodata("ch.are.reisezeit-oev")
#' # Download data on building lines for motorways
#' download_geodata("ch.astra.baulinien-nationalstrassen")
#' }
#'
#' @export
Expand Down
77 changes: 38 additions & 39 deletions R/get_available_geodata.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#' of the Swiss Confederation (\url{https://data.geo.admin.ch/}).
#'
#' @importFrom httr GET content
#' @importFrom rvest html_nodes html_text
#' @importFrom xml2 xml_attrs
#' @importFrom purrr map_chr
#' @importFrom rvest html_nodes html_text html_attr
#' @importFrom tibble tibble
#' @importFrom dplyr mutate select group_by slice ungroup n lag
#' @importFrom tidyr pivot_wider
#' @importFrom dplyr mutate arrange rename select
#' @importFrom purrr map_chr
#' @importFrom stringr str_replace_all str_split str_detect
#' @importFrom tidyr fill complete pivot_wider
#' @importFrom magrittr "%>%"
#' @importFrom utils tail
#'
#' @param include_links if \code{TRUE}, links to available resources are included in the results.
#'
Expand All @@ -32,51 +32,50 @@ get_available_geodata <- function(include_links = FALSE) {
# Content
pg <- httr::GET("https://data.geo.admin.ch/")
pgc <- httr::content(pg, encoding = "UTF-8")

# Attributs
# Nodes
.nodes <- rvest::html_nodes(pgc, "#data > a")
.attrs <- purrr::map_chr(.nodes, xml2::xml_attrs)
.names <- rvest::html_text(.nodes)

# Results
res1 <- tibble::tibble(
name = .names,
attr = .attrs
) %>%

# Step 1: Build data frame from content
temp1 <-
tibble::tibble(
type = rvest::html_text(.nodes),
url = rvest::html_attr(.nodes, "href")
) %>%
dplyr::mutate(
name = stringr::str_replace_all(name, "\\s", "_"),
id = 1:dplyr::n(),
id = ifelse(!name == "download", dplyr::lag(id, 1), id),
id = ifelse(!name == "download", dplyr::lag(id, 1), id),
id = ifelse(!name == "download", dplyr::lag(id, 1), id),
id = ifelse(!name == "download", dplyr::lag(id, 1), id)
name = purrr::map_chr(url, function(x) stringr::str_split(x, "=") %>% unlist() %>% utils::tail(1)),
name = ifelse(!type == "metadata", NA, name)
) %>%
tidyr::pivot_wider(names_from = name, values_from = attr) %>%
dplyr::mutate(id = 1:dplyr::n())

names(res1) <- c("id", "download", "preview", "metadata", "STAC_API")

res2 <- res1 %>%
tidyr::fill(name, .direction = "up") %>%
tidyr::complete(name, type) %>%
dplyr::arrange(type) %>%
tidyr::pivot_wider(names_from = type, values_from = url) %>%

# Manual correction swissTLMRegio (2022-11-21)
dplyr::mutate(name = ifelse(name == "2a190233-498a-46c4-91ca-509a97d797a2", "swissTLMRegio", name)) %>%
dplyr::arrange(name) %>%
dplyr::rename("STAC_API" = "API")

# Step 2: Add retrieval function
temp2 <-
temp1 %>%
dplyr::mutate(
name = stringr::str_split(metadata, "="),
download = ifelse(
!stringr::str_detect(download, "https://"),
paste("https://data.geo.admin.ch", download, "data.zip", sep = "/"),
download
)
) %>%
dplyr::mutate(retrieval_function = ifelse(is.na(STAC_API), "swissgd::download_geodata()", "swissgd::get_stac_assets()")) %>%
dplyr::select(id, name, retrieval_function, names(res1)[!names(res1) %in% c("name", "id")]) %>%
tidyr::unnest(name) %>%
dplyr::group_by(id) %>%
dplyr::slice(2) %>%
dplyr::ungroup() %>%
dplyr::select(-id)
),
retrieval_function = ifelse(
is.na(STAC_API),
"swissgd::download_geodata()",
"swissgd::get_stac_assets()"),
.before = 2
)

# Links
if (!include_links) res2 <- dplyr::select(res2, name, retrieval_function)
if (!include_links) temp2 <- dplyr::select(temp2, name, retrieval_function)

# Return
return(res2)
return(temp2)

}
2 changes: 1 addition & 1 deletion R/swissgd.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(getRversion() >= "2.15.1") {

utils::globalVariables(
c(
"name", "id", "metadata", "download", "STAC_API",
"name", "type", "metadata", "download", "STAC_API",
"retrieval_function", "extent_spatial", "extent_temporal"
)
)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This R package is an interface to parts of the [Geo-Information Platform of the
The acquisition and use of data or services is free of charge, subject to the provisions on fair use. For more information, please see the [Terms of Use](https://www.geo.admin.ch/en/geo-services/geo-services/terms-of-use.html).

## Installation
Install from GitHub for a regularly updated version (latest: 0.1.1):
Install from GitHub for a regularly updated version (latest: 0.1.2):

```r
install.packages("devtools")
Expand Down Expand Up @@ -140,9 +140,9 @@ swissgd::download_stac_assets(res)
**Idea**: Examine and visualise the spatial distribution of place name suffixes using spatial kernel density estimation.
**Datasets**: ch.swisstopo.swissnames3d, ch.swisstopo.swissboundaries3d-land-flaeche.fill
**Packages**: `sf`, `raster`, `btb`, `ggplot2` and friends
**Script**: [ex1_swissnames.R](https://github.com/zumbov2/swissgd/blob/main/examples/ex1_swissnames.R)
**Script**: [ex1_swissnames.R](https://github.com/zumbov2/swissgd/blob/main/examples/example1/ex1_swissnames.R)

### Some Results
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/ex1_1.png" width="600">
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/ex1_2.png" width="600">
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/ex1_3.png" width="600">
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/example1/ex1_1.png" width="600">
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/example1/ex1_2.png" width="600">
<img src="https://raw.githubusercontent.com/zumbov2/swissgd/main/examples/example1/ex1_3.png" width="600">
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
4 changes: 2 additions & 2 deletions man/download_geodata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions swissgd.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source

0 comments on commit c5adc08

Please sign in to comment.