sf (version 0.5-2)

st_write: Write simple features object to file or database

Description

Write simple features object to file or database

Write simple feature table to a spatial database

Usage

st_write(obj, dsn, layer = file_path_sans_ext(basename(dsn)),
  driver = guess_driver_can_write(dsn), ..., dataset_options = NULL,
  layer_options = NULL, quiet = FALSE, factorsAsCharacter = TRUE,
  update = driver %in% db_drivers, delete_dsn = FALSE,
  delete_layer = FALSE)

write_sf(..., quiet = TRUE, delete_layer = TRUE)

st_write_db(conn = NULL, obj, table = deparse(substitute(obj)), geom_name = "wkb_geometry", ..., drop = FALSE, debug = FALSE, binary = TRUE, append = FALSE)

Arguments

obj

object of class sf or sfc

dsn

data source name (interpretation varies by driver - for some drivers, dsn is a file name, but may also be a folder or contain a database name)

layer

layer name (varies by driver, may be a file name without extension); if layer is missing, the basename of dsn is taken.

driver

character; driver name to be used, if missing, a driver name is guessed from dsn; st_drivers() returns the drivers that are available with their properties; links to full driver documentation are found at http://www.gdal.org/ogr_formats.html.

...

ignored for st_write, for st_write_db arguments passed on to dbWriteTable

dataset_options

character; driver dependent dataset creation options; multiple options supported.

layer_options

character; driver dependent layer creation options; multiple options supported.

quiet

logical; suppress info on name, driver, size and spatial reference

factorsAsCharacter

logical; convert factor objects into character strings (default), else into numbers by as.numeric.

update

logical; FALSE by default for single-layer drivers but TRUE by default for database drivers as defined by db_drivers. For database-type drivers (e.g. GPKG) TRUE values will make GDAL try to update (append to) the existing data source, e.g. adding a table to an existing database.

delete_dsn

logical; delete data source dsn before attempting to write?

delete_layer

logical; delete layer layer before attempting to write? (not yet implemented)

conn

open database connection

table

character; name for the table in the database, possibly of length 2, c("schema", "name"); default schema is public

geom_name

name of the geometry column in the database

drop

logical; should table be dropped first?

debug

logical; print SQL statements to screen before executing them.

binary

logical; use well-known-binary for transfer?

append

logical; append to table? (NOTE: experimental, might not work)

Details

columns (variables) of a class not supported are dropped with a warning. When deleting layers or data sources is not successful, no error is emitted. delete_dsn and delete_layers should be handled with care; the former may erase complete directories or databases.

st_write_db was written with help of Josh London, see https://github.com/r-spatial/sf/issues/285

See Also

st_drivers

Examples

Run this code
# NOT RUN {
nc = st_read(system.file("shape/nc.shp", package="sf"))
st_write(nc, "nc.shp")
st_write(nc, "nc.shp", delete_layer = TRUE) # overwrites
data(meuse, package = "sp") # loads data.frame from sp
meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992)
st_write(meuse_sf, "meuse.csv", layer_options = "GEOMETRY=AS_XY") # writes X and Y as columns
st_write(meuse_sf, "meuse.csv", layer_options = "GEOMETRY=AS_WKT", delete_dsn=TRUE) # overwrites
# }
# NOT RUN {
library(sp)
example(meuse, ask = FALSE, echo = FALSE)
st_write(st_as_sf(meuse), "PG:dbname=postgis", "meuse_sf",
    layer_options = c("OVERWRITE=yes", "LAUNDER=true"))
demo(nc, ask = FALSE)
st_write(nc, "PG:dbname=postgis", "sids", layer_options = "OVERWRITE=true")
# }
# NOT RUN {
  library(sp)
  data(meuse)
  sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992)
  library(RPostgreSQL) 
  conn = dbConnect(PostgreSQL(), dbname = "postgis")
  st_write_db(conn, sf, "meuse_tbl", drop = FALSE)
# }

Run the code above in your browser using DataLab