diff --git a/.Rbuildignore b/.Rbuildignore index ea105ca6510d9d3a486dfe31fc5ca2ee02f7197a..9e2f5e5f1b1f7d96b3a6d0dc9f99457b4e9f2efc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,3 +3,4 @@ ^README\.Rmd$ ^LICENSE\.md$ ^man-roxygen$ +^docs$ diff --git a/.gitignore b/.gitignore index 5b6a0652566d10360493952aec6d4a4febc77083..234f0289784f91ce7588b028bd72e1de86c1beb3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData .Ruserdata +docs diff --git a/DESCRIPTION b/DESCRIPTION index 66d8374eb8d4cc3b20363c287517717c00072351..a3ffcfe07c4bcfb58340ead050efc4fbc0b95d57 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bnpe Type: Package Title: Retrieval Functions for the French national data bank of quantitative withdrawals (BNPE) -Version: 0.1.0 +Version: 0.1.0.9000 Authors@R: c( person("David", "Dorchies", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6595-7984"), email = "david.dorchies@inrae.fr") ) diff --git a/NAMESPACE b/NAMESPACE index 9959807e56b6474f1c9879e90c19b8c0cd9f173a..3932bb8410947a5aad67301ccd82e5653b326575 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,5 +3,6 @@ export(doQuery) export(getComSeriesDep) export(getCookie) +export(getOuvrageSeries) export(getTimeSeriesCom) export(getTimeSeriesDep) diff --git a/R/getOuvrageSeries.R b/R/getOuvrageSeries.R new file mode 100644 index 0000000000000000000000000000000000000000..95b31af0a5eea2ba4c58d4268e5781939bb4f673 --- /dev/null +++ b/R/getOuvrageSeries.R @@ -0,0 +1,63 @@ +#' Retrieve data by device ("ouvrage") (metadata and annual withdrawal volumes) +#' +#' @param code_sandre [character] Sandre identifier of the device +#' @param cookie a named [character] [vector] with the values of the cookies (See [getCookie]) +#' @inheritParams getCookie +#' +#' @return [list] compiling data of the device with the following items: +#' - `id`: BNPE identifier +#' - `code`: code Sandre +#' - `codeCommune`: insee code of the commune +#' - `codePrecision`: ??? +#' - `codeStatut`: ??? +#' - `codeTypeEau`: the withdrawal source which can take the following values: +#' - "CONT" for continental surface +#' - "SOUT" for subsurface +#' - "LIT" for littoral +#' - `codeUsage`: a numeric code related to water destination: energy, canals, turbined, AEP, industry or irrigation +#' - `commentaire`: comment +#' - `exploitationDebut`: start date of the +#' +#' @export +#' +#' @examples +#' getOuvrageSeries("OPR0000200109") +getOuvrageSeries <- function(code_sandre, + cfg = config::get(file = system.file("config.yml", + package = "bnpe")), + cookie = getCookie(cfg)) { + params <- list( + code = code_sandre + ) + + resp <- doQuery(url_path = cfg$url_ouvrage, + params = params, + cfg = cfg, + cookie = cookie) + l <- httr::content(resp, "parsed")[[1]] + + l$exploitationDebut <- as.Date( + format( + as.POSIXct(l$exploitationDebut / 1000, origin = "1970-01-01", tz = "CET") + ) + ) + + params <- list( + code_ouvrage = code_sandre, + ecrasant = "false" + ) + + resp <- doQuery(url_path = cfg$url_ouv_series, + params = params, + cfg = cfg, + cookie = cookie) + lTS <- httr::content(resp, "parsed") + hasVolume <- sapply(lTS, function(x) { !is.null(x$volume) }) + annees <- sapply(lTS, "[[", "annee") + volumes <- unlist(sapply(lTS, "[[", "volume")) + peuplements <- unlist(sapply(lTS, "[[", "peuplement")) + l$TS <- data.frame(annee = annees[hasVolume], + volume = volumes, + peuplement = peuplements[hasVolume]) + return(l) +} diff --git a/README.Rmd b/README.Rmd index 33f642219ad8a20c48c9fbf201d0dffdf4cee0b7..186b64011e9c99dbce998fb644f5e75278cef1a5 100644 --- a/README.Rmd +++ b/README.Rmd @@ -72,3 +72,12 @@ For getting list of communes of departement with corresponding withdrawal volume getComSeriesDep("08", 2016, code_usage = "AEP", code_type_eau = "CONT") ``` +### Properties and time series from one device ("ouvrage") + +For getting metadata and withdrawal yearly time series from on device identified by its "code Sandre" (See https://www.sandre.eaufrance.fr/atlas/srv/fre/catalog.search#/metadata/e315633f-0930-41e8-a1c4-61fb2303039c): + +```{r} +# metadata and timeseries from withdrawal "COPRIMANCHE-forage LD LE PACO (40m)" +ouvrage <- getOuvrageSeries("OPR0000200109") +str(ouvrage) +``` diff --git a/README.md b/README.md index 88a0ba30639ab8128094dc16431e343b80f653e2..c3fefc5a430b2eebfe8900f929fa1201d26983e4 100644 --- a/README.md +++ b/README.md @@ -112,3 +112,35 @@ getComSeriesDep("08", 2016, code_usage = "AEP", code_type_eau = "CONT") #> 3 08247 439424 #> 4 08420 95274 ``` + +### Properties and time series from one device (“ouvrageâ€) + +For getting metadata and withdrawal yearly time series from on device +identified by its “code Sandre†(See +<https://www.sandre.eaufrance.fr/atlas/srv/fre/catalog.search#/metadata/e315633f-0930-41e8-a1c4-61fb2303039c>): + +``` r +# metadata and timeseries from withdrawal "COPRIMANCHE-forage LD LE PACO (40m)" +ouvrage <- getOuvrageSeries("OPR0000200109") +str(ouvrage) +#> List of 12 +#> $ id : int 200108 +#> $ code : chr "OPR0000200109" +#> $ codeCommune : chr "50151" +#> $ codePrecision : int 1 +#> $ codeStatut : chr "Validé" +#> $ codeTypeEau : chr "SOUT" +#> $ codeUsage : chr "4" +#> $ commentaire : chr "" +#> $ exploitationDebut: Date[1:1], format: "1987-01-01" +#> $ geom :List of 2 +#> ..$ type : chr "Point" +#> ..$ coordinates:List of 2 +#> .. ..$ : num -1.58 +#> .. ..$ : num 49.2 +#> $ ouvNom : chr "COPRIMANCHE-forage LD LE PACO (40m)" +#> $ TS :'data.frame': 6 obs. of 3 variables: +#> ..$ annee : chr [1:6] "2013" "2014" "2015" "2016" ... +#> ..$ volume : int [1:6] 101970 84430 85220 87820 106219 118124 +#> ..$ peuplement: chr [1:6] "1" "1" "1" "1" ... +``` diff --git a/inst/config.yml b/inst/config.yml index b0bada0d45d634feb4c35c4407b74e378ae1d5ff..e1fd1ed1913a3d229159293dd5d51362e059c886 100644 --- a/inst/config.yml +++ b/inst/config.yml @@ -1,5 +1,7 @@ default: - url: https://bnpe.eaufrance.fr/Bnpe-Diffusion/synthese - url_time_series: synthese_temporelle - url_com_series: synthese_geographique + url: https://bnpe.eaufrance.fr/Bnpe-Diffusion + url_time_series: synthese/synthese_temporelle + url_com_series: synthese/synthese_geographique + url_ouvrage: suggest/get_ouvrages + url_ouv_series: synthese/synthese_temporelle_ouvrage user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 diff --git a/man/getOuvrageSeries.Rd b/man/getOuvrageSeries.Rd new file mode 100644 index 0000000000000000000000000000000000000000..17f3ed8cc43218e181953d9ed959fe6303914848 --- /dev/null +++ b/man/getOuvrageSeries.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/getOuvrageSeries.R +\name{getOuvrageSeries} +\alias{getOuvrageSeries} +\title{Retrieve data by device ("ouvrage") (metadata and annual withdrawal volumes)} +\usage{ +getOuvrageSeries( + code_sandre, + cfg = config::get(file = system.file("config.yml", package = "bnpe")), + cookie = getCookie(cfg) +) +} +\arguments{ +\item{code_sandre}{\link{character} Sandre identifier of the device} + +\item{cfg}{a \link{config} object Configuration of the communication. Use by default the internal package +configuration stored at location \code{system.file("config.yml", package = "bnpe")}} + +\item{cookie}{a named \link{character} \link{vector} with the values of the cookies (See \link{getCookie})} +} +\value{ +\link{list} compiling data of the device with the following items: +\itemize{ +\item \code{id}: BNPE identifier +\item \code{code}: code Sandre +\item \code{codeCommune}: insee code of the commune +\item \code{codePrecision}: ??? +\item \code{codeStatut}: ??? +\item \code{codeTypeEau}: the withdrawal source which can take the following values: +- "CONT" for continental surface +- "SOUT" for subsurface +- "LIT" for littoral +\item \code{codeUsage}: a numeric code related to water destination: energy, canals, turbined, AEP, industry or irrigation +\item \code{commentaire}: comment +\item \code{exploitationDebut}: start date of the +} +} +\description{ +Retrieve data by device ("ouvrage") (metadata and annual withdrawal volumes) +} +\examples{ +getOuvrageSeries("OPR0000200109") +}