r - 为多个 shapefile 保存 writeOGR

标签 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")

}

我希望 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/

相关文章:

在 ggplot2 中旋转 x 轴标签和更改主题

MySQL 地理空间函数来识别矩形是否在非均匀的四边形多边形中有点,反之亦然

python - 使用 shapefile 或 geopandas 绘制蒙面南极洲

r - 尝试在具有属性的 tmap shapefile 中绘制

java - 带有 openmap 的 shapefile

r - R/shiny 中的 selectInput 框 - 多个值具有相同标签的选择列表

r - 在R中使用混合类型将字符数字转换为嵌套列表中的数字

r - 设置绘图热图的 x 和 y 范围

entity-framework - .NET 4.5 Beta DbGeography NotImplementedException

r - 如何在 R 中堆叠光栅文件?