Search code examples
rpostgresqlpostgisr-sf

Set geometry column name in sf::st_write()


As of sf version 0.9.0, st_write_db() and st_read_db() got defunct. The functions had the nice argument geom_name = 'wkb_geometry' which allowed a user to set the name of the simple feature column of the newly created table.

Can I still do this somehow with st_write() or do I have to do this in a separate call? E.g. for Postgres: ALTER TABLE x REANAME COLUMN geometry TO geom;

Maybe with the layer_options = argument? However I don't know where to look up its possibilities?


  • Local machine

    • sf_0.9-7 with GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0.
  • Server

    • PostgreSQL 9.6.20
    • PostGIS POSTGIS="2.3.3 r15473" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.4" LIBJSON="0.12.1" TOPOLOGY RASTER

Solution

  • sf uses GDAL to read and write data. The GDAL documentation contains for every driver the list of possible layer creation options. For Postgres this can be found under https://gdal.org/drivers/vector/pg.html#layer-creation-options

    Based on this layer_options="GEOMETRY_NAME=wkb_geometry" should do the trick for you.