Skip to content

Commit 5cfe70b

Browse files
jcheng5schloerke
andauthored
Add options to addRasterImage (#692)
* Add options to addRasterImage * Use correct options Co-authored-by: Barret Schloerke <[email protected]> * Roxygenize * Pass addRasterImage options correctly --------- Co-authored-by: Barret Schloerke <[email protected]>
1 parent 7ae5ca3 commit 5cfe70b

File tree

8 files changed

+58
-13
lines changed

8 files changed

+58
-13
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ Suggests:
7979
Config/testthat/edition: 3
8080
Encoding: UTF-8
8181
LazyData: true
82-
RoxygenNote: 7.2.3
82+
RoxygenNote: 7.3.1

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export(fitBounds)
115115
export(flyTo)
116116
export(flyToBounds)
117117
export(getMapData)
118+
export(gridOptions)
118119
export(groupOptions)
119120
export(hideGroup)
120121
export(highlightOptions)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
* `addRasterImage` now takes `options = gridOptions()`, so that arbitrary Leaflet layer options can be controlled. (#692)
8+
79
# leaflet 2.2.1
810

911
* 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).

R/layers.R

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
225225
#' Ignored if \code{project = FALSE}. See \code{\link{projectRaster}} for details.
226226
#' @param maxBytes the maximum number of bytes to allow for the projected image
227227
#' (before base64 encoding); defaults to 4MB.
228+
#' @param options a list of additional options, intended to be provided by
229+
#' a call to \code{\link{gridOptions}}
228230
#' @template data-getMapData
229231
#'
230232
#' @seealso \code{\link{addRasterLegend}} for an easy way to add a legend for a
@@ -254,6 +256,7 @@ addRasterImage <- function(
254256
project = TRUE,
255257
method = c("auto", "bilinear", "ngb"),
256258
maxBytes = 4 * 1024 * 1024,
259+
options = gridOptions(),
257260
data = getMapData(map)
258261
) {
259262
if (inherits(x, "SpatRaster")) {
@@ -268,6 +271,7 @@ addRasterImage <- function(
268271
project = project,
269272
method = method,
270273
maxBytes = maxBytes,
274+
options = options,
271275
data = data
272276
)
273277
} else if (inherits(x, "RasterLayer")) {
@@ -282,6 +286,7 @@ addRasterImage <- function(
282286
project = project,
283287
method = method,
284288
maxBytes = maxBytes,
289+
options = options,
285290
data = data
286291
)
287292
} else {
@@ -389,10 +394,14 @@ addRasterImage_RasterLayer <- function(
389394
project = TRUE,
390395
method = c("auto", "bilinear", "ngb"),
391396
maxBytes = 4 * 1024 * 1024,
397+
options = gridOptions(),
392398
data = getMapData(map)
393399
) {
394400

395401

402+
options$opacity <- opacity
403+
options$attribution <- attribution
404+
396405
raster_is_factor <- raster::is.factor(x)
397406
method <- match.arg(method)
398407
if (method == "auto") {
@@ -444,7 +453,7 @@ addRasterImage_RasterLayer <- function(
444453
list(raster::ymin(bounds), raster::xmax(bounds))
445454
)
446455

447-
invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
456+
invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>%
448457
expandLimits(
449458
c(raster::ymin(bounds), raster::ymax(bounds)),
450459
c(raster::xmin(bounds), raster::xmax(bounds))
@@ -462,6 +471,7 @@ addRasterImage_SpatRaster <- function(
462471
project = TRUE,
463472
method = c("auto", "bilinear", "ngb"),
464473
maxBytes = 4 * 1024 * 1024,
474+
options = gridOptions(),
465475
data = getMapData(map)
466476
) {
467477
if (!is_installed("terra", "1.6-3")) { # for terra::has.RGB()
@@ -471,6 +481,9 @@ addRasterImage_SpatRaster <- function(
471481
)
472482
}
473483

484+
options$opacity <- opacity
485+
options$attribution <- attribution
486+
474487
if (terra::has.RGB(x)) {
475488
# RGB(A) channels to color table
476489
x <- terra::colorize(x, "col")
@@ -548,7 +561,7 @@ addRasterImage_SpatRaster <- function(
548561
list(terra::ymin(bounds), terra::xmax(bounds))
549562
)
550563

551-
invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
564+
invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>%
552565
expandLimits(
553566
c(terra::ymin(bounds), terra::ymax(bounds)),
554567
c(terra::xmin(bounds), terra::xmax(bounds))
@@ -639,6 +652,23 @@ tileOptions <- function(
639652
))
640653
}
641654

655+
#' @describeIn map-options Options for grid layers
656+
#' @export
657+
gridOptions <- function(
658+
tileSize = 256,
659+
updateWhenIdle = NULL,
660+
zIndex = 1,
661+
minZoom = 0,
662+
maxZoom = NULL,
663+
...
664+
) {
665+
filterNULL(list(
666+
tileSize = tileSize, updateWhenIdle = updateWhenIdle, zIndex = zIndex,
667+
minZoom = minZoom, maxZoom = maxZoom,
668+
...
669+
))
670+
}
671+
642672
#' Remove elements from a map
643673
#'
644674
#' Remove one or more features from a map, identified by \code{layerId}; or,

inst/htmlwidgets/assets/leaflet.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,7 @@ methods.setGroupOptions = function (group, options) {
22662266
this.showHideGroupsOnZoom();
22672267
};
22682268

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

23872387
img.src = uri;
23882388

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

javascript/src/methods.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ methods.setGroupOptions = function(group, options) {
983983
this.showHideGroupsOnZoom();
984984
};
985985

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

@@ -1104,12 +1104,10 @@ methods.addRasterImage = function(uri, bounds, opacity, attribution, layerId, gr
11041104
};
11051105
img.src = uri;
11061106

1107-
let canvasTiles = L.gridLayer({
1108-
opacity: opacity,
1109-
attribution: attribution,
1107+
let canvasTiles = L.gridLayer(Object.assign({}, options, {
11101108
detectRetina: true,
11111109
async: true
1112-
});
1110+
}));
11131111

11141112
// NOTE: The done() function MUST NOT be invoked until after the current
11151113
// tick; done() looks in Leaflet's tile cache for the current tile, and

man/addRasterImage.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/map-options.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)