-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from rOpenSpain/beach
Add beaches
- Loading branch information
Showing
30 changed files
with
724 additions
and
457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,9 +192,6 @@ references: | |
email: [email protected] | ||
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 | ||
|
@@ -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: [email protected] | ||
orcid: https://orcid.org/0000-0001-8457-4658 | ||
affiliation: rOpenSpain | ||
year: '2024' | ||
- type: software | ||
title: rmarkdown | ||
abstract: 'rmarkdown: Dynamic Documents for R' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.