Skip to content

Commit

Permalink
feat(get_rv_meta)
Browse files Browse the repository at this point in the history
fixes #27
  • Loading branch information
dimfalk committed Dec 27, 2024
1 parent edd8a2e commit 959cc75
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: NRWgauges
Title: Web-scraping gauge (meta-)data from various providers in NRW, Germany
Version: 0.3.4
Version: 0.3.5
Date: 2024-12-27
Authors@R:
person("Dimitri", "Falk", , "[email protected]", role = c("aut", "cre"))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
export(get_eglv_gauges)
export(get_eglv_measurements)
export(get_eglv_meta)
export(get_rv_meta)
144 changes: 144 additions & 0 deletions R/get_rv_meta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#' Get (extended) metadata for selected RV gauges
#'
#' @param x Sf object containing gauges to be used for subsequent queries,
#' as provided by `get_rv_gauges()`.
#'
#' @return Tibble containing metadata.
#' @export
#'
#' @seealso [get_rv_gauges()]
#'
#' @examples
#' \dontrun{
#' gauge <- get_rv_gauges() |> dplyr::filter(station_nr == "2762715000100")
#' get_rv_meta(gauge)
#'
#' gauges <- get_rv_gauges() |> dplyr::filter(parameter_name == "Wasserstand")
#' get_rv_meta(gauges)
#' }
get_rv_meta <- function(x = NULL) {

# debugging ------------------------------------------------------------------

# x <- get_rv_gauges() |> dplyr::filter(station_nr == "2762715000100")

# x <- get_rv_gauges() |> dplyr::filter(parameter_name == "Wasserstand")

# check arguments ------------------------------------------------------------

checkmate::assert_tibble(x)

# ----------------------------------------------------------------------------

# init object to be returned
meta <- data.frame("id" = NA,
"name" = NA,
"operator" = NA,
"waterbody" = NA,
"municipality" = NA,
"X" = NA,
"Y" = NA,
"river_km" = NA,
"catchment_area" = NA,
"level_zero" = NA)

base_url <- "https://www.talsperrenleitzentrale-ruhr.de/online-daten/gewaesserpegel/"

# iterate over individual stations, initialize progress bar
ids <- x[["station_nr"]] |> unique()

n <- length(ids)

pb <- progress::progress_bar$new(format = "(:spin) [:bar] :percent || Iteration: :current/:total || Elapsed time: :elapsedfull",
total = n,
complete = "#",
incomplete = "-",
current = ">",
clear = FALSE,
width = 100)

for (i in 1:n) {

url <- paste0(base_url, ids[i], "/")

# query definition
query <- list("tx_onlinedata_gauges%5baction%5d" = "show",
"tx_onlinedata_gauges%5bcontroller%5d" = "Gauges")

# send request
r_raw <- httr::GET(url, query = query)

# TODO: dimfalk/NRWgauges#28
if(r_raw[["status_code"]] == 404) {

paste0("Gauge ID ", ids[i], " did not return any results. Skipping.") |> warning()

pb$tick()

next()
}

# parse response: html to text
a <- rvest::read_html(r_raw) |>
rvest::html_elements("div") |>
rvest::html_elements(".col-lg-5") |> # class
rvest::html_text() |>
stringr::str_remove_all("\\n") |>
stringr::str_split(pattern = "\\t") |>
unlist() |>
stringi::stri_remove_empty() |>
utils::tail(-1)

# TODO: dimfalk/NRWgauges#29
if(stringr::str_detect(a[1], pattern = "Zum Pegelbetreiber")) {

paste0("Gauge ID ", ids[i], " seems to be operated by LANUV NRW. Skipping for now.") |> warning()

pb$tick()

next()
}

len <- length(a)

keys <- a[seq(1, len, by = 2)]

vals <- a[seq(2, len, by = 2)]

coords <- vals[8] |>
stringr::str_remove("Lat ") |>
stringr::str_split("Long ") |>
unlist() |>
as.numeric()

meta["id"] <- vals[1]
meta["name"] <- vals[2]
meta["operator"] <- vals[3]
meta["waterbody"] <- vals[5]
meta["municipality"] <- NA
meta["X"] <- coords[2]
meta["Y"] <- coords[1]
meta["river_km"] <- NA
meta["catchment_area"] <- vals[7] |>
stringr::str_split_i(" ", i = 1) |>
stringr::str_replace(pattern = ",", replacement = ".") |>
as.numeric()
meta["level_zero"] <- vals[6] |> as.numeric()

# concatenate objects
if (!exists("meta_all")) {

meta_all <- tibble::as_tibble(meta)

} else {

meta_all <- rbind(meta_all, meta)
}

Sys.sleep(0.5)

pb$tick()
}

meta_all
}
30 changes: 30 additions & 0 deletions man/get_rv_meta.Rd

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

0 comments on commit 959cc75

Please sign in to comment.