From 4f58d62409feb4fcac76d4754cc5ef2c9f1cd5aa Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Tue, 28 May 2024 12:58:41 +0200 Subject: [PATCH] fix: fixes #58 - expose option to addStarsImage --- NAMESPACE | 1 + NEWS | 1 + R/addStarsImage.R | 38 +++++++++++--------------------------- man/addStarsImage.Rd | 33 ++++++++++++++++++++------------- 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 808cc61..18c4990 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,7 @@ importFrom(leaflet,addRasterImage) importFrom(leaflet,colorNumeric) importFrom(leaflet,expandLimits) importFrom(leaflet,getMapData) +importFrom(leaflet,gridOptions) importFrom(leaflet,invokeMethod) importFrom(methods,formalArgs) importFrom(png,writePNG) diff --git a/NEWS b/NEWS index 56350bc..ad4d907 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ leafem 0.2.3.9001 (2024-04-17) features and improvements bug fixes + * addStarsImage() now accepts an `option` argument. Requires leaflet >= 2.2.2 #58 documentation etc diff --git a/R/addStarsImage.R b/R/addStarsImage.R index 60b9a1b..75e7bd1 100644 --- a/R/addStarsImage.R +++ b/R/addStarsImage.R @@ -3,27 +3,10 @@ #' @param map a mapview or leaflet object. #' @param x a stars layer. #' @param band the band number to be plotted. -#' @param colors the color palette (see colorNumeric) or function to use to -#' color the raster values (hint: if providing a function, set na.color -#' to "#00000000" to make 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 addTiles) -#' @param project if TRUE, automatically project x to the map projection -#' expected by Leaflet (EPSG:3857); if FALSE, it's the caller's responsibility -#' to ensure that x is already projected, and that extent(x) is -#' expressed in WGS84 latitude/longitude coordinates -#' @param method the method used for computing values of the new, -#' projected raster image. "bilinear" (the default) is appropriate for -#' continuous data, "ngb" - nearest neighbor - is appropriate for categorical data. -#' Ignored if project = FALSE. See projectRaster for details. -#' @param maxBytes the maximum number of bytes to allow for the projected image -#' (before base64 encoding); defaults to 4MB. #' @param 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. +#' @inheritParams leaflet::addRasterImage #' @param ... currently not used. #' #' @details @@ -43,7 +26,7 @@ #' } #' #' @importFrom grDevices col2rgb colors -#' @importFrom leaflet colorNumeric expandLimits getMapData invokeMethod +#' @importFrom leaflet colorNumeric expandLimits getMapData invokeMethod gridOptions #' @importFrom sf st_as_sfc st_bbox st_transform #' @importFrom base64enc base64encode #' @importFrom png writePNG @@ -60,6 +43,7 @@ addStarsImage <- function( , project = FALSE , method = c("auto", "bilinear", "ngb") , maxBytes = 4 * 1024 * 1024 + , options = gridOptions() , data = getMapData(map) , ... ) { @@ -162,16 +146,16 @@ addStarsImage <- function( list(bounds[4], bounds[1]), list(bounds[2], bounds[3]) ) + options$opacity <- opacity + options$attribution <- attribution map = leaflet::invokeMethod( map, data, "addRasterImage", uri, latlng, - opacity, attribution, layerId, group - ) - - leaflet::expandLimits( - map, - c(bounds[2], bounds[4]), - c(bounds[1], bounds[3]) - ) + layerId, group, options + ) %>% + leaflet::expandLimits( + c(bounds[2], bounds[4]), + c(bounds[1], bounds[3]) + ) } diff --git a/man/addStarsImage.Rd b/man/addStarsImage.Rd index cd0a976..4905286 100644 --- a/man/addStarsImage.Rd +++ b/man/addStarsImage.Rd @@ -16,6 +16,7 @@ addStarsImage( project = FALSE, method = c("auto", "bilinear", "ngb"), maxBytes = 4 * 1024 * 1024, + options = gridOptions(), data = getMapData(map), ... ) @@ -27,9 +28,11 @@ addStarsImage( \item{band}{the band number to be plotted.} -\item{colors}{the color palette (see colorNumeric) or function to use to -color the raster values (hint: if providing a function, set na.color -to "#00000000" to make NA areas transparent)} +\item{colors}{the color palette (see \code{\link[leaflet]{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). +The palette is ignored if \code{x} is a SpatRaster with a color table or if +it has RGB channels.} \item{opacity}{the base opacity of the raster, expressed from 0 to 1} @@ -37,22 +40,26 @@ to "#00000000" to make NA areas transparent)} \item{layerId}{the layer id} -\item{group}{the name of the group this raster image should belong to -(see the same parameter under addTiles)} +\item{group}{the name of the group this raster image should belong to (see +the same parameter under \code{\link[leaflet]{addTiles}})} -\item{project}{if TRUE, automatically project x to the map projection -expected by Leaflet (EPSG:3857); if FALSE, it's the caller's responsibility -to ensure that x is already projected, and that extent(x) is -expressed in WGS84 latitude/longitude coordinates} +\item{project}{if \code{TRUE}, automatically project \code{x} to the map +projection expected by Leaflet (\code{EPSG:3857}); if \code{FALSE}, it's +the caller's responsibility to ensure that \code{x} is already projected, +and that \code{extent(x)} is expressed in WGS84 latitude/longitude +coordinates} -\item{method}{the method used for computing values of the new, -projected raster image. "bilinear" (the default) is appropriate for -continuous data, "ngb" - nearest neighbor - is appropriate for categorical data. -Ignored if project = FALSE. See projectRaster for details.} +\item{method}{the method used for computing values of the new, projected raster image. +\code{"bilinear"} (the default) is appropriate for continuous data, +\code{"ngb"} - nearest neighbor - is appropriate for categorical data. +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 additional options, intended to be provided by +a call to \code{\link[leaflet]{gridOptions}}} + \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.}