spatialize_image() converts a rayshader image into a georeferenced terra::SpatRaster. If they are omitted, rayshader reuses cached extent and CRS metadata from the active scene, following the same cache-resolution rules used by the render_*() helpers. Cached metadata can come from plot_3d() or plot_gg(), and panel can be used to select a specific facet panel for faceted ggplot scenes. Explicit extent and crs arguments always take precedence.

spatialize_image(
  image,
  extent = NULL,
  crs = NULL,
  panel = NULL,
  include_alpha = FALSE,
  to_rgb = TRUE,
  flip_vertical = FALSE,
  flip_horizontal = FALSE,
  layer_names = NULL,
  include_height = FALSE,
  heightmap = NULL,
  height_layer_name = "height",
  ...
)

Arguments

image

A matrix, RGB/RGBA array, or any input accepted by rayimage::ray_read_image().

extent

Default NULL. Spatial extent metadata for the output raster. Explicit extent overrides cached metadata. Accepts the same extent-like inputs supported by get_extent(), including numeric c(xmin, xmax, ymin, ymax) vectors, terra::SpatExtent, terra::SpatRaster, raster::Extent, Raster*, sf/sfc, and sp objects. If omitted, rayshader reuses cached scene extent metadata.

crs

Default NULL. CRS for the output raster. Explicit crs overrides everything else. If omitted, rayshader first tries to inherit CRS from the explicit extent object, then from cached scene metadata.

panel

Default NULL. Facet panel identifier for scenes created with plot_gg(). Required to disambiguate faceted ggplot scenes when panel- specific cached metadata is needed. Ignored for non-faceted/non-ggplot scenes.

include_alpha

Default FALSE. If FALSE and image has four channels, drop alpha. If TRUE, include alpha as a fourth raster layer.

to_rgb

Default TRUE. If TRUE, convert color channels from linear rayshader values to sRGB with rayimage::render_gamma_linear(), then scale to the 0-255 range and clamp. Alpha is not gamma-adjusted, but is scaled and clamped to match.

flip_vertical

Default FALSE. If TRUE, reverse the image row order before rasterization.

flip_horizontal

Default FALSE. If TRUE, reverse the image column order before rasterization.

layer_names

Default NULL. Optional output layer names. Defaults are "value" for a single layer, c("red", "green", "blue") for RGB output, and c("red", "green", "blue", "alpha") for RGBA output.

include_height

Default FALSE. If TRUE, append a final layer with raw height values from heightmap or cached rayshader scene metadata. When the output image resolution differs from the source heightmap, heights are bilinearly resampled onto the output raster grid.

heightmap

Default NULL. Optional source elevation matrix or spatial raster used when include_height = TRUE. Explicit heightmap overrides cached heightmap metadata. If omitted, rayshader reuses cached hillshade or scene heightmap metadata when available. Faceted cached plot_gg() scenes require an explicit heightmap for height export.

height_layer_name

Default "height". Name of the appended height layer when include_height = TRUE.

...

Deprecated argument spellings are accepted with a warning and forwarded to their current names.

Details

If no explicit extent is supplied and no cached extent metadata is available, spatialize_image() errors. CRS is resolved in this order: explicit crs, CRS carried by the explicit extent object, cached scene CRS, then empty CRS if none is available.

Cache fallback messages are disabled by default. Set options(rayshader.verbose_scene_cache = TRUE) to print when cached metadata is reused.

Examples

hillshade_img = montereybay_spatial |>
  sphere_shade() |>
  add_shadow(ray_shade(), 0.5)

hillshade_rast = spatialize_image(
  hillshade_img,
  extent = raster::extent(c(0, 1, 0, 1)),
  crs = "EPSG:4326"
)