我想在循环中使用 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")
}
我希望 writeOGR 将每个生成的 shapefile 保存在一个单独的文件夹中,并带有文件名。例如,当“i”为“school17.csv”时,writeOGR 将创建子文件夹 .\school17\并将 3 个形状文件命名为:(school17.dbf | school17.shp | school17.shx)
我不明白 dsn 和 layer 参数是如何工作的。
提前致谢,开发人员
最佳答案
只需将 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"
但请记住,每次您创建“shapefile”时,小猫都会死去。
关于r - 为多个 shapefile 保存 writeOGR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25625009/