diff --git a/.gitignore b/.gitignore index 234f028..3ec1363 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,49 @@ -.Rproj.user +# History files .Rhistory +.Rapp.history + +# Session Data files .RData +.RDataTmp + +# User-specific files .Ruserdata -docs + +# Example code in package build process +*-Ex.R + +# Output files from R CMD build +/*.tar.gz + +# Output files from R CMD check +/*.Rcheck/ + +# RStudio files +.Rproj.user/ + +# produced vignettes +vignettes/*.html +*.pdf + +# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 +.httr-oauth + +# knitr and R markdown default cache directories +*_cache/ +/cache/ + +# Temporary files created by R markdown +*.utf8.md +*.knit.md + +# R Environment Variables +.Renviron + +# pkgdown site +docs/ + +# translation temp files +po/*~ + +# RStudio Connect folder +rsconnect/ diff --git a/NAMESPACE b/NAMESPACE index 9a6ed41..10d991c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,7 @@ export(get_ecoulement_campagnes) export(get_ecoulement_observations) export(get_ecoulement_stations) export(get_hydrobio_indices) -export(get_hydrobio_stations) +export(get_hydrobio_stations_hydrobio) export(get_hydrobio_taxons) export(get_hydrometrie_obs_elab) export(get_hydrometrie_observations_tr) diff --git a/R/get_hydrobio.R b/R/get_hydrobio.R index ed63f41..24d5b1b 100644 --- a/R/get_hydrobio.R +++ b/R/get_hydrobio.R @@ -4,7 +4,7 @@ #' The data originate from the "NAIADES" database. #' Available endpoints are: #' -#' - `get_hydrobio_stations` retrieves site data and locations +#' - `get_hydrobio_stations_hydrobio` retrieves site data and locations #' - `get_hydrobio_indices` retrieves bioassessment indices values #' - `get_hydrobio_taxons` retrieves taxa data #' @@ -21,9 +21,9 @@ #' if(interactive()) { #' # Retrieve the hydrobiology monitoring sites in the Pays-de-Loire region #' list_params(api = "hydrobio", -#' endpoint = "stations") +#' endpoint = "stations_hydrobio") #' -#' stations_pdl <- get_hydrobio_stations( +#' stations_pdl <- get_hydrobio_stations_hydrobio( #' list(code_region = 52)) #' #' # Retrieve the hydrobiological bioassessment indices in the city of Rennes @@ -40,10 +40,10 @@ #' taxons_rennes <- hubeau::get_hydrobio_taxons( #' list(code_commune = 35051)) #' } -get_hydrobio_stations <- function(params) +get_hydrobio_stations_hydrobio <- function(params) { l <- doApiQuery(api = "hydrobio", - endpoint = "stations", + endpoint = "stations_hydrobio", params = params) select_non_nested(l) %>% diff --git a/R/sysdata.rda b/R/sysdata.rda index 1fb8e30..5f313ce 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/utils.R b/R/utils.R index 57b8d4b..9a2839a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -9,7 +9,15 @@ #' #' @examples #' \dontrun{ -#' my_flat_list <- select_non_nested(my_nested_list) +#' l <- doApiQuery(api = "hydrobio", +#' endpoint = "stations_hydrobio", +#' params = list(code_region = 52)) +#' +#' select_non_nested(l) %>% +#' select(-code_commune, +#' -libelle_departement, +#' -(libelle_region:longitude)) %>% +#' distinct() #' } select_non_nested <- function(my_list) diff --git a/data-raw/config.yml b/data-raw/config.yml index 8c339be..dbef1e3 100644 --- a/data-raw/config.yml +++ b/data-raw/config.yml @@ -1,565 +1,21 @@ default: api_url: https://hubeau.eaufrance.fr/api + api_docs: api-docs user_agent: https://github.com/inrae/hubeau apis: prelevements: path: v1/prelevements - endpoints: - points_prelevement: - path: referentiel/points_prelevement - fields: - - code_bdlisa - - code_bss_point_eau - - code_commune_insee - - code_departement - - code_entite_hydro_cours_eau - - code_entite_hydro_plan_eau - - code_mer_ocean - - code_nature - - code_ouvrage - - code_point_prelevement - - code_type_milieu - - code_zone_hydro - - date_exploitation - - fields - - libelle_departement - - nappe_accompagnement - - nom_commune - - nom_point_prelevement - chroniques: - path: chroniques - fields: - - annee - - bbox - - code_commune_insee - - code_departement - - code_mode_obtention_volume - - code_ouvrage - - code_qualification_volume - - code_statut_instruction - - code_statut_volume - - code_usage - - distance - - fields - - latitude - - libelle_departement - - longitude - - nom_commune - - prelevement_ecrasant - - producteur_donnee - - volume_max - - volume_min - ouvrages: - path: referentiel/ouvrages - fields: - - bbox - - code_bdlisa - - code_commune_insee - - code_departement - - code_entite_hydro_cours_eau - - code_entite_hydro_plan_eau - - code_mer_ocean - - code_ouvrage - - code_type_milieu - - codes_points_prelevements - - date_exploitation - - distance - - fields - - latitude - - libelle_departement - - longitude - - nom_commune - - nom_ouvrage indicateurs_services: path: v0/indicateurs_services - endpoints: - communes: - path: communes - fields: - - annee - - code_commune - - code_departement - - detail_service - - fields - - type_service - indicateurs: - path: indicateurs - fields: - - annee - - code_indicateur - - fields - services: - path: services - fields: - - annee - - code_commune - - code_departement - - fields - - type_service hydrometrie: path: v1/hydrometrie - endpoints: - stations: - path: referentiel/stations - fields: - - bbox - - code_commune_station - - code_cours_eau - - code_departement - - code_region - - code_sandre_reseau_station - - code_site - - code_station - - date_fermeture_station - - date_ouverture_station - - distance - - en_service - - fields - - latitude - - libelle_cours_eau - - libelle_site - - libelle_station - - longitude - sites: - path: referentiel/sites - fields: - - bbox - - code_commune_site - - code_cours_eau - - code_departement - - code_region - - code_site - - code_troncon_hydro_site - - code_zone_hydro_site - - distance - - fields - - latitude - - libelle_cours_eau - - libelle_site - - longitude - observations_tr: - path: observations_tr - fields: - - bbox - - code_entite - - cursor - - date_debut_obs - - date_fin_obs - - distance - - fields - - grandeur_hydro - - latitude - - longitude - - timestep - obs_elab: - path: obs_elab - fields: - - bbox - - code_entite - - cursor - - date_debut_obs_elab - - date_fin_obs_elab - - distance - - fields - - grandeur_hydro_elab - - latitude - - longitude - - resultat_max - - resultat_min niveaux_nappes: path: v1/niveaux_nappes - endpoints: - chroniques: - path: chroniques - fields: - - code_bss - - date_debut_mesure - - date_fin_mesure - - fields - chroniques_tr: - path: chroniques_tr - fields: - - bbox - - bss_id - - code_bss - - date_debut_mesure - - date_fin_mesure - - fields - - niveau_ngf_max - - niveau_ngf_min - - profondeur_max - - profondeur_min - stations: - path: stations - fields: - - bbox - - bss_id - - code_bdlisa - - code_bss - - code_commune - - code_departement - - codes_masse_eau_edl - - date_recherche - - fields - - srid poisson: path: v1/etat_piscicole - endpoints: - observations: - path: observations - fields: - - code_operation - - date_operation - - etat_avancement_operation - - code_qualification_operation - - libelle_qualification_operation - - code_station - - libelle_station - - uri_station - - coordonnee_x_station - - coordonnee_y_station - - code_epsg_projection_station - - code_projection_station - - libelle_projection_station - - code_point_prelevement - - code_point_prelevement_aspe - - libelle_point_prelevement_aspe - - coordonnee_x_point_prelevement - - coordonnee_y_point_prelevement - - code_epsg_projection_point_prelevement - - code_projection_point_prelevement - - libelle_projection_point_prelevement - - lieu_dit_point_prelevement - - localisation_precise_point_prelevement - - longitude - - code_entite_hydro - - latitude - - libelle_entite_hydro - - uri_entite_hydro - - code_commune - - libelle_commune - - code_departement - - libelle_departement - - code_region - - libelle_region - - code_bassin - - libelle_bassin - - codes_dispositifs_collecte - - libelles_dispositifs_collecte - - uris_dispositifs_collecte - - protocole_peche - - objectifs_operation - - code_prelevement_elementaire - - type_prelevement_elementaire - - duree_prelevement_elementaire - - numero_passage - - type_points - - nombre_points - - nombre_points_sans_poisson - - nombre_points_facies_courant - - nombre_points_facies_plat - - nombre_points_facies_profond - - nombre_points_annexes - - nombre_points_berge - - nombre_points_chenal - - nom_ambiance - - longueur_ambiance - - largeur_ambiance - - profondeur_ambiance - - code_facies_ambiance - - libelle_facies_ambiance - - situation_ambiance - - localisation_ambiance - - classe_vitesse_courant_ambiance - - code_ombrage_ambiance - - libelle_ombrage_ambiance - - code_granulo_ambiance_dominant - - libelle_granulo_ambiance_dominant - - code_granulo_ambiance_accessoire - - libelle_granulo_ambiance_accessoire - - vegetation_ambiance - - abondance_cache_vegetation_aquatique_ambiance - - abondance_cache_embacles_ambiance - - abondance_cache_vegetation_bordure_ambiance - - abondance_cache_sous_berge_ambiance - - abondance_cache_granulo_ambiance - - observation_ambiance - - code_lot - - code_type_lot - - libelle_type_lot - - effectif_lot - - code_taxon - - code_alternatif_taxon - - nom_commun_taxon - - nom_latin_taxon - - uri_taxon - - taille_min_lot - - taille_max_lot - - type_longueur_lot - - poids_lot_mesure - - poids_lot_estime - - methode_estimation_poids_lot - - code_individu - - taille_individu - - type_longueur_individu - - mesure_individu_reelle - - poids_individu_mesure - - poids_individu_estime - - methode_estimation_poids_individu - - sexe_individu - - age_individu - - codes_pathologies_lot - - libelles_pathologies_lot - - codes_fractions_pathologies_lot - - libelles_fractions_pathologies_lot - - uris_fractions_pathologies_lot - - importances_pathologies_lot - - codes_pathologies_individu - - libelles_pathologies_individu - - codes_fractions_pathologies_individu - - libelles_fractions_pathologies_individu - - uris_fractions_pathologies_individu - - importances_pathologies_individu - - fields ecoulement: path: vbeta/ecoulement - endpoints: - stations: - path: stations - fields: - - code_station - - libelle_station - - uri_station - - code_departement - - libelle_departement - - code_commune - - libelle_commune - - code_region - - libelle_region - - code_bassin - - libelle_bassin - - coordonnee_x_station - - coordonnee_y_station - - code_projection_station - - libelle_projection_station - - code_epsg_station - - code_cours_eau - - libelle_cours_eau - - uri_cours_eau - - etat_station - - date_maj_station - - latitude - - longitude - - distance - - bbox - - fields - observations: - path: observations - fields: - - format - - code_station - - libelle_station - - code_departement - - libelle_departement - - code_commune - - libelle_commune - - code_region - - libelle_region - - code_bassin - - libelle_bassin - - code_cours_eau - - libelle_cours_eau - - code_campagne - - code_reseau - - libelle_reseau - - date_observation_min - - date_observation_max - - code_ecoulement - - libelle_ecoulement - - longitude - - latitude - - distance - - bbox - - page - - size - - fields - - accept - campagnes: - path: campagnes - fields: - - code_campagne - - date_campagne_min - - date_campagne_max - - code_type_campagne - - libelle_type_campagne - - code_reseau - - libelle_reseau - - code_departement - - libelle_departement - - page - - size - - fields - - accept hydrobio: path: v1/hydrobio - endpoints: - stations: - path: stations_hydrobio - fields: - - bbox - - code_bassin - - code_commune - - code_cours_eau - - code_departement - - code_masse_eau - - code_region - - code_sous_bassin - - code_station_hydrobio - - codes_appel_taxons - - codes_indices - - codes_reseaux - - codes_supports - - date_service - - distance - - fields - - format - - latitude - - libelle_station_hydrobio - - longitude - - page - - size - indices: - path: indices - fields: - - bbox - - code_banque_reference - - code_bassin - - code_commune - - code_cours_eau - - code_departement - - code_indice - - code_masse_eau - - code_prelevement - - code_qualification - - code_region - - code_sous_bassin - - code_station_hydrobio - - code_support - - date_debut_prelevement - - date_fin_prelevement - - distance - - fields - - format - - latitude - - libelle_indice - - longitude - - page - - resultat_indice_max - - resultat_indice_min - - size - taxons: - path: taxons - fields: - - bbox - - code_appel_taxon - - code_banque_reference - - code_bassin - - code_commune - - code_cours_eau - - code_departement - - code_indice - - code_masse_eau - - code_prelevement - - code_qualification - - code_region - - code_sous_bassin - - code_station_hydrobio - - code_support - - code_type_resultat - - codes_indices_operation - - date_debut_prelevement - - date_fin_prelevement - - distance - - fields - - format - - latitude - - longitude - - page - - size temperature: path: v1/temperature - endpoints: - station: - path: station - fields: - - bbox - - code_banque_reference - - code_bassin - - code_commune - - code_cours_eau - - code_departement - - code_eu_masse_eau - - code_masse_eau - - code_region - - code_sous_bassin - - code_station - - code_troncon_hydro - - date_debut_maj - - date_debut_mesure - - date_fin_maj - - date_fin_mesure - - distance - - exact_count - - fields - - latitude - - libelle_bassin - - libelle_commune - - libelle_cours_eau - - libelle_departement - - libelle_masse_eau - - libelle_region - - libelle_sous_bassin - - libelle_station - - longitude - - size - - type_entite_hydro - chronique: - path: chronique - fields: - - bbox - - code_banque_reference - - code_bassin - - code_commune - - code_cours_eau - - code_departement - - code_eu_masse_eau - - code_masse_eau - - code_qualification - - code_region - - code_sous_bassin - - code_station - - code_statut - - code_troncon_hydro - - date_debut_maj - - date_debut_mesure - - date_fin_maj - - date_fin_mesure - - distance - - fields - - format - - latitude - - libelle_bassin - - libelle_commune - - libelle_cours_eau - - libelle_departement - - libelle_masse_eau - - libelle_qualification - - libelle_region - - libelle_sous_bassin - - libelle_station - - libelle_statut - - longitude - - resultat_max - - resultat_min - - size - - type_entite_hydro diff --git a/data-raw/configuration.R b/data-raw/configuration.R index d6e873f..422887a 100644 --- a/data-raw/configuration.R +++ b/data-raw/configuration.R @@ -1,6 +1,46 @@ ## code to prepare `.cfg` dataset update.packages("config") +library(httr) + .cfg <- config::get(file = "data-raw/config.yml") +defineApis <- function(api, cfg) { + message("Processing API: ", api$path) + query <- file.path(cfg$api_url, api$path, cfg$api_docs) + d <- content(GET(query), as = "parsed") + sapply(c("swagger", "openapi"), function(fmt) { + if (!is.null(d[[fmt]])) { + message(fmt, " ", d[[fmt]]) + } + }) + path_endpoints <- names(d$paths) + path_endpoints <- path_endpoints[!grepl(".csv", path_endpoints, fixed = TRUE)] + endpoints <- basename(path_endpoints) + fields <- lapply(path_endpoints, function(ep) { + if(!is.null(d$paths[[ep]]$get$parameters)) { + sapply(d$paths[[ep]]$get$parameters, "[[", "name") + } + }) + l <- lapply(seq_along(endpoints), function(i) { + path <- gsub(api$path, "", path_endpoints[i]) + path <- gsub("^/+", "", path) + list(path = path, + fields = fields[[i]]) + }) + names(l) <- endpoints + return(list(path = api$path, endpoints = l)) +} + +.cfg$apis <- lapply(.cfg$apis, defineApis, cfg = .cfg) + +# Fix issue https://github.com/BRGM/hubeau/issues/127 +ecoulement_ep <- names(.cfg$apis$ecoulement$endpoints) +for (ep in ecoulement_ep) { + which_fields <- which(grepl("fields", .cfg$apis$ecoulement$endpoints[[ep]]$fields)) + .cfg$apis$ecoulement$endpoints[[ep]]$fields[which_fields] <- "fields" +} + + + usethis::use_data(.cfg, overwrite = TRUE, internal = TRUE) diff --git a/man/get_hydrobio.Rd b/man/get_hydrobio.Rd index 1e1f0ea..21e007a 100644 --- a/man/get_hydrobio.Rd +++ b/man/get_hydrobio.Rd @@ -1,12 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/get_hydrobio.R -\name{get_hydrobio_stations} -\alias{get_hydrobio_stations} +\name{get_hydrobio_stations_hydrobio} +\alias{get_hydrobio_stations_hydrobio} \alias{get_hydrobio_indices} \alias{get_hydrobio_taxons} \title{Retrieve data from API "Hydrobiologie"} \usage{ -get_hydrobio_stations(params) +get_hydrobio_stations_hydrobio(params) get_hydrobio_indices(params) @@ -22,7 +22,7 @@ A \link[tibble:tibble]{tibble::tibble} with one row by record and one column by The data originate from the "NAIADES" database. Available endpoints are: \itemize{ -\item \code{get_hydrobio_stations} retrieves site data and locations +\item \code{get_hydrobio_stations_hydrobio} retrieves site data and locations \item \code{get_hydrobio_indices} retrieves bioassessment indices values \item \code{get_hydrobio_taxons} retrieves taxa data } @@ -33,9 +33,9 @@ See the API documentation for available filter parameters: \url{https://hubeau.e if(interactive()) { # Retrieve the hydrobiology monitoring sites in the Pays-de-Loire region list_params(api = "hydrobio", - endpoint = "stations") + endpoint = "stations_hydrobio") -stations_pdl <- get_hydrobio_stations( +stations_pdl <- get_hydrobio_stations_hydrobio( list(code_region = 52)) # Retrieve the hydrobiological bioassessment indices in the city of Rennes diff --git a/man/utils.Rd b/man/utils.Rd index bd87282..192d790 100644 --- a/man/utils.Rd +++ b/man/utils.Rd @@ -17,6 +17,14 @@ Selected the non-list elements in a nested list } \examples{ \dontrun{ -my_flat_list <- select_non_nested(my_nested_list) +l <- doApiQuery(api = "hydrobio", + endpoint = "stations_hydrobio", + params = list(code_region = 52)) + +select_non_nested(l) \%>\% + select(-code_commune, + -libelle_departement, + -(libelle_region:longitude)) \%>\% + distinct() } } diff --git a/tests/testthat/test-run_all_examples.R b/tests/testthat/test-run_all_examples.R new file mode 100644 index 0000000..afb0d91 --- /dev/null +++ b/tests/testthat/test-run_all_examples.R @@ -0,0 +1,29 @@ +skip_on_cran() +package <- "hubeau" + +with_warnings <- c("get_hydrometrie_obs_elab") + +db <- tools::Rd_db(package) + +lapply(db, function(Rd_fun) { + tags <- tools:::RdTags(Rd_fun) + which_ex <- which(tags=="\\examples") + name <- unlist(Rd_fun[tags == "\\name"]) + if (length(which_ex) > 0) { + ex <- unlist(Rd_fun[which_ex]) + ex <- gsub("\n", "", ex) + ex <- c("interactive <- function() TRUE", ex) + file <- file.path(tempdir(), paste0(name, ".R")) + writeLines(ex, file) + test_that(paste("Examples", name, "run without error"), { + if (name %in% with_warnings) { + suppressWarnings(source(file)) + } else { + source(file) + } + expect_equal(TRUE, TRUE) + }) + } +}) + +