| vector_to_MEM | R Documentation |
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).
vector_to_MEM(
data,
xsize,
ysize,
nbands = 1L,
gt = NULL,
bbox = NULL,
srs = NULL
)
data |
An R vector of type |
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 |
bbox |
A numeric vector of length four containing the raster bounding
box geospatial coordinates ( |
srs |
Optional character string containing the raster spatial reference
coordinate system as a WKT string. |
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 |
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.
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.
GDALRaster-class
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.