Skip to content

Commit

Permalink
implemented tm_minimap
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jul 16, 2024
1 parent 702a226 commit a106b74
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 8 deletions.
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions R/tm_components.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))
}


23 changes: 23 additions & 0 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions R/tmapLeafletComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

6 changes: 6 additions & 0 deletions R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions R/v4-not-implemented.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
27 changes: 27 additions & 0 deletions man/tm_minimap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions man/v4-not-yet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a106b74

Please sign in to comment.