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)