Write simple features object to file or database
st_write(obj, dsn, layer, ...) # S3 method for sfc st_write(obj, dsn, layer, ...) # S3 method for sf st_write( obj, dsn, layer = NULL, ..., driver = guess_driver_can_write(dsn), dataset_options = NULL, layer_options = NULL, quiet = FALSE, factorsAsCharacter = TRUE, append = NA, delete_dsn = FALSE, delete_layer = isFALSE(append), fid_column_name = NULL ) # S3 method for data.frame st_write(obj, dsn, layer = NULL, ...) write_sf(..., quiet = TRUE, append = FALSE, delete_layer = TRUE) # S4 method for PostgreSQLConnection,character,sf dbWriteTable( conn, name, value, ..., row.names = FALSE, overwrite = FALSE, append = FALSE, field.types = NULL, factorsAsCharacter = TRUE, binary = TRUE ) # S4 method for DBIObject,character,sf dbWriteTable( conn, name, value, ..., row.names = FALSE, overwrite = FALSE, append = FALSE, field.types = NULL, factorsAsCharacter = TRUE, binary = TRUE )
obj | object of class |
---|---|
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) or a Database Connection (currently official support is for RPostgreSQL connections) |
layer | layer name (varies by driver, may be a file name without extension); if layer is missing, the
basename of |
... | other arguments passed to dbWriteTable when |
driver | character; name of driver to be used; if missing and |
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 |
append | Append rows to existing table; default |
delete_dsn | logical; delete data source |
delete_layer | logical; delete layer |
fid_column_name | character, name of column with feature IDs; if specified, this column is no longer written as feature attribute. |
conn | DBIObject |
name | character vector of names (table names, fields, keywords). |
value | a data.frame. |
row.names | Add a |
overwrite | Will try to |
field.types | default |
binary | Send geometries serialized as Well-Known Binary (WKB);
if |
obj
, invisibly; in case obj
is of class sfc
,
it is returned as an sf
object.
Columns (variables) of a class not supported are dropped with a warning.
When updating an existing layer, records are appended to it if the updating object has the right variable names and types. If names don't match an error is raised. If types don't match, behaviour is undefined: GDAL may raise warnings or errors or fail silently.
When deleting layers or data sources is not successful, no error is emitted.
delete_dsn
and delete_layer
should be
handled with care; the former may erase complete directories or databases.
#> Reading layer `nc' from data source `/tmp/RtmpCdQsky/temp_libpath64f92385e079/sf/shape/nc.shp' using driver `ESRI Shapefile' #> Simple feature collection with 100 features and 14 fields #> geometry type: MULTIPOLYGON #> dimension: XY #> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 #> CRS: 4267#> Writing layer `nc' to data source `/tmp/Rtmp0dwyov/nc.shp' using driver `ESRI Shapefile' #> Writing 100 features with 14 fields and geometry type Multi Polygon.#> Deleting layer `nc' using driver `ESRI Shapefile' #> Writing layer `nc' to data source `/tmp/Rtmp0dwyov/nc.shp' using driver `ESRI Shapefile' #> Writing 100 features with 14 fields and geometry type Multi Polygon.data(meuse, package = "sp") # loads data.frame from sp meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) # writes X and Y as columns: st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_XY")#> Writing layer `meuse' to data source `/tmp/Rtmp0dwyov/meuse.csv' using driver `CSV' #> options: GEOMETRY=AS_XY #> Writing 155 features with 12 fields and geometry type Point.st_write(meuse_sf, paste0(tempdir(), "/", "meuse.csv"), layer_options = "GEOMETRY=AS_WKT", delete_dsn=TRUE) # overwrites#> Deleting source `/tmp/Rtmp0dwyov/meuse.csv' using driver `CSV' #> Writing layer `meuse' to data source `/tmp/Rtmp0dwyov/meuse.csv' using driver `CSV' #> options: GEOMETRY=AS_WKT #> Writing 155 features with 12 fields and geometry type Point.