Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raster image options #524

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ BUG FIXES AND FEATURES

* 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)
Expand Down
33 changes: 25 additions & 8 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +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
#' @param attribution the HTML string to show as the attribution for this layer
#' @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}})
Expand All @@ -218,6 +218,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 extra options for tile layers. See \code{\link{tileOptions}}.
#' @seealso \code{\link{tileOptions}}
#' @template data-getMapData
#'
#' @examples
Expand All @@ -236,17 +238,32 @@ addRasterImage <- function(
map,
x,
colors = if (raster::is.factor(x)) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
opacity = NULL, # deprecated
attribution = NULL, # deprecated
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = tileOptions(),
data = getMapData(map)
) {
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") {
Expand Down Expand Up @@ -304,7 +321,7 @@ addRasterImage <- 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 Down Expand Up @@ -555,7 +572,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
Expand Down Expand Up @@ -877,7 +894,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}
Expand Down Expand Up @@ -1349,5 +1366,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")
}
9 changes: 2 additions & 7 deletions inst/htmlwidgets/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,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 @@ -2253,12 +2253,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
Expand Down
9 changes: 2 additions & 7 deletions javascript/src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions man/addRasterImage.Rd

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

4 changes: 2 additions & 2 deletions man/map-options.Rd

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