Commit 07159a03 authored by Grelot Frederic's avatar Grelot Frederic :swimmer_tone5:
Browse files

Merge branch '1-add-gaspar-data' into 'master'

Resolve "add gaspar data"

Closes #1

See merge request !2

geau Version 1.0.1.0
0 errors :heavy_check_mark: | 0 warnings :heavy_check_mark: | 0 notes :heavy_check_mark:

- map_so_ii
    - ajout du thème gaspar
    - ajout de on.exit
    - traçage du contour en fin de dessin
- gaspar
    - dataset du nb de déclaration par commune et par année
Showing with 135 additions and 25 deletions
+135 -25
Package: geau Package: geau
Title: Utilities very useful to share within geau-inondation team Title: Utilities very useful to share within geau-inondation team
Version: 1.0.0.0 Version: 1.0.1.0
Authors@R: Authors@R:
person(given = "Frédéric", person(given = "Frédéric",
family = "Grelot", family = "Grelot",
...@@ -17,8 +17,9 @@ Imports: ...@@ -17,8 +17,9 @@ Imports:
kableExtra, kableExtra,
knitr, knitr,
rio, rio,
terra, scales,
sf sf,
terra
Suggests: Suggests:
rmarkdown, rmarkdown,
testthat testthat
......
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
#' List of all collectivities included in so-ii #' List of all collectivities included in so-ii
#' #'
#' A dataset containing the INSEE code of all local collectivities #' A dataset containing the INSEE code of all local collectivities
#' included in so-ii #' included in so-ii.
#' #'
#' Basically this dataset is obtained as a selection from the layer #' Basically this dataset is obtained as a selection from the layer
#' COMMUNE in ADMIN EXPRESS, more a renaming of variables. #' COMMUNE in ADMIN EXPRESS, more a renaming of variables.
#' #'
...@@ -18,19 +18,21 @@ ...@@ -18,19 +18,21 @@
#' \describe{ #' \describe{
#' \item{id}{id, from IGN ADMIN EXPRESS} #' \item{id}{id, from IGN ADMIN EXPRESS}
#' \item{commune}{character, official name of the commune} #' \item{commune}{character, official name of the commune}
#' \item{commune_majuscule}{character, official capitalized name of the commune} #' \item{commune_majuscule}{character, official capitalized name of the
#' commune}
#' \item{code}{character, INSEE code of the commune} #' \item{code}{character, INSEE code of the commune}
#' \{statut}{character, "statut" of the commune} #' \item{statut}{character, statut of the commune}
#' \{pop_yyy}{integer, official population of year yyyy in the commune} #' \item{pop_yyy}{integer, official population of year yyyy in the commune}
#' \{epci}{characeter, INSEE ID of the EPCI of the commune} #' \item{epci}{character, INSEE ID of the EPCI of the commune}
#' } #' }
#'
#' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/} #' @source \url{https://www.data.gouv.fr/fr/datasets/admin-express/}
"so_ii_commune" "so_ii_commune"
#' Spatial perimeter of so-ii #' Spatial perimeter of so-ii
#' #'
#' A dataset containing the perimeter of so-ii. #' A dataset containing the perimeter of so-ii.
#' #'
#' Basically, this dataset is obtained as #' Basically, this dataset is obtained as
#' \code{sf::st_union(so_ii_commune)} #' \code{sf::st_union(so_ii_commune)}
#' #'
...@@ -46,4 +48,19 @@ ...@@ -46,4 +48,19 @@
#' \item{clc_2018}{character, classification from CLC 2018} #' \item{clc_2018}{character, classification from CLC 2018}
#' \item{color}{character, default color to be used to plot so_ii_clc} #' \item{color}{character, default color to be used to plot so_ii_clc}
#' } #' }
"so_ii_limit" "so_ii_limit"
\ No newline at end of file
#' Flood frequency for the municipalities of so-ii
#'
#' A dataset containing the flood frequency by year and so-ii municipality
#' according to the GASPAR database.
#'
#' @format matrix 78 rows, 41 variables
"so_ii_gaspar"
#' CLC information for so-ii
#'
#' A dataset containing the 2018 version of CLC information for so-ii
#'
#' @format sf object
"so_ii_clc"
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#' @param theme character, choice for the theme (if any) #' @param theme character, choice for the theme (if any)
#' @param bar logical, should a bar be plotted #' @param bar logical, should a bar be plotted
#' @param path character, the name of the file to save the plot #' @param path character, the name of the file to save the plot
#' @param gaspar_year character, the year chosen for gaspar theme
#' @param ... some parameters that will be used by plot (from sf) #' @param ... some parameters that will be used by plot (from sf)
#' #'
#' @return Nothing useful. #' @return Nothing useful.
...@@ -20,7 +21,15 @@ ...@@ -20,7 +21,15 @@
#' # To be added (soon) #' # To be added (soon)
#' } #' }
map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, path = NULL, ...) { map_so_ii = function(
dataset,
dataset_legend = NULL,
theme = c("clc", "gaspar"),
bar = TRUE,
path = NULL,
gaspar_year,
...
) {
if (!is.null(path)) { if (!is.null(path)) {
switch( switch(
...@@ -29,12 +38,12 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, ...@@ -29,12 +38,12 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE,
"png" = grDevices::png(path), "png" = grDevices::png(path),
stop(sprintf("%s not recognized", tolower(tools::file_ext(path)))) stop(sprintf("%s not recognized", tolower(tools::file_ext(path))))
) )
on.exit(grDevices::dev.off())
} }
## Init map ## Init map
graphics::par(mai = c(.65, .60, .50, .15)) graphics::par(mai = c(.65, .60, .50, .15))
plot(geau::so_ii_limit, axes = TRUE) plot(geau::so_ii_limit, axes = TRUE)
plot(geau::so_ii_limit, lwd = 2, add = TRUE)
if ("clc" %in% theme) { if ("clc" %in% theme) {
plot( plot(
...@@ -45,10 +54,38 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, ...@@ -45,10 +54,38 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE,
) )
} }
if ("gaspar" %in% theme) {
border = NA
color = NA
if (!missing(gaspar_year)) {
border = "grey80"
color = ifelse(
geau::so_ii_gaspar[ , as.character(gaspar_year)] > 0,
scales::alpha("grey80", .5),
NA
)
}
plot(
geau::so_ii_commune[["geometry"]],
border = border,
col = color,
add = TRUE
)
}
plot(dataset[["geometry"]], add = TRUE, ...) plot(dataset[["geometry"]], add = TRUE, ...)
plot(geau::so_ii_limit, lwd = 2, add = TRUE)
if (bar == TRUE) { if (bar == TRUE) {
terra::sbar(10, c(3.55, 43.47), type = "bar", below = "km", label = c(0, 5, 10), cex = .8) terra::sbar(
10, c(3.55, 43.47),
type = "bar",
below = "km",
label = c(0, 5, 10),
cex = .8
)
} }
if (!is.null(dataset_legend)) { if (!is.null(dataset_legend)) {
...@@ -61,5 +98,5 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE, ...@@ -61,5 +98,5 @@ map_so_ii = function(dataset, dataset_legend = NULL, theme = "clc", bar = TRUE,
do.call(graphics::legend, dataset_legend) do.call(graphics::legend, dataset_legend)
} }
if (!is.null(path)) invisible(grDevices::dev.off()) return(invisible(NULL))
} }
\ No newline at end of file
# code to prepare `so_ii_scope` dataset goes here # code to prepare `so_ii_scope` dataset goes here
so_ii_scope = read.csv2(current_version("data-common/so-ii/scope"), colClasses = "character")[["code"]] so_ii_scope = read.csv2(
current_version("data-common/so-ii/scope"),
colClasses = "character"
)[["code"]]
so_ii_scope = sort(so_ii_scope)
# code to prepare `so_ii_commune` dataset goes here # code to prepare `so_ii_commune` dataset goes here
...@@ -8,7 +12,8 @@ admin_express = current_version("data-common/data/IGN/ADMIN-EXPRESS/version") ...@@ -8,7 +12,8 @@ admin_express = current_version("data-common/data/IGN/ADMIN-EXPRESS/version")
selection = c("ID", "NOM", "NOM_M", "INSEE_COM", "STATUT", "POPULATION", "SIREN_EPCI") 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] 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") names(so_ii_commune) = c("id", "commune", "commune_majuscule", "code", "statut", "pop_2021", "epci", "geometry")
so_ii_commune = so_ii_commune[so_ii_commune[["code"]] %in% so_ii_scope, ] 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 # code to prepare `so_ii_limit` dataset goes here
...@@ -28,6 +33,17 @@ so_ii_clc[["color"]] = as.character( ...@@ -28,6 +33,17 @@ so_ii_clc[["color"]] = as.character(
) )
) )
# code to prepare `so_ii_gaspar` dataset goes here
so_ii_gaspar = read.csv2(
current_version("data-common/so-ii/gaspar", "catnat_year_n"),
header = TRUE,
row.names = 1
)
so_ii_gaspar = as.matrix(so_ii_gaspar)
colnames(so_ii_gaspar) = gsub("^X", "", colnames(so_ii_gaspar))
so_ii_gaspar = so_ii_gaspar[so_ii_scope, ]
# updating datasets # updating datasets
# actual = setwd(file.path(system.file(package = "geau"), "..")) # actual = setwd(file.path(system.file(package = "geau"), ".."))
...@@ -36,4 +52,5 @@ usethis::use_data(so_ii_scope, internal = FALSE, overwrite = TRUE) ...@@ -36,4 +52,5 @@ 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_commune, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_limit, 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(so_ii_clc, internal = FALSE, overwrite = TRUE)
usethis::use_data(so_ii_gaspar, internal = FALSE, overwrite = TRUE)
setwd(actual) setwd(actual)
No preview for this file type
File added
No preview for this file type
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
map_so_ii( map_so_ii(
dataset, dataset,
dataset_legend = NULL, dataset_legend = NULL,
theme = "clc", theme = c("clc", "gaspar"),
bar = TRUE, bar = TRUE,
path = NULL, path = NULL,
gaspar_year,
... ...
) )
} }
...@@ -25,6 +26,8 @@ map_so_ii( ...@@ -25,6 +26,8 @@ map_so_ii(
\item{path}{character, the name of the file to save the plot} \item{path}{character, the name of the file to save the plot}
\item{gaspar_year}{character, the year chosen for gaspar theme}
\item{...}{some parameters that will be used by plot (from sf)} \item{...}{some parameters that will be used by plot (from sf)}
} }
\value{ \value{
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_clc}
\alias{so_ii_clc}
\title{CLC information for so-ii}
\format{
sf object
}
\usage{
so_ii_clc
}
\description{
A dataset containing the 2018 version of CLC information for so-ii
}
\keyword{datasets}
...@@ -9,11 +9,12 @@ sf data.frame 69 rows, 7 variables ...@@ -9,11 +9,12 @@ sf data.frame 69 rows, 7 variables
\describe{ \describe{
\item{id}{id, from IGN ADMIN EXPRESS} \item{id}{id, from IGN ADMIN EXPRESS}
\item{commune}{character, official name of the commune} \item{commune}{character, official name of the commune}
\item{commune_majuscule}{character, official capitalized name of the commune} \item{commune_majuscule}{character, official capitalized name of the
commune}
\item{code}{character, INSEE code of the commune} \item{code}{character, INSEE code of the commune}
\{statut}{character, "statut" of the commune} \item{statut}{character, statut of the commune}
\{pop_yyy}{integer, official population of year yyyy in the commune} \item{pop_yyy}{integer, official population of year yyyy in the commune}
\{epci}{characeter, INSEE ID of the EPCI of the commune} \item{epci}{character, INSEE ID of the EPCI of the commune}
} }
} }
\source{ \source{
...@@ -24,7 +25,7 @@ so_ii_commune ...@@ -24,7 +25,7 @@ so_ii_commune
} }
\description{ \description{
A dataset containing the INSEE code of all local collectivities A dataset containing the INSEE code of all local collectivities
included in so-ii included in so-ii.
} }
\details{ \details{
Basically this dataset is obtained as a selection from the layer Basically this dataset is obtained as a selection from the layer
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.r
\docType{data}
\name{so_ii_gaspar}
\alias{so_ii_gaspar}
\title{Flood frequency for the municipalities of so-ii}
\format{
matrix 78 rows, 41 variables
}
\usage{
so_ii_gaspar
}
\description{
A dataset containing the flood frequency by year and so-ii municipality
according to the GASPAR database.
}
\keyword{datasets}
...@@ -24,4 +24,5 @@ dataset_legend = list( ...@@ -24,4 +24,5 @@ dataset_legend = list(
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.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.png", bg = bg, cex = cex, col = col, pch = pch)
map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22) map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22)
\ No newline at end of file map_so_ii(dataset, dataset_legend, path = "toto.pdf", bg = bg, pch = 22, theme = "gaspar")
\ No newline at end of file
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