【问题标题】:writeOGR saving for multiple shapefileswriteOGR 保存多个 shapefile
【发布时间】:2014-10-26 19:32:34
【问题描述】:

我想在循环中使用 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 个 shapefile 命名为:(school17.dbf | school17.shp | school17.shx)

我不知道 dsn 和 layer 参数是如何工作的。

提前致谢,开发者

【问题讨论】:

  • 将 dsn 和层设置为 gsub(".csv","",i)。 dsn 将是一个文件夹

标签: r spatial shapefile


【解决方案1】:

只需将 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”时,小猫都会死亡。

【讨论】:

  • 谢谢,成功了。关于 gdal 包还有很多东西要学习。以牺牲小猫为代价,因为这是必需的。
猜你喜欢
  • 2018-09-24
  • 1970-01-01
  • 2020-12-17
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2022-06-27
相关资源
最近更新 更多