Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beaches #58

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/check-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
schedule:
- cron: '27 17 * * 1,5'
workflow_dispatch:
Expand Down
16 changes: 13 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ references:
email: jeroen@berkeley.edu
orcid: https://orcid.org/0000-0002-4035-0289
year: '2024'
identifiers:
- type: url
value: https://arxiv.org/abs/1403.2805
version: '>= 1.7.0'
- type: software
title: rappdirs
Expand Down Expand Up @@ -360,6 +357,19 @@ references:
- family-names: Wickham
given-names: Hadley
year: '2024'
- type: software
title: mapSpain
abstract: 'mapSpain: Administrative Boundaries of Spain'
notes: Suggests
url: https://ropenspain.github.io/mapSpain/
repository: https://CRAN.R-project.org/package=mapSpain
authors:
- family-names: Hernangómez
given-names: Diego
email: diego.hernangomezherrero@gmail.com
orcid: https://orcid.org/0000-0001-8457-4658
affiliation: rOpenSpain
year: '2024'
- type: software
title: rmarkdown
abstract: 'rmarkdown: Dynamic Documents for R'
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ Suggests:
knitr,
lifecycle,
lubridate,
mapSpain,
rmarkdown,
scales,
sf (>= 0.9.0),
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Config/Needs/website: mapSpain, classInt, terra, sf, gstat, geoR,
tidyverse, tidyterra, scales, gifski, reactable, leaflet, geofacet,
devtools, crosstalk, usethis
Config/Needs/website: classInt, terra, sf, gstat, geoR, tidyverse,
tidyterra, scales, gifski, reactable, leaflet, geofacet, devtools,
crosstalk, usethis
Config/testthat/edition: 3
Config/testthat/parallel: true
Copyright: © AEMET. See file COPYRIGHTS
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(aemet_api_key)
export(aemet_beaches)
export(aemet_daily_clim)
export(aemet_daily_period)
export(aemet_daily_period_all)
export(aemet_detect_api_key)
export(aemet_extremes_clim)
export(aemet_forecast_beaches)
export(aemet_forecast_daily)
export(aemet_forecast_hourly)
export(aemet_forecast_tidy)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# climaemet (development version)

- Migrate from **httr** to **httr2** (#50).
- New functions for beaches: `aemet_forecast_beaches()` and `aemet_beaches()`
(#54).
- Use progress bars in downloads thanks to **cli**. New argument
`progress = TRUE` in most functions.
- It is possible to use several API keys to avoid API throttling, see
`?climaemet::aemet_api_key` (#53).
- New helper function `dms2decdegrees_2()`.
- Update `aemet_munic` with January 2024 data.
- New package in 'Suggests': **mapSpain**.

# climaemet 1.2.1

Expand Down
108 changes: 108 additions & 0 deletions R/aemet_beaches.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# valores-climatologicos
# https://opendata.aemet.es/dist/index.html#/

#' AEMET beaches
#'
#' Get AEMET beaches.
#'
#' @family aemet_api_data
#'
#'
#' @inheritParams aemet_daily_clim
#'
#' @inheritParams aemet_last_obs
#'
#' @return A [`tibble`][tibble::tibble()] or a \CRANpkg{sf} object.
#'
#' @inheritSection aemet_daily_clim API Key
#'
#' @seealso [aemet_forecast_beaches()]
#'
#' @details
#' The first result of the API call on each session is (temporarily) cached in
#' the assigned [tempdir()] for avoiding unneeded API calls.
#'
#' @examplesIf aemet_detect_api_key()
#' library(tibble)
#' beaches <- aemet_beaches()
#' beaches
#'
#' # Cached during this R session
#' beaches2 <- aemet_beaches(verbose = TRUE)
#'
#' identical(beaches, beaches2)
#'
#' # Select an map beaches
#' library(dplyr)
#' library(ggplot2)
#' library(mapSpain)
#'
#' # Alicante / Alacant
#' beaches_sf <- aemet_beaches(return_sf = TRUE) %>%
#' filter(ID_PROVINCIA == "03")
#'
#' prov <- mapSpain::esp_get_prov("Alicante")
#'
#' ggplot(prov) +
#' geom_sf() +
#' geom_sf(
#' data = beaches_sf, shape = 4, size = 2.5,
#' color = "blue"
#' )
#'
#' @export
aemet_beaches <- function(verbose = FALSE, return_sf = FALSE) {
# Validate inputs----
stopifnot(is.logical(verbose))
stopifnot(is.logical(return_sf))

cached_df <- file.path(tempdir(), "aemet_beaches.rds")
cached_date <- file.path(tempdir(), "aemet_beaches_date.rds")

if (file.exists(cached_df)) {
df <- readRDS(cached_df)
dat <- readRDS(cached_date)

if (verbose) {
message(
"Loading beaches from temporal cached file saved at ",
format(dat, usetz = TRUE)
)
}
} else {
# download beaches
url <- paste0(
"https://www.aemet.es/documentos/es/eltiempo/",
"prediccion/playas/Playas_codigos.csv"
)
r <- httr2::request(url)
r <- httr2::req_perform(r)
body <- httr2::resp_body_raw(r)
df <- readr::read_delim(body,
delim = ";",
show_col_types = FALSE,
locale = readr::locale(
encoding = "ISO-8859-1"
),
trim_ws = TRUE
)

# Formats----

df$longitud <- vapply(df$LONGITUD, dms2decdegrees_2, FUN.VALUE = numeric(1))
df$latitud <- vapply(df$LATITUD, dms2decdegrees_2, FUN.VALUE = numeric(1))



# Cache on temp dir
saveRDS(df, cached_df)
saveRDS(Sys.time(), cached_date)
}

# Validate sf----
if (return_sf) {
df <- aemet_hlp_sf(df, "latitud", "longitud", verbose)
}

return(df)
}
Loading
Loading