diff --git a/NAMESPACE b/NAMESPACE index 0bf79c9ae6071cfd10192d43e6ca273429e77e9e..f3d41cac6b6a17f5eff5b24510d46ba97f92162c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(doApiQuery) export(get_available_params) +export(get_hydrometrie_sites) export(get_hydrometrie_stations) export(get_indicateurs_services_communes) export(get_indicateurs_services_indicateurs) diff --git a/R/get_hydrometrie_sites.R b/R/get_hydrometrie_sites.R new file mode 100644 index 0000000000000000000000000000000000000000..c80f443f74322283610db94f7d62297d45bee429 --- /dev/null +++ b/R/get_hydrometrie_sites.R @@ -0,0 +1,61 @@ +#' Retrieve hydrometric sites from API "Hydrométrie" +#' +#' See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-hydrometrie} +#' +#' @template param_get_common +#' @param unique_site optional [logical], if set to `FALSE` sites with several different locations produce one row by different location otherwise the first location found is used for fields `code_commune_site`, `libelle_commune`, `code_departement`, `code_region`, `libelle_region`, `libelle_departement` +#' +#' @return a [tibble::tibble] with all available parameters in columns and one row by device, year and usage. +#' @export +#' +#' @examples +#' # For retrieving the hydrometric sites in the department of Aube +#' get_hydrometrie_sites(list(code_departement = "10")) +#' +#' # The same operation returning 2 rows for the site 'H0203020' which has 2 different locations +#' get_hydrometrie_sites(list(code_departement = "10"), unique_site = FALSE) +#' +get_hydrometrie_sites <- function(params, + unique_site = TRUE, + cfg = config::get(file = system.file("config.yml", + package = "hubeau"))) { + l <- doApiQuery( + api = "hydrometrie", + operation = "sites", + params = params, + cfg = cfg + ) + l <- lapply(l, function(x) { + fields <- + c( + "code_commune_site", + "libelle_commune", + "code_departement", + "code_region", + "libelle_region", + "libelle_departement" + ) + bFirst <- TRUE + for (field in fields) { + fieldValue <- unique(unlist(x[[field]])) + if (unique_site && length(fieldValue) > 1) { + if(bFirst) { + warning( + "The site '", + x$code_site, + "' has ", + length(fieldValue), + " different locations, only the first one is returned", + call. = FALSE + ) + bFirst <- FALSE + } + fieldValue <- x[[field]][[1]] + } + x[[field]] <- fieldValue + } + x$geometry <- NULL + x + }) + convert_list_to_tibble(l) +} diff --git a/R/hubeau.R b/R/hubeau.R index d994ce5b7d83c6a346d4c8be2b0d8a5352eaa8b1..02c8aff6d77231ce5bf85f5cc72f303e7d860694 100644 --- a/R/hubeau.R +++ b/R/hubeau.R @@ -10,6 +10,7 @@ #' #' Available functions: #' +#' - [get_hydrometrie_sites] #' - [get_hydrometrie_stations] #' #' **API "Indicateurs des services"** diff --git a/inst/config.yml b/inst/config.yml index 542126d5c059a982bcbb82e26fb7b88ec2387e65..74f4d628ff8d8f50053806601c1032d77fc58251 100644 --- a/inst/config.yml +++ b/inst/config.yml @@ -119,3 +119,20 @@ default: - 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 diff --git a/man/get_hydrometrie_sites.Rd b/man/get_hydrometrie_sites.Rd new file mode 100644 index 0000000000000000000000000000000000000000..07086d2fe1361eef04a608c107816b6e5997e79a --- /dev/null +++ b/man/get_hydrometrie_sites.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_hydrometrie_sites.R +\name{get_hydrometrie_sites} +\alias{get_hydrometrie_sites} +\title{Retrieve hydrometric sites from API "Hydrométrie"} +\usage{ +get_hydrometrie_sites( + params, + unique_site = TRUE, + cfg = config::get(file = system.file("config.yml", package = "hubeau")) +) +} +\arguments{ +\item{params}{a \link{list} the list of parameters of the queries and their values in the format \code{list(ParamName = "Param value", ...)}, use the function \link{get_available_params} for a list of the available parameters for a given operation in an API and see the API documentation for the complete list of available filter parameters} + +\item{unique_site}{optional \link{logical}, if set to \code{FALSE} sites with several different locations produce one row by different location otherwise the first location found is used for fields \code{code_commune_site}, \code{libelle_commune}, \code{code_departement}, \code{code_region}, \code{libelle_region}, \code{libelle_departement}} + +\item{cfg}{a \link{config} object Configuration of the communication. Use by default the internal package +configuration} +} +\value{ +a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns and one row by device, year and usage. +} +\description{ +See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-hydrometrie} +} +\examples{ +# For retrieving the hydrometric sites in the department of Aube +get_hydrometrie_sites(list(code_departement = "10")) + +# The same operation returning 2 rows for the site 'H0203020' which has 2 different locations +get_hydrometrie_sites(list(code_departement = "10"), unique_site = FALSE) + +} diff --git a/man/hubeau.Rd b/man/hubeau.Rd index 130be14dfb7bad43e9a92851b3700b19140f93b0..f43f154a7827e0df84a17bc7c19021cd4879a951 100644 --- a/man/hubeau.Rd +++ b/man/hubeau.Rd @@ -16,6 +16,7 @@ API documentation: \url{https://hubeau.eaufrance.fr/page/api-hydrometrie} Available functions: \itemize{ +\item \link{get_hydrometrie_sites} \item \link{get_hydrometrie_stations} }