How do I write an sf object as a shapefile to a file geodatabase using st_write?
I don't quite understand the 'dsn', 'layer', and 'driver' arguments of st_write in relation to file geodatabases.
For example, I've tried both of these and no luck
st_write(sf.object, dsn = "filepath/FileGeoDatabase.gbd",layer="name of output layer", driver="OpenFileGDB")
st_write(sf.object, dsn = "filepath/FileGeoDatabase.gbd",layer="name of output layer", driver="ESRI Shapefile")
A couple things here: first, you can't write a shapefile to an ESRI geodatabase as only feature classes and feature datasets can be stored in there. Second, you can't write to geodatabases via sf
; you can only read them.
You have a couple of options. You can save your data as a shapefile (or any other spatial data format) outside of the geodatabase with sf
:
library(sf)
## it will guess the driver automatically based on the .shp extension
st_write(sf.object, "data/my_shapefile.shp")
Or, if you absolutely need to write in a geodatabase, you can use the arcgisbinding
library, but note you will need to be using a machine with an active ArcGIS license. Hence, this is a no-go on GNU/Linux and Mac.
I can't verify that this works since I am on GNU/Linux, but it should be something along these lines:
library(arcgisbinding)
arc.write("data.gdb/fc", sf.object)
Details on the R-ArcGIS Bridge (and the arcgisbinding
package) can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With