
Sends R raster to a PostGIS database table.
pgWriteRast(
conn,
name,
raster,
bit.depth = NULL,
blocks = NULL,
constraints = TRUE,
overwrite = FALSE,
append = FALSE
)
TRUE for successful import.
A connection object to a PostgreSQL database
A character string specifying a PostgreSQL schema (if
necessary) and table name to hold the
raster (e.g., name = c("schema","table")
)
An R RasterLayer
, RasterBrick
, or RasterStack
from
raster package; a SpatialGrid*
or SpatialPixels*
from sp package
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)
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.
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.
Whether to overwrite the existing table (name
).
Whether to append to the existing table (name
).
David Bucklin david.bucklin@gmail.com
RasterLayer 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
package are converted to raster
package objects prior to insert.
If blocks = NULL
the attempted block size will be around
10,000 pixels in size (100 x 100 cells), so number of blocks will
vary by raster size. 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.
Function follows process from http://postgis.net/docs/using_raster_dataman.html#RT_Creating_Rasters.
if (FALSE) {
pgWriteRast(conn, c("schema", "tablename"), raster_name)
# basic test
r <- raster::raster(nrows=180, ncols=360, xmn=-180, xmx=180,
ymn=-90, ymx=90, vals=1)
pgWriteRast(conn, c("schema", "test"), raster = r,
bit.depth = "2BUI", overwrite = TRUE)
}
Run the code above in your browser using DataLab