编写函数以打开不同文件夹中的多个shapefile R [英] Write a function to open multiple shapefiles in different folders R

查看:76
本文介绍了编写函数以打开不同文件夹中的多个shapefile R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数来打开多个shapefile(其中有46个),每个都存储在自己的文件夹中.我正在处理非营利组织的数据,这就是他们存储数据的方式,所以我正在编写代码以符合其数据存储惯例(我知道这并不理想).

I would like to write a function to open multiple shapefiles (there are 46 of them), each stored in its own folder. I'm working with a nonprofit's data and this is how they store it, so I'm writing the code to conform with their data storage practices (I know it's not ideal).

我试图编写此函数,但有时可以,有时不能.我在打开文件时遇到了问题(奇怪的是它可以与某些shapefile一起工作,而对于另一些我却出错了).另外,我不知道如何将文件分配给对象.

I tried to write this function, and it sometimes works, sometimes doesn't. I'm having problems opening the file (oddly with some shapefiles it works, and for others I get an error). In addition, I don't know how to assign the file to an object.

我首先获得wd中shapefile文件夹的列表:

I start by getting a list of the shapefile folders within the wd:

shapefiles <- list.dirs(path = "./StateShapefiles", full.names = FALSE, 
    recursive = FALSE)

然后我编写我的函数(文件和文件文件夹具有相同的名称):

Then I write my function (the file and the filefolder have the same name):

readshapefile <- function(x){
    statename <- deparse(substitute(x))
    filefolder <- paste(wd,statename, sep="/")
    assign(x,readOGR(dsn=filefolder,layer=statename))
}

我尝试调用该函数并得到此错误:

I try to call the function and get this error:

readshapefile(alabama)

Error in assign(x, readOGR(dsn = filefolder, layer = statename)) : 
    object 'alabama' not found

然后我想做类似的事情:

And then I would like to do something like:

for (x in shapefiles){
    readshapefile(x)
}

我不知道这是否行得通,因为我不知道前面的部分.

I can't tell if that will work because I can't figure out the earlier part.

我怀疑问题在于readOGR不会将对象作为参数;它只想要字符串,但我不知道.如果有人可以提供不涉及告诉组织移动文件的解决方案(不幸的是,那将不会发生),那就太好了!我总是可以写46行的readOGR,但这并不理想.

I suspect the issues is that readOGR won't take objects as arguments; it only wants strings, but I don't know. If anyone could help with a solution that doesn't involve telling the organization to move their files (unfortunately, that's just not going to happen), that would be great! I can always write 46 lines of readOGR, but that's not ideal.

推荐答案

没有示例数据,很难为您提供良好的答案.这可能更接近您的需求.

Without sample data, its hard to give you good answers. This may be closer to what you need.

shapefiles <- list.dirs(path = "./StateShapefiles", full.names = FALSE, 
    recursive = FALSE)

#function reads and returns object for given state
readshapefile <- function(statename){
    filefolder <- paste(getwd(), statename, sep="/")
    # display a message to help with debugging
    cat("Reading ", statename, " from ", filefolder, "\n")
    return(readOGR(dsn=filefolder, layer=statename))
}

#test the function, assigning to an object name
alabama_object <- readshapefile("alabama")

# lapply will call the function once for each state name 
# passing the state name as a parameter
all_shapes <- lapply(shapefiles, readshapefile)

这篇关于编写函数以打开不同文件夹中的多个shapefile R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆