在R中使用writeOGR()将多层写入GeoPackage?


11

我试图在R中的同一GeoPackage中写入多个图层,但出现错误Creation of output file failed。我试图搜索使用RGDAL读写.gpkg文件的文档,例如,弄清楚是否writeOGR()实际上支持多层,但收效甚微。如果可能的话,这怎么可能呢?最小的工作示例:

library(sp)
library(maptools)
library(rgdal)

data(wrld_simpl)

norway <- wrld_simpl[wrld_simpl$NAME == "Norway", ]
sweden <- wrld_simpl[wrld_simpl$NAME == "Sweden", ]

file <- tempfile("scandinavia", fileext = c(".gpkg"))

writeOGR(norway, dsn = file, layer = "norway", driver = "GPKG")
writeOGR(sweden, dsn = file, layer = "sweden", driver = "GPKG")

ogrListLayers(file)

显然,有一个可以完成技巧的ogr2ogr shell命令(hat tip mdsumner),我可以将其包装在R函数中。但是,如果writeOGR()和/或st_write()这将是整齐的sf包有这个内置的。我想这取决于GDAL的layer_options,但似乎没有要在GPKG追加型选项GDAL


我可以编写一个简单的包装函数,st_write()但可以提供本机支持,sf或者rgdal会更好。


不可能afaik。尝试使用sf,我也会很高兴进行探索-一件事比rgdal更容易修复
mdsumner

1
sf中的@mdsumner st_write()给出相同的结果。我认为GDAL的图层创建选项中缺少附加选项是writeOGR()和st_write()问题的根源。
eivindhammers

Answers:


10

您可以使用append标记sf::st_write()

library(sf)

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

st_write(nc,     "nc.gpkg", "nc")
st_write(storms, "nc.gpkg", "storms", append = TRUE)

st_layers("nc.gpkg")
## Driver: GPKG 
## Available layers:
##   layer_name  geometry_type features fields
## 1         nc  Multi Polygon      100     14
## 2     storms 3D Line String       71      0
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.