Skip to content
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ Suggests:
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export(fitBounds)
export(flyTo)
export(flyToBounds)
export(getMapData)
export(gridOptions)
export(groupOptions)
export(hideGroup)
export(highlightOptions)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Replace viridis dependency by viridisLite (@olivroy, #897)

* `addRasterImage` now takes `options = gridOptions()`, so that arbitrary Leaflet layer options can be controlled. (#692)

# leaflet 2.2.1

* When `addProviderTiles()` is used with `{leaflet.providers}` version 2.0.0 or later, the `leaflet-providers` HTML dependency produced can be correctly cached by knitr. When used with older versions of `{leaflet.providers}`, the HTML dependency uses temp files that break knitr's caching mechanism (thanks @qdread, @jaredlander; #884).
Expand Down
34 changes: 32 additions & 2 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' Ignored if \code{project = FALSE}. See \code{\link{projectRaster}} for details.
#' @param maxBytes the maximum number of bytes to allow for the projected image
#' (before base64 encoding); defaults to 4MB.
#' @param options a list of additional options, intended to be provided by
#' a call to \code{\link{gridOptions}}
#' @template data-getMapData
#'
#' @seealso \code{\link{addRasterLegend}} for an easy way to add a legend for a
Expand Down Expand Up @@ -254,6 +256,7 @@ addRasterImage <- function(
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {
if (inherits(x, "SpatRaster")) {
Expand All @@ -268,6 +271,7 @@ addRasterImage <- function(
project = project,
method = method,
maxBytes = maxBytes,
options = options,
data = data
)
} else if (inherits(x, "RasterLayer")) {
Expand All @@ -282,6 +286,7 @@ addRasterImage <- function(
project = project,
method = method,
maxBytes = maxBytes,
options = options,
data = data
)
} else {
Expand Down Expand Up @@ -389,10 +394,14 @@ addRasterImage_RasterLayer <- function(
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {


options$opacity <- opacity
options$attribution <- attribution

raster_is_factor <- raster::is.factor(x)
method <- match.arg(method)
if (method == "auto") {
Expand Down Expand Up @@ -444,7 +453,7 @@ addRasterImage_RasterLayer <- function(
list(raster::ymin(bounds), raster::xmax(bounds))
)

invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>%
expandLimits(
c(raster::ymin(bounds), raster::ymax(bounds)),
c(raster::xmin(bounds), raster::xmax(bounds))
Expand All @@ -462,6 +471,7 @@ addRasterImage_SpatRaster <- function(
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {
if (!is_installed("terra", "1.6-3")) { # for terra::has.RGB()
Expand All @@ -471,6 +481,9 @@ addRasterImage_SpatRaster <- function(
)
}

options$opacity <- opacity
options$attribution <- attribution

if (terra::has.RGB(x)) {
# RGB(A) channels to color table
x <- terra::colorize(x, "col")
Expand Down Expand Up @@ -548,7 +561,7 @@ addRasterImage_SpatRaster <- function(
list(terra::ymin(bounds), terra::xmax(bounds))
)

invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>%
expandLimits(
c(terra::ymin(bounds), terra::ymax(bounds)),
c(terra::xmin(bounds), terra::xmax(bounds))
Expand Down Expand Up @@ -639,6 +652,23 @@ tileOptions <- function(
))
}

#' @describeIn map-options Options for grid layers
#' @export
gridOptions <- function(
tileSize = 256,
updateWhenIdle = NULL,
zIndex = 1,
minZoom = 0,
maxZoom = NULL,
...
) {
filterNULL(list(
tileSize = tileSize, updateWhenIdle = updateWhenIdle, zIndex = zIndex,
minZoom = minZoom, maxZoom = maxZoom,
...
))
}

#' Remove elements from a map
#'
#' Remove one or more features from a map, identified by \code{layerId}; or,
Expand Down
8 changes: 3 additions & 5 deletions inst/htmlwidgets/assets/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,7 @@ methods.setGroupOptions = function (group, options) {
this.showHideGroupsOnZoom();
};

methods.addRasterImage = function (uri, bounds, opacity, attribution, layerId, group) {
methods.addRasterImage = function (uri, bounds, layerId, group, options) {
// uri is a data URI containing an image. We want to paint this image as a
// layer at (top-left) bounds[0] to (bottom-right) bounds[1].
// We can't simply use ImageOverlay, as it uses bilinear scaling which looks
Expand Down Expand Up @@ -2386,12 +2386,10 @@ methods.addRasterImage = function (uri, bounds, opacity, attribution, layerId, g

img.src = uri;

var canvasTiles = _leaflet2["default"].gridLayer({
opacity: opacity,
attribution: attribution,
var canvasTiles = _leaflet2["default"].gridLayer(Object.assign({}, options, {
detectRetina: true,
async: true
}); // NOTE: The done() function MUST NOT be invoked until after the current
})); // NOTE: The done() function MUST NOT be invoked until after the current
// tick; done() looks in Leaflet's tile cache for the current tile, and
// since it's still being constructed, it won't be found.

Expand Down
8 changes: 3 additions & 5 deletions javascript/src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ methods.setGroupOptions = function(group, options) {
this.showHideGroupsOnZoom();
};

methods.addRasterImage = function(uri, bounds, opacity, attribution, layerId, group) {
methods.addRasterImage = function(uri, bounds, layerId, group, options) {
// uri is a data URI containing an image. We want to paint this image as a
// layer at (top-left) bounds[0] to (bottom-right) bounds[1].

Expand Down Expand Up @@ -1104,12 +1104,10 @@ methods.addRasterImage = function(uri, bounds, opacity, attribution, layerId, gr
};
img.src = uri;

let canvasTiles = L.gridLayer({
opacity: opacity,
attribution: attribution,
let canvasTiles = L.gridLayer(Object.assign({}, options, {
detectRetina: true,
async: true
});
}));

// NOTE: The done() function MUST NOT be invoked until after the current
// tick; done() looks in Leaflet's tile cache for the current tile, and
Expand Down
4 changes: 4 additions & 0 deletions man/addRasterImage.Rd

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

12 changes: 12 additions & 0 deletions man/map-options.Rd

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