Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

st_write refuses to overwrite layers in geopackage in R

Using the sf package in R, I am reading in a series of layers stored in a geopackage, filtering them and then re-saving them as a new geopackage.
When I set up my script I tested one of the layers and it worked fine, but when running then same code on the list of layers, it failed because the test layer already existed. The documentation for st_write has both append and overwrite arguments, but setting either to replace rather than append (append = FALSE or overwrite = TRUE) cause it to fail with the error

Error in st_write.sf(., dsn = gpkg_out, layer = layername, overwrite = TRUE) : unrecognized argument(s) overwrite

Error in st_write.sf(., dsn = gpkg_out, layer = layername, append = FALSE) : unrecognized argument(s) append

I updated my copy of the sf package, but it still failed with the same error. I found a work around by deleting the file in my file system, but this would be problematic if I were just trying to replace one layer.

Does anyone know if this is a known issue or a way to make it replace rather than append the layer?

my code:

library(sf)
library(tidyverse)

gpkg <- "D:/GIS/Sugar/SugarEC_hydropts.gpkg"  # name of existing geopackage

layers = st_layers(gpkg)$name
layers
# [1] "ec1520"  "ec17200" "ec224"   "ec2500" 
#  "ec271"   "ec4680"  "ec488"   "ec5540"  "ec8140"  "ec9860" 

gpkg_out <- "D:/GIS/Sugar/SugarEC_hydropts_wet.gpkg"

# the following works, but appends to layer if it exists
read_write <-  function (layername) {
      st_read(gpkg, layer = layername ) %>%
      filter(Water_Elev_ft >0) %>%
      st_write(dsn = gpkg_out, layer = layername)  # Appends
#   
#   This following versions fails though:
#     st_write(dsn = gpkg_out, layer = layername, overwrite = TRUE) 
#   or
#     st_write(dsn = gpkg_out, layer = layername, append = FALSE)

      invisible()
}

lapply(layers, read_write)
like image 506
Brian Fisher Avatar asked Jan 06 '20 22:01

Brian Fisher


2 Answers

It looks like you need to use delete_dsn = TRUE if the data may already exist. A warning will be thrown if the data doesn't already exist, but the file will still be written.

nc <-  read_sf(system.file("shape/nc.shp", package="sf"))

Reading layer `nc' from data source `/home/mrhellmann/R/x86_64-pc-linux-gnu-library/3.6/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
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs


st_write(nc, 'nc_test.shp', 'nc')

Writing layer `nc' to data source `nc_test.shp' using driver `ESRI Shapefile'
Writing 100 features with 14 fields and geometry type Multi Polygon.

## Error here, as it already exists.
st_write(nc, 'nc_test.shp', 'nc')

Dataset nc_test.shp already exists: remove first, use update=TRUE to append,
delete_layer=TRUE to delete layer, or delete_dsn=TRUE to remove the entire data source before writing.
Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  : 
  Dataset already exists.


# No error when delete_dsn = T
st_write(nc, 'nc_test.shp', 'nc', delete_dsn = T)

Deleting source `nc_test.shp' using driver `ESRI Shapefile'
Writing layer `nc' to data source `nc_test.shp' using driver `ESRI Shapefile'
Writing 100 features with 14 fields and geometry type Multi Polygon.


nc_saved <- read_sf('nc_test.shp')

identical(nc, nc_saved)
#[1] TRUE


like image 165
mrhellmann Avatar answered Sep 28 '22 15:09

mrhellmann


Use delete_layer = TRUE to overwrite/update layers within a GeoPackage.

like image 23
derelict Avatar answered Sep 28 '22 16:09

derelict