Skip to content

Commit 5e6e4a8

Browse files
committed
Add options to addRasterImage
1 parent e15a997 commit 5e6e4a8

File tree

8 files changed

+50
-12
lines changed

8 files changed

+50
-12
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
@@ -2,6 +2,8 @@
22

33
* Fixed #893: Correctly call `terra::crs()` when checking the CRS of a `SpatVector` object in `pointData()` or `polygonData()` (thanks @mkoohafkan, #894).
44

5+
* `addRasterImage` now takes `options = gridOptions()`, so that arbitrary Leaflet layer options can be controlled. (#692)
6+
57
# leaflet 2.2.1
68

79
* 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: 24 additions & 1 deletion
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 = tileOptions(),
257260
data = getMapData(map)
258261
) {
259262
if (inherits(x, "SpatRaster")) {
@@ -393,6 +396,9 @@ addRasterImage_RasterLayer <- function(
393396
) {
394397

395398

399+
options$opacity <- opacity
400+
options$attribution <- attribution
401+
396402
raster_is_factor <- raster::is.factor(x)
397403
method <- match.arg(method)
398404
if (method == "auto") {
@@ -444,7 +450,7 @@ addRasterImage_RasterLayer <- function(
444450
list(raster::ymin(bounds), raster::xmax(bounds))
445451
)
446452

447-
invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
453+
invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group, options) %>%
448454
expandLimits(
449455
c(raster::ymin(bounds), raster::ymax(bounds)),
450456
c(raster::xmin(bounds), raster::xmax(bounds))
@@ -639,6 +645,23 @@ tileOptions <- function(
639645
))
640646
}
641647

648+
#' @describeIn map-options Options for grid layers
649+
#' @export
650+
gridOptions <- function(
651+
tileSize = 256,
652+
updateWhenIdle = NULL,
653+
zIndex = 1,
654+
minZoom = 0,
655+
maxZoom = NULL,
656+
...
657+
) {
658+
filterNULL(list(
659+
tileSize = tileSize, updateWhenIdle = updateWhenIdle, zIndex = zIndex,
660+
minZoom = minZoom, maxZoom = maxZoom,
661+
...
662+
))
663+
}
664+
642665
#' Remove elements from a map
643666
#'
644667
#' 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, opacity, attribution, 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, opacity, attribution, 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)