Skip to contents

Sends R raster to a PostGIS database table.

Usage

pgWriteRast(
  conn,
  name,
  raster,
  bit.depth = NULL,
  blocks = NULL,
  constraints = TRUE,
  overwrite = FALSE,
  append = FALSE,
  progress = TRUE
)

Arguments

conn

A connection object to a PostgreSQL database.

name

A character string specifying a PostgreSQL schema in the database (if necessary) and table name to hold the raster (e.g., name = c("schema","table")).

raster

An terra SpatRaster; objects from the raster package (RasterLayer, RasterBrick, or RasterStack); a SpatialGrid* or SpatialPixels* from sp package.

bit.depth

The bit depth of the raster. Will be set to 32-bit (unsigned int, signed int, or float, depending on the data) if left null, but can be specified (as character) as one of the PostGIS pixel types (see http://postgis.net/docs/RT_ST_BandPixelType.html).

blocks

Optional desired number of blocks (tiles) to split the raster into in the resulting PostGIS table. This should be specified as a one or two-length (columns, rows) integer vector. See also 'Details'.

constraints

Whether to create constraints from raster data. Recommended to leave TRUE unless applying constraints manually (see http://postgis.net/docs/RT_AddRasterConstraints.html). Note that constraint notices may print to the console, depending on the PostgreSQL server settings.

overwrite

Whether to overwrite the existing table (name).

append

Whether to append to the existing table (name).

progress

whether to show a progress bar (TRUE by default). The progress bar mark the progress of writing blocks into the database.

Value

TRUE for successful import.

Details

SpatRaster band names will be stored in an array in the column "band_names", which will be restored in R when imported with the function pgGetRast.

Rasters from the sp and raster packages are converted to terra objects prior to insert.

If blocks = NULL, the number of block will vary by raster size, with a default value of 100 copies of the data in the memory at any point in time. If a specified number of blocks is desired, set blocks to a one or two-length integer vector. Note that fewer, larger blocks generally results in faster write times.

Author

David Bucklin david.bucklin@gmail.com and Adrián Cidre González adrian.cidre@gmail.com

Examples

if (FALSE) { # \dontrun{
pgWriteRast(conn, c("schema", "tablename"), raster_name)

# basic test
r <- terra::rast(nrows=180, ncols=360, xmin=-180, xmax=180,
    ymin=-90, ymax=90, vals=1)
pgWriteRast(conn, c("schema", "test"), raster = r,
    bit.depth = "2BUI", overwrite = TRUE)
} # }