From 1cc6557b42c5121104b33dfdc723efdf89b67b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Grelot?= <frederic.grelot@irstea.fr> Date: Wed, 26 Jul 2023 14:44:57 +0200 Subject: [PATCH] =?UTF-8?q?Pr=C3=A9paration=20de=20la=20version=201.0.29.0?= =?UTF-8?q?=20de=20so.ii?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dev/tag-message | 11 +++---- so.ii/DESCRIPTION | 2 +- so.ii/NAMESPACE | 1 + so.ii/R/adjust_terra_scale.R | 58 ++++++++++++++++++++++++++++++++++++ so.ii/R/map_so_ii.R | 33 +++++--------------- so.ii/man/add_bar.Rd | 40 +++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 so.ii/man/add_bar.Rd diff --git a/dev/tag-message b/dev/tag-message index 54af56c..a05446c 100644 --- a/dev/tag-message +++ b/dev/tag-message @@ -1,16 +1,13 @@ -so.ii Version: 1.0.27.0 +so.ii Version: 1.0.28.0 0 errors ✔ | 0 warnings ✔ | 0 notes ✔ **Note de version** - * ajout de aggregate_wide - * ajout de add_margin + * ajout de add_bar **Détails** - * aggregate_wide permet de renvoyer directement une matrice quand un - aggregate est réalisé sur 2 dimensions. - * add_margin permet d'utiliser addmargins directement sur un data.frame + * add_bar permet d'ajouter une barre d'échelle à des cartes. -# git tag -a v1.0.27.0 -F dev/tag-message +# git tag -a v1.0.28.0 -F dev/tag-message # git push --tags diff --git a/so.ii/DESCRIPTION b/so.ii/DESCRIPTION index cf42619..1dc34ba 100644 --- a/so.ii/DESCRIPTION +++ b/so.ii/DESCRIPTION @@ -1,6 +1,6 @@ Package: so.ii Title: Utilities very useful to share within so_ii team -Version: 1.0.27.0 +Version: 1.0.28.0 Authors@R: c( person(given = "Frédéric", diff --git a/so.ii/NAMESPACE b/so.ii/NAMESPACE index 437c8b8..979ca07 100644 --- a/so.ii/NAMESPACE +++ b/so.ii/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(add_bar) export(add_inset) export(add_margin) export(aggregate_wide) diff --git a/so.ii/R/adjust_terra_scale.R b/so.ii/R/adjust_terra_scale.R index 7bcbe79..2fdcfc8 100644 --- a/so.ii/R/adjust_terra_scale.R +++ b/so.ii/R/adjust_terra_scale.R @@ -1,3 +1,61 @@ +#' @title Add a bar to a map +#' +#' @details +#' Correct some things in sbar from terra to add scale bar to plots. +#' +#' @param d integer, size of the bar in km. +#' @param xy either a character or a vector giving the position of the bar in +#' the plot. +#' @param adj numeric of length 2, adjustment for text placement. +#' @param lon numeric of length 1, may be adjusted for some plots with +#' trial-and-error strategy. +#' +#' @return NULL. +#' +#' @export +#' +#' @encoding UTF-8 +#' @author Frédéric Grelot +#' +#' @examples +#' +#' \dontrun{ +#' library(sf) +#' plot(so_ii_collectivity[0]) +#' add_bar(d = 10, xy = c(3.55, 43.47), adj = c(0.5, -1)) +#' } + + +add_bar = function( + d = NULL, + xy = NULL, + adj = NULL, + lon = 0.4788987 +) { + d = adjust_terra_scale(d) + xy = calculate_terra_xy(xy) + if (is.null(adj)) { + # Magical adjustment when scope vary to have text readable + # 0.4788987 should correspond to longitude range for so.ii scope + adj = diff(graphics::par()$usr[3:4]) + adj = c( + 0.5, + -1 * exp(lon / adj - 1)^.024 + ) + } + + terra::sbar( + d = d[1], + xy = xy, + type = "bar", + below = "km", + label = c(0, d[2]/2, d[2]), + lonlat = FALSE, + adj = adj, + cex = .8 + ) +} + adjust_terra_scale = function(d = NULL, lonlat = TRUE) { destpoint = function (p, d, b = 90, r = 6378137) { toRad = pi/180 diff --git a/so.ii/R/map_so_ii.R b/so.ii/R/map_so_ii.R index 54ff95b..4e5783b 100644 --- a/so.ii/R/map_so_ii.R +++ b/so.ii/R/map_so_ii.R @@ -289,33 +289,16 @@ map_so_ii = function( ## Plot bar if (bar == TRUE) { if (identical(scope, so.ii::so_ii_limit)) { - # d = 10 - d = adjust_terra_scale(10) - xy = c(3.55, 43.47) - adj = c(0.5, -1) - } else { - # area = sf::st_area(sf::st_as_sfc(sf::st_bbox(scope))) - # d = max(pretty(units::set_units(sqrt(area) / 5, "km"))) - d = adjust_terra_scale() - xy = calculate_terra_xy("bottomleft") - # Magical adjustment when scope vary to have text readable - # 0.4788987 should correspond to longitude range for so.ii scope - adj = diff(graphics::par()$usr[3:4]) - adj = c( - 0.5, - -1 * exp(0.4788987 / adj - 1)^.024 + # Parameters fitted for so_ii scope + add_bar( + d = 10, + xy = c(3.55, 43.47), + adj = c(0.5, -1), + lon = 0.4788987 ) + } else { + add_bar() } - terra::sbar( - d = d[1], - xy = xy, - type = "bar", - below = "km", - label = c(0, d[2]/2, d[2]), - lonlat = FALSE, - adj = adj, - cex = .8 - ) } ## Plot inset diff --git a/so.ii/man/add_bar.Rd b/so.ii/man/add_bar.Rd new file mode 100644 index 0000000..d8abd09 --- /dev/null +++ b/so.ii/man/add_bar.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/adjust_terra_scale.R +\encoding{UTF-8} +\name{add_bar} +\alias{add_bar} +\title{Add a bar to a map} +\usage{ +add_bar(d = NULL, xy = NULL, adj = NULL, lon = 0.4788987) +} +\arguments{ +\item{d}{integer, size of the bar in km.} + +\item{xy}{either a character or a vector giving the position of the bar in +the plot.} + +\item{adj}{numeric of length 2, adjustment for text placement.} + +\item{lon}{numeric of length 1, may be adjusted for some plots with +trial-and-error strategy.} +} +\value{ +NULL. +} +\description{ +Add a bar to a map +} +\details{ +Correct some things in sbar from terra to add scale bar to plots. +} +\examples{ + +\dontrun{ +library(sf) +plot(so_ii_collectivity[0]) +add_bar(d = 10, xy = c(3.55, 43.47), adj = c(0.5, -1)) +} +} +\author{ +Frédéric Grelot +} -- GitLab