Commit 20cde0d8 authored by Grelot Frederic's avatar Grelot Frederic 🏊🏿
Browse files

Merge branch 'master' into map_production

parents 50be8e5b 2e078e8e
......@@ -8,11 +8,11 @@ devtools::build_vignettes(package)
# devtools::run_examples(package)
### Checks
# system("mv ~/.Rprofile ~/.Rprofile-temp");devtools::check(package);system("mv ~/.Rprofile-temp ~/.Rprofile")
# system("mv ~/.Rprofile ~/.Rprofile-temp");devtools::check(package);system("mv ~/.Rprofile-temp ~/.Rprofile") # nolint
### Build
devtools::build(package, path = "library", vignettes = TRUE)
### install -> sudo
# devtools::install_local(package)
sudo su - -c "R -e \"devtools::install_gitlab('geau-inondation/geau-utility', subdir = 'geau', host = 'gitlab.irstea.fr', upgrade = 'never', auth_token = 'rCEfcrjoms9UNykCuM5c')\""
\ No newline at end of file
sudo su - -c "R -e \"devtools::install_gitlab('geau-inondation/geau-utility', subdir = 'geau', host = 'gitlab.irstea.fr', upgrade = 'never', auth_token = 'rCEfcrjoms9UNykCuM5c')\"" # nolint
\ No newline at end of file
Package: geau
Title: Utilities very useful to share within geau-inondation team
Version: 1.0.3.0
Version: 1.0.15.0
Authors@R:
c(
person(given = "Frédéric",
......@@ -23,6 +23,7 @@ LazyData: true
Imports:
kableExtra,
knitr,
readODS,
rio,
scales,
sf,
......
......@@ -3,5 +3,6 @@
export(add.inset)
export(current_version)
export(estimate_catnat_freq)
export(format_presence)
export(kable_units)
export(map_so_ii)
#' Local collectivities included in so-ii
#' Color and label for CLC
#'
#' A dataset containing the INSEE code of all local collectivities
#' (communes) included in so-ii
#' A dataset proposing default colors and labels for plotting CLC
#'
#' @format a vector of 69 INSEE code
"so_ii_scope"
#' @format data.frame 5 rows, 3 variables
"clc_color"
#' List of all collectivities included in so-ii
#'
#' A dataset containing the INSEE code of all local collectivities
#' included in so-ii.
#' Catchment areas of interest within the so-ii perimeter
#'
#' Basically this dataset is obtained as a selection from the layer
#' COMMUNE in ADMIN EXPRESS, more a renaming of variables.
#' A dataset containing the official catchments areas of interest from the BD
#' TOPAGE within the so-ii perimeter. For degre = 3, the data are basically
#' what is found in BD TOPAGE. For degres 1 and 2, the data result from
#' sf::st_union of data of degre 3 to give a more synthetic representation.
#'
#' @format sf data.frame 69 rows, 7 variables
#' @format sf data.frame 15 rows, 4 variables
#' \describe{
#' \item{id}{id, from IGN ADMIN EXPRESS}
#' \item{commune}{character, official name of the commune}
#' \item{commune_majuscule}{character, official capitalized name of the
#' commune}
#' \item{code}{character, INSEE code of the commune}
#' \item{statut}{character, statut of the commune}
#' \item{pop_yyy}{integer, official population of year yyyy in the commune}
#' \item{epci}{character, INSEE ID of the EPCI of the commune}
#' \item{id}{id, from BD TOPAGE (corresponding to CdOh) or NA when catchment
#' is constructed by so-ii team.}
#' \item{name}{character, name of the catchment area in BD TOPAGE, or given
#' name for catchments constructed by so-ii team.}
#' \item{degre}{factor, importance of the catchment used to plot the
#' catchment areas with different levels of detail ("1", "2", "3").}
#' }
#'
#' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/}
"so_ii_commune"
#' @source \url{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_catchment"
#' Spatial perimeter of so-ii
#' Number of Cat Nat events for the municipalities of so-ii
#'
#' A dataset containing the perimeter of so-ii.
#' A dataset containing the number of Cat Nat events (linked to flood) by year
#' and so-ii municipality according to the GASPAR database.
#'
#' Basically, this dataset is obtained as
#' \code{sf::st_union(so_ii_commune)}
#' @format array with 3 dimensions
#' \describe{
#' \item{first}{commune as in so_ii_scope}
#' \item{second}{year of Cat Nat events}
#' \item{third}{type of hazard}
#' }
#'
#' @format sfc_POLYGON of length 1
"so_ii_limit"
#' @source \url{https://www.georisques.gouv.fr/donnees/bases-de-donnees/base-gaspar} # nolint
"so_ii_catnat"
#' CLC information for so-ii
#'
......@@ -48,62 +49,140 @@
#' \item{clc_2018}{character, classification from CLC 2018}
#' \item{color}{character, default color to be used to plot so_ii_clc}
#' }
"so_ii_limit"
"so_ii_clc"
#' Population for so-ii
#' Spatial definition of collectivities included in so-ii
#'
#' A dataset containing the population of commune in so-ii according to INSEE.
#' A dataset containing the spatial definition of all collectivities
#' included in so-ii and some administrative informations.
#'
#' @details
#' Basically this dataset is obtained as a selection from the layer
#' COMMUNE in ADMIN EXPRESS, more a renaming of variables. It is then added
#' information from EPCI in ADMIN EXPRESS and the membership to SYBLE and
#' SYMBO.
#'
#' @format numeric matrix
#' @format sf data.frame 78 rows, 11 variables
#' \describe{
#' \item{row}{commune as in so_ii_scope}
#' \item{column}{year}
#' \item{commune_name}{character, INSEE code of the collectivity}
#' \item{syble}{logical, membership in SYBLE}
#' \item{symbo}{logical, membership in SYMBO}
#' \item{commune_name}{character, official name of the collectivity}
#' \item{commune_name_cap}{character, official capitalized name of the
#' collectivity}
#' \item{departement}{character, INSEE code of the departement of the
#' collectivity}
#' \item{region}{character, INSEE code of the region of the
#' collectivity}
#' \item{epci}{character, INSEE code of the EPCI of the collectivity}
#' \item{epci_name}{character, Name of the EPCI of the collectivity}
#' \item{epci_nature}{character, Nature of the EPCI of the collectivity}
#' }
#'
#' @source \url{https://www.insee.fr/fr/statistiques/2522602}
"so_ii_population"
#' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/}
"so_ii_collectivity"
#' Number of Cat Nat events for the municipalities of so-ii
#' Hydrographic network within the so-ii perimeter
#'
#' A dataset containing the number of Cat Nat events (linked to flood) by year
#' and so-ii municipality according to the GASPAR database.
#' A dataset containing the official hydrographic network from the BD TOPAGE
#' within the so-ii perimeter.
#'
#' @format array with 3 dimensions
#' @format sf data.frame 125 rows, 4 variables
#' \describe{
#' \item{first}{commune as in so_ii_scope}
#' \item{second}{year of Cat Nat events}
#' \item{third}{type of hazard}
#' \item{id}{id, from BD TOPAGE (corresponding to CdOh)}
#' \item{name}{character, name of the hydrographic elements in the BD TOPAGE}
#' \item{degre}{factor, level of importance of the hydrographic element
#' used to plot the hydrographic network with different levels of
#' detail ("1", "2", "3").}
#' \item{type}{factor, type of hydrographic element ("canal", "river",
#' "waterbody")}
#' }
#'
#' @source \url{https://www.georisques.gouv.fr/donnees/bases-de-donnees/base-gaspar} # nolint
"so_ii_catnat"
#' @source \url{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_hydro"
#' CLC information for so-ii
#' Spatial perimeter of so-ii
#'
#' A dataset containing the 2018 version of CLC information for so-ii
#' A dataset containing the perimeter of so-ii.
#'
#' @format sf object
"so_ii_clc"
#' Basically, this dataset is obtained as
#' \code{sf::st_union(so_ii_commune)}
#'
#' @format sfc_POLYGON of length 1
"so_ii_limit"
#' Color and label for CLC
#' Spatial definition of districts of Montpellier city
#'
#' A dataset proposing default colors and labels for plotting CLC
#' A dataset containing the spatial definition of all districts
#' for Montpellier.
#'
#' @format data.frame 5 rows, 3 variables
"clc_color"
#' @format sf data.frame 31 rows, 2 variables
#' \describe{
#' \item{district}{character, id of each district as given by montpellier3m}
#' \item{district_name}{character, name of each district}
#' \item{district_group}{character, how districts are grouped by montpellier3m}
#' }
#'
#' @source \url{https://data.montpellier3m.fr/dataset/sous-quartiers-de-montpellier}
"so_ii_montpellier"
#' Hydrographic network within the so-ii perimeter
#' Local collectivities included in so-ii
#'
#' A dataset containing the official hydrographic network from the BD TOPAGE
#' within the so-ii perimeter.
#' A dataset containing the INSEE code of all local collectivities
#' (communes) included in so-ii
#'
#' @format sf data.frame 125 rows, 4 variables
#' @format a vector of 78 INSEE code
"so_ii_scope"
#' ONRN information for so-ii
#'
#' A dataset containing part of the information available at the ONRN for so-ii
#' communities. The information chosen is exclusively related to floods. It is
#' mainly related to impacts and therefore to the claims in from the Cat-Nat
#' system. These data on claims are taken from the CCR, the others from the
#' gaspar database.
#'
#' @format data.frame 78 rows, 23 variables
#' \describe{
#' \item{id}{id, from BD TOPAGE (corresponding to CdOh)}
#' \item{name}{character, name of the river or part of the river in BD
#' TOPAGE}
#' \item{degre}{character, level of detail to plot the hydrographic network}
#' \item{n_catnat}{Number of Cat Nat events}
#' \item{freq_sin}{Number of claims divided by number of contracts
#' for 1995 to 2018. freq_sin is calculated as the mean of freq_sin_min
#' and freq_sin_max (range for each category).}
#' \item{cost}{Cumulative cost of claims for 1995 to 2018. Cost is calculated
#' as the mean of cost_min and cost_max (range for each category).}
#' \item{cost_mean}{Mean cost of claims (cost divided by claims) for 1995 to
#' 2018. cost_mean is calculated as the mean of cost_mean_min and
#' cost_mean_max (range for each category).}
#' \item{cost_hab}{Cost divided by the population for 1995 to 2018. cost_hab
#' is calculated as the mean of cost_hab_min and cost_hab_max (range for
#' each category).}
#' \item{ratio}{Cost divided by premium for 1995 to 2018. ratio is calculated
#' as the mean of cost_hab_min and cost_hab_max (range for each
#' category).}
#' \item{balance}{Cost minus premium for 1995 to 2018. This is an estimation
#' made by so-ii team by considering a mean premium for each habitant
#' of 24.92829 euro per habitant (total premium in 2018 divided by
#' total population)}
#' \item{ppri_year}{Year given for the last PPRI.}
#' \item{ppri_state}{State of the last PPRI.}
#' \item{ppri_state_sub}{Some details on the state of the last PPRI.}
#' \item{ppri_state_age}{State of the last PPRI for age information.}
#' \item{ppri_age_min}{Lower boundary for the age of the PPRI.}
#' \item{ppri_age_min}{Upper boundary for the age of the PPRI..}
#' }
#'
#' @source \url{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_hydro"
\ No newline at end of file
#' @source \url{https://www.georisques.gouv.fr/articles-risques/acceder-aux-indicateurs-sinistralite}
"so_ii_onrn"
#' Population for so-ii
#'
#' A dataset containing the population of commune in so-ii according to INSEE.
#'
#' @format numeric matrix 78 rows, 33 columns
#' \describe{
#' \item{row}{commune as in so_ii_scope}
#' \item{column}{year}
#' }
#'
#' @source \url{https://www.insee.fr/fr/statistiques/2522602}
"so_ii_population"
\ No newline at end of file
#' @title Transform ods table presence in md table (for Mattermost)
#'
#' @param x character or data.frame, path of the ods table or the table itself
#'
#' @return character, md version of table behind x.
#'
#' @export
#'
#' @encoding UTF-8
#' @author Frédéric Grelot
format_presence = function(x) {
result = if (is.character(x)) readODS::read_ods(x[1]) else x
result[] = lapply(result, gsub, pattern = "B", replacement = "**B**")
result[] = lapply(result, gsub, pattern = "C", replacement = "*C*")
knitr::kable(result, align = c("l", rep("c", length(result) - 1)))
}
This diff is collapsed.
# code to prepare `so_ii_catchment` dataset goes here
file_dir = geau::current_version(
"data-common/so-ii/topage",
pattern = "^[0-9-]+$"
)
so_ii_catchment = sf::st_read(file.path(file_dir, "bassin_versant.shp"))
names(so_ii_catchment) = c("id", "catchment_name", "degre", "geometry")
Encoding(so_ii_catchment[["catchment_name"]]) = "UTF-8"
so_ii_catchment[["degre"]] = factor(so_ii_catchment[["degre"]])
# updating datasets
actual = setwd("geau")
usethis::use_data(so_ii_catchment, internal = FALSE, overwrite = TRUE)
setwd(actual)
# code to prepare `so_ii_collectivity` and `so_ii_limit` datasets goes here
## epci
so_ii_epci = read.csv2(
geau::current_version("data-common/so-ii/epci")
)
rownames(so_ii_epci) = so_ii_epci[["epci"]]
epci = names(so_ii_epci)
## collectivity
admin_express = geau::current_version("data-common/data/IGN/ADMIN-EXPRESS/version")
so_ii_collectivity = sf::st_read(file.path(admin_express, "COMMUNE.shp"))
so_ii_collectivity = so_ii_collectivity["INSEE_COM"]
names(so_ii_collectivity) = c("commune", "geometry")
rownames(so_ii_collectivity) = so_ii_collectivity[["commune"]]
so_ii_collectivity = so_ii_collectivity[geau::so_ii_scope, ]
so_ii_collectivity = merge(
so_ii_collectivity[geau::so_ii_scope, ],
read.csv2(geau::current_version("data-common/so-ii/commune"))
)
collectivity = names(so_ii_collectivity)[-length(names(so_ii_collectivity))]
so_ii_collectivity = merge(so_ii_collectivity, so_ii_epci)
rownames(so_ii_collectivity) = so_ii_collectivity[["commune"]]
so_ii_collectivity = so_ii_collectivity[
geau::so_ii_scope,
union(collectivity, epci)
]
Encoding(so_ii_collectivity[["commune_name"]]) = "UTF-8"
Encoding(so_ii_collectivity[["epci_name"]]) = "UTF-8"
Encoding(so_ii_collectivity[["epci_nature"]]) = "UTF-8"
so_ii_limit = sf::st_union(so_ii_collectivity)
# updating dataset
actual = setwd("geau")
usethis::use_data(so_ii_collectivity, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_limit, internal = FALSE, overwrite = TRUE)
setwd(actual)
# code to prepare `so_ii_hydro` dataset goes here
selection = c("CdOH", "TopoOH")
file_dir = current_version(
file_dir = geau::current_version(
"data-common/so-ii/topage",
pattern = "^[0-9-]+$"
)
so_ii_hydro = sf::st_read(file.path(file_dir, "cours-eau-so-ii.shp"))
so_ii_hydro = sf::st_transform(
so_ii_hydro[selection],
river = sf::st_read(file.path(file_dir, "cours_eau.shp"))
river = sf::st_transform(
river[selection],
sf::st_crs(geau::so_ii_limit)
)
names(so_ii_hydro) = c("id", "name", "geometry")
names(river) = c("id", "name", "geometry")
classification = read.csv2(
current_version("data-common/so-ii/topage", pattern = "courseau"),
geau::current_version("data-common/so-ii/topage", pattern = "cours_eau"),
colClasses = "character",
row.names = 1
)
river = merge(river, classification)
waterbody = sf::st_read(file.path(file_dir, "plan_eau.shp"))
waterbody[["name"]] = gsub("C[?]ur", "Cœur", waterbody[["name"]])
classification = read.csv2(
geau::current_version("data-common/so-ii/topage", pattern = "plan_eau"),
colClasses = "character"
)[c("id", "name", "degre")]
)
waterbody = merge(waterbody, classification)
so_ii_hydro = rbind(river, waterbody)
so_ii_hydro = merge(so_ii_hydro, classification)
so_ii_hydro[["degre"]] = factor(so_ii_hydro[["degre"]])
so_ii_hydro[["type"]] = factor(so_ii_hydro[["type"]])
Encoding(waterbody[["name"]]) = "UTF-8"
# updating datasets
# actual = setwd(file.path(system.file(package = "geau"), ".."))
actual = setwd("geau")
usethis::use_data(so_ii_hydro, internal = FALSE, overwrite = TRUE)
setwd(actual)
# code to prepare `so_ii_onrn` dataset goes here
so_ii_montpellier = sf::st_read(
geau::current_version("data-common/so-ii/montpellier", "shp")
)
so_ii_montpellier = sf::st_transform(
so_ii_montpellier,
crs = sf::st_crs(geau::so_ii_limit)
)
so_ii_montpellier = so_ii_montpellier[c("SQUARTIER_", "LIBSQUART", "QUARTIER")]
names(so_ii_montpellier) = c("district", "district_name", "district_group", "geometry")
Encoding(so_ii_montpellier[["district_name"]]) = "UTF-8"
so_ii_montpellier[["district"]] = formatC(so_ii_montpellier[["district"]], flag = "0", width = 2)
so_ii_montpellier[["district_group"]] = as.character(so_ii_montpellier[["district_group"]])
# updating datasets
# actual = setwd(file.path(system.file(package = "geau"), ".."))
actual = setwd("geau")
usethis::use_data(so_ii_montpellier, internal = FALSE, overwrite = TRUE)
setwd(actual)
# code to prepare `so_ii_onrn` dataset goes here
so_ii_onrn = read.csv2(
geau::current_version("data-common/so-ii/onrn"),
row.names = 1
)
so_ii_onrn = so_ii_onrn[geau::so_ii_scope, ]
# updating datasets
# actual = setwd(file.path(system.file(package = "geau"), ".."))
actual = setwd("geau")
usethis::use_data(so_ii_onrn, internal = FALSE, overwrite = TRUE)
setwd(actual)
# code to prepare `so_ii_osm` dataset goes here
library(sf)
library(maptiles)
so_ii_osm = maptiles::get_tiles(geau::so_ii_limit, zoom = 10, crop = TRUE)
# updating datasets
actual = setwd("geau")
terra::writeRaster(so_ii_osm, "inst/extdata/so_ii_osm.tif", overwrite = TRUE)
setwd(actual)
......@@ -11,7 +11,7 @@ so_ii_population = readxl::read_xlsx(
class(so_ii_population) = "data.frame"
rownames(so_ii_population) = so_ii_population[["CODGEO"]]
selection = grep(
"PMUN|PSCDC|PTOT",
"PMUN|PSDC|PTOT",
colnames(so_ii_population),
value = TRUE
)
......@@ -19,6 +19,7 @@ so_ii_population = as.matrix(
so_ii_population[geau::so_ii_scope, selection]
)
year = gsub("PMUN", "20", selection)
year = gsub("PSDC", "19", year)
year = gsub("PTOT", "19", year)
year = gsub("1919", "19", year)
year = gsub("1918", "18", year)
......
......@@ -6,19 +6,6 @@ so_ii_scope = read.csv2(
)[["code"]]
so_ii_scope = sort(so_ii_scope)
# code to prepare `so_ii_commune` dataset goes here
admin_express = current_version("data-common/data/IGN/ADMIN-EXPRESS/version")
selection = c("ID", "NOM", "NOM_M", "INSEE_COM", "STATUT", "POPULATION", "SIREN_EPCI")
so_ii_commune = sf::st_read(file.path(admin_express, "COMMUNE.shp"))[selection]
names(so_ii_commune) = c("id", "commune", "commune_majuscule", "code", "statut", "pop_2021", "epci", "geometry")
rownames(so_ii_commune) = so_ii_commune[["code"]]
so_ii_commune = so_ii_commune[so_ii_scope, ]
# code to prepare `so_ii_limit` dataset goes here
so_ii_limit = sf::st_union(so_ii_commune)
# code to prepare `so_ii_clc` dataset goes here
so_ii_clc = readRDS("data-common/data/so-ii/so-ii_clc.rds")
......@@ -63,8 +50,6 @@ so_ii_clc[["color"]] = as.character(
# actual = setwd(file.path(system.file(package = "geau"), ".."))
actual = setwd("geau")
usethis::use_data(so_ii_scope, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_commune, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_limit, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_clc, internal = FALSE, overwrite = TRUE)
usethis::use_data(clc_color, internal = FALSE, overwrite = TRUE)
setwd(actual)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment