diff --git a/dev/package.development.R b/dev/package.development.R index 07d65fa1cc78cbe4139a19936da766ccdbd9292e..ceee3bdf02f39b256a1a22953e4e318478b9eef3 100644 --- a/dev/package.development.R +++ b/dev/package.development.R @@ -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 diff --git a/geau/DESCRIPTION b/geau/DESCRIPTION index c78a0cfc46e3fabfb87fd63f2a32ced593dc3a6f..c688320a42ea6733315f4c9e9ab75fff85a162be 100644 --- a/geau/DESCRIPTION +++ b/geau/DESCRIPTION @@ -1,6 +1,6 @@ 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, diff --git a/geau/NAMESPACE b/geau/NAMESPACE index f4d57d02574fc4c75a14bf7043817f4494019914..879c502667d927b550a42a28cb2a3e6522914ca4 100644 --- a/geau/NAMESPACE +++ b/geau/NAMESPACE @@ -3,5 +3,6 @@ export(add.inset) export(current_version) export(estimate_catnat_freq) +export(format_presence) export(kable_units) export(map_so_ii) diff --git a/geau/R/data.r b/geau/R/data.r index c2e33bbde6e587c4faef5c001a87a2a12d41f752..c9e084641dc1e16b646c03636deca6f7ee4615da 100644 --- a/geau/R/data.r +++ b/geau/R/data.r @@ -1,43 +1,44 @@ -#' 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 diff --git a/geau/R/format_presence.R b/geau/R/format_presence.R new file mode 100644 index 0000000000000000000000000000000000000000..87954fc3effb7b5b5e9b23133cbae143af6daf57 --- /dev/null +++ b/geau/R/format_presence.R @@ -0,0 +1,17 @@ +#' @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))) +} diff --git a/geau/R/map_so_ii.r b/geau/R/map_so_ii.r index b1edc391c566ed460827bf2e4f4b906dd86c4b4a..a053910ddece453cefc090d4b3e3acce84f60338 100644 --- a/geau/R/map_so_ii.r +++ b/geau/R/map_so_ii.r @@ -1,26 +1,94 @@ #' @title Plot a thematic map of so-ii #' -#' @details -#' For theme "catnat", detail must be chosen in c("inondation", "submersion", -#' "nappe"). -#' For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal". -#' +#' @details +#' \subsection{theme specification}{ +#' For the specification of detail, it depends on the theme chosen. +#' \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. +#' } +#' #' @param dataset sf objectf, data to be plotted #' @param dataset_legend list of parameters to be passed to legend -#' @param theme character, choice for the theme (if any) +#' @param theme character, choice for the theme (if any). See details. +#' @param theme_legend logical, should a legend be plotted for the theme +#' @param detail character, detail for theme, depends on theme. See details. +#' @param year character, the year chosen for some themes. See details. #' @param bar logical, should a bar be plotted for the dataset -#' @param path character, the name of the file to save the plot -#' @param legend_theme logical, should a legend be plotted for the theme -#' @param year character, the year chosen for some themes (catnat, population) -#' @param detail character, detail for theme, depends on theme +#' @param path character, the name of the file to save the plot. Graphical +#' device is chosen depending on extension. See details. #' @param ... some parameters that will be used by plot (from sf) #' #' @return Nothing useful. #' -#' @export +#' @export map_so_ii #' #' @encoding UTF-8 #' @author Frédéric Grelot +#' @author David Nortes Martinez #' #' @examples #' @@ -31,12 +99,12 @@ map_so_ii = function( dataset, dataset_legend = NULL, - theme = c("none", "clc", "catnat", "hydro", "population"), + theme = c("none", "collectivity", "catchment", "catnat", "clc", "hydro", "onrn", "osm", "population"), + theme_legend = FALSE, + detail, + year, bar = TRUE, path = NULL, - legend_theme = FALSE, - year, - detail, ... ) { theme = match.arg(theme) @@ -44,8 +112,9 @@ map_so_ii = function( if (!is.null(path)) { switch( EXPR = tolower(tools::file_ext(path)), - "pdf" = grDevices::pdf(path), + "pdf" = grDevices::cairo_pdf(path), "png" = grDevices::png(path), + "svg" = grDevices::svg(path), stop(sprintf("%s not recognized", tolower(tools::file_ext(path)))) ) on.exit(grDevices::dev.off()) @@ -53,52 +122,497 @@ map_so_ii = function( ## Init map graphics::par(mai = c(.65, .60, .50, .15)) - plot(geau::so_ii_limit, axes = TRUE) + plot(geau::so_ii_limit, axes = TRUE, main = list(...)[["main"]], cex.main = 3) - if ("clc" %in% theme) { - plot( - geau::so_ii_clc[["geometry"]], - border = NA, - col = geau::so_ii_clc[["color"]], - add = TRUE + ## Plot theme if any, return theme_legend + theme_legend = switch( + EXPR = theme, + "catchment" = map_theme_catchment(detail, theme_legend), + "catnat" = map_theme_catnat(detail, year, theme_legend), + "clc" = map_theme_clc(theme_legend), + "collectivity" = map_theme_collectivity(detail, theme_legend), + "hydro" = map_theme_hydro(detail, theme_legend), + "onrn" = map_theme_onrn(detail, theme_legend), + "osm" = map_theme_osm(), + "population" = map_theme_population(detail, year, theme_legend), + NULL + ) + + ## Plot dataset if any + if (!missing(dataset)) plot(dataset[["geometry"]], add = TRUE, ...) + + ## Make so_ii_limit visible + plot(geau::so_ii_limit, lwd = 2, add = TRUE) + + ## Plot bar + if (bar == TRUE) { + terra::sbar( + 10, c(3.55, 43.47), + type = "bar", + below = "km", + label = c(0, 5, 10), + cex = .8 ) + } - theme_legend = list( - title = "CLC (2018)", - legend = geau::clc_color[["label_fr"]], - x = "topright", + ## Plotdataset_legend if any + if (!is.null(dataset_legend)) { + dataset_legend = c( + x = "bottomright", cex = .8, bg = "white", inset = 0.01, - fill = geau::clc_color[["color"]] - ) + dataset_legend) + do.call(graphics::legend, dataset_legend) } - if ("population" %in% theme) { - if (missing(year)) { - year = utils::tail(sort(colnames(geau::so_ii_population)), 1) + ## Plot theme_legend if any + if (!is.null(theme_legend)) { + if (!is.null(theme_legend[["text.width"]])) { + text_legend = theme_legend[["legend"]] + theme_legend[["legend"]] = rep("", length(text_legend)) + } + temp = do.call(graphics::legend, theme_legend) + if (!is.null(theme_legend[["text.width"]])) { + graphics::text( + x = temp[["rect"]][["left"]] + temp[["rect"]][["w"]], + y = temp[["text"]][["y"]], + labels = text_legend, + pos = 2 + ) } - population_palette = scales::colour_ramp(c("white", "red"), alpha = .5) + } + + return(invisible(NULL)) +} + +map_theme_catchment = function(detail, add_legend) { + 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 + ) + plot(geometry, border = border, col = color, lwd = lwd, add = TRUE) + + if (add_legend == TRUE && detail != "3") { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_catnat = function(detail, year, add_legend) { + if (missing(detail)) { + detail = dimnames(geau::so_ii_catnat)[["hazard"]] + } + detail = match.arg( + detail, + dimnames(geau::so_ii_catnat)[["hazard"]], + several.ok = TRUE + ) + + if (missing(year)) { + year = range(dimnames(geau::so_ii_catnat)[["period"]]) + } + year = match.arg( + as.character(year), + dimnames(geau::so_ii_catnat)[["period"]], + several.ok = TRUE + ) + year = as.character(seq(min(year), max(year))) + catnat = apply( + geau::so_ii_catnat[, year, detail, drop = FALSE], + 1, + sum + ) + + border = "grey80" + catnat_palette = scales::colour_ramp(c("white", "grey50"), alpha = .5) + color = scales::cscale( + c(0, catnat), + catnat_palette + )[-1] + plot( + geau::so_ii_collectivity[["geometry"]], + border = border, + col = color, + add = TRUE + ) + + legend_title = sprintf( + "Cat Nat %s", + if (length(detail) == 3) "" else paste(sort(detail), collapse = " & ") + ) + legend_title = sprintf( + "%s [%s]", + legend_title, + if (length(year) == 1) year else paste(range(year), collapse = "-") + ) + + value_legend = unique(sort(c(min(catnat), round(seq(0, max(catnat), length.out = 5))))) + color_legend = scales::cscale( + value_legend, + catnat_palette + ) + + theme_legend = list( + title = legend_title, + legend = value_legend, + x = "topright", + cex = .8, + bg = "white", + inset = 0.01, + fill = color_legend, + border = border, + text.width = max(graphics::strwidth(value_legend)) + ) + + if (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_clc = function(add_legend) { + 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 (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_collectivity = function(detail, add_legend) { + if (missing(detail)) { + detail = "none" + } + detail = match.arg( + detail, + c("none", "syble", "symbo", "epci", "syndicate") + ) + + border = "grey80" + color = NA + + theme_legend = list( + title = "Caract\u00e9ristiques des communes", + legend = "Commune", + x = "topright", + cex = .8, + bg = "white", + inset = 0.01, + fill = color, + border = border + ) + geometry = geau::so_ii_collectivity[["geometry"]] + plot(geometry, border = border, col = color, add = TRUE) + + if (detail %in% c("syble", "syndicate")) { + color_legend = scales::alpha("orange", .3) + color = ifelse( + geau::so_ii_collectivity[["syble"]], + color_legend, + NA + ) + plot(geometry, border = border, col = color, add = TRUE) + theme_legend[["legend"]] = c(theme_legend[["legend"]], "SYBLE") + theme_legend[["fill"]] = c(theme_legend[["fill"]], color_legend) + } + if (detail %in% c("symbo", "syndicate")) { + color_legend = scales::alpha("green", .3) + color = ifelse( + geau::so_ii_collectivity[["symbo"]], + color_legend, + NA + ) + plot(geometry, border = border, col = color, add = TRUE) + theme_legend[["legend"]] = c(theme_legend[["legend"]], "SYMBO") + theme_legend[["fill"]] = c(theme_legend[["fill"]], color_legend) + } + if (detail == "epci") { + epci = as.factor(geau::so_ii_collectivity[["epci_name"]]) + color_legend = grDevices::hcl.colors(nlevels(epci), "Lisbon", alpha = .3) + color = color_legend[epci] + plot(geometry, border = border, col = color, add = TRUE) + theme_legend[["legend"]] = levels(epci) + theme_legend[["fill"]] = color_legend + } + + if (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_hydro = function(detail, add_legend) { + 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 (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_onrn = function(detail, add_legend) { + 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_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 = " " + ) + text.width = max(graphics::strwidth(text_legend)) + } + 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" = "Arr\u00eat\u00e9s Cat-Nat [1982-2021]", + "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 = title_onrn, + legend = text_legend, + x = "topright", + cex = .8, + bg = "white", + inset = 0.01, + fill = color_legend, + border = border + ) + if (detail %in% c("balance", "cost")) { + theme_legend[["text.width"]] = max(graphics::strwidth(text_legend)) + } + + if (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) + } +} + +map_theme_osm = function() { + so_ii_osm = terra::rast( + system.file("extdata", "so_ii_osm.tif", package = "geau", mustWork = TRUE) + ) + try(terra::plot(so_ii_osm, add = TRUE), silent = TRUE) + graphics::mtext( + text = "Fond de carte : \u00a9 Contributeurs OpenStreetMap", + side = 1, line = -1, adj = 1, cex = .6, font = 3 + ) + return(NULL) +} + +map_theme_population = function(detail, year, add_legend) { + if (missing(year)) { + year = utils::tail(sort(colnames(geau::so_ii_population)), 1) + } + year = match.arg( + as.character(year), + sort(colnames(geau::so_ii_population)), + several.ok = TRUE + ) + + border = "grey80" + + if (length(year) == 1) { + pop_palette = scales::colour_ramp(c("white", "red"), alpha = .5) color = matrix( scales::cscale( geau::so_ii_population, - population_palette, + pop_palette, trans = scales::log_trans()), nrow = nrow(geau::so_ii_population), dimnames = dimnames(geau::so_ii_population) ) - border = "grey80" + plot( - geau::so_ii_commune[["geometry"]], + geau::so_ii_collectivity[["geometry"]], border = border, col = color[ , year], add = TRUE ) + max_pop = max(geau::so_ii_population[ , year]) + min_pop = min(geau::so_ii_population[ , year]) + base = 10 - value_legend = c(100, 1000, 10000, 100000, 250000) + value_legend = unique(c( + min_pop, + base^(ceiling(log(min_pop)/log(base)):floor(log(max_pop)/log(base))), + max_pop + )) color_legend = scales::cscale( c(range(geau::so_ii_population), value_legend), - population_palette, + pop_palette, trans = scales::log_trans() )[-(1:2)] text_legend = formatC( @@ -108,129 +622,145 @@ map_so_ii = function( theme_legend = list( title = sprintf("Population %s", year), - legend = rep("", length(text_legend)), + legend = text_legend, x = "topright", cex = .8, bg = "white", inset = 0.01, fill = color_legend, border = border, - text.width = graphics::strwidth(utils::tail(text_legend, 1)) + text.width = max(graphics::strwidth(text_legend)) ) } - - if ("catnat" %in% theme) { + + if (length(year) > 1) { 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)) { - border = "grey80" - catnat = apply( - geau::so_ii_catnat[, as.character(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 - ) + detail = "absolute" } + detail = match.arg(as.character(detail), c("absolute", "relative")) + year = range(year) + pop_palette = scales::colour_ramp( + c("red", "white", "green"), + alpha = .5 + ) + pop_data = switch( + EXPR = detail, + "absolute" = geau::so_ii_population[ , year[2]] - + geau::so_ii_population[ , year[1]], + "relative" = (geau::so_ii_population[ , year[2]] - + geau::so_ii_population[ , year[1]]) / + geau::so_ii_population[ , year[1]] + ) + range_data = max(abs(range(pop_data))) * c(-1, 1) + pop_trans = switch( + EXPR = detail, + "absolute" = scales::modulus_trans(0.2), + "relative" = scales::modulus_trans(0.1), + NULL + ) + color = scales::cscale( + c(range_data, pop_data), + pop_palette, + trans = pop_trans + )[-(1:2)] plot( - geau::so_ii_commune[["geometry"]], + geau::so_ii_collectivity[["geometry"]], border = border, col = color, add = TRUE ) - } - if ("hydro" %in% theme) { - if (missing(detail)) { - detail = "0" - } - detail = match.arg( - as.character(detail), - choices = c("0", "1", "2", "3", "canal") - ) - if (detail == "canal") { - selection = geau::so_ii_hydro[["degre"]] == detail - geometry = geau::so_ii_hydro[["geometry"]][selection] - color = scales::alpha("red", .3) - lwd = 1 - } else { - selection = geau::so_ii_hydro[["degre"]] <= detail - geometry = geau::so_ii_hydro[["geometry"]][selection] - color = scales::alpha("blue", .3) - lwd = 4 - as.numeric(geau::so_ii_hydro[["degre"]][selection]) + max_pop = max(pop_data) + min_pop = min(pop_data) + + if (detail == "absolute") { + range_pop = max(abs(c(max_pop, min_pop))) + base = max(10, 10^floor(ceiling(log(range_pop)/log(10)) / 2)) + + if (sign(min_pop) == -1) { + value_legend = c( + -base^(floor(log(abs(min_pop))/log(base)):1), + base^(1:floor(log(max_pop)/log(base))) + ) + value_legend = value_legend[ + value_legend < max_pop & + value_legend > min_pop & + abs(value_legend) >= base + ] + value_legend = sort(c(0, range(pop_data), value_legend)) + } else { + value_legend = unique(c( + min_pop, + base^(ceiling(log(min_pop)/log(base)):floor(log(max_pop)/log(base))), + max_pop + )) + } + color_legend = scales::cscale( + c(range_data, value_legend), + pop_palette, + trans = pop_trans + )[-(1:2)] + text_legend = formatC( + as.integer(value_legend), + big.mark = " " + ) + title_legend = sprintf("Population \u00e9volution [%s-%s]", year[1], year[2]) } - plot(geometry, col = color, lwd = lwd, add = TRUE) + if (detail == "relative") { + max_pop = max(pop_data) * 100 + min_pop = min(pop_data) * 100 + range_pop = max(abs(c(max_pop, min_pop))) + base = max(10, 10^floor(ceiling(log(range_pop)/log(10)) / 2)) + + if (sign(min_pop) == -1) { + value_legend = unique(c( + min_pop, + -base^(floor(log(abs(min_pop))/log(base)):0), + 0, + base^(0:floor(log(max_pop)/log(base))), + max_pop + )) + } else { + value_legend = unique(c( + min_pop, + base^(ceiling(log(min_pop)/log(base)):floor(log(max_pop)/log(base))), + max_pop + )) + } + color_legend = scales::cscale( + c(range_data, value_legend / 100), + pop_palette, + trans = pop_trans + )[-(1:2)] + text_legend = sprintf( + "%s %%", + formatC( + signif(value_legend, 3), + digits = 2, format = "f", flag = "+", + big.mark = " " + ) + ) + title_legend = sprintf("Population \u00e9volution [%s-%s]", year[1], year[2]) + } theme_legend = list( - title = sprintf("R\u00e9seau hydrographique"), - legend = ifelse(detail == "canal", "canal", "cours d'eau"), + title = title_legend, + legend = text_legend, x = "topright", cex = .8, bg = "white", inset = 0.01, - col = color, - lwd = 2 - ) - } - - if (!missing(dataset)) plot(dataset[["geometry"]], add = TRUE, ...) - - plot(geau::so_ii_limit, lwd = 2, add = TRUE) - - if (bar == TRUE) { - terra::sbar( - 10, c(3.55, 43.47), - type = "bar", - below = "km", - label = c(0, 5, 10), - cex = .8 + fill = color_legend, + border = border, + text.width = max(graphics::strwidth(text_legend)) ) } - if (!is.null(dataset_legend)) { - dataset_legend = c( - x = "bottomright", - cex = .8, - bg = "white", - inset = 0.01, - dataset_legend) - do.call(graphics::legend, dataset_legend) - } - - if (legend_theme == TRUE && exists("theme_legend")) { - temp = do.call(graphics::legend, theme_legend) - if (exists("text_legend")) { - graphics::text( - x = temp[["rect"]][["left"]] + temp[["rect"]][["w"]], - y = temp[["text"]][["y"]], - labels = text_legend, - pos = 2 - ) - } + if (add_legend == TRUE) { + return(theme_legend) + } else { + return(NULL) } - - return(invisible(NULL)) } diff --git a/geau/data-raw/so_ii_catchment.R b/geau/data-raw/so_ii_catchment.R new file mode 100644 index 0000000000000000000000000000000000000000..fa2e210d604c20a14397a25ee97e07a9d6fd2e35 --- /dev/null +++ b/geau/data-raw/so_ii_catchment.R @@ -0,0 +1,16 @@ +# 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) diff --git a/geau/data-raw/so_ii_collectivity.R b/geau/data-raw/so_ii_collectivity.R new file mode 100644 index 0000000000000000000000000000000000000000..842007b2080b5f0a6b4dbd95eb8ffc73c835afae --- /dev/null +++ b/geau/data-raw/so_ii_collectivity.R @@ -0,0 +1,44 @@ +# 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) diff --git a/geau/data-raw/so_ii_hydro.R b/geau/data-raw/so_ii_hydro.R index f86ebcc6ce1f13d0132d128dbdd3bc66a4b9f3d5..f3eb9d79adabb5e81da0e8fe145b0acdcdbbd128 100644 --- a/geau/data-raw/so_ii_hydro.R +++ b/geau/data-raw/so_ii_hydro.R @@ -1,27 +1,41 @@ # 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) diff --git a/geau/data-raw/so_ii_montpellier.R b/geau/data-raw/so_ii_montpellier.R new file mode 100644 index 0000000000000000000000000000000000000000..e5a5db70f4e73c192a5f4583f4c2bcb9a8397c3c --- /dev/null +++ b/geau/data-raw/so_ii_montpellier.R @@ -0,0 +1,21 @@ +# 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) diff --git a/geau/data-raw/so_ii_onrn.R b/geau/data-raw/so_ii_onrn.R new file mode 100644 index 0000000000000000000000000000000000000000..3fb1f76b88734743a7e6745b37e661caa9053f9c --- /dev/null +++ b/geau/data-raw/so_ii_onrn.R @@ -0,0 +1,14 @@ +# 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) diff --git a/geau/data-raw/so_ii_osm.R b/geau/data-raw/so_ii_osm.R new file mode 100644 index 0000000000000000000000000000000000000000..f61a55c78d523a02deff4d8baccce2cd50919e4d --- /dev/null +++ b/geau/data-raw/so_ii_osm.R @@ -0,0 +1,11 @@ +# 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) diff --git a/geau/data-raw/so_ii_population.R b/geau/data-raw/so_ii_population.R index a771c06304b70495be2fb2a1afd1e8d74fb31555..48c33714989d3607a51eb4201d7295f742eacee3 100644 --- a/geau/data-raw/so_ii_population.R +++ b/geau/data-raw/so_ii_population.R @@ -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) diff --git a/geau/data-raw/so_ii_scope.R b/geau/data-raw/so_ii_scope.R index c69a3905d4c0de33c6a14c4f4cc948e612bfa742..d058bd0758b3fc3ee9aa65c8e441efe98bce110a 100644 --- a/geau/data-raw/so_ii_scope.R +++ b/geau/data-raw/so_ii_scope.R @@ -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) diff --git a/geau/data/so_ii_catchment.rda b/geau/data/so_ii_catchment.rda new file mode 100644 index 0000000000000000000000000000000000000000..4b567b77c387dae46c70acbd261594288a68f480 Binary files /dev/null and b/geau/data/so_ii_catchment.rda differ diff --git a/geau/data/so_ii_collectivity.rda b/geau/data/so_ii_collectivity.rda new file mode 100644 index 0000000000000000000000000000000000000000..521aceb0657d4da464d8dd2536c6740d6748afa8 Binary files /dev/null and b/geau/data/so_ii_collectivity.rda differ diff --git a/geau/data/so_ii_commune.rda b/geau/data/so_ii_commune.rda deleted file mode 100644 index af05cfa70cffbcb870553a80ea3a611a38c6bc58..0000000000000000000000000000000000000000 Binary files a/geau/data/so_ii_commune.rda and /dev/null differ diff --git a/geau/data/so_ii_hydro.rda b/geau/data/so_ii_hydro.rda index 07c507b7dee470b6d8fdfe7c38fb9ca8a8a4ee90..1f6235f046cd315fc11ea7a7d3b01ae917596108 100644 Binary files a/geau/data/so_ii_hydro.rda and b/geau/data/so_ii_hydro.rda differ diff --git a/geau/data/so_ii_montpellier.rda b/geau/data/so_ii_montpellier.rda new file mode 100644 index 0000000000000000000000000000000000000000..2e142cd10b2a569c0b64f1c2459e0d1ee51f1d3f Binary files /dev/null and b/geau/data/so_ii_montpellier.rda differ diff --git a/geau/data/so_ii_onrn.rda b/geau/data/so_ii_onrn.rda new file mode 100644 index 0000000000000000000000000000000000000000..abc37b177092e92315c09e26ccdc278834f42cf9 Binary files /dev/null and b/geau/data/so_ii_onrn.rda differ diff --git a/geau/data/so_ii_population.rda b/geau/data/so_ii_population.rda index bc3a1b17f4b91879f41c30609c3c58215fe403f4..40b9f762816882b26cb8a91b2948dbdab5d9a6ed 100644 Binary files a/geau/data/so_ii_population.rda and b/geau/data/so_ii_population.rda differ diff --git a/geau/inst/extdata/so_ii_osm.tif b/geau/inst/extdata/so_ii_osm.tif new file mode 100644 index 0000000000000000000000000000000000000000..5db3ff661158aa454207497c711031511ed38ccc Binary files /dev/null and b/geau/inst/extdata/so_ii_osm.tif differ diff --git a/geau/inst/extdata/so_ii_osm.tif.aux.xml b/geau/inst/extdata/so_ii_osm.tif.aux.xml new file mode 100644 index 0000000000000000000000000000000000000000..fc27fcd04a288e028d24faf046b98e7f548165ad --- /dev/null +++ b/geau/inst/extdata/so_ii_osm.tif.aux.xml @@ -0,0 +1,29 @@ +<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> diff --git a/geau/man/format_presence.Rd b/geau/man/format_presence.Rd new file mode 100644 index 0000000000000000000000000000000000000000..ca96940ebe6aca13e966d64056b0e0175bf22fdc --- /dev/null +++ b/geau/man/format_presence.Rd @@ -0,0 +1,21 @@ +% 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 +} diff --git a/geau/man/map_so_ii.Rd b/geau/man/map_so_ii.Rd index e2c1ac696702834840d863d455056d39fe9d1fbb..2dc0259884e2f37619b6fb7d6467a9ba592ba723 100644 --- a/geau/man/map_so_ii.Rd +++ b/geau/man/map_so_ii.Rd @@ -8,12 +8,13 @@ map_so_ii( dataset, 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, path = NULL, - legend_theme = FALSE, - year, - detail, ... ) } @@ -22,17 +23,18 @@ map_so_ii( \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)} } @@ -43,9 +45,75 @@ Nothing useful. Plot a thematic map of so-ii } \details{ -For theme "catnat", detail must be chosen in c("inondation", "submersion", -"nappe"). -For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal". +\subsection{theme specification}{ +For the specification of detail, it depends on the theme chosen. +\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{ @@ -55,4 +123,6 @@ For theme "hydro" detail must be chosen in "0", "1", "2", "3" or "canal". } \author{ Frédéric Grelot + +David Nortes Martinez } diff --git a/geau/man/so_ii_catchment.Rd b/geau/man/so_ii_catchment.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0cfe4ebe5646c2bb3ef083c5be367f97a8bbdcf7 --- /dev/null +++ b/geau/man/so_ii_catchment.Rd @@ -0,0 +1,30 @@ +% 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} diff --git a/geau/man/so_ii_clc.Rd b/geau/man/so_ii_clc.Rd index 0d62e5c3312b105257842ef172f80c92eb82a5d3..2f08e4b98eb345495a0034e483eb3e16c39a5ad9 100644 --- a/geau/man/so_ii_clc.Rd +++ b/geau/man/so_ii_clc.Rd @@ -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} diff --git a/geau/man/so_ii_collectivity.Rd b/geau/man/so_ii_collectivity.Rd new file mode 100644 index 0000000000000000000000000000000000000000..28fb45833f55ca9eea6371451d6b0fff4e19aac0 --- /dev/null +++ b/geau/man/so_ii_collectivity.Rd @@ -0,0 +1,41 @@ +% 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} diff --git a/geau/man/so_ii_commune.Rd b/geau/man/so_ii_commune.Rd deleted file mode 100644 index 1a1052133369797131767da0795b29f437b70cca..0000000000000000000000000000000000000000 --- a/geau/man/so_ii_commune.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% 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} diff --git a/geau/man/so_ii_hydro.Rd b/geau/man/so_ii_hydro.Rd index 58325f5936d98b4edf2b331f1f9bae4f271d38ef..8af21fddb61aa39d694ccc101e7c9462672a8011 100644 --- a/geau/man/so_ii_hydro.Rd +++ b/geau/man/so_ii_hydro.Rd @@ -8,9 +8,12 @@ sf data.frame 125 rows, 4 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{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{ diff --git a/geau/man/so_ii_limit.Rd b/geau/man/so_ii_limit.Rd index c54cc951581fd6acfe61094363e09a659dab080d..64c439bb842a427152f28d3e0eea3125a51aa92c 100644 --- a/geau/man/so_ii_limit.Rd +++ b/geau/man/so_ii_limit.Rd @@ -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 diff --git a/geau/man/so_ii_montpellier.Rd b/geau/man/so_ii_montpellier.Rd new file mode 100644 index 0000000000000000000000000000000000000000..1d7aa744e603aa5be728366ea3b4a88a501f6dc7 --- /dev/null +++ b/geau/man/so_ii_montpellier.Rd @@ -0,0 +1,25 @@ +% 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} diff --git a/geau/man/so_ii_onrn.Rd b/geau/man/so_ii_onrn.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0991fd9530b650df388a3fb8ceadf37330e9d76c --- /dev/null +++ b/geau/man/so_ii_onrn.Rd @@ -0,0 +1,50 @@ +% 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} diff --git a/geau/man/so_ii_population.Rd b/geau/man/so_ii_population.Rd index e2fd7371abdf02717f458323fab18ebc9d262bdc..7719fce8ccb0614e5ae5da951da6cea92ef16a4d 100644 --- a/geau/man/so_ii_population.Rd +++ b/geau/man/so_ii_population.Rd @@ -5,7 +5,7 @@ \alias{so_ii_population} \title{Population for so-ii} \format{ -numeric matrix +numeric matrix 78 rows, 33 columns \describe{ \item{row}{commune as in so_ii_scope} \item{column}{year} diff --git a/geau/man/so_ii_scope.Rd b/geau/man/so_ii_scope.Rd index ad9328056d38f4c9e9c46d8e2ed0d425bf9d3bb4..270cf442bc19555c283abef0f203eeaa47d32d15 100644 --- a/geau/man/so_ii_scope.Rd +++ b/geau/man/so_ii_scope.Rd @@ -5,7 +5,7 @@ \alias{so_ii_scope} \title{Local collectivities included in so-ii} \format{ -a vector of 69 INSEE code +a vector of 78 INSEE code } \usage{ so_ii_scope diff --git a/map/map_so_ii.R b/map/map_so_ii.R deleted file mode 100644 index 2283457c9f1b9ec607f97f6eb6fa28f3d2bff000..0000000000000000000000000000000000000000 --- a/map/map_so_ii.R +++ /dev/null @@ -1,76 +0,0 @@ -# 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 diff --git a/map/map_so_ii.rmd b/map/map_so_ii.rmd new file mode 100644 index 0000000000000000000000000000000000000000..fdfde9550b9df8eafb65194cd19d7a237e33fe20 --- /dev/null +++ b/map/map_so_ii.rmd @@ -0,0 +1,61 @@ + +```{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 diff --git a/map_so_ii.rmd b/map_so_ii.rmd index b77ea05555b2049509db4395312065b02bf9a597..0502e4a37e17d6b89dcd2fa55657e6daf6dd54a7 100644 --- a/map_so_ii.rmd +++ b/map_so_ii.rmd @@ -1,3 +1,36 @@ +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 # Data to be plotted @@ -24,11 +57,11 @@ dataset_legend = list( 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 = "catnat", year = 2020, legend_theme = 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 = "population", 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, theme_legend = 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", 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-catnat.pdf", bg = bg, pch = pch, theme = "catnat", year = 2020, legend_theme = TRUE) -map_so_ii(dataset, dataset_legend, path = "rex-population.pdf", bg = bg, pch = pch, theme = "population", legend_theme = TRUE) \ No newline at end of file +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, theme_legend = 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 diff --git a/script/admin_express.R b/script/admin_express.R new file mode 100644 index 0000000000000000000000000000000000000000..626120f9805b76278f9741c96e7f960f5e7e8576 --- /dev/null +++ b/script/admin_express.R @@ -0,0 +1,33 @@ +# 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 diff --git a/script/onrn.R b/script/onrn.R new file mode 100644 index 0000000000000000000000000000000000000000..b8c9688c614566108d5b442191749a2a12ad5e40 --- /dev/null +++ b/script/onrn.R @@ -0,0 +1,295 @@ +# 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) + diff --git a/script/topage.R b/script/topage.R new file mode 100644 index 0000000000000000000000000000000000000000..72984ddd0e6f65d3bdc88b55b1aab74d034c8715 --- /dev/null +++ b/script/topage.R @@ -0,0 +1,90 @@ +# Libraries + +library(geau) +library(sf) + +# Waterbody + +## Data + +waterbody = sf::st_read(geau::current_version("data-common/data/topage/plan_eau", "shp")) +waterbody = sf::st_transform(waterbody, crs = st_crs(geau::so_ii_limit)) +waterbody = waterbody[ + lengths(sf::st_intersects(waterbody, geau::so_ii_limit)) > 0, + c("CdOH", "TopoOH") +] +names(waterbody) = c("id", "name", "geometry") +waterbody = sf::st_intersection(waterbody, geau::so_ii_limit) + +## Save + +sf::st_write(waterbody, "data-common/so-ii/topage/2021-09/plan_eau.shp", append = FALSE) + +# Catchment + +## Functions + +prepare_catchment = function (sf_data, name, degre) { + selection = sf_data[[sprintf("detail_%s", degre)]] == name + sf::st_sf( + "id" = NA, + "catchment_name" = name, + "degre" = degre, + "geometry" = sf::st_union(sf_data[selection, ]) + ) +} + +## Data + +input_dir = geau::current_version("data-common/data/topage/bassin_versant") +catchment = sf::st_read(file.path(input_dir, "06_Rhone-Mediterranee_BassinVersantTopographique.shp")) +classification = read.csv2(geau::current_version("data-common/so-ii/topage", "bassin")) + +## Treatments + +### CRS + +if (!sf::st_crs(catchment) == sf::st_crs(geau::so_ii_limit)) { + catchment = sf::st_transform(catchment, crs = sf::st_crs(geau::so_ii_limit)) +} + +### Selection & renaming + +selection_col = c("CdOH", "geometry") +selection_row = classification[["id"]] + +catchment = catchment[catchment[["CdOH"]] %in% unlist(unname(selection_row)), selection_col] +names(catchment) = c("id", "geometry") +catchment = merge(catchment, classification) + +### Making catchments + +detail_1 = do.call(rbind, + lapply( + unique(catchment[[sprintf("detail_%s", 1)]]), + prepare_catchment, + sf_data = catchment, + degre = 1 + ) +) +detail_2 = do.call(rbind, + lapply( + unique(catchment[[sprintf("detail_%s", 2)]]), + prepare_catchment, + sf_data = catchment, + degre = 2 + ) +) +detail_3 = catchment[c("id", "catchment_name")] +detail_3[["degre"]] = "3" +detail_3 = detail_3[names(detail_1)] + +catchment = do.call(rbind, list(detail_1, detail_2, detail_3)) + +### Save + +sf::st_write( + catchment, + "data-common/so-ii/topage/2021-09/bassin_versant.shp", + append = FALSE +)