Commit 9621767a authored by Grelot Frederic's avatar Grelot Frederic :swimmer_tone5:
Browse files

Merge branch '7-add-onrn-dataset' into 'master'

Resolve "add onrn dataset"

Closes #7

See merge request !9
Showing with 745 additions and 220 deletions
+745 -220
Package: geau
Title: Utilities very useful to share within geau-inondation team
Version: 1.0.6.1
Version: 1.0.7.0
Authors@R:
c(
person(given = "Frédéric",
......
#' 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 78 INSEE code
"so_ii_scope"
#' @format data.frame 5 rows, 3 variables
"clc_color"
#' Catchment areas of interest within the so-ii perimeter
#'
#' 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 15 rows, 4 variables
#' \describe{
#' \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{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_catchment"
#' Number of Cat Nat events for the municipalities 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.
#'
#' @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}
#' }
#'
#' @source \url{https://www.georisques.gouv.fr/donnees/bases-de-donnees/base-gaspar} # nolint
"so_ii_catnat"
#' CLC information for so-ii
#'
#' A dataset containing the Corine Land Cover information on so-ii.
#'
#' @format sf data.frame 1337 rows, 2 variables
#' \describe{
#' \item{clc_2018}{character, classification from CLC 2018}
#' \item{color}{character, default color to be used to plot so_ii_clc}
#' }
"so_ii_clc"
#' Spatial definition of collectivities included in so-ii
#'
......@@ -37,6 +82,25 @@
#' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/}
"so_ii_collectivity"
#' Hydrographic network within the so-ii perimeter
#'
#' A dataset containing the official hydrographic network from the BD TOPAGE
#' within the so-ii perimeter.
#'
#' @format sf data.frame 125 rows, 4 variables
#' \describe{
#' \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{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_hydro"
#' Spatial perimeter of so-ii
#'
#' A dataset containing the perimeter of so-ii.
......@@ -47,16 +111,53 @@
#' @format sfc_POLYGON of length 1
"so_ii_limit"
#' CLC information for so-ii
#' Local collectivities included in so-ii
#'
#' A dataset containing the Corine Land Cover information on so-ii.
#' A dataset containing the INSEE code of all local collectivities
#' (communes) included in so-ii
#'
#' @format sf data.frame 1337 rows, 2 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{clc_2018}{character, classification from CLC 2018}
#' \item{color}{character, default color to be used to plot so_ii_clc}
#' \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..}
#' }
"so_ii_limit"
#'
#' @source \url{https://www.georisques.gouv.fr/articles-risques/acceder-aux-indicateurs-sinistralite}
"so_ii_onrn"
#' Population for so-ii
#'
......@@ -69,72 +170,4 @@
#' }
#'
#' @source \url{https://www.insee.fr/fr/statistiques/2522602}
"so_ii_population"
#' Number of Cat Nat events for the municipalities 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.
#'
#' @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}
#' }
#'
#' @source \url{https://www.georisques.gouv.fr/donnees/bases-de-donnees/base-gaspar} # nolint
"so_ii_catnat"
#' CLC information for so-ii
#'
#' A dataset containing the 2018 version of CLC information for so-ii
#'
#' @format sf object
"so_ii_clc"
#' Color and label for CLC
#'
#' A dataset proposing default colors and labels for plotting CLC
#'
#' @format data.frame 5 rows, 3 variables
"clc_color"
#' Hydrographic network within the so-ii perimeter
#'
#' A dataset containing the official hydrographic network from the BD TOPAGE
#' within the so-ii perimeter.
#'
#' @format sf data.frame 125 rows, 4 variables
#' \describe{
#' \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{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_hydro"
#' Catchment areas of interest within the so-ii perimeter
#'
#' 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 15 rows, 4 variables
#' \describe{
#' \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{http://bdtopage.eaufrance.fr/page/objectifs}
"so_ii_catchment"
\ No newline at end of file
"so_ii_population"
\ No newline at end of file
......@@ -16,6 +16,8 @@
#' levels of detail or "canal", "river", "waterbody" for types of
#' hydrographic elements. If missing, "none" will be chosen, and
#' everything is plotted.}
#' \item{\strong{onrn}: detail must be chosen in "n_catnat", "freq_sin",
#' "cost", "cost_hab", "cost_mean", "ratio", "balance", "ppri_year".}
#' }
#' }
#' \subsection{year specification}{
......@@ -55,7 +57,7 @@
map_so_ii = function(
dataset,
dataset_legend = NULL,
theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro", "population"),
theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro", "onrn", "population"),
bar = TRUE,
path = NULL,
legend_theme = FALSE,
......@@ -79,6 +81,102 @@ map_so_ii = function(
graphics::par(mai = c(.65, .60, .50, .15))
plot(geau::so_ii_limit, axes = TRUE)
if ("catchment" %in% theme) {
if (missing(detail)) {
detail = "1"
}
detail = match.arg(
as.character(detail),
choices = levels(geau::so_ii_catchment[["degre"]])
)
selection = geau::so_ii_catchment[["degre"]] == detail
geometry = geau::so_ii_catchment[["geometry"]][selection]
catchment = as.factor(geau::so_ii_catchment[["catchment_name"]][selection])
color_legend = grDevices::hcl.colors(nlevels(catchment), "Pastel 1", alpha = .3)
color = color_legend[catchment]
border = "grey80"
lwd = 2
theme_legend = list(
title = sprintf("Bassin versant"),
legend = levels(catchment),
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = color_legend,
border = border
)
if (detail == "3") rm(theme_legend)
plot(geometry, border = border, col = color, lwd = lwd, add = TRUE)
}
if ("catnat" %in% theme) {
if (missing(detail)) {
detail = dimnames(geau::so_ii_catnat)[["hazard"]]
}
detail = match.arg(
detail,
dimnames(geau::so_ii_catnat)[["hazard"]],
several.ok = TRUE
)
border = NA
color = NA
if (!missing(year)) {
year = match.arg(
as.character(year),
dimnames(geau::so_ii_catnat)[["period"]]
)
border = "grey80"
catnat = apply(
geau::so_ii_catnat[, year, detail, drop = FALSE],
1:2,
sum
)
color = ifelse(
catnat > 0,
scales::alpha("grey80", .5),
NA
)
theme_legend = list(
title = sprintf("Cat-Nat %s", year),
legend = c("Sans d\u00e9claration", "Avec d\u00e9claration"),
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = unique(color),
border = border
)
}
plot(
geau::so_ii_collectivity[["geometry"]],
border = border,
col = color,
add = TRUE
)
}
if ("clc" %in% theme) {
plot(
geau::so_ii_clc[["geometry"]],
border = NA,
col = geau::so_ii_clc[["color"]],
add = TRUE
)
theme_legend = list(
title = "CLC (2018)",
legend = geau::clc_color[["label_fr"]],
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = geau::clc_color[["color"]]
)
}
if ("collectivity" %in% theme) {
if (missing(detail)) {
detail = "none"
......@@ -136,23 +234,167 @@ map_so_ii = function(
}
}
if ("clc" %in% theme) {
if ("hydro" %in% theme) {
if (missing(detail)) {
detail = "none"
}
detail = match.arg(
as.character(detail),
choices = c(
"none",
levels(geau::so_ii_hydro[["degre"]]),
levels(geau::so_ii_hydro[["type"]])
)
)
color = scales::alpha("blue", .3)
bg = scales::alpha("blue", .3)
border = NA
selection = seq(nrow(geau::so_ii_hydro))
theme_legend = list(
title = sprintf("R\u00e9seau hydrographique"),
legend = "\u00e9l\u00e9ment du r\u00e9seau",
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
col = color,
lwd = 1
)
if (detail %in% levels(geau::so_ii_hydro[["type"]])) {
selection = as.character(geau::so_ii_hydro[["type"]]) == detail
theme_legend[["legend"]] = detail
}
if (detail %in% levels(geau::so_ii_hydro[["degre"]])) {
selection = as.character(geau::so_ii_hydro[["degre"]]) <= detail
}
geometry = geau::so_ii_hydro[["geometry"]][selection]
lwd = 4 - as.numeric(geau::so_ii_hydro[["degre"]][selection])
plot(geometry, col = color, lwd = lwd, border = border, add = TRUE)
}
if ("onrn" %in% theme) {
if (missing(detail)) {
detail = "cost"
}
detail = match.arg(
as.character(detail),
sort(colnames(geau::so_ii_onrn)[1:8])
)
onrn_palette = switch(
EXPR = detail,
"n_catnat" = scales::colour_ramp(c("white", "red"), alpha = .5),
"freq_sin" = scales::colour_ramp(c("white", "red"), alpha = .5),
"cost" = scales::colour_ramp(c("white", "red"), alpha = .5),
"cost_hab" = scales::colour_ramp(c("white", "red"), alpha = .5),
"cost_mean" = scales::colour_ramp(c("white", "red"), alpha = .5),
"ratio" = scales::colour_ramp(c("green", "white", "red"), alpha = .5),
"balance" = scales::colour_ramp(c("red", "white", "green"), alpha = .5),
"ppri_year" = scales::colour_ramp(c("grey80", "grey50"), alpha = .5),
NULL
)
onrn_trans = switch(
EXPR = detail,
"n_catnat" = scales::identity_trans(),
"freq_sin" = scales::identity_trans(),
"cost" = scales::sqrt_trans(),
"cost_hab" = scales::sqrt_trans(),
"cost_mean" = scales::sqrt_trans(),
"ratio" = scales::sqrt_trans(),
"balance" = scales::modulus_trans(.5),
"ppri_year" = scales::identity_trans(),
NULL
)
onrn_range = switch(
EXPR = detail,
"ratio" = c(0, 4),
"balance" = max(abs(range(geau::so_ii_onrn[["balance"]]))) * c(-1, 1),
NULL
)
color = scales::cscale(
c(onrn_range, geau::so_ii_onrn[[detail]]),
onrn_palette,
trans = onrn_trans)
if (length(onrn_range) > 0) {
color = color[-seq(onrn_range)]
}
border = "grey80"
plot(
geau::so_ii_clc[["geometry"]],
border = NA,
col = geau::so_ii_clc[["color"]],
geau::so_ii_collectivity[["geometry"]],
border = border,
col = color,
add = TRUE
)
if (sprintf("%s_min", detail) %in% names(geau::so_ii_onrn)) {
selection = c(detail, sprintf("%s_min", detail), sprintf("%s_max", detail))
temp = unique(geau::so_ii_onrn[selection])
temp = temp[order(temp[[detail]]), ]
text_legend = gsub("0 - 0", "0",
sprintf(
"%s - %s",
temp[[sprintf("%s_min", detail)]],
temp[[sprintf("%s_max", detail)]]
)
)
value_legend = temp[[detail]]
}
if (detail %in% c("n_catnat", "ppri_year")) {
value_legend = round(
seq(
min(geau::so_ii_onrn[[detail]], na.rm = TRUE),
max(geau::so_ii_onrn[[detail]], na.rm = TRUE),
length.out = 5
)
)
text_legend = value_legend
}
if (detail %in% c("balance")) {
value_legend = unique(
c(
seq(min(geau::so_ii_onrn[[detail]]), 0, length.out = 4),
seq(0, max(geau::so_ii_onrn[[detail]]), length.out = 4)
)
)
text_legend = formatC(
as.integer(signif(round(value_legend), 2)),
big.mark = " "
)
}
color_legend = scales::cscale(
c(onrn_range, value_legend),
onrn_palette,
trans = onrn_trans
)
if (length(onrn_range) > 0) {
color_legend = color_legend[-seq(onrn_range)]
}
title_onrn = switch(
EXPR = detail,
"n_catnat" = "N arr\u00eat\u00e9s Cat-Nat (ONRN)",
"freq_sin" = "Sinistre / Risque [1995-2018]",
"cost" = "Co\u00fbt cumul\u00e9 (\u20AC) [1995-2018]",
"cost_hab" = "Co\u00fbt / hab (\u20ac) [1995-2018]",
"cost_mean" = "Co\u00fbt / sinistre (\u20ac) [1995-2018]",
"ratio" = "Co\u00fbt / Prime [1995-2018]",
"balance" = "Co\u00fbt - Prime (\u20ac) [1995-2018]",
"ppri_year" = "Ann\u00e9e des PPRI",
NULL
)
theme_legend = list(
title = "CLC (2018)",
legend = geau::clc_color[["label_fr"]],
title = title_onrn,
legend = text_legend,
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = geau::clc_color[["color"]]
fill = color_legend,
border = border
)
rm(text_legend)
}
if ("population" %in% theme) {
......@@ -204,122 +446,6 @@ map_so_ii = function(
)
}
if ("catnat" %in% theme) {
if (missing(detail)) {
detail = dimnames(geau::so_ii_catnat)[["hazard"]]
}
detail = match.arg(
detail,
dimnames(geau::so_ii_catnat)[["hazard"]],
several.ok = TRUE
)
border = NA
color = NA
if (!missing(year)) {
year = match.arg(
as.character(year),
dimnames(geau::so_ii_catnat)[["period"]]
)
border = "grey80"
catnat = apply(
geau::so_ii_catnat[, year, detail, drop = FALSE],
1:2,
sum
)
color = ifelse(
catnat > 0,
scales::alpha("grey80", .5),
NA
)
theme_legend = list(
title = sprintf("Cat-Nat %s", year),
legend = c("Sans d\u00e9claration", "Avec d\u00e9claration"),
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = unique(color),
border = border
)
}
plot(
geau::so_ii_collectivity[["geometry"]],
border = border,
col = color,
add = TRUE
)
}
if ("hydro" %in% theme) {
if (missing(detail)) {
detail = "none"
}
detail = match.arg(
as.character(detail),
choices = c(
"none",
levels(geau::so_ii_hydro[["degre"]]),
levels(geau::so_ii_hydro[["type"]])
)
)
color = scales::alpha("blue", .3)
bg = scales::alpha("blue", .3)
border = NA
selection = seq(nrow(geau::so_ii_hydro))
theme_legend = list(
title = sprintf("R\u00e9seau hydrographique"),
legend = "\u00e9l\u00e9ment du r\u00e9seau",
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
col = color,
lwd = 1
)
if (detail %in% levels(geau::so_ii_hydro[["type"]])) {
selection = as.character(geau::so_ii_hydro[["type"]]) == detail
theme_legend[["legend"]] = detail
}
if (detail %in% levels(geau::so_ii_hydro[["degre"]])) {
selection = as.character(geau::so_ii_hydro[["degre"]]) <= detail
}
geometry = geau::so_ii_hydro[["geometry"]][selection]
lwd = 4 - as.numeric(geau::so_ii_hydro[["degre"]][selection])
plot(geometry, col = color, lwd = lwd, border = border, add = TRUE)
}
if ("catchment" %in% theme) {
if (missing(detail)) {
detail = "1"
}
detail = match.arg(
as.character(detail),
choices = levels(geau::so_ii_catchment[["degre"]])
)
selection = geau::so_ii_catchment[["degre"]] == detail
geometry = geau::so_ii_catchment[["geometry"]][selection]
catchment = as.factor(geau::so_ii_catchment[["catchment_name"]][selection])
color_legend = grDevices::hcl.colors(nlevels(catchment), "Pastel 1", alpha = .3)
color = color_legend[catchment]
border = "grey80"
lwd = 2
theme_legend = list(
title = sprintf("Bassin versant"),
legend = levels(catchment),
x = "topright",
cex = .8,
bg = "white",
inset = 0.01,
fill = color_legend,
border = border
)
if (detail == "3") rm(theme_legend)
plot(geometry, border = border, col = color, lwd = lwd, add = TRUE)
}
if (!missing(dataset)) plot(dataset[["geometry"]], add = TRUE, ...)
plot(geau::so_ii_limit, lwd = 2, add = TRUE)
......@@ -344,9 +470,9 @@ map_so_ii = function(
do.call(graphics::legend, dataset_legend)
}
if (legend_theme == TRUE && exists("theme_legend")) {
if (legend_theme == TRUE && exists("theme_legend", inherits = FALSE)) {
temp = do.call(graphics::legend, theme_legend)
if (exists("text_legend")) {
if (exists("text_legend", inherits = FALSE)) {
graphics::text(
x = temp[["rect"]][["left"]] + temp[["rect"]][["w"]],
y = temp[["text"]][["y"]],
......
# 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
)
class(so_ii_population) = "data.frame"
rownames(so_ii_population) = so_ii_population[["CODGEO"]]
selection = grep(
"PMUN|PSCDC|PTOT",
colnames(so_ii_population),
value = TRUE
)
so_ii_population = as.matrix(
so_ii_population[geau::so_ii_scope, selection]
)
year = gsub("PMUN", "20", selection)
year = gsub("PTOT", "19", year)
year = gsub("1919", "19", year)
year = gsub("1918", "18", year)
dimnames(so_ii_population)[[2]] = year
# updating datasets
# actual = setwd(file.path(system.file(package = "geau"), ".."))
actual = setwd("geau")
usethis::use_data(so_ii_population, internal = FALSE, overwrite = TRUE)
setwd(actual)
File added
......@@ -8,7 +8,7 @@
map_so_ii(
dataset,
dataset_legend = NULL,
theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro",
theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro", "onrn",
"population"),
bar = TRUE,
path = NULL,
......@@ -59,6 +59,8 @@ and only the boundaries of collectivities are plotted.}
levels of detail or "canal", "river", "waterbody" for types of
hydrographic elements. If missing, "none" will be chosen, and
everything is plotted.}
\item{\strong{onrn}: detail must be chosen in "n_catnat", "freq_sin",
"cost", "cost_hab", "cost_mean", "ratio", "balance", "ppri_year".}
}
}
\subsection{year specification}{
......
......@@ -5,12 +5,16 @@
\alias{so_ii_clc}
\title{CLC information for so-ii}
\format{
sf object
sf data.frame 1337 rows, 2 variables
\describe{
\item{clc_2018}{character, classification from CLC 2018}
\item{color}{character, default color to be used to plot so_ii_clc}
}
}
\usage{
so_ii_clc
}
\description{
A dataset containing the 2018 version of CLC information for so-ii
A dataset containing the Corine Land Cover information on so-ii.
}
\keyword{datasets}
......@@ -6,22 +6,12 @@
\title{Spatial perimeter of so-ii}
\format{
sfc_POLYGON of length 1
sf data.frame 1337 rows, 2 variables
\describe{
\item{clc_2018}{character, classification from CLC 2018}
\item{color}{character, default color to be used to plot so_ii_clc}
}
}
\usage{
so_ii_limit
so_ii_limit
}
\description{
A dataset containing the perimeter of so-ii.
A dataset containing the Corine Land Cover information on so-ii.
}
\details{
Basically, this dataset is obtained as
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_onrn}
\alias{so_ii_onrn}
\title{ONRN information for so-ii}
\format{
data.frame 78 rows, 23 variables
\describe{
\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{https://www.georisques.gouv.fr/articles-risques/acceder-aux-indicateurs-sinistralite}
}
\usage{
so_ii_onrn
}
\description{
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.
}
\keyword{datasets}
......@@ -16,6 +16,14 @@ map_so_ii(theme = "hydro", detail = "river")
map_so_ii(theme = "catchment")
map_so_ii(theme = "catchment", detail = 2, legend_theme = TRUE)
map_so_ii(theme = "catchment", detail = 3, legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "n_catnat", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "freq_sin", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "cost", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "cost_hab", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "cost_mean", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "ratio", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "balance", legend_theme = TRUE)
map_so_ii(theme = "onrn", detail = "ppri_year", legend_theme = TRUE)
# Can only work if data-common is a symbolic link
......
script/onrn.R 0 → 100644
# Libraries
# library(geau)
# library(sf)
# Updating data from remote
## Local
today = Sys.Date()
archive_dir = sprintf("data-common/data/ONRN/archive/%s", today)
dir.create(archive_dir, showWarnings = FALSE, recursive = TRUE)
## Remote
remote_dir = "https://files.georisques.fr/onrn"
archive = c(
"ONRN_Population_EAIP_CE",
"ONRN_Population_EAIP_SM",
"ONRN_Emprise_totale_bat_EAIP_CE",
"ONRN_Emprise_totale_bat_EAIP_SM",
"ONRN_Emprise_habitations_sans_etage_EAIP_CE",
"ONRN_Emprise_habitations_sans_etage_EAIP_SM",
"ONRN_Entreprises_EAIP",
"sinistralite/ONRN_nbReco_Inondation",
"sinistralite/ONRN_CoutMoyen_Inondation",
"sinistralite/ONRN_CoutCommune_Inondation",
"sinistralite/ONRN_Frequence_Inondation",
"sinistralite/ONRN_SsurP_Inondation",
"sinistralite/ONRN_CoutParHabitant_Inondation",
"sinistralite/ONRN_nbReco_Inondation",
"sinistralite/ONRN_nbReco_Inondation",
"ONRN_Avancement_PPRNI",
"ONRN_Anciennete_PPRNI"
)
## Download
mapply(
utils::download.file,
url = file.path(remote_dir, sprintf("%s.zip", archive)),
destfile = file.path(archive_dir, gsub("sinistralite/", "", sprintf("%s.zip", archive))),
method = "wget"
)
## Unzip
mapply(
utils::unzip,
zipfile = file.path(archive_dir, gsub("sinistralite/", "", sprintf("%s.zip", archive))),
exdir = file.path(archive_dir, "raw")
)
## Convert to UTF-8
onrn_raw = file.path(archive_dir, "raw")
for(f in dir(onrn_raw, pattern = ".csv")) {
system(sprintf("iconv -f ISO-8859-1 -t UTF-8 %s -o %s", file.path(onrn_raw, f), file.path(onrn_raw, "temp.csv")))
system(sprintf("mv %s %s", file.path(onrn_raw, "temp.csv"), file.path(onrn_raw, f)))
}
## Remove pdf
unlink(dir(onrn_raw, pattern = ".pdf", full.names = TRUE))
# Treatment
## Selection
selection = geau::so_ii_scope
## Nombre reconnaissance Cat-Nat "ONRN_nbRecos_Inon"
pattern = "ONRN_nbRecos_Inon"
variable = "n_catnat"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
temp[[variable]][temp[[variable]] == "Pas de reconnaissance"] = 0
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = temp
### Fréquence sinistre: "ONRN_FreqMoyenne_Inon"
pattern = "ONRN_FreqMoyenne_Inon"
variable = "freq_sin"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
conversion = data.frame(
freq_sin = c("Pas de sinistre ou de risque répertoriés à CCR", "Entre 0 et 1 ‰", "Entre 1 et 2 ‰", "Entre 2 et 5 ‰", "Entre 5 et 10 ‰", "Plus de 10 ‰"),
freq_sin_min = c(0, 0, 1, 2, 5, 10)/1000,
freq_sin_max = c(0, 1, 2, 5, 10, 1000)/1000)
temp = merge(temp, conversion, all.x = TRUE)[-1]
temp[[variable]] = (temp[[2]] + temp[[3]]) / 2
temp[[variable]][temp[["freq_sin_max"]] == 1] = 1.5 * temp[[2]][temp[["freq_sin_max"]] == 1]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = merge(result, temp, by = "commune", all.x = TRUE)
### Coût des inondations: "ONRN_CoutCum_Inon"
pattern = "ONRN_CoutCum_Inon"
variable = "cost"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
conversion = data.frame(
cost = c(
"Pas de sinistre répertorié à CCR",
"Entre 0 k€ et 100 k€",
"Entre 100 k€ et 500 k€",
"Entre 500 k€ et 2 M€",
"Entre 2 M€ et 5 M€",
"Entre 5 M€ et 10 M€",
"Entre 10 M€ et 50 M€",
"Entre 50 M€ et 100 M€",
"Supérieur à 100 M€"
),
cost_min = c(0, 0, 1e5, 5e5, 2e6, 5e6, 10e6, 50e6, 100e6),
cost_max = c(0, 1e5, 5e5, 2e6, 5e6, 10e6, 50e6, 100e6, +Inf)
)
all(unique(temp[[variable]]) %in% conversion[[variable]])
temp = merge(temp, conversion, all.x = TRUE)[-1]
temp[[variable]] = (temp[[2]] + temp[[3]]) / 2
temp[[variable]][is.infinite(temp[[variable]])] = 1.5 * temp[[2]][is.infinite(temp[[variable]])]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = merge(result, temp, by = "commune", all.x = TRUE)
### Coût moyen des inondations: "ONRN_CtMoyen_Inon"
pattern = "ONRN_CtMoyen_Inon"
variable = "cost_mean"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
conversion = data.frame(
cost_mean = c(
"Pas de sinistre répertorié à CCR",
"Entre 0 et 2,5 k€",
"Entre 2,5 et 5 k€",
"Entre 5 et 10 k€",
"Entre 10 et 20k€",
"Plus de 20 k€"
),
cost_mean_min = c(0, 0, 2.5e3, 5e3, 10e3, 20e3),
cost_mean_max = c(0, 2.5e3, 5e3, 10e3, 20e3, +Inf)
)
all(unique(temp[[variable]]) %in% conversion[[variable]])
temp = merge(temp, conversion, all.x = TRUE)[-1]
temp[[variable]] = (temp[[2]] + temp[[3]]) / 2
temp[[variable]][is.infinite(temp[[variable]])] = 1.5 * temp[[2]][is.infinite(temp[[variable]])]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = merge(result, temp, by = "commune", all.x = TRUE)
### Coût par habitant des inondations: "ONRN_CoutInon_parHabitant"
pattern = "ONRN_CoutInon_parHabitant"
variable = "cost_hab"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
conversion = data.frame(
cost_hab = c(
"Pas de sinistre répertorié à CCR",
"Moins de 100 €/habitant",
"Entre 100 € et 500€/habitant",
"Entre 500 € et 1 k€/habitant",
"Entre 1 k€ € et 10 k€/habitant",
"Supérieur à 10 k€/habitant",
NA
),
cost_hab_min = c(0, 0, 100, 500, 1000, 10000, NA),
cost_hab_max = c(0, 100, 500, 1000, 10000, +Inf, NA)
)
all(unique(temp[[variable]]) %in% conversion[[variable]])
temp = merge(temp, conversion, all.x = TRUE)[-1]
temp[[variable]] = (temp[[2]] + temp[[3]]) / 2
temp[[variable]][is.infinite(temp[[variable]])] = 1.5 * temp[[2]][is.infinite(temp[[variable]])]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = merge(result, temp, by = "commune", all.x = TRUE)
### Sinistre sur Prime des inondations : "ONRN_SsurP_Inon"
pattern = "ONRN_SsurP_Inon"
variable = "ratio"
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame", col_types = "text")[c(1, 3)]
names(temp) = c("commune", variable)
conversion = data.frame(
ratio = c(
"Pas de sinistre ou de prime répertoriés à CCR",
"Entre 0 et 10 %",
"Entre 10 et 50 %",
"Entre 50 et 100%",
"Entre 100 et 200 %",
"Plus de 200%"),
ratio_min = c(0, 0, 0.1, 0.5, 1, 2),
ratio_max = c(0, 0.1, 0.5, 1, 2, +Inf))
all(unique(temp[[variable]]) %in% conversion[[variable]])
temp = merge(temp, conversion, all.x = TRUE)[-1]
temp[[variable]] = (temp[[2]] + temp[[3]]) / 2
temp[[variable]][is.infinite(temp[[variable]])] = 1.5 * temp[[2]][is.infinite(temp[[variable]])]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
result = merge(result, temp, by = "commune", all.x = TRUE)
### PPRI approuvé: "PPRi_anciennete_avancement"
pattern = "PPRi_anciennete_avancement"
variable = c("ppri_year", "ppri_state", "ppri_state_sub", "ppri_age_ori")
temp = rio::import(
dir(onrn_raw, pattern = pattern, full.names = TRUE),
setclass = "data.frame")[c(1, 8, 7, 2, 9)]
names(temp) = c("commune", variable)
variable = "state"
conversion = data.frame(
ppri_age_ori = c(
"Approuvé depuis moins de 5 ans",
"Approuvé entre 5 et 10 ans",
"Approuvé entre 10 et 20 ans",
"Approuvé depuis plus de 20 ans",
"Prescrit depuis moins de 4 ans",
"Prescrit depuis plus de 4 ans"
),
ppri_state_age = c("approuve", "approuve", "approuve", "approuve", "prescrit", "prescrit"),
ppri_age_min = c(0, 5, 10, 20, 0, 4),
ppri_age_max = c(5, 10, 20, +Inf, 4, +Inf)
)
all(unique(temp[[variable]]) %in% conversion[[variable]])
temp = merge(temp, conversion, all.x = TRUE)[-1]
rownames(temp) = temp[["commune"]]
temp = temp[selection, ]
temp[["commune"]] = selection
result = merge(result, temp, by = "commune", all.x = TRUE)
### Bilan Sinistre - Prime : estimation
pop_france = 66992159 # INSEE (2018)
premium_france = 1670000000 # (CCR2019a pour 2018)
premium_hab = premium_france / pop_france
result[["balance"]] = (1 - result[["ratio"]]) * geau::so_ii_population[ , "2018"] * premium_hab
result = result[c(
"commune", "n_catnat", "freq_sin", "cost", "cost_mean", "cost_hab", "ratio", "balance",
"ppri_year", "ppri_state", "ppri_state_sub", "ppri_state_age", "ppri_age_min", "ppri_age_max",
"freq_sin_min", "freq_sin_max", "cost_min", "cost_max", "cost_mean_min", "cost_mean_max",
"cost_hab_min", "cost_hab_max", "ratio_min", "ratio_max"
)]
write.csv2(result, sprintf("data-common/so-ii/onrn/onrn-%s.csv", today), row.names = FALSE)
unlink(onrn_raw, recursive = TRUE, force = TRUE)
Supports Markdown
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