Converts a spatial raster into a semi-transparent RGBA overlay for the current rayshader scene. Raster values can be drawn with a single color or mapped through a color palette.
generate_raster_overlay(
raster,
extent = NULL,
heightmap = NULL,
width = NA,
height = NA,
resolution_multiply = 1,
palette = "red",
alpha = 0.5,
range = NULL,
resample_method = "bilinear"
)A raster layer. This can be a terra::SpatRaster, a
raster::RasterLayer/RasterBrick/RasterStack, or a raster filename.
Default NULL. Either an object representing the spatial extent of the scene
(either from the raster, terra, sf, or sp packages),
a length-4 numeric vector specifying c("xmin", "xmax","ymin","ymax"), or the spatial object (from
the previously aforementioned packages) which will be automatically converted to an extent object.
If omitted, rayshader will infer the extent from heightmap when possible,
otherwise reuse cached extent metadata from the active scene or the most recent
raster-backed hillshade call.
Default NULL. The original height map. Pass this in to extract the dimensions of the resulting
overlay automatically. If omitted, rayshader will reuse the cached heightmap
from the active scene or the most recent hillshade call.
Default NA. Width of the resulting overlay. Default the same dimensions as height map.
Default NA. Height of the resulting overlay. Default the same dimensions as height map.
Default 1. If passing in heightmap instead of width/height, amount to
increase the resolution of the overlay.
Should be combined with add_overlay() with rescale_original = TRUE to ensure those added details are captured
in the final map.
Default "red". Single color, vector of colors, or palette function.
A single color draws every finite raster cell with that color. Multiple colors
or a palette function map finite raster values continuously from low to high.
Default 0.5. Alpha multiplier for finite raster cells. Must be between 0 and 1.
Alpha values included in palette colors are multiplied by this value.
Default NULL. Numeric length-2 range used to map raster values
through palette. Values outside this range are clamped. If NULL, the
finite value range of the aligned raster is used.
Default "bilinear". Method passed to
terra::resample() or terra::project() when aligning raster to the
scene grid.
4-layer RGBA array representing the raster overlay.
if (interactive() || identical(Sys.getenv("IN_PKGDOWN"), "true")) {
# Create a semi-transparent overlay from low elevations:
low_elevation = montereybay_spatial
low_elevation[low_elevation > 0] = NA
montereybay_spatial |>
height_shade() |>
add_overlay(generate_raster_overlay(low_elevation, palette = "dodgerblue3")) |>
plot_map()
}