Commit 20cde0d8 authored by Grelot Frederic's avatar Grelot Frederic :swimmer_tone5:
Browse files

Merge branch 'master' into map_production

Showing with 722 additions and 147 deletions
+722 -147
No preview for this file type
File added
<PAMDataset>
<PAMRasterBand band="1">
<Description>red</Description>
<Metadata>
<MDI key="STATISTICS_MAXIMUM">253</MDI>
<MDI key="STATISTICS_MEAN">-9999</MDI>
<MDI key="STATISTICS_MINIMUM">37</MDI>
<MDI key="STATISTICS_STDDEV">-9999</MDI>
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="2">
<Description>green</Description>
<Metadata>
<MDI key="STATISTICS_MAXIMUM">250</MDI>
<MDI key="STATISTICS_MEAN">-9999</MDI>
<MDI key="STATISTICS_MINIMUM">37</MDI>
<MDI key="STATISTICS_STDDEV">-9999</MDI>
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="3">
<Description>blue</Description>
<Metadata>
<MDI key="STATISTICS_MAXIMUM">246</MDI>
<MDI key="STATISTICS_MEAN">-9999</MDI>
<MDI key="STATISTICS_MINIMUM">37</MDI>
<MDI key="STATISTICS_STDDEV">-9999</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/presence.R
\encoding{UTF-8}
\name{format_presence}
\alias{format_presence}
\title{Transform ods table presence in md table (for Mattermost)}
\usage{
format_presence(x)
}
\arguments{
\item{x}{character or data.frame, path of the ods table or the table itself}
}
\value{
character, md version of table behind x.
}
\description{
Transform ods table presence in md table (for Mattermost)
}
\author{
Frédéric Grelot
}
...@@ -8,12 +8,13 @@ ...@@ -8,12 +8,13 @@
map_so_ii( map_so_ii(
dataset, dataset,
dataset_legend = NULL, dataset_legend = NULL,
theme = c("none", "clc", "catnat", "hydro"), theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro", "onrn",
"osm", "population"),
theme_legend = FALSE,
detail,
year,
bar = TRUE, bar = TRUE,
path = NULL, path = NULL,
legend_theme = FALSE,
year,
detail,
... ...
) )
} }
...@@ -22,17 +23,18 @@ map_so_ii( ...@@ -22,17 +23,18 @@ map_so_ii(
\item{dataset_legend}{list of parameters to be passed to legend} \item{dataset_legend}{list of parameters to be passed to legend}
\item{theme}{character, choice for the theme (if any)} \item{theme}{character, choice for the theme (if any). See details.}
\item{bar}{logical, should a bar be plotted for the dataset} \item{theme_legend}{logical, should a legend be plotted for the theme}
\item{path}{character, the name of the file to save the plot} \item{detail}{character, detail for theme, depends on theme. See details.}
\item{legend_theme}{logical, should a legend be plotted for the theme} \item{year}{character, the year chosen for some themes. See details.}
\item{year}{character, the year chosen for some themes (catnat, population)} \item{bar}{logical, should a bar be plotted for the dataset}
\item{detail}{character, detail for theme, depends on theme} \item{path}{character, the name of the file to save the plot. Graphical
device is chosen depending on extension. See details.}
\item{...}{some parameters that will be used by plot (from sf)} \item{...}{some parameters that will be used by plot (from sf)}
} }
...@@ -43,9 +45,75 @@ Nothing useful. ...@@ -43,9 +45,75 @@ Nothing useful.
Plot a thematic map of so-ii Plot a thematic map of so-ii
} }
\details{ \details{
For theme "catnat", detail must be chosen in c("inondation", "submersion", \subsection{theme specification}{
"nappe"). For the specification of detail, it depends on the theme chosen.
For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal". \itemize{
\item{\strong{none}: perimeter of so_ii is plotted.}
\item{\strong{catchment}: The area of catchments are plotted with a scope
depending on detail. At least, a division between Lez and
Bassin de l'Or is plotted.}
\item{\strong{catnat}: Informations on the number of "Arrêtés Cat
Nat are provided at the scale of collectivities."}
\item{\strong{collectivty}: Boundaries of collectivities are plotted, more
some administrative informations depending on detail.}
\item{\strong{hydro}: The hydrophic network is plotted. Depending on
detail, only a part (rivers, canals, water bodies) or a degre of detail
is plotted.}
\item{\strong{onrn}: Informations on the claims coming from Cat Nat system
are plotted at the scale of the collectivities. With detail a selection
of the data is made, with year a selection of the period.}
\item{\strong{osm}: A tile from OSM is plotted.}
\item{\strong{population}: Informations on the population coming from
INSEE are plotted at the scale of the collectivities. With year a
selection of the period is made, with detail a selection of how
evolution between 2 years.}
}
}
\subsection{detail specification}{
For the specification of detail, it depends on the theme chosen.
\itemize{
\item{\strong{catchment}: detail must be chosen in "none", "1", "2", "3"
for levels of detail. If missing, "1" will be chosen.}
\item{\strong{catnat}: detail must be chosen in "inondation",
"submersion", or "nappe". If missing all type will be chosen and
aggregated before plotting.}
\item{\strong{collectivity}: detail must be chosen in "none", "syble",
"symbo", "epci" or "syndicate". If missing, "none" will be chosen,
and only the boundaries of collectivities are plotted.}
\item{\strong{hydro}: detail must be chosen in "none", "1", "2", "3" for
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".}
\item{\strong{population}: detail must be chosen in "absolute",
"relative". It used only when more than one year is provided to plot
aither absolute or relative evolution.}
}
}
\subsection{year specification}{
For the specification of year, it depends on the theme chosen.
\itemize{
\item{\strong{catnat}: year corresponds to the year of data. If 2 or more
years are given, the sum of the period corresponding to the range of
given years is plotted. If missing, the whole available period is
plotted.}
\item{\strong{population}: year corresponds to the year of data. If
missing, last available year is plotted. If 2 or more years are
provided an analysis of the evolution between the range of given
years is plotted.}
}
}
\subsection{path specification}{
Depending on the extension a device is chosen.
\itemize{
\item{\strong{pdf}: grDevices::cairo_pdf}
\item{\strong{png}: grDevices::png}
\item{\strong{svg}: grDevices::svg}
}
If path is NULL, standard plotting is used. If an extension is not managed,
an error is raised.
}
} }
\examples{ \examples{
...@@ -55,4 +123,6 @@ For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal". ...@@ -55,4 +123,6 @@ For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal".
} }
\author{ \author{
Frédéric Grelot Frédéric Grelot
David Nortes Martinez
} }
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_catchment}
\alias{so_ii_catchment}
\title{Catchment areas of interest within the so-ii perimeter}
\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}
}
\usage{
so_ii_catchment
}
\description{
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.
}
\keyword{datasets}
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
\alias{so_ii_clc} \alias{so_ii_clc}
\title{CLC information for so-ii} \title{CLC information for so-ii}
\format{ \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{ \usage{
so_ii_clc so_ii_clc
} }
\description{ \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} \keyword{datasets}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_collectivity}
\alias{so_ii_collectivity}
\title{Spatial definition of collectivities included in so-ii}
\format{
sf data.frame 78 rows, 11 variables
\describe{
\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.data.gouv.fr/fr/datasets/admin-express/}
}
\usage{
so_ii_collectivity
}
\description{
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.
}
\keyword{datasets}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_commune}
\alias{so_ii_commune}
\title{List of all collectivities included in so-ii}
\format{
sf data.frame 69 rows, 7 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}
}
}
\source{
\url{https://www.data.gouv.fr/fr/datasets/admin-express/}
}
\usage{
so_ii_commune
}
\description{
A dataset containing the INSEE code of all local collectivities
included in so-ii.
}
\details{
Basically this dataset is obtained as a selection from the layer
COMMUNE in ADMIN EXPRESS, more a renaming of variables.
}
\keyword{datasets}
...@@ -8,9 +8,12 @@ ...@@ -8,9 +8,12 @@
sf data.frame 125 rows, 4 variables sf data.frame 125 rows, 4 variables
\describe{ \describe{
\item{id}{id, from BD TOPAGE (corresponding to CdOh)} \item{id}{id, from BD TOPAGE (corresponding to CdOh)}
\item{name}{character, name of the river or part of the river in BD \item{name}{character, name of the hydrographic elements in the BD TOPAGE}
TOPAGE} \item{degre}{factor, level of importance of the hydrographic element
\item{degre}{character, level of detail to plot the hydrographic network} 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{ \source{
......
...@@ -6,22 +6,12 @@ ...@@ -6,22 +6,12 @@
\title{Spatial perimeter of so-ii} \title{Spatial perimeter of so-ii}
\format{ \format{
sfc_POLYGON of length 1 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{ \usage{
so_ii_limit
so_ii_limit so_ii_limit
} }
\description{ \description{
A dataset containing the perimeter of so-ii. A dataset containing the perimeter of so-ii.
A dataset containing the Corine Land Cover information on so-ii.
} }
\details{ \details{
Basically, this dataset is obtained as 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_montpellier}
\alias{so_ii_montpellier}
\title{Spatial definition of districts of Montpellier city}
\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}
}
\usage{
so_ii_montpellier
}
\description{
A dataset containing the spatial definition of all districts
for Montpellier.
}
\keyword{datasets}
% 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}
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
\alias{so_ii_population} \alias{so_ii_population}
\title{Population for so-ii} \title{Population for so-ii}
\format{ \format{
numeric matrix numeric matrix 78 rows, 33 columns
\describe{ \describe{
\item{row}{commune as in so_ii_scope} \item{row}{commune as in so_ii_scope}
\item{column}{year} \item{column}{year}
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
\alias{so_ii_scope} \alias{so_ii_scope}
\title{Local collectivities included in so-ii} \title{Local collectivities included in so-ii}
\format{ \format{
a vector of 69 INSEE code a vector of 78 INSEE code
} }
\usage{ \usage{
so_ii_scope so_ii_scope
......
# Data to be included in library
so_ii = readRDS("data-common/data/so-ii/so-ii_perim.rds")
so_ii_clc = readRDS("data-common/data/so-ii/so-ii_clc.rds")
# Map
map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, path = NULL, ...) {
if (!is.null(path)) {
switch(
EXPR = tolower(tools::file_ext(path)),
"pdf" = pdf(path),
"png" = png(path),
error(sprintf("%s not recognized", tolower(tools::file_ext(path))))
)
}
## Init map
par(mai = c(.65, .60, .50, .15))
plot(so_ii, axes = TRUE)
plot(so_ii, lwd = 2, add = TRUE)
if ("clc" %in% theme) {
color_clc = scales::alpha(c("red3", "darkolivegreen3", "darkgreen", "#4C90B4", "lightblue"), .2)
color = as.character(cut(
as.integer(substr(so_ii_clc[["code_18"]], 1, 1)),
breaks = 5,
labels = color_clc))
plot(so_ii_clc$geometry, border = NA, col = color, add = TRUE)
}
plot(dataset$geometry, add = TRUE, ...)
if (bar == TRUE) {
terra::sbar(10, c(3.55, 43.47), type = "bar", below = "km", label = c(0, 5, 10), cex = .8)
}
if (!is.null(dataset_legend)) {
dataset_legend = c(
x = "bottomright",
cex = .8,
bg = "white",
inset = 0.01,
dataset_legend)
do.call(legend, dataset_legend)
}
if (!is.null(path)) invisible(dev.off())
}
# Data to be plot
dataset = rio::import("data-common/table/so-ii/rex-2020.ods", which = 1)
dataset = dataset[!is.na(dataset$latitude), ]
dataset = sf::st_as_sf(dataset, coords = c("longitude", "latitude"), crs = "WGS84")
# Color
bg = rep(NA, nrow(dataset))
bg[dataset[["viticulture"]]] = "deeppink4"
bg[dataset[["habitant"]]] = "cornflowerblue"
col = "black"
cex = 1.4
pch = 21
# Legend
dataset_legend = list(
title = "Enquêtes du REX 19 septembre 2020",
legend = c("Agriculteurs", "Habitants membre ROI"),
pch = 21,
pt.bg = c("deeppink4", "cornflowerblue"),
pt.cex = 1.4
)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, cex = cex, col = col, pch = pch)
map_so_ii(dataset, dataset_legend, path = "toto.png", bg = bg, cex = cex, col = col, pch = pch)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22)
\ No newline at end of file
```{r library}
library(sf)
library(geau)
```
```{r update-theme}
path = "data-common/figure/so-ii/map/theme/so-ii-%s.pdf"
map_so_ii(theme = "osm", theme_legend = TRUE, path = sprintf(path, "osm"))
map_so_ii(theme = "collectivity", path = sprintf(path, "collectivity"))
map_so_ii(theme = "collectivity", theme_legend = TRUE, detail = "syndicate", path = sprintf(path, "syndicate"))
map_so_ii(theme = "collectivity", theme_legend = TRUE, detail = "epci", path = sprintf(path, "epci"))
map_so_ii(theme = "clc", theme_legend = TRUE, path = sprintf(path, "clc"))
map_so_ii(theme = "population", theme_legend = TRUE, path = sprintf(path, "population"))
map_so_ii(theme = "population", year = 1931, theme_legend = TRUE, path = sprintf(path, "population-1931"))
map_so_ii(theme = "population", year = 2018:2019, theme_legend = TRUE, path = sprintf(path, "population-last"))
map_so_ii(theme = "population", year = 2014:2019, theme_legend = TRUE, path = sprintf(path, "population-last-5"))
map_so_ii(theme = "catnat", theme_legend = TRUE, path = sprintf(path, "catnat"))
map_so_ii(theme = "catnat", year = 2000:2019, theme_legend = TRUE, path = sprintf(path, "catnat-2000-2019"))
map_so_ii(theme = "catnat", year = 2021, theme_legend = TRUE, path = sprintf(path, "catnat-2021"))
map_so_ii(so_ii_hydro[as.character(so_ii_hydro$degre) <= 1, ], col = scales::colour_ramp(c("white", "blue"))(.5), border = NA, theme = "catchment", theme_legend = TRUE, path = sprintf(path, "catchment"))
map_so_ii(so_ii_hydro[as.character(so_ii_hydro$degre) <= 2, ], col = scales::colour_ramp(c("white", "blue"))(.5), border = NA, theme = "catchment", detail = 2, theme_legend = TRUE, path = sprintf(path, "catchment_sub"))
map_so_ii(theme = "onrn", detail = "freq_sin", theme_legend = TRUE, path = sprintf(path, "freq_sin"))
map_so_ii(theme = "onrn", detail = "cost", theme_legend = TRUE, path = sprintf(path, "cost"))
map_so_ii(theme = "onrn", detail = "cost_hab", theme_legend = TRUE, path = sprintf(path, "cost_hab"))
map_so_ii(theme = "onrn", detail = "cost_mean", theme_legend = TRUE, path = sprintf(path, "cost_mean"))
map_so_ii(theme = "onrn", detail = "ratio", theme_legend = TRUE, path = sprintf(path, "ratio"))
map_so_ii(theme = "onrn", detail = "balance", theme_legend = TRUE, path = sprintf(path, "balance"))
map_so_ii(theme = "onrn", detail = "ppri_year", theme_legend = TRUE, path = sprintf(path, "ppri_year"))
```
```{r update-rex-example}
path = "data-common/figure/so-ii/map/rex/so-ii-%s.pdf"
dataset = readODS::read_ods("data-common/table/so-ii/rex-2020-09-19-enquete.ods")
dataset = sf::st_as_sf(dataset, coords = c("longitude", "latitude"), crs = "WGS84")
pch = c(
"habitant" = 21,
"agriculteur" = 24
)
color = c(
"cereale" = "yellow",
"habitant" = "black",
"viticulture" = "purple",
"maraichage" = "red"
)
cex = 2
map_so_ii(
dataset,
main = "Contact",
pch = pch[dataset[["statut"]]],
bg = color[dataset[["activite"]]],
cex = 2,
theme = "clc"
)
```
\ No newline at end of file
library(sf)
map_so_ii()
map_so_ii(theme = "osm")
map_so_ii(theme = "collectivity")
map_so_ii(theme = "collectivity", detail = "syndicate")
map_so_ii(theme = "collectivity", detail = "syble")
map_so_ii(theme = "collectivity", detail = "symbo")
map_so_ii(theme = "collectivity", detail = "epci", theme_legend = TRUE)
map_so_ii(theme = "clc")
map_so_ii(theme = "population", theme_legend = TRUE)
map_so_ii(theme = "population", year = "2006", theme_legend = TRUE)
map_so_ii(theme = "population", year = c(2006, 2019), theme_legend = TRUE)
map_so_ii(theme = "population", year = c(1876, 2019), detail ="relative", theme_legend = TRUE)
map_so_ii(theme = "catnat", theme_legend = TRUE)
map_so_ii(theme = "catnat", detail = "nappe", theme_legend = TRUE)
map_so_ii(theme = "catnat", detail = "inondation", year = 2003, theme_legend = TRUE)
map_so_ii(theme = "catnat", detail = "inondation", year = 2003:2014, theme_legend = TRUE)
map_so_ii(theme = "hydro")
map_so_ii(theme = "hydro", detail = 2)
map_so_ii(theme = "hydro", detail = "river")
map_so_ii(theme = "catchment")
map_so_ii(theme = "catchment", detail = 2, theme_legend = TRUE)
map_so_ii(theme = "catchment", detail = 3, theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "n_catnat", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "freq_sin", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "cost", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "cost_hab", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "cost_mean", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "ratio", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "balance", theme_legend = TRUE)
map_so_ii(theme = "onrn", detail = "ppri_year", theme_legend = TRUE)
# Can only work if data-common is a symbolic link # Can only work if data-common is a symbolic link
# Data to be plotted # Data to be plotted
...@@ -24,11 +57,11 @@ dataset_legend = list( ...@@ -24,11 +57,11 @@ dataset_legend = list(
pt.cex = cex pt.cex = cex
) )
map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "clc", legend_theme = TRUE) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "clc", theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, legend_theme = TRUE) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, hazard = "nappe", legend_theme = TRUE) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "catnat", year = 2020, hazard = "nappe", theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "population", legend_theme = TRUE) map_so_ii(dataset, dataset_legend, bg = bg, pch = pch, theme = "population", theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, path = "rex-clc.pdf", bg = bg, pch = pch, theme = "clc", year = 2020, legend_theme = TRUE) map_so_ii(dataset, dataset_legend, path = "rex-clc.pdf", bg = bg, pch = pch, theme = "clc", year = 2020, theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, path = "rex-catnat.pdf", bg = bg, pch = pch, theme = "catnat", year = 2020, legend_theme = TRUE) map_so_ii(dataset, dataset_legend, path = "rex-catnat.pdf", bg = bg, pch = pch, theme = "catnat", year = 2020, theme_legend = TRUE)
map_so_ii(dataset, dataset_legend, path = "rex-population.pdf", bg = bg, pch = pch, theme = "population", legend_theme = TRUE) map_so_ii(dataset, dataset_legend, path = "rex-population.pdf", bg = bg, pch = pch, theme = "population", theme_legend = TRUE)
\ No newline at end of file \ No newline at end of file
# Functions
# Data
input_dir = geau::current_version("data-common/data/IGN/ADMIN-EXPRESS/version")
commune = sf::st_read(file.path(input_dir, "COMMUNE.shp"))
commune = sf::st_drop_geometry(commune)
commune = commune[c("NOM", "NOM_M", "INSEE_COM", "INSEE_DEP", "INSEE_REG", "SIREN_EPCI")]
names(commune) = c("commune_name", "commune_nam_cap", "commune", "departement", "region", "epci")
rownames(commune) = commune[["commune"]]
commune = commune[geau::so_ii_scope, ]
commune_so_ii = read.csv2(geau::current_version("data-common/so-ii/commune"))
commune_so_ii = merge(commune_so_ii[c("commune", "syble", "symbo")], commune)
epci = sf::st_read(file.path(input_dir, "EPCI.shp"))
epci = sf::st_drop_geometry(epci)
names(epci) = c("id", "epci", "epci_name", "epci_nature")
rownames(epci) = epci[["epci"]]
epci = epci[unique(commune[["epci"]]), c("epci", "epci_name", "epci_nature")]
# Save
today = Sys.Date()
write.csv2(
commune_so_ii,
sprintf("data-common/so-ii/commune/commune-%s.csv", today),
row.names = FALSE
)
write.csv2(
epci,
sprintf("data-common/so-ii/epci/epci-%s.csv", today),
row.names = FALSE
)
\ No newline at end of file
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
# result = read.csv2(geau::current_version("data-common/so-ii/onrn"))
#### Some data to compute premium per habitant
pop_france = 66992159 # INSEE (2018)
premium_france = 1670000000 # (CCR2019a pour 2018)
premium_hab = premium_france / pop_france
#### Need to compute cumulative population
period = seq(1995, 2018)
available = as.integer(dimnames(geau::so_ii_population)[[2]])
selection = as.character(available[sapply(period, function(x){which.min(abs(available - x))})])
pop_commune = rowSums(geau::so_ii_population[, selection])
result[["balance"]] = (1 - result[["ratio"]]) * pop_commune * 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)
# write.csv2(result, geau::current_version("data-common/so-ii/onrn"), 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