Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Imports:
base64enc,
htmltools,
htmlwidgets,
jsonlite,
lattice,
leafem,
leaflet (>= 2.0.0),
Expand Down
6 changes: 4 additions & 2 deletions R/fgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ sfFgb = function(x,
if (is.null(map.types) |
identical(mapviewGetOption("basemaps"), map.types)) {
if (getGeometryType(x) %in% c("pl", "pt")) {
map.types <- as.vector(stats::na.omit(basemaps(col.regions)))
bm <- basemaps(col.regions)
map.types <- bm[!is.na(bm)]
} else {
map.types <- as.vector(stats::na.omit(basemaps(color)))
bm <- basemaps(color)
map.types <- bm[!is.na(bm)]
}
}

Expand Down
73 changes: 59 additions & 14 deletions R/leafletControls.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ appendMapCallEntries_lf <- function(map1, map2) {
mp_deps = append(m1_deps, m2_deps)
mp_deps = mp_deps[!duplicated(mp_deps)]

## jsHooks (e.g. vector tiles onRender hooks)
m1_hooks = map1$jsHooks$render
m2_hooks = map2$jsHooks$render
mp_hooks = append(m1_hooks, m2_hooks)
mp_hooks = mp_hooks[!duplicated(mp_hooks)]

## base map controls
ctrls1 <- getLayerControlEntriesFromMap(map1)
ctrls2 <- getLayerControlEntriesFromMap(map2)
Expand Down Expand Up @@ -148,6 +154,9 @@ appendMapCallEntries_lf <- function(map1, map2) {

map1$x$calls <- mpcalls
map1$dependencies = mp_deps
if (length(mp_hooks) > 0) {
map1$jsHooks$render = mp_hooks
}
return(map1)
}

Expand Down Expand Up @@ -227,19 +236,55 @@ initBaseMaps <- function(map.types, canvas = FALSE, viewer.suppress = FALSE) {
)
)
if (!(is.null(map.types))) {
m <- leaflet::addProviderTiles(m, provider = map.types[1],
layerId = map.types[1], group = map.types[1],
options = providerTileOptions(
pane = "tilePane"
))
if (length(map.types) > 1) {
for (i in 2:length(map.types)) {
m <- leaflet::addProviderTiles(m, provider = map.types[i],
layerId = map.types[i], group = map.types[i],
options = providerTileOptions(
pane = "tilePane"
))
m = removeDuplicatedMapDependencies(m)
known_providers <- names(leaflet::providers)
labels <- getBasemapLabels(map.types)
providers <- unname(as.character(map.types))
vector_styles <- list()

for (i in seq_along(providers)) {
provider <- providers[i]
label <- labels[i]
if (!(provider %in% known_providers)) {
resolved <- resolveVectorTileStyle(provider)
if (!is.null(resolved)) {
vector_styles[[label]] <- resolved
}
}
}

if (length(vector_styles) > 0) {
initial_style <- if (labels[1] %in% names(vector_styles)) labels[1] else NULL
m <- addVectorTiles(
m,
initialStyle = initial_style,
styles = vector_styles
)
}

for (i in seq_along(providers)) {
provider <- providers[i]
label <- labels[i]
if (label %in% names(vector_styles)) {
m <- leaflet::addTiles(
m,
urlTemplate = "",
layerId = label,
group = label,
options = leaflet::tileOptions(pane = "tilePane")
)
} else {
m <- leaflet::addProviderTiles(
m,
provider = provider,
layerId = label,
group = label,
options = providerTileOptions(
pane = "tilePane"
)
)
}
if (i > 1) {
m <- removeDuplicatedMapDependencies(m)
}
}
}
Expand Down Expand Up @@ -496,7 +541,7 @@ mapViewLayersControl <- function(map, map.types, names, native.crs = FALSE) {
ind = getCallEntryFromMap(map, call = "addLayersControl")

if (!length(ind)) {
if (!(is.null(map.types))) bgm = map.types else bgm = character(0)
if (!(is.null(map.types))) bgm = getBasemapLabels(map.types) else bgm = character(0)
} else {
bgm <- map$x$calls[[ind[1]]]$args[[1]]
}
Expand Down
9 changes: 5 additions & 4 deletions R/mapviewControls.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ regionOpacity = function(x) {


basemaps = function(colors, shuffle = mapviewGetOption("basemaps.color.shuffle")) {
if (!shuffle) {
return(mapviewGetOption("basemaps"))
bm = mapviewGetOption("basemaps")
if (!shuffle || length(bm) < 2) {
return(bm)
}

ml = mean(as.numeric(sapply(colors, luminence)))
Expand All @@ -195,9 +196,9 @@ basemaps = function(colors, shuffle = mapviewGetOption("basemaps.color.shuffle")
unique_cyan = FALSE
}
if (ml > 0.8 | unique_cyan) {
mapviewGetOption("basemaps")[c(2, 1, 3:5)]
bm[c(2, 1, seq_len(length(bm))[-c(1, 2)])]
} else {
mapviewGetOption("basemaps")
bm
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ leafletMissing = function(map.types, ...) {
m = initBaseMaps(map.types)
m = leaflet::setView(map = m, 8.770862, 50.814772, zoom = 18)
m = leaflet::addLayersControl(map = m,
baseGroups = map.types,
baseGroups = getBasemapLabels(map.types),
position = mapviewGetOption(
"layers.control.pos"))
if (isAvailableInLeaflet()$scl) m = leaflet::addScaleBar(map = m, position = "bottomleft")
Expand Down
43 changes: 35 additions & 8 deletions R/ops.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ setMethod("+",
# if (length(
# getCallEntryFromMap(e1@map, "addProviderTiles")
# ) == 0) {
idx = getCallEntryFromMap(e2@map, "addProviderTiles")
idx = getCallEntryFromMap(e2@map, c("addProviderTiles", "addTiles"))
if (length(idx) > 0) {
e2@map$x$calls[idx] = NULL
}
idx = getCallEntryFromMap(e2@map, "addLayersControl")
if (length(idx) > 0) {
e2@map$x$calls[idx][[1]]$args[[1]] = character(0)
}
if (is.null(e1@map$jsHooks$render) ||
!any(sapply(e1@map$jsHooks$render, function(h) grepl("_vectorStyleMaps", h$code, fixed = TRUE)))) {
if (!is.null(e2@map$jsHooks$render)) {
e2@map$jsHooks$render = Filter(
function(h) !grepl("_vectorStyleMaps", h$code, fixed = TRUE),
e2@map$jsHooks$render
)
}
}
# }


Expand Down Expand Up @@ -236,14 +245,20 @@ setMethod("|",
)
}

e1_tile_idx = getCallEntryFromMap(e1@map, "addProviderTiles")
e1_tile_idx = getCallEntryFromMap(e1@map, c("addProviderTiles", "addTiles"))
if (length(e1_tile_idx) > 1) {
e1@map$x$calls[2:length(e1_tile_idx)] = NULL
e1@map$x$calls[e1_tile_idx[-1]] = NULL
}
e1_tile_idx = e1_tile_idx[1]
e1_tile_pane_idx = grep("pane", e1@map$x$calls[[e1_tile_idx]]$args)
e1@map$x$calls[[e1_tile_idx]]$args[[2]] = "left"
e1@map$x$calls[[e1_tile_idx]]$args[[e1_tile_pane_idx]][[4]] = "left"
e1@map$x$calls[[e1_tile_idx]]$args[[e1_tile_pane_idx]]$pane = "left"
if (!is.null(e1@map$jsHooks$render)) {
e1@map$jsHooks$render = lapply(e1@map$jsHooks$render, function(h) {
h$code = sub('var pane = "tilePane";', 'var pane = "left";', h$code, fixed = TRUE)
h
})
}

e1_lyrctrl_idx = getCallEntryFromMap(e1@map, "addLayersControl")
# bsgrps = e1@map$x$calls[[e1_lyrctrl_idx]]$args[[1]][1]
Expand Down Expand Up @@ -283,14 +298,20 @@ setMethod("|",


## e2 - right
e2_tile_idx = getCallEntryFromMap(e2@map, "addProviderTiles")
e2_tile_idx = getCallEntryFromMap(e2@map, c("addProviderTiles", "addTiles"))
if (length(e2_tile_idx) > 1) {
e2@map$x$calls[2:length(e2_tile_idx)] = NULL
e2@map$x$calls[e2_tile_idx[-1]] = NULL
}
e2_tile_idx = e2_tile_idx[1]
e2_tile_pane_idx = grep("pane", e2@map$x$calls[[e2_tile_idx]]$args)
e2@map$x$calls[[e2_tile_idx]]$args[[2]] = "right"
e2@map$x$calls[[e2_tile_idx]]$args[[e2_tile_pane_idx]][[4]] = "right"
e2@map$x$calls[[e2_tile_idx]]$args[[e2_tile_pane_idx]]$pane = "right"
if (!is.null(e2@map$jsHooks$render)) {
e2@map$jsHooks$render = lapply(e2@map$jsHooks$render, function(h) {
h$code = sub('var pane = "tilePane";', 'var pane = "right";', h$code, fixed = TRUE)
h
})
}

e2_lyrctrl_idx = getCallEntryFromMap(e2@map, "addLayersControl")
# bsgrps = e2@map$x$calls[[e2_lyrctrl_idx]]$args[[1]][1]
Expand Down Expand Up @@ -346,11 +367,17 @@ setMethod("|",
, m@map$x$calls
)

### make sure all dependencies are present
### make sure all dependencies and render hooks are present
m_deps = append(e1_deps, e2_deps)
m_deps = m_deps[!duplicated(m_deps)]
m@map$dependencies = m_deps

m_hooks = append(e1@map$jsHooks$render, e2@map$jsHooks$render)
m_hooks = m_hooks[!duplicated(m_hooks)]
if (length(m_hooks) > 0) {
m@map$jsHooks$render = m_hooks
}

m@map = leaflet::addLayersControl(
m@map
, overlayGroups = c(
Expand Down
22 changes: 17 additions & 5 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' Current options are "leaflet", "mapdeck", and "leafgl".
#' @param basemaps character. The basemaps to be used for rendering data. See
#' \url{https://leaflet-extras.github.io/leaflet-providers/preview/} for possible
#' values
#' raster values, or pass (optionally named) vector tile style JSON URLs or file paths.
#' @param basemaps.color.shuffle logical. Should basemaps order be changed to
#' enhance contrast based on layer coloring. Set to FALSE if you supply custom
#' basemaps or want to ensure that "CartoDB.Positron" is always the default.
Expand Down Expand Up @@ -122,6 +122,14 @@
#' @examples
#' mapviewOptions()
#' mapviewOptions(na.color = "pink")
#' mapviewOptions(
#' basemaps = c(
#' # vector tile style JSON URL (or local file path)
#' "OpenFreeMap.Liberty" = "https://tiles.openfreemap.org/styles/liberty",
#' # raster tile provider from leaflet::providers
#' "OpenStreetMap"
#' )
#' )
#' mapviewOptions()
#'
#' mapviewGetOption("platform")
Expand Down Expand Up @@ -222,7 +230,9 @@ mapviewOptions = function(platform,
pf = getOption('mapviewPlatform')
if (is.null(pf) || pf %in% c("leaflet", "leafgl")) {
default = c(
"Stadia.AlidadeSmooth"
"OpenFreeMap.Positron" = "https://tiles.openfreemap.org/styles/positron"
, "OpenFreeMap.Dark" = "https://tiles.openfreemap.org/styles/dark"
, "Stadia.AlidadeSmooth"
, "Stadia.AlidadeSmoothDark"
, "OpenStreetMap"
, "Esri.WorldImagery"
Expand Down Expand Up @@ -825,8 +835,10 @@ mapviewOptions = function(platform,

## global
options(mapviewPlatform = "leaflet")
options(mapviewBasemaps = c("CartoDB.Positron",
"CartoDB.DarkMatter",
options(mapviewBasemaps = c("OpenFreeMap.Positron" = "https://tiles.openfreemap.org/styles/positron",
"OpenFreeMap.Dark" = "https://tiles.openfreemap.org/styles/dark",
"Stadia.AlidadeSmooth",
"Stadia.AlidadeSmoothDark",
"OpenStreetMap",
"Esri.WorldImagery",
"OpenTopoMap"))
Expand Down Expand Up @@ -985,7 +997,7 @@ mapviewOptions = function(platform,
## global
cat("\n global options: \n\n")
cat('platform :', lst$platform, '\n' )
cat('basemaps :', lst$basemaps, '\n')
cat('basemaps :', getBasemapLabels(lst$basemaps), '\n')
cat('basemaps.color.shuffle :', lst$basemaps.color.shuffle, '\n')
cat('raster.palette :', format(.rasterPalette())[1], '\n')
cat('vector.palette :', format(.vectorPalette())[1], '\n')
Expand Down
6 changes: 4 additions & 2 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,11 @@ leaflet_sfc <- function(x,
if (is.null(map.types) |
identical(mapviewGetOption("basemaps"), map.types)) {
if (getGeometryType(x) %in% c("pl", "pt")) {
map.types <- as.vector(stats::na.omit(basemaps(col.regions)))
bm <- basemaps(col.regions)
map.types <- bm[!is.na(bm)]
} else {
map.types <- as.vector(stats::na.omit(basemaps(color)))
bm <- basemaps(color)
map.types <- bm[!is.na(bm)]
}
}

Expand Down
Loading
Loading