st_write 拒绝在 R 中覆盖地理包中的图层
st_write refuses to overwrite layers in geopackage in R
使用 R 中的 sf
包,我正在读取存储在地理包中的一系列图层,过滤它们,然后将它们重新保存为新的地理包。
当我设置我的脚本时,我测试了其中一个层并且它工作正常,但是当 运行 然后层列表上的相同代码时,它失败了,因为测试层已经存在。 st_write
的文档同时包含追加和覆盖参数,但设置为替换而不是追加(append = FALSE
或 overwrite = TRUE
)会导致失败并显示错误
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
我更新了 sf 包的副本,但它仍然失败并出现相同的错误。我通过删除文件系统中的文件找到了解决方法,但如果我只是想替换一层,这就会有问题。
有谁知道这是否是一个已知问题,或者是否有办法替换而不是附加图层?
我的代码:
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)
如果数据可能已经存在,您似乎需要使用 delete_dsn = TRUE
。如果数据不存在,将抛出警告,但仍会写入文件。
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
在 GeoPackage 中使用 delete_layer = TRUE
到 overwrite/update 层。
使用 R 中的 sf
包,我正在读取存储在地理包中的一系列图层,过滤它们,然后将它们重新保存为新的地理包。
当我设置我的脚本时,我测试了其中一个层并且它工作正常,但是当 运行 然后层列表上的相同代码时,它失败了,因为测试层已经存在。 st_write
的文档同时包含追加和覆盖参数,但设置为替换而不是追加(append = FALSE
或 overwrite = TRUE
)会导致失败并显示错误
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
我更新了 sf 包的副本,但它仍然失败并出现相同的错误。我通过删除文件系统中的文件找到了解决方法,但如果我只是想替换一层,这就会有问题。
有谁知道这是否是一个已知问题,或者是否有办法替换而不是附加图层?
我的代码:
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)
如果数据可能已经存在,您似乎需要使用 delete_dsn = TRUE
。如果数据不存在,将抛出警告,但仍会写入文件。
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
在 GeoPackage 中使用 delete_layer = TRUE
到 overwrite/update 层。