resize_spatial() mirrors the ergonomics of rayshader::resize_matrix(), but returns a terra::SpatRaster with extent and CRS intact.

resize_spatial(
  x,
  scale = 1,
  width = NULL,
  height = NULL,
  method_down = "mean",
  method_up = "bilinear",
  aggregate_if_possible = TRUE,
  tol = 1e-08,
  threads = FALSE,
  filename = "",
  overwrite = FALSE,
  write_args = list(),
  ...
)

Arguments

x

A terra::SpatRaster.

scale

Default 1. Relative output size used only when both width and height are NULL.

width

Default NULL. Target number of columns. When supplied alone, height is derived while preserving the current aspect ratio.

height

Default NULL. Target number of rows. When supplied alone, width is derived while preserving the current aspect ratio.

method_down

Default "mean". Method used when reducing resolution. Integer-factor reductions use terra::aggregate() when possible; other reductions use terra::resample(). The default becomes "modal" when any input layer is categorical.

method_up

Default "bilinear". Method passed to terra::resample() when increasing resolution. The default becomes "near" when any input layer is categorical.

aggregate_if_possible

Default TRUE. If TRUE, use terra::aggregate() for integer-factor downsampling when method_down is an aggregation method.

tol

Default 1e-8. Tolerance for deciding whether an implied aggregation factor is effectively an integer.

threads

Default FALSE. Passed to terra::resample().

filename

Default "". Optional output filename.

overwrite

Default FALSE. Whether to overwrite filename.

write_args

Default list(). Named arguments passed only to terra::writeRaster() when filename is supplied.

...

Arguments passed only to the resize operation that is used: terra::aggregate() for aggregation or terra::resample() for resampling.

Details

Downsampling and upsampling are handled separately because the best visual method is often different in each direction. Integer-factor reductions can use terra::aggregate(); everything else uses terra::resample() onto a template raster with the requested geometry.