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

Ajout de l'option scope à map_so_ii et adaptation des options bar et insert en conséquences

Showing with 120 additions and 46 deletions
+120 -46
so.ii Version: 1.0.21.0 so.ii Version: 1.0.22.0
0 errors ✔ | 0 warnings ✔ | 0 notes ✔ 0 errors ✔ | 0 warnings ✔ | 0 notes ✔
**Note de version** **Note de version**
* ajout des options add, limit à map_so_ii * ajout de l'option scope à map_so_ii
* adaptation de l'option inset
* adaptation de l'option bar
**Détails** **Détails**
* l'option limit (logical) de map_so_ii permet de gérer le fait que la * l'option "scope" (character) de map_so_ii permet de définir le scope de la
limite du paramètre est effectivement affichée : carte :
* si l'option est à TRUE (défaut), le périmètre est affiché une fois que * si l'option est à "so-ii" (défaut), le périmètre total est affiché.
le thème ou le dataset le sont (mais plus avant). Donc si aucun theme ni C'est le même comportement que précédemment.
dataset n'est donné, rien n'est affiché ! * si l'option est un vecteur différent de "so-ii", alors il est cherché
* si l'option est à FALSE, le périmètre n'est jamais affiché. l'ensemble des communes de so-ii dont le code INSEE correspond aux
* l'option add (logical) permet d'utiliser map_so_ii pour ajouter des valeurs de scope. Seules les bonnes valeurs sont gardées. Si aucune
informations à un carte existante : bonne valeur n'est trouvée, un avertissement est lancé et la valeur par
* si l'option est à TRUE, les informations sont ajoutées. L'option "bar" d'afut est prise
est mise à FALSE. Si l'option "path" donne un chemin de fichier, une * l'option "inset" permet de choisir la valeur "so-ii" pour situer un scope
sauvegarde de la carte en cours de création est crée en utilisant plus petit que le périmètre total à l'intérieur de so-ii.
dev.copy juste avant de sortir de la fonction. * l'option "bar" n'est pas mofifiée dans son appel, mais dans son
* si l'option est à FALSE, le comportement ne change pas avec ce qui implémentation pour permettre d'ajuster la taille de la barre au scope
était précédemment donné. choisi.
# git tag -a v1.0.21.0 -F dev/tag-message # git tag -a v1.0.22.0 -F dev/tag-message
# git push --tags # git push --tags
Package: so.ii Package: so.ii
Title: Utilities very useful to share within so_ii team Title: Utilities very useful to share within so_ii team
Version: 1.0.21.0 Version: 1.0.22.0
Authors@R: Authors@R:
c( c(
person(given = "Frédéric", person(given = "Frédéric",
...@@ -27,7 +27,8 @@ Imports: ...@@ -27,7 +27,8 @@ Imports:
rio, rio,
scales, scales,
sf, sf,
terra terra,
units
Suggests: Suggests:
rmarkdown, rmarkdown,
testthat testthat
......
#' @title Plot a thematic map of so-ii #' @title Plot a thematic map of so-ii
#' #'
#' @details #' @details
#' \subsection{theme specification}{ #' \subsection{theme specification}{
#' For the specification of detail, it depends on the theme chosen. #' For the specification of detail, it depends on the theme chosen.
...@@ -68,18 +68,31 @@ ...@@ -68,18 +68,31 @@
#' as.character(inset). Non-case sensitive partial matching is used, with "é" #' as.character(inset). Non-case sensitive partial matching is used, with "é"
#' interpreted as "e". #' interpreted as "e".
#' \itemize{ #' \itemize{
#' \item{\strong{department}: so-ii perimeter is located within Hérault #' \item{\strong{so-ii}: scope perimeter is located within so-ii. Only
#' departement, if inset may be interpreted as "department", "département", #' useful when scope is less than so-ii.}
#' "hérault", "34".} #' \item{\strong{department}: scope perimeter is located within Hérault
#' \item{\strong{region}: so-ii perimeter is located within Occitanie region, #' departement, if inset may be interpreted as "department", "département",
#' if inset may be interpreted as "région", "Occitanie", "76" (INSEE code for #' "hérault", "34".}
#' Occitanie region).} #' \item{\strong{region}: scope perimeter is located within Occitanie region,
#' \item{\strong{nation}: so-ii perimeter is located within the metropolitean #' if inset may be interpreted as "région", "Occitanie", "76" (INSEE code
#' part of France, if inset may be interpreted as "France", "métropole", #' for Occitanie region).}
#' "nation".} #' \item{\strong{nation}: scope perimeter is located within the metropolitean
#' part of France, if inset may be interpreted as "France", "métropole",
#' "nation".}
#' } #' }
#' If all other cases, nothing is added. #' If all other cases, nothing is added.
#' } #' }
#' \subsection{scope specification}{
#' The scope should be either:
#' \itemize{
#' \item{\strong{so.ii}: the whole perimiter of so.ii is chosen. This is
#' default value.}
#' \item{\strong{vector of INSEE codes of collectivities in so.ii}: a
#' perimeter is build with those collectivies. Only valid codes are taken
#' into account. If none is chosen, a warning is dropped, and default
#' value is used.}
#' }
#' }
#' \subsection{path specification}{ #' \subsection{path specification}{
#' Depending on the extension a device is chosen. #' Depending on the extension a device is chosen.
#' \itemize{ #' \itemize{
...@@ -98,6 +111,7 @@ ...@@ -98,6 +111,7 @@
#' @param theme_legend logical, should a legend be plotted for the theme. #' @param theme_legend logical, should a legend be plotted for the theme.
#' @param detail character, detail for theme, depends on theme. See details. #' @param detail character, detail for theme, depends on theme. See details.
#' @param year character, the year chosen for some themes. See details. #' @param year character, the year chosen for some themes. See details.
#' @param scope character, choice for the scope of the map. See details.
#' @param bar logical, should a bar be plotted for the dataset. See details. #' @param bar logical, should a bar be plotted for the dataset. See details.
#' @param limit logical, should the limit of so.ii be plotted for the dataset. #' @param limit logical, should the limit of so.ii be plotted for the dataset.
#' Default to TRUE. #' Default to TRUE.
...@@ -129,6 +143,7 @@ map_so_ii = function( ...@@ -129,6 +143,7 @@ map_so_ii = function(
theme_legend = FALSE, theme_legend = FALSE,
detail, detail,
year, year,
scope = "so.ii",
bar = TRUE, bar = TRUE,
limit = TRUE, limit = TRUE,
inset = NULL, inset = NULL,
...@@ -210,10 +225,27 @@ map_so_ii = function( ...@@ -210,10 +225,27 @@ map_so_ii = function(
} }
} }
## Check scope
if (identical(scope, "so.ii")) {
scope = so.ii::so_ii_limit
} else {
scope = scope[scope %in% so.ii::so_ii_collectivity[["commune"]]]
if (length(scope) == 0) {
warnings("Nothing pertinent found in scope, default value is used.")
scope = so.ii::so_ii_limit
} else {
scope = sf::st_union(
so.ii::so_ii_collectivity[
so.ii::so_ii_collectivity[["commune"]] %in% scope,
]
)
}
}
## Init map ## Init map
if (add != TRUE) { if (add != TRUE) {
graphics::par(mai = c(.65, .60, .50, .15)) graphics::par(mai = c(.65, .60, .50, .15))
plot(so.ii::so_ii_limit, border = NA, axes = TRUE, main = list(...)[["main"]], cex.main = 2.5) plot(scope, border = NA, axes = TRUE, main = list(...)[["main"]], cex.main = 2.5)
} else { } else {
bar = FALSE bar = FALSE
} }
...@@ -235,18 +267,31 @@ map_so_ii = function( ...@@ -235,18 +267,31 @@ map_so_ii = function(
## Plot dataset if any ## Plot dataset if any
if (!missing(dataset)) plot(dataset[["geometry"]], add = TRUE, ...) if (!missing(dataset)) plot(dataset[["geometry"]], add = TRUE, ...)
## Make so_ii_limit visible ## Make scope visible
if (limit == TRUE) { if (limit == TRUE) {
plot(so.ii::so_ii_limit, lwd = 2, add = TRUE) plot(scope, lwd = 2, add = TRUE)
} }
## Plot bar ## Plot bar
if (bar == TRUE) { if (bar == TRUE) {
if (identical(scope, so.ii::so_ii_limit)) {
d = 10
xy = c(3.55, 43.47)
below = "km"
label = c(0, 5, 10)
} else {
area = sf::st_area(sf::st_as_sfc(sf::st_bbox(scope)))
d = max(pretty(units::set_units(sqrt(area) / 5, "km")))
xy = "bottomleft"
below = "km"
label = c(0, d/2, d)
}
terra::sbar( terra::sbar(
10, c(3.55, 43.47), d = d,
xy = xy,
type = "bar", type = "bar",
below = "km", below = below,
label = c(0, 5, 10), label = label,
cex = .8 cex = .8
) )
} }
...@@ -255,9 +300,10 @@ map_so_ii = function( ...@@ -255,9 +300,10 @@ map_so_ii = function(
if (!is.null(inset)) { if (!is.null(inset)) {
inset = gsub("\u00e9", "e", tolower(as.character(inset)[1])) inset = gsub("\u00e9", "e", tolower(as.character(inset)[1]))
admissible = list( admissible = list(
department = c("34", "departement", "department", "herault"), "so-ii" = c("so-ii"),
region = c("76", "occitanie", "region"), "department" = c("34", "departement", "department", "herault"),
nation = c("france", "metropole", "nation") "region" = c("76", "occitanie", "region"),
"nation" = c("france", "metropole", "nation")
) )
inset = names(which(sapply( inset = names(which(sapply(
admissible, admissible,
...@@ -265,10 +311,20 @@ map_so_ii = function( ...@@ -265,10 +311,20 @@ map_so_ii = function(
inset inset
))) )))
if (length(inset) == 1 && inset %in% rownames(so.ii::so_ii_inset)) { if (length(inset) == 1 && inset %in% rownames(so.ii::so_ii_inset)) {
scope_sf = sf::st_as_sf(scope)
names(scope_sf) = "geometry"
sf::st_geometry(scope_sf) = "geometry"
inset = rbind(
so.ii::so_ii_inset[inset, "geometry"],
sf::st_as_sf(scope_sf)
)
add_inset( add_inset(
so.ii::so_ii_inset[c(inset, "so-ii"), ], inset,
so.ii::so_ii_limit, scope,
col = c("gray85", "red"), border = c("gray", "red"), lwd = 1:2 col = c("gray85", "red"),
border = c("gray", "red"),
lwd = 1:2
) )
} }
} }
......
...@@ -13,6 +13,7 @@ map_so_ii( ...@@ -13,6 +13,7 @@ map_so_ii(
theme_legend = FALSE, theme_legend = FALSE,
detail, detail,
year, year,
scope = "so.ii",
bar = TRUE, bar = TRUE,
limit = TRUE, limit = TRUE,
inset = NULL, inset = NULL,
...@@ -34,6 +35,8 @@ map_so_ii( ...@@ -34,6 +35,8 @@ map_so_ii(
\item{year}{character, the year chosen for some themes. See details.} \item{year}{character, the year chosen for some themes. See details.}
\item{scope}{character, choice for the scope of the map. See details.}
\item{bar}{logical, should a bar be plotted for the dataset. See details.} \item{bar}{logical, should a bar be plotted for the dataset. See details.}
\item{limit}{logical, should the limit of so.ii be plotted for the dataset. \item{limit}{logical, should the limit of so.ii be plotted for the dataset.
...@@ -123,18 +126,30 @@ If inset is not NULL, an inset will be plotted, depending on the value of ...@@ -123,18 +126,30 @@ If inset is not NULL, an inset will be plotted, depending on the value of
as.character(inset). Non-case sensitive partial matching is used, with "é" as.character(inset). Non-case sensitive partial matching is used, with "é"
interpreted as "e". interpreted as "e".
\itemize{ \itemize{
\item{\strong{department}: so-ii perimeter is located within Hérault \item{\strong{so-ii}: scope perimeter is located within so-ii.}
\item{\strong{department}: scope perimeter is located within Hérault
departement, if inset may be interpreted as "department", "département", departement, if inset may be interpreted as "department", "département",
"hérault", "34".} "hérault", "34".}
\item{\strong{region}: so-ii perimeter is located within Occitanie region, \item{\strong{region}: scope perimeter is located within Occitanie region,
if inset may be interpreted as "région", "Occitanie", "76" (INSEE code for if inset may be interpreted as "région", "Occitanie", "76" (INSEE code
Occitanie region).} for Occitanie region).}
\item{\strong{nation}: so-ii perimeter is located within the metropolitean \item{\strong{nation}: scope perimeter is located within the metropolitean
part of France, if inset may be interpreted as "France", "métropole", part of France, if inset may be interpreted as "France", "métropole",
"nation".} "nation".}
} }
If all other cases, nothing is added. If all other cases, nothing is added.
} }
\subsection{scope specification}{
The scope should be either:
\itemize{
\item{\strong{so.ii}: the whole perimiter of so.ii is chosen. This is
default value.}
\item{\strong{vector of INSEE codes of collectivities in so.ii}: a
perimeter is build with those collectivies. Only valid codes are taken
into account. If none is chosen, a warning is dropped, and default
value is used.}
}
}
\subsection{path specification}{ \subsection{path specification}{
Depending on the extension a device is chosen. Depending on the extension a device is chosen.
\itemize{ \itemize{
......
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