为多个 shapefile 保存 writeOGR

use*_*782 3 r spatial shapefile

我想在循环中使用 writeOGR 将 shapefile 保存到文件夹。我无法弄清楚如何使用名称来保存实际文件。

假设你有这个代码:

require(sp)
require(rgdal)

for (i in listafiles){
ffile <-read.csv(i)    #reads file
###do some stuff on the file and obtain a polygon sp_poly

sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))
sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data=data.frame(ID=1))
##here comes the problem
writeOGR(sp_poly_df, dsn, layer, driver="ESRI Shapefile")

}
Run Code Online (Sandbox Code Playgroud)

我希望 writeOGR 将每个生成的 shapefile 保存在一个单独的文件夹中,并带有文件名。例如,当 'i' 是 'school17.csv' 时,writeOGR 将创建子文件夹 .\school17\ 并且将 3 个 shapefile 命名为:(school17.dbf | school17.shp | school17.shx)

我无法弄清楚 dsn 和图层参数是如何工作的。

提前致谢,开发

mds*_*ner 5

只需将 dsn 和 layer 设置为您拥有的名称:

{
  ### ... we are in the loop
  dsn <- layer <- gsub(".csv","",i)
  writeOGR(sp_poly_df, dsn, layer, driver="ESRI Shapefile")
}
## E.g. 
list.files()
## [1] "a" "b" "c"
list.files(list.files()[1])
##[1] "a.dbf" "a.prj" "a.shp" "a.shx"
Run Code Online (Sandbox Code Playgroud)

但请记住,每次创建“shapefile”时,小猫都会死亡。