【问题标题】:looping through folders while performing task在执行任务时循环浏览文件夹
【发布时间】:2013-12-11 04:10:45
【问题描述】:

我正在尝试遍历目录中的文件夹,同时读取文件并将其分配给R 中的变量。

我要分配给变量的文件是shapefiles,所以我使用rgdal 包中的函数readOGR。目的是稍后合并属于特定物种的所有 shapefile。目录的层次结构/结构是type1 > species > idsshapefiles 看起来像 id.shp 等。

可以在herehere下载示例shapefile

#code
setwd("~/type1/")

#Extract ids belonging to $species. Later use in readOCR function

sp_id <- function(species){
 wd = "~/type1/"
 list_shp <- list.files(path=paste(wd,species,sep='/'), full.names = F, recursive = F, include.dirs = F)
 vec <- character() 

  for (shp in list_shp){
   y <- unlist(strsplit(shp, '\\.', perl=T))
   vec <- unique(c(vec,y[1]))
  }

 #"1905" "4279"

#Extract dirs for where to perform readOCR function

  list_dir <- list.dirs(path=paste(wd,species,sep='/'), full.names = F, recursive = F)

   for (id in list_dir){
    setwd(id)
    print(getwd())
   }

#"~/type1/speciesX1/1905"
#"~/type1/speciesX1/4279"

    for (i in vec){
     assign(paste("", i, sep=""), readOGR(".", i))
    break
    }
 }

sp_id('speciesX1')

[1] "~/type1/speciesX1/1905"
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "1905"
with 10 features and 3 fields
Feature type: wkbPolygon with 2 dimensions
[1] "~/type1/speciesX1/4279"
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 
Cannot open layer

问题是代码仅在dirs 之一中为一个shapefile 执行readOGR,似乎再次更改目录但不执行最后一个readOGR

【问题讨论】:

    标签: r for-loop directory shapefile listdir


    【解决方案1】:

    您的功能似乎在许多不同的部分被破坏,但您可以这样做:

    library(rgdal)
    
    setwd("~/type1/")
    species <- 'speciesX1'
    list_shp <- list.files(path=species, pattern="*.shp", full.names = TRUE,
                           recursive = TRUE, include.dirs = FALSE)
    shp_objects <- lapply(list_shp, function(x) {readOGR(dsn=x, 
                                                         layer=ogrListLayers(x))})
    

    这将为您提供SpatialPolygonDataframe 对象的列表,然后您可以合并这些对象。

    【讨论】:

    • 太棒了,谢谢!必须记住更频繁地使用apply 函数。只是为了学习的情况,能告诉我我的功能哪里坏了吗?
    • 1.您的函数名称 sp_id 与您稍后调用的名称不同 (sp_mrgid('speciesX1')) 2. 第 25 行的 setwd(id) 将导致错误(据我所见,您从未将 wd 设置为 species 哪个子文件夹您正在尝试访问这些只是快速观察,没有尝试认真调试。
    • 这是一个 gist 演示合并。
    • 为评论和合并演示干杯。啊,是的,这个名字只是写线程时的一个错字。我倾向于从具有明显缺点的代码中更改名称。如果我正确,您提到的另一件事不是错误。 list_dir &lt;- list.dirs(path=paste(wd,species,sep='/'), full.names = F, recursive = F) 其中species(函数参数)是子文件夹。正如我所指出的,这在代码中有效......
    【解决方案2】:

    我无法让ogrInfo() 读取那些出现缺少层错误的文件)。这提供了一种读取和获取某些属性的方法。 (Mac 会采用重复的目录名称并在它们后面附加“(n)”,因此名称相同但不同的文件是 iho.zip 和 iho.zip:

    library(sp)
    
    ca3 = readShapeSpatial("~/Downloads/iho/iho.shp")
    ca3 = readShapeSpatial("~/Downloads/iho(2)/iho.shp")
    
    > attributes(ca3)$data
                                   name   id mrgid
    0 Mediterranean Sea - Western Basin 28Aa  4279
    1               Strait of Gibraltar  28a  3346
    2                       Alboran Sea  28b  3324
    3                      Balearic Sea  28c  3322
    4                      Ligurian Sea  28d  3363
    5                    Tyrrhenian Sea  28e  3386
    > attributes(ca2)$data
                                   name   id mrgid
    0 Mediterranean Sea - Western Basin 28Aa  4279
    1 Mediterranean Sea - Eastern Basin 28Bb  4280
    2               Strait of Gibraltar  28a  3346
    3                       Alboran Sea  28b  3324
    4                      Balearic Sea  28c  3322
    5                      Ligurian Sea  28d  3363
    6                    Tyrrhenian Sea  28e  3386
    7                      Adriatic Sea  28g  3314
    8                        Ionian Sea  28f  3351
    9                        Aegean Sea  28h  3315
    

    【讨论】:

    • 为评论干杯。你能解释一下这如何帮助我解决上述问题吗?
    • 好吧,我同意您的 shp 文件来源可能存在问题。很难说出你的错误到底是什么。我感觉到这些不是您的真实文件,并且您没有制作可重现的示例。如果这些是您使用的实际文件,而不是容易获得的用于说明的文件,并且没有人提供更好的方法,那么也许您可以访问 R-SIG-GEO 邮件列表。
    • 啊,好的。我得到了ogrInfo() 的工作...例如ogrInfo(".", "1905")(重命名以匹配 mrgid)
    • 我认为问题在于我的功能本身。我想我需要nextbreak 来重启循环(?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多