Skip to content

Commit

Permalink
Merge pull request #1 from richaben/master
Browse files Browse the repository at this point in the history
Proposition d'ajouts pour ondetools
  • Loading branch information
PascalIrz authored Feb 1, 2024
2 parents 915df78 + b2817f8 commit d0e6de0
Show file tree
Hide file tree
Showing 51 changed files with 2,610 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
^pkgdown$
^doc$
^\.github/workflows$
^dev/dev_history_file\.R$
^dev$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
Meta
raw_data
docs
dev/dev_history_file.R
inst/doc
/doc/
/Meta/
26 changes: 23 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,46 @@ Description: Le package propose un ensemble de fonctions facilitant l'exploitati
downloading the annual data files, tidying them and displaying the results on an interactive
map.
License: GPL-3 + file LICENSE
Imports: data.table,
Imports:
cli,
data.table,
dplyr,
forcats,
furrr,
ggplot2,
ggrepel,
ggspatial,
glue,
grDevices,
hubeau (>= 0.4.1),
leafpop,
lubridate,
magrittr,
mapview,
progressr,
purrr,
rlang,
rmarkdown,
rvest,
scales,
sf,
stringr,
tidyr,
utils,
xml2
xml2
Encoding: UTF-8
LazyData: true
LazyDataCompression: bzip2
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown
flextable,
patchwork,
magick
VignetteBuilder: knitr
URL: https://pascalirz.github.io/ondetools/
Depends:
R (>= 2.10)
Remotes:
MaelTheuliere/COGiter,
inrae/hubeau
76 changes: 76 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(ajouter_donnees_assecs_aux_stations)
export(ajouter_zones_propluvia)
export(assembler_fichiers_onde_annuels_csv)
export(calculer_assecs_ete)
export(calculer_assecs_heatmap)
export(calculer_bilan_ecoulement)
export(calculer_indice_onde)
export(calculer_recurrence_assecs)
export(completer_observations_mois_manquants)
export(creer_couche_geo_stations)
export(creer_variable_Mois)
Expand All @@ -11,45 +16,116 @@ export(gerer_les_campagnes)
export(lire_couche_sages)
export(passer_en_format_large)
export(produire_carte_dynamique)
export(produire_carte_statique)
export(produire_graph_heatmap_assecs)
export(produire_graph_pour_toutes_les_stations)
export(produire_graph_pour_une_station)
export(produire_graph_pour_une_station_v2)
export(produire_graph_reccurrence_assecs)
export(produire_graph_type_ecoulement)
export(produire_rapport_mensuel_dpt)
export(telecharger_donnees_onde_api)
export(telecharger_fichiers_onde_annuels)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_h1)
importFrom(data.table,fread)
importFrom(data.table,rleid)
importFrom(dplyr,across)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,if_else)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,pull)
importFrom(dplyr,recode)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(forcats,fct_recode)
importFrom(forcats,fct_rev)
importFrom(furrr,future_walk)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,coord_sf)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
importFrom(ggplot2,facet_grid)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,geom_tile)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggsave)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,guide_legend)
importFrom(ggplot2,guides)
importFrom(ggplot2,labs)
importFrom(ggplot2,position_stack)
importFrom(ggplot2,scale_color_manual)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(ggplot2,scale_fill_manual)
importFrom(ggplot2,scale_size)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,theme_void)
importFrom(ggplot2,unit)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(ggrepel,geom_text_repel)
importFrom(ggspatial,annotation_north_arrow)
importFrom(ggspatial,annotation_scale)
importFrom(glue,glue)
importFrom(grDevices,adjustcolor)
importFrom(grDevices,hcl.colors)
importFrom(hubeau,get_ecoulement_campagnes)
importFrom(hubeau,get_ecoulement_observations)
importFrom(hubeau,get_ecoulement_stations)
importFrom(hubeau,list_params)
importFrom(leafpop,popupGraph)
importFrom(lubridate,as_date)
importFrom(lubridate,month)
importFrom(lubridate,year)
importFrom(lubridate,ym)
importFrom(lubridate,ymd)
importFrom(magrittr,"%>%")
importFrom(mapview,mapview)
importFrom(progressr,progressor)
importFrom(progressr,with_progress)
importFrom(purrr,map)
importFrom(purrr,map_df)
importFrom(purrr,pwalk)
importFrom(purrr,reduce)
importFrom(rlang,":=")
importFrom(rmarkdown,render)
importFrom(rvest,html_attr)
importFrom(rvest,html_nodes)
importFrom(scales,breaks_width)
importFrom(scales,percent_format)
importFrom(sf,st_as_sf)
importFrom(sf,st_as_sfc)
importFrom(sf,st_bbox)
importFrom(sf,st_buffer)
importFrom(sf,st_crs)
importFrom(sf,st_geometry)
importFrom(sf,st_intersection)
importFrom(sf,st_join)
importFrom(sf,st_read)
importFrom(sf,st_transform)
importFrom(stats,setNames)
importFrom(stringr,str_extract)
importFrom(stringr,str_pad)
importFrom(stringr,str_replace_all)
importFrom(stringr,str_split)
Expand Down
36 changes: 36 additions & 0 deletions R/ajouter_zones_propluvia.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' Ajouter les données de zones d'alerte Propluvia au tableau de données onde
#'
#' @param onde_df un tableau de données onde téléchargé avec la
#' fonction \code{telecharger_donnees_onde_api}
#' @param propluvia_shape la couche spatiale pour les zones d'alerte Propluvia.
#' Une version existe en tant que jeux de données dans le package : (\code{propluvia_zone}).
#'
#' @return un dataframe comprenant les données onde et les données Propluvia jointées spatialement.
#' @export
#'
#' @importFrom dplyr filter
#' @importFrom sf st_as_sf st_transform st_buffer st_join
#'
#' @examples
#' \dontrun{
#' ajouter_zones_propluvia(onde_df = onde_14, propluvia_shape = propluvia_zone)
#' }

ajouter_zones_propluvia <- function(onde_df,
propluvia_shape){
onde_df <-
onde_df %>%
sf::st_as_sf(
coords = c("longitude", "latitude"),
crs = 4326
) %>%
sf::st_transform(crs = 2154)

propluvia_shape <-
propluvia_shape %>%
dplyr::filter(dpt %in% unique(onde_df$code_departement)) %>%
sf::st_transform(crs = st_crs(onde_df)) %>%
sf::st_buffer(dist = 50)

sf::st_join(onde_df, propluvia_shape)
}
41 changes: 41 additions & 0 deletions R/calculer_assecs_heatmap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Fonction pour calculer les pourcentages de stations en assec

#' @description
#' Calcule les pourcentages de stations en assec (sur les mois de campagnes usuelles)
#' et met en forme du tableau pour le graphique de type \code{heatmap}
#'
#' @param onde_df un tableau de données onde téléchargé avec la
#' fonction \code{telecharger_donnees_onde_api}
#'
#' @return un tableau résumant au format pour le graphique avec la
#' fonction \code{produire_graph_heatmap_assecs}
#' @export
#'
#' @importFrom dplyr filter summarise n mutate arrange
#' @importFrom glue glue
#' @importFrom tidyr complete
#'
#' @examples
#' \dontrun{
#' onde_14 <- telecharger_donnees_onde_api(dpt = c('14'))
#' calculer_assecs_heatmap(onde_df = onde_14)
#' }

calculer_assecs_heatmap <- function(onde_df) {
onde_df %>%
dplyr::filter(libelle_type_campagne == 'usuelle') %>%
dplyr::mutate(Mois = format(as.Date(date_campagne), '%m')) %>%
dplyr::group_by(Mois, Annee, libelle_type_campagne, code_campagne, code_departement) %>%
dplyr::summarise(n_donnees = dplyr::n(),
n_assecs = length(libelle_ecoulement[libelle_ecoulement == 'Assec']),
.groups = "drop") %>%
dplyr::mutate(pourcentage_assecs = round(n_assecs / n_donnees * 100, digits = 2),
taille_point = sqrt(pourcentage_assecs+1)) %>%
dplyr::arrange(Annee, Mois) %>%
tidyr::complete(Annee, Mois, libelle_type_campagne) %>%
dplyr::mutate(Mois = factor(Mois)) %>%
# label pourcentage
dplyr::mutate(Label = ifelse(is.na(n_assecs), "", glue::glue("{n_assecs}/{n_donnees}"))) %>%
# label (nb stations / nb total)
dplyr::mutate(Label_p = ifelse(is.na(n_assecs), "", glue::glue("{round(pourcentage_assecs,0)}%")))
}
120 changes: 120 additions & 0 deletions R/calculer_bilan_ecoulement.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#' Calculer un bilan des types d'écoulement pour les campagnes ONDE usuelles selon
#' la typologie nationale ou départementale
#'
#' @param onde_df un tableau de données onde téléchargé avec la fonction \code{telecharger_donnees_onde_api}
#' @param referentiel_onde le nom de la typologie à utiliser,
#' soit \code{Typologie nationale} ou \code{Typologie départementale}
#' @param mod Le nom pour la colonne créée pour la typologie. Par exemple, soit
#' \code{lib_ecoul3mod} pour la typologie nationale ou \code{lib_ecoul4mod} pour la typologie départementale,
#' @param mod_levels les niveaux de facteurs souhaités en fonction des typologies.
#' @param force_complementaire booléen, si TRUE les complémentaires sont prises en compte dans le calcul. Par défaut, FALSE.
#' @param ... paramètre optionnel
#'
#' @return un dataframe avec les pourcentages par types d’écoulement selon la typologie choisie
#' @export
#'
#' @importFrom dplyr filter mutate case_when group_by summarise n arrange desc across
#' @importFrom glue glue
#' @importFrom rlang :=
#'
#' @examples
#' \dontrun{
#' ## bilan écoulement sur typologie nationale sur les usuelles uniquement
#' calculer_bilan_ecoulement(onde_df = onde_df,
#' mod = lib_ecoul,
#' mod_levels = c("Ecoulement visible",
#' "Ecoulement non visible",
#' "Assec",
#' "Observation impossible",
#' "Donnée manquante"),
#' referentiel_onde = "Typologie nationale",
#' force_complementaire = F)
#'
#' ## bilan écoulement sur typologie départementale avec les complémentaires
#' calculer_bilan_ecoulement(onde_df = onde_df,
#' mod = lib_ecoul,
#' mod_levels = c("Ecoulement visible acceptable",
#' "Ecoulement visible faible",
#' "Ecoulement non visible",
#' "Assec",
#' "Observation impossible",
#' "Donnée manquante"),
#' referentiel_onde = "Typologie départementale",
#' force_complementaire = F)
#'
#' ## bilan écoulement sur typologie nationale avec les complémentaires
#' calculer_bilan_ecoulement(onde_df = onde_df,
#' mod = lib_ecoul,
#' mod_levels = c("Ecoulement visible",
#' "Ecoulement non visible",
#' "Assec",
#' "Observation impossible",
#' "Donnée manquante"),
#' referentiel_onde = "Typologie nationale",
#' force_complementaire = T)
#'
#'}
calculer_bilan_ecoulement <-
function(onde_df,
referentiel_onde = 'Typologie nationale',
mod,
mod_levels,
force_complementaire = F,
...) {

onde_df <-
if(force_complementaire == FALSE){
onde_df %>%
dplyr::filter(libelle_type_campagne == 'usuelle')
} else {
onde_df
}

onde_df %>%
{
if ({{referentiel_onde}} == 'Typologie nationale'){

dplyr::mutate(.,
{{mod}} := dplyr::case_when(
libelle_ecoulement == 'Ecoulement visible faible' ~ 'Ecoulement visible',
libelle_ecoulement == 'Ecoulement visible acceptable' ~ 'Ecoulement visible',
TRUE ~ libelle_ecoulement
)) %>%
dplyr::group_by(.,
date_campagne,
code_campagne,
code_departement,
libelle_type_campagne,
{{mod}})
} else {


dplyr::mutate(.,
{{mod}} := dplyr::case_when(
libelle_ecoulement == 'Ecoulement visible' ~ 'Ecoulement visible acceptable',
TRUE ~ libelle_ecoulement
)) %>%
dplyr::group_by(.,
date_campagne,
code_campagne,
code_departement,
libelle_type_campagne,
{{mod}})
} } %>%
dplyr::summarise(NB = dplyr::n(), .groups = "drop_last") %>%
dplyr::mutate(frq = NB / sum(NB) *100) %>%
dplyr::arrange(code_departement, dplyr::desc(date_campagne), ...) %>%
dplyr::mutate(Label = ifelse(is.na(NB),"",glue::glue("{NB}"))) %>%
dplyr::mutate(Label_p = ifelse(is.na(frq),"",glue::glue("{round(frq,0)}%"))) %>%
dplyr::mutate(Mois = format(date_campagne, "%m"),
Annee = format(date_campagne, "%Y")) %>%
dplyr::mutate(Typologie = rep({{referentiel_onde}}, dplyr::n())) %>%
dplyr::mutate(
dplyr::across(
{{mod}},
function(x) {
factor(x, levels = mod_levels, ordered = TRUE)
}
)
)
}
Loading

0 comments on commit d0e6de0

Please sign in to comment.