vector_to_MEM: Create a GDAL in-memory dataset from R data without copying

View source: R/gdal_helpers.R

vector_to_MEMR Documentation

Create a GDAL in-memory dataset from R data without copying

Description

vector_to_MEM() creates a GDAL MEM dataset that references pixel data in an existing R vector. It returns an object of class GDALRaster for a writable in-memory dataset without copying the source data. The underlying R object is protected from garbage collection until the returned dataset is closed. GDAL MEM datasets support most kinds of auxiliary information including metadata, coordinate systems, georeferencing, color interpretation, nodata, color tables and all pixel data types (see Details).

Usage

vector_to_MEM(
  data,
  xsize,
  ysize,
  nbands = 1L,
  gt = NULL,
  bbox = NULL,
  srs = NULL
)

Arguments

data

An R vector of type "double", "integer", "raw" or "complex", containing pixel values to be exposed as a GDAL in-memory raster. The pixels must be arranged in left-to-right, top-to-bottom order interleaved by band. length(data) must equal xsize * ysize * nbands.

xsize

Integer value giving the number of raster columns.

ysize

Integer value giving the number of raster rows.

nbands

Integer value giving the number of raster bands.

gt

A numeric vector of length six containing the affine geotransform for the raster. Defaults to c(0, 1, 0, 0, 0, 1) if neither gt nor bbox are given.

bbox

A numeric vector of length four containing the raster bounding box geospatial coordinates (c(xmin, ymin, xmax, ymax)). Ignored if gt is given.

srs

Optional character string containing the raster spatial reference coordinate system as a WKT string. epsg_to_wkt() or srs_to_wkt() can be used to convert from other formats to WKT if necessary.

Details

The returned dataset is open with write access. Methods of the GDALRaster object can be called to modify dataset and band properties, e.g., to set nodata values, metadata items, band descriptions, color tables, etc. The original R vector will be modified in place if the object's ⁠$write()⁠ method is used.

The MEM dataset will have a GDAL data type matching the type of the input vector:

R vector type GDAL raster type
double Float64
integer Int32
raw UInt8 (Byte in GDAL < 3.13)
complex CFloat64

Value

An object of class GDALRaster providing a GDAL MEM dataset with write access pointing to the underlying C array for data. The R object referenced by data is protected from garbage collection during the lifetime of the returned dataset, i.e., until its ⁠$close()⁠ is called or the dataset object itself is garbage collected. An error is raised if creation of the MEM dataset fails.

Note

The ⁠$close()⁠ method should be called when the GDALRaster object is no longer needed so that resources can be freed. MEM datasets cannot be re-opened once the object's ⁠$close()⁠ method has been called.

See Also

GDALRaster-class

Examples

v <- sample(0:255, 50, replace = TRUE)
(ds_mem <- vector_to_MEM(v, xsize = 10, ysize = 5))

all((ds_mem$read(1, 0, 0, 10, 5, 10, 5) == v))

ds_mem$write(1, 0, 0, 10, 5, (v * -1))
print(v)

ds_mem$close()

gdalraster documentation built on March 25, 2026, 9:07 a.m.