leaflet (version 2.2.2)

addRasterImage: Add a raster image as a layer

Description

Create an image overlay from a RasterLayer or a SpatRaster object. This is only suitable for small to medium sized rasters, as the entire image will be embedded into the HTML page (or passed over the websocket in a Shiny context).

Usage

addRasterImage(
  map,
  x,
  colors = if (is.factor(x)[1]) "Set1" else "Spectral",
  opacity = 1,
  attribution = NULL,
  layerId = NULL,
  group = NULL,
  project = TRUE,
  method = c("auto", "bilinear", "ngb"),
  maxBytes = 4 * 1024 * 1024,
  options = gridOptions(),
  data = getMapData(map)
)

projectRasterForLeaflet(x, method)

Arguments

map

a map widget object

x

a SpatRaster or a RasterLayer object--see raster

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). The palette is ignored if x is a SpatRaster with a color table or if it has RGB channels.

opacity

the base opacity of the raster, expressed from 0 to 1

attribution

the HTML string to show as the attribution for this layer

layerId

the layer id

group

the name of the group this raster image should belong to (see the same parameter under addTiles)

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

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.

maxBytes

the maximum number of bytes to allow for the projected image (before base64 encoding); defaults to 4MB.

options

a list of additional options, intended to be provided by a call to gridOptions

data

the data object from which the argument values are derived; by default, it is the data object provided to leaflet() initially, but can be overridden

Details

The maxBytes parameter serves to prevent you from accidentally embedding an excessively large amount of data into your htmlwidget. This value is compared to the size of the final compressed image (after the raster has been projected, colored, and PNG encoded, but before base64 encoding is applied). Set maxBytes to Inf to disable this check, but be aware that very large rasters may not only make your map a large download but also may cause the browser to become slow or unresponsive.

To reduce the size of a SpatRaster, you can use spatSample as in x = spatSample(x, 100000, method="regular", as.raster=TRUE). With a RasterLayer you can use sampleRegular as in sampleRegular(x, 100000, asRaster=TRUE).

By default, the addRasterImage function will project the raster data x to the Pseudo-Mercator projection (EPSG:3857). This can be a time-consuming operation for even moderately sized rasters; although it is much faster for SpatRasters than for RasterLayers. If you are repeatedly adding a particular raster to your Leaflet maps, you can perform the projection ahead of time using projectRasterForLeaflet(), and call addRasterImage with project = FALSE.

See Also

addRasterLegend for an easy way to add a legend for a SpatRaster with a color table.

Examples

Run this code
library(raster)

r <- raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05, nrows = 30, ncols = 30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")

pal <- colorNumeric("Spectral", domain = c(0, 1000))
leaflet() %>% addTiles() %>%
  addRasterImage(r, colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = c(0, 1000))

Run the code above in your browser using DataCamp Workspace