An array in memory can be referenced by a GDAL data source. The DSN points directly at the memory of x, no copy is made.

mem(
  x,
  extent = NULL,
  projection = "",
  dimension = NULL,
  PIXELOFFSET = NULL,
  LINEOFFSET = NULL,
  BANDOFFSET = NULL
)

Arguments

x

an R array of numeric (double) type, see Details

extent

optional extent of the data in x,y c(xmin, xmax, ymin, ymax)

projection

projection string (optional, sets the SPATIALREFERENCE of the MEM driver since GDAL 3.7)

dimension

size in pixels c(PIXELS, LINES), defaults to dim(x), see Details

PIXELOFFSET

pixel offset

LINEOFFSET

line offset

BANDOFFSET

band offset

Value

character string, a DSN for use by GDAL

Details

The DSN is only valid while x is alive and unmodified: keep a reference to x for as long as the DSN is in use, or R's garbage collector may free or reuse that memory. For the same reason x must already be a double array (storage.mode(x) <- "double" to convert beforehand); mem() will not convert it for you, because converting makes a temporary copy whose memory does not survive this function returning.

This DSN only works in the current R session, with GDAL read and query tools (terra, sf, gdalcubes, vapour, gdalraster, etc.). Since GDAL 3.10, opening a MEM:::DATAPOINTER DSN is disabled by default for security reasons: set the configuration option GDAL_MEM_ENABLE_OPEN=YES (e.g. Sys.setenv(GDAL_MEM_ENABLE_OPEN = "YES")) to allow it.

dimension defaults to dim(x); for an R matrix that is (nrow, ncol) used as (PIXELS, LINES), so GDAL scanlines run down the columns of the R matrix (memory order is preserved, orientation is transposed relative to the on-screen orientation of graphics::image()).

Examples

mem(volcano)
#> [1] "MEM:::DATAPOINTER=94563368060224,PIXELS=87,LINES=61,BANDS=1,DATATYPE=Float64,GEOTRANSFORM=0/1/0/61/0/-1,PIXELOFFSET=0,LINEOFFSET=0,BANDOFFSET=42456"

m <- matrix(c(0, 0, 0, 1), 5L, 4L)
mem(m)
#> [1] "MEM:::DATAPOINTER=94563317861840,PIXELS=5,LINES=4,BANDS=1,DATATYPE=Float64,GEOTRANSFORM=0/1/0/4/0/-1,PIXELOFFSET=0,LINEOFFSET=0,BANDOFFSET=160"