From a106b74f594317486716ef764145dcd95662060a Mon Sep 17 00:00:00 2001 From: mtennekes Date: Tue, 16 Jul 2024 22:53:34 +0200 Subject: [PATCH] implemented tm_minimap --- NAMESPACE | 8 +++++++ R/tm_components.R | 24 +++++++++++++++++++++ R/tmapGridComp.R | 23 ++++++++++++++++++++ R/tmapLeafletComp.R | 49 ++++++++++++++++++++++++++++++++++++++++++ R/tmap_options.R | 6 ++++++ R/v4-not-implemented.R | 5 ----- man/tm_minimap.Rd | 27 +++++++++++++++++++++++ man/v4-not-yet.Rd | 3 --- 8 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 man/tm_minimap.Rd diff --git a/NAMESPACE b/NAMESPACE index ab651ee0..86499d0f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ S3method(tmapGridCompHeight,tm_compass) S3method(tmapGridCompHeight,tm_credits) S3method(tmapGridCompHeight,tm_legend_standard_landscape) S3method(tmapGridCompHeight,tm_legend_standard_portrait) +S3method(tmapGridCompHeight,tm_minimap) S3method(tmapGridCompHeight,tm_mouse_coordinates) S3method(tmapGridCompHeight,tm_scalebar) S3method(tmapGridCompHeight,tm_title) @@ -33,6 +34,7 @@ S3method(tmapGridCompPrepare,tm_compass) S3method(tmapGridCompPrepare,tm_credits) S3method(tmapGridCompPrepare,tm_legend_standard_landscape) S3method(tmapGridCompPrepare,tm_legend_standard_portrait) +S3method(tmapGridCompPrepare,tm_minimap) S3method(tmapGridCompPrepare,tm_mouse_coordinates) S3method(tmapGridCompPrepare,tm_scalebar) S3method(tmapGridCompPrepare,tm_title) @@ -41,6 +43,7 @@ S3method(tmapGridCompWidth,tm_compass) S3method(tmapGridCompWidth,tm_credits) S3method(tmapGridCompWidth,tm_legend_standard_landscape) S3method(tmapGridCompWidth,tm_legend_standard_portrait) +S3method(tmapGridCompWidth,tm_minimap) S3method(tmapGridCompWidth,tm_mouse_coordinates) S3method(tmapGridCompWidth,tm_scalebar) S3method(tmapGridCompWidth,tm_title) @@ -54,6 +57,7 @@ S3method(tmapGridLegPlot,tm_compass) S3method(tmapGridLegPlot,tm_credits) S3method(tmapGridLegPlot,tm_legend_standard_landscape) S3method(tmapGridLegPlot,tm_legend_standard_portrait) +S3method(tmapGridLegPlot,tm_minimap) S3method(tmapGridLegPlot,tm_mouse_coordinates) S3method(tmapGridLegPlot,tm_scalebar) S3method(tmapGridLegPlot,tm_title) @@ -62,6 +66,7 @@ S3method(tmapLeafletCompHeight,tm_compass) S3method(tmapLeafletCompHeight,tm_credits) S3method(tmapLeafletCompHeight,tm_legend_standard_landscape) S3method(tmapLeafletCompHeight,tm_legend_standard_portrait) +S3method(tmapLeafletCompHeight,tm_minimap) S3method(tmapLeafletCompHeight,tm_mouse_coordinates) S3method(tmapLeafletCompHeight,tm_scalebar) S3method(tmapLeafletCompHeight,tm_title) @@ -71,6 +76,7 @@ S3method(tmapLeafletCompPrepare,tm_compass) S3method(tmapLeafletCompPrepare,tm_credits) S3method(tmapLeafletCompPrepare,tm_legend_standard_landscape) S3method(tmapLeafletCompPrepare,tm_legend_standard_portrait) +S3method(tmapLeafletCompPrepare,tm_minimap) S3method(tmapLeafletCompPrepare,tm_mouse_coordinates) S3method(tmapLeafletCompPrepare,tm_scalebar) S3method(tmapLeafletCompPrepare,tm_title) @@ -79,6 +85,7 @@ S3method(tmapLeafletCompWidth,tm_compass) S3method(tmapLeafletCompWidth,tm_credits) S3method(tmapLeafletCompWidth,tm_legend_standard_landscape) S3method(tmapLeafletCompWidth,tm_legend_standard_portrait) +S3method(tmapLeafletCompWidth,tm_minimap) S3method(tmapLeafletCompWidth,tm_mouse_coordinates) S3method(tmapLeafletCompWidth,tm_scalebar) S3method(tmapLeafletCompWidth,tm_title) @@ -88,6 +95,7 @@ S3method(tmapLeafletLegPlot,tm_compass) S3method(tmapLeafletLegPlot,tm_credits) S3method(tmapLeafletLegPlot,tm_legend_standard_landscape) S3method(tmapLeafletLegPlot,tm_legend_standard_portrait) +S3method(tmapLeafletLegPlot,tm_minimap) S3method(tmapLeafletLegPlot,tm_mouse_coordinates) S3method(tmapLeafletLegPlot,tm_scalebar) S3method(tmapLeafletLegPlot,tm_title) diff --git a/R/tm_components.R b/R/tm_components.R index 37c225ec..8bed1fee 100644 --- a/R/tm_components.R +++ b/R/tm_components.R @@ -217,4 +217,28 @@ tm_mouse_coordinates <- function(stack, tm_element_list(do.call(tm_element, c(args, list(subclass = c("tm_mouse_coordinates", "tm_component"))))) } +#' Map component: minimap +#' +#' Map component that adds a minimap in view mode +#' +#' @param server name of the provider or an URL (see \code{\link{tm_tiles}}). By default, it shows the same map as the basemap, and moreover, it will automatically change when the user switches basemaps. Note the latter does not happen when \code{server} is specified. +#' @param toggle should the minimap have a button to minimise it? By default \code{TRUE}. +#' @param position position of the scale bar Vector of two values, specifying the x and y coordinates. The first is either "left" or "right", the second either "top" or "bottom". +#' @param stack stack +#' @param position position +#' @param z z +#' @param ... arguments passed on to \code{\link[leaflet:addMiniMap]{addMiniMap}}. +#' @seealso \code{\link[leaflet:addMiniMap]{addMiniMap}} +#' @export +tm_minimap <- function(server, + toggle, + stack, + position, + z, + ...) { + args = lapply(as.list(match.call()[-1]), eval, envir = parent.frame()) + if (!("z" %in% names(args))) args$z = as.integer(NA) + tm_element_list(do.call(tm_element, c(args, list(subclass = c("tm_minimap", "tm_component"))))) +} + diff --git a/R/tmapGridComp.R b/R/tmapGridComp.R index 79eaf3f1..c5786d7b 100644 --- a/R/tmapGridComp.R +++ b/R/tmapGridComp.R @@ -705,3 +705,26 @@ tmapGridLegPlot.tm_mouse_coordinates = function(comp, o, fH, fW) { NULL } + +#' @export +tmapGridCompPrepare.tm_minimap = function(comp, o) { + message("tm_minimap ignored for 'plot' mode") + comp$show = FALSE + comp +} + + +#' @export +tmapGridCompHeight.tm_minimap = function(comp, o) { + comp +} + +#' @export +tmapGridCompWidth.tm_minimap = function(comp, o) { + comp +} + +#' @export +tmapGridLegPlot.tm_minimap = function(comp, o, fH, fW) { + NULL +} diff --git a/R/tmapLeafletComp.R b/R/tmapLeafletComp.R index 8637e847..665da4b1 100644 --- a/R/tmapLeafletComp.R +++ b/R/tmapLeafletComp.R @@ -157,3 +157,52 @@ tmapLeafletLegPlot.tm_mouse_coordinates = function(comp, lf, o) { } + +#' @export +tmapLeafletCompPrepare.tm_minimap = function(comp, o) { + comp$show = TRUE + + extra = comp[setdiff(intersect(names(comp), names(formals(leaflet::addMiniMap))), c("position", "map"))] + + comp$specified_tiles = !is.na(comp$server) + + comp$args = c(list(toggleDisplay = comp$toggle), extra) + comp +} + + +#' @export +tmapLeafletCompHeight.tm_minimap = function(comp, o) { + comp +} + +#' @export +tmapLeafletCompWidth.tm_minimap = function(comp, o) { + comp +} + + +#' @export +tmapLeafletLegPlot.tm_minimap = function(comp, lf, o) { + comp$args$tiles = if (comp$specified_tiles) { + comp$server + } else if (length(.TMAP_LEAFLET$tiles)) { + .TMAP_LEAFLET$tiles[[1]][[1]]$server[1] + } else { + o$basemap.server[1] + } + lf2 = do.call(addMiniMap, c(list(map = lf), comp$args)) + if (!comp$specified_tiles && (length(comp$args$tiles) > 0)) { + lf2 <- lf2 %>% + htmlwidgets::onRender(" + function(el, x) { + var myMap = this; + myMap.on('baselayerchange', + function (e) { + myMap.minimap.changeLayer(L.tileLayer.provider(e.name)); + }) + }") + } + lf2 +} + diff --git a/R/tmap_options.R b/R/tmap_options.R index 33bb5ef1..698faa6c 100644 --- a/R/tmap_options.R +++ b/R/tmap_options.R @@ -550,6 +550,12 @@ tmapMode = function(id, name, ...) { mouse_coordinates.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"), mouse_coordinates.show = FALSE, + minimap.server = NA, + minimap.toggle = TRUE, + minimap.stack = "vertical", + minimap.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"), + minimap.show = FALSE, + panel.show = TRUE, panel.labels = NA, panel.label.size = 1, diff --git a/R/v4-not-implemented.R b/R/v4-not-implemented.R index 4abe5858..be7a07a0 100644 --- a/R/v4-not-implemented.R +++ b/R/v4-not-implemented.R @@ -16,11 +16,6 @@ tm_logo <- function(...) { } #' @export #' @rdname v4-not-yet -tm_minimap <- function(...) { - stop("Not yet implemented in v4") -} -#' @export -#' @rdname v4-not-yet tmap_grob <- function(...) { stop("Not yet implemented in v4") } diff --git a/man/tm_minimap.Rd b/man/tm_minimap.Rd new file mode 100644 index 00000000..e7202074 --- /dev/null +++ b/man/tm_minimap.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tm_components.R +\name{tm_minimap} +\alias{tm_minimap} +\title{Map component: minimap} +\usage{ +tm_minimap(server, toggle, stack, position, z, ...) +} +\arguments{ +\item{server}{name of the provider or an URL (see \code{\link{tm_tiles}}). By default, it shows the same map as the basemap, and moreover, it will automatically change when the user switches basemaps. Note the latter does not happen when \code{server} is specified.} + +\item{toggle}{should the minimap have a button to minimise it? By default \code{TRUE}.} + +\item{stack}{stack} + +\item{position}{position} + +\item{z}{z} + +\item{...}{arguments passed on to \code{\link[leaflet:addMiniMap]{addMiniMap}}.} +} +\description{ +Map component that adds a minimap in view mode +} +\seealso{ +\code{\link[leaflet:addMiniMap]{addMiniMap}} +} diff --git a/man/v4-not-yet.Rd b/man/v4-not-yet.Rd index 80a6b4d5..a51e880b 100644 --- a/man/v4-not-yet.Rd +++ b/man/v4-not-yet.Rd @@ -4,7 +4,6 @@ \alias{v4-not-yet} \alias{tm_iso} \alias{tm_logo} -\alias{tm_minimap} \alias{tmap_grob} \alias{tm_rgba} \alias{tmap_tip} @@ -16,8 +15,6 @@ tm_iso(...) tm_logo(...) -tm_minimap(...) - tmap_grob(...) tm_rgba(...)