From c370a7eea5d92ce6e119bd6bbebc1cd56023d103 Mon Sep 17 00:00:00 2001 From: edisz Date: Sun, 1 Apr 2018 13:18:44 +0200 Subject: [PATCH 1/6] added options to addRasterImage; remove opacity & attribution agruments (can be passed with options). https://github.com/rstudio/leaflet/issues/507 --- R/layers.R | 13 ++++++++----- inst/htmlwidgets/leaflet.js | 9 ++------- javascript/src/methods.js | 9 ++------- man/addRasterImage.Rd | 16 +++++++++------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/R/layers.R b/R/layers.R index e17e237ac..50af71174 100644 --- a/R/layers.R +++ b/R/layers.R @@ -202,8 +202,6 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y #' @param colors the color palette (see \code{\link{colorNumeric}}) or function #' to use to color the raster values (hint: if providing a function, set #' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent) -#' @param opacity the base opacity of the raster, expressed from 0 to 1 -#' @param attribution the HTML string to show as the attribution for this layer #' @param layerId the layer id #' @param group the name of the group this raster image should belong to (see #' the same parameter under \code{\link{addTiles}}) @@ -218,6 +216,9 @@ 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 extra options for tile layers, popups, paths +#' (circles, rectangles, polygons, ...), or other map elements +#' @seealso \code{\link{tileOptions}} #' @template data-getMapData #' #' @examples @@ -234,13 +235,12 @@ addRasterImage <- function( map, x, colors = if (raster::is.factor(x)) "Set1" else "Spectral", - opacity = 1, - attribution = NULL, layerId = NULL, group = NULL, project = TRUE, method = c("auto", "bilinear", "ngb"), maxBytes = 4 * 1024 * 1024, + options = tileOptions(), data = getMapData(map) ) { stopifnot(inherits(x, "RasterLayer")) @@ -302,7 +302,10 @@ addRasterImage <- function( list(raster::ymin(bounds), raster::xmax(bounds)) ) - invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>% + options$detectRetina = TRUE + options$async = TRUE + + invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>% expandLimits( c(raster::ymin(bounds), raster::ymax(bounds)), c(raster::xmin(bounds), raster::xmax(bounds)) diff --git a/inst/htmlwidgets/leaflet.js b/inst/htmlwidgets/leaflet.js index 16b3c48b6..3be4d0ccf 100644 --- a/inst/htmlwidgets/leaflet.js +++ b/inst/htmlwidgets/leaflet.js @@ -2063,7 +2063,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]. @@ -2182,12 +2182,7 @@ methods.addRasterImage = function (uri, bounds, opacity, attribution, layerId, g }; img.src = uri; - var canvasTiles = _leaflet2.default.gridLayer({ - opacity: opacity, - attribution: attribution, - detectRetina: true, - async: true - }); + var canvasTiles = _leaflet2.default.gridLayer(options); // 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 diff --git a/javascript/src/methods.js b/javascript/src/methods.js index 459092ab3..029185ef0 100644 --- a/javascript/src/methods.js +++ b/javascript/src/methods.js @@ -969,7 +969,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]. @@ -1090,12 +1090,7 @@ methods.addRasterImage = function(uri, bounds, opacity, attribution, layerId, gr }; img.src = uri; - let canvasTiles = L.gridLayer({ - opacity: opacity, - attribution: attribution, - detectRetina: true, - async: true - }); + let canvasTiles = L.gridLayer(options); // 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 diff --git a/man/addRasterImage.Rd b/man/addRasterImage.Rd index e22e58099..76e8db046 100644 --- a/man/addRasterImage.Rd +++ b/man/addRasterImage.Rd @@ -6,9 +6,9 @@ \title{Add a raster image as a layer} \usage{ addRasterImage(map, x, colors = if (raster::is.factor(x)) "Set1" else - "Spectral", opacity = 1, attribution = NULL, layerId = NULL, - group = NULL, project = TRUE, method = c("auto", "bilinear", "ngb"), - maxBytes = 4 * 1024 * 1024, data = getMapData(map)) + "Spectral", layerId = NULL, group = NULL, project = TRUE, + method = c("auto", "bilinear", "ngb"), maxBytes = 4 * 1024 * 1024, + options = tileOptions(), data = getMapData(map)) projectRasterForLeaflet(x, method) } @@ -21,10 +21,6 @@ projectRasterForLeaflet(x, method) to use to color the raster values (hint: if providing a function, set \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent)} -\item{opacity}{the base opacity of the raster, expressed from 0 to 1} - -\item{attribution}{the HTML string to show as the attribution for this layer} - \item{layerId}{the layer id} \item{group}{the name of the group this raster image should belong to (see @@ -44,6 +40,9 @@ Ignored if \code{project = FALSE}. See \code{\link{projectRaster}} for details.} \item{maxBytes}{the maximum number of bytes to allow for the projected image (before base64 encoding); defaults to 4MB.} +\item{options}{a list of extra options for tile layers, popups, paths +(circles, rectangles, polygons, ...), or other map elements} + \item{data}{the data object from which the argument values are derived; by default, it is the \code{data} object provided to \code{leaflet()} initially, but can be overridden} @@ -83,3 +82,6 @@ crs(r) <- CRS("+init=epsg:4326") leaflet() \%>\% addTiles() \%>\% addRasterImage(r, colors = "Spectral", opacity = 0.8) } +\seealso{ +\code{\link{tileOptions}} +} From 6aa7b76764e6fa833e24484c338c3d5afaef7380 Mon Sep 17 00:00:00 2001 From: edisz Date: Sun, 1 Apr 2018 13:47:36 +0200 Subject: [PATCH 2/6] added back opacity and attribtion (will overwrite tileOptions()) for backward compatibility. https://github.com/rstudio/leaflet/issues/507 --- R/layers.R | 12 ++++++++++++ man/addRasterImage.Rd | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/R/layers.R b/R/layers.R index 50af71174..66a6a61e0 100644 --- a/R/layers.R +++ b/R/layers.R @@ -202,6 +202,10 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y #' @param colors the color palette (see \code{\link{colorNumeric}}) or function #' to use to color the raster values (hint: if providing a function, set #' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent) +#' @param opacity the base opacity of the raster, expressed from 0 to 1 +#' (if set will overwrite \code{options}) +#' @param attribution the HTML string to show as the attribution for this layer +#' (can be also passed via \code{options}) #' @param layerId the layer id #' @param group the name of the group this raster image should belong to (see #' the same parameter under \code{\link{addTiles}}) @@ -235,6 +239,8 @@ addRasterImage <- function( map, x, colors = if (raster::is.factor(x)) "Set1" else "Spectral", + opacity = NULL, + attribution = NULL, layerId = NULL, group = NULL, project = TRUE, @@ -304,6 +310,12 @@ addRasterImage <- function( options$detectRetina = TRUE options$async = TRUE + # if attribution not passed via options + if (!exists('attribution', where = options)) + options$attribution <- attribution + # if opacity is set + if (!is.null(opacity)) + options$opacity <- opacity invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>% expandLimits( diff --git a/man/addRasterImage.Rd b/man/addRasterImage.Rd index 76e8db046..b29acf987 100644 --- a/man/addRasterImage.Rd +++ b/man/addRasterImage.Rd @@ -6,9 +6,10 @@ \title{Add a raster image as a layer} \usage{ addRasterImage(map, x, colors = if (raster::is.factor(x)) "Set1" else - "Spectral", layerId = NULL, group = NULL, project = TRUE, - method = c("auto", "bilinear", "ngb"), maxBytes = 4 * 1024 * 1024, - options = tileOptions(), data = getMapData(map)) + "Spectral", opacity = NULL, attribution = NULL, layerId = NULL, + group = NULL, project = TRUE, method = c("auto", "bilinear", "ngb"), + maxBytes = 4 * 1024 * 1024, options = tileOptions(), + data = getMapData(map)) projectRasterForLeaflet(x, method) } @@ -21,6 +22,12 @@ projectRasterForLeaflet(x, method) to use to color the raster values (hint: if providing a function, set \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent)} +\item{opacity}{the base opacity of the raster, expressed from 0 to 1 +(if set will overwrite \code{options})} + +\item{attribution}{the HTML string to show as the attribution for this layer +(can be also passed via \code{options})} + \item{layerId}{the layer id} \item{group}{the name of the group this raster image should belong to (see From 2f00ab53fc97db88090c9947301ea4c48374fa20 Mon Sep 17 00:00:00 2001 From: edisz Date: Sun, 1 Apr 2018 13:53:53 +0200 Subject: [PATCH 3/6] add deprecation warning to opactity & attribution agruments https://github.com/rstudio/leaflet/issues/507 --- R/layers.R | 14 +++++++++----- man/addRasterImage.Rd | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/R/layers.R b/R/layers.R index 66a6a61e0..2dde214dc 100644 --- a/R/layers.R +++ b/R/layers.R @@ -203,9 +203,9 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y #' to use to color the raster values (hint: if providing a function, set #' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent) #' @param opacity the base opacity of the raster, expressed from 0 to 1 -#' (if set will overwrite \code{options}) +#' (deprecated, if set will overwrite \code{options}) #' @param attribution the HTML string to show as the attribution for this layer -#' (can be also passed via \code{options}) +#' (deprecated, if set will overwrite \code{options}) #' @param layerId the layer id #' @param group the name of the group this raster image should belong to (see #' the same parameter under \code{\link{addTiles}}) @@ -310,12 +310,16 @@ addRasterImage <- function( options$detectRetina = TRUE options$async = TRUE - # if attribution not passed via options - if (!exists('attribution', where = options)) + # if attribution is set + if (!missing(attribution)) { + warning("argument 'attribution' is deprecated. Use options= instead.") options$attribution <- attribution + } # if opacity is set - if (!is.null(opacity)) + if (!missing(opacity)) { + warning("argument 'opacity' is deprecated. Use options= instead.") options$opacity <- opacity + } invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>% expandLimits( diff --git a/man/addRasterImage.Rd b/man/addRasterImage.Rd index b29acf987..fe24d6336 100644 --- a/man/addRasterImage.Rd +++ b/man/addRasterImage.Rd @@ -23,10 +23,10 @@ to use to color the raster values (hint: if providing a function, set \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent)} \item{opacity}{the base opacity of the raster, expressed from 0 to 1 -(if set will overwrite \code{options})} +(deprecated, if set will overwrite \code{options})} \item{attribution}{the HTML string to show as the attribution for this layer -(can be also passed via \code{options})} +(deprecated, if set will overwrite \code{options})} \item{layerId}{the layer id} From 7b4e55d94a2fd01d9f6e86323fd5c22d4fc1aabb Mon Sep 17 00:00:00 2001 From: edisz Date: Sun, 1 Apr 2018 14:00:38 +0200 Subject: [PATCH 4/6] add NEWs, R CMD CHECK passed --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 48dc6bee2..5ce0f3a25 100644 --- a/NEWS +++ b/NEWS @@ -49,6 +49,8 @@ leaflet 2.0.0 * Added `method` argument to `addRasterImage()` to enable nearest neighbor interpolation when projecting categorical rasters (#462) +* Added `options` argument to `addRasterImage()` to give finer control over the added tile. (#507) + * Added an `'auto'` method for `addRasterImage()`. Projected factor results are coerced into factors. (9accc7e) * Added `data` parameter to remaining `addXXX()` methods, including addLegend. (f273edd, #491, #485) From a7a977368f932dc6e8214ae2b4d2b7f2d9bc9fac Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 13 Apr 2018 15:54:37 -0400 Subject: [PATCH 5/6] move option setting up inside function and adjust warning message --- R/layers.R | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/R/layers.R b/R/layers.R index 3447c64a6..aff0d666d 100644 --- a/R/layers.R +++ b/R/layers.R @@ -251,6 +251,20 @@ addRasterImage <- function( ) { stopifnot(inherits(x, "RasterLayer")) + options$detectRetina <- TRUE + # options$async <- TRUE removed in 1.x + + # if opacity is set + if (!missing(opacity)) { + warning("argument 'opacity' is deprecated. Use `options = tileOptions(opacity = ", as.character(opacity), ")` instead.") + options$opacity <- opacity + } + # if attribution is set + if (!missing(attribution)) { + warning("argument 'attribution' is deprecated. Use `options = tileOptions(attribution = ", as.character(attribution), ")` instead.") + options$attribution <- attribution + } + raster_is_factor <- raster::is.factor(x) method <- match.arg(method) if (method == "auto") { @@ -308,19 +322,6 @@ addRasterImage <- function( list(raster::ymin(bounds), raster::xmax(bounds)) ) - options$detectRetina = TRUE - options$async = TRUE - # if attribution is set - if (!missing(attribution)) { - warning("argument 'attribution' is deprecated. Use options= instead.") - options$attribution <- attribution - } - # if opacity is set - if (!missing(opacity)) { - warning("argument 'opacity' is deprecated. Use options= instead.") - options$opacity <- opacity - } - invokeMethod(map, data, "addRasterImage", uri, latlng, layerId, group, options) %>% expandLimits( c(raster::ymin(bounds), raster::ymax(bounds)), From b7a5fa27240329d11680a4547189208a128fa7ab Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 13 Apr 2018 15:55:04 -0400 Subject: [PATCH 6/6] keep deprecated documentation consistent --- R/layers.R | 19 ++++++++----------- man/addRasterImage.Rd | 9 +++------ man/map-options.Rd | 4 ++-- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/R/layers.R b/R/layers.R index aff0d666d..47706405a 100644 --- a/R/layers.R +++ b/R/layers.R @@ -202,10 +202,8 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y #' @param colors the color palette (see \code{\link{colorNumeric}}) or function #' to use to color the raster values (hint: if providing a function, set #' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent) -#' @param opacity the base opacity of the raster, expressed from 0 to 1 -#' (deprecated, if set will overwrite \code{options}) -#' @param attribution the HTML string to show as the attribution for this layer -#' (deprecated, if set will overwrite \code{options}) +#' @param opacity Deprecated. If set, will overwrite \code{options$opacity}). +#' @param attribution Deprecated. If set, will overwrite \code{options$attribution}. #' @param layerId the layer id #' @param group the name of the group this raster image should belong to (see #' the same parameter under \code{\link{addTiles}}) @@ -220,8 +218,7 @@ 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 extra options for tile layers, popups, paths -#' (circles, rectangles, polygons, ...), or other map elements +#' @param options a list of extra options for tile layers. See \code{\link{tileOptions}}. #' @seealso \code{\link{tileOptions}} #' @template data-getMapData #' @@ -239,8 +236,8 @@ addRasterImage <- function( map, x, colors = if (raster::is.factor(x)) "Set1" else "Spectral", - opacity = NULL, - attribution = NULL, + opacity = NULL, # deprecated + attribution = NULL, # deprecated layerId = NULL, group = NULL, project = TRUE, @@ -573,7 +570,7 @@ safeLabel <- function(label, data) { #' @param textsize Change the text size of a single tooltip #' @param textOnly Display only the text, no regular surrounding box. #' @param style list of css style to be added to the tooltip -#' @param zoomAnimation deprecated. See \url{https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5} +#' @param zoomAnimation Deprecated. See \url{https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5} #' @param sticky If true, the tooltip will follow the mouse instead of being fixed at the feature center. Default value is \code{TRUE} (different from leaflet.js \code{FALSE}); see \url{http://leafletjs.com/reference-1.3.1.html#tooltip-sticky} #' @describeIn map-options Options for labels #' @export @@ -895,7 +892,7 @@ b64EncodePackedIcons <- function(packedIcons) { } #' @param interactive whether the element emits mouse events -#' @param clickable DEPRECATED! Use the \code{interactive} option. +#' @param clickable Deprecated. Use the \code{interactive} option. #' @param #' draggable,keyboard,title,alt,zIndexOffset,riseOnHover,riseOffset #' marker options; see \url{http://leafletjs.com/reference-1.3.1.html#marker-option} @@ -1367,5 +1364,5 @@ removeLayersControl <- function(map) { zoomAnimationWarning <- function() { - warning("zoomAnimation has been deprecated by Leaflet.js. See https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5\nignoring 'zoomAnimation' parameter") + warning("zoomAnimation has been deprecated by Leaflet.js. See https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5\nignoring 'zoomAnimation' argument") } diff --git a/man/addRasterImage.Rd b/man/addRasterImage.Rd index fe24d6336..10483f4a1 100644 --- a/man/addRasterImage.Rd +++ b/man/addRasterImage.Rd @@ -22,11 +22,9 @@ projectRasterForLeaflet(x, method) to use to color the raster values (hint: if providing a function, set \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent)} -\item{opacity}{the base opacity of the raster, expressed from 0 to 1 -(deprecated, if set will overwrite \code{options})} +\item{opacity}{Deprecated. If set, will overwrite \code{options$opacity}).} -\item{attribution}{the HTML string to show as the attribution for this layer -(deprecated, if set will overwrite \code{options})} +\item{attribution}{Deprecated. If set, will overwrite \code{options$attribution}.} \item{layerId}{the layer id} @@ -47,8 +45,7 @@ Ignored if \code{project = FALSE}. See \code{\link{projectRaster}} for details.} \item{maxBytes}{the maximum number of bytes to allow for the projected image (before base64 encoding); defaults to 4MB.} -\item{options}{a list of extra options for tile layers, popups, paths -(circles, rectangles, polygons, ...), or other map elements} +\item{options}{a list of extra options for tile layers. See \code{\link{tileOptions}}.} \item{data}{the data object from which the argument values are derived; by default, it is the \code{data} object provided to \code{leaflet()} diff --git a/man/map-options.Rd b/man/map-options.Rd index 12f444d05..0428e0cdd 100644 --- a/man/map-options.Rd +++ b/man/map-options.Rd @@ -62,13 +62,13 @@ transparency} \item{maxWidth, minWidth, maxHeight, autoPan, keepInView, closeButton, closeOnClick}{popup options; see \url{http://leafletjs.com/reference-1.3.1.html#popup-option}} -\item{zoomAnimation}{deprecated. See \url{https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5}} +\item{zoomAnimation}{Deprecated. See \url{https://github.com/Leaflet/Leaflet/blob/master/CHANGELOG.md#api-changes-5}} \item{className}{a CSS class name set on an element} \item{interactive}{whether the element emits mouse events} -\item{clickable}{DEPRECATED! Use the \code{interactive} option.} +\item{clickable}{Deprecated. Use the \code{interactive} option.} \item{noHide, direction, offset, permanent}{label options; see \url{http://leafletjs.com/reference-1.3.1.html#tooltip-option}}