使用readOGR循环读取shapefile

时间:2018-05-08 03:06:29

标签: r gis shapefile sp rgdal

我有500个形状文件,包含点(lon / lat)数据和边框缓冲区。

我还有以下代码来读取每个shapefile,将它与缓冲区连接,计算一些值,然后将这些值添加到数据框的列中。

这是我的代码:

    path="/Users/fun/Drought shapefiles/"


 file.names <- list.files(path, pattern="*.shp", full.names=T, recursive=FALSE)

#Now automate this process:
for (m in 1:500){
    dsn=path

    ##Load the buffer and the map:
Buffer <- readOGR("/Users/fun/Degrees Buffer Projections", "Germania buffer 10km")
Map <- readOGR("/Users/fun/Drought shapefiles/", m)

#Now convert the map into a dataframe and intersect it:
over(Map,as(Buffer,"SpatialPolygons"))
nrow(Map)
pts = Shapefile[!is.na(over(Shapefile,as(Buffer,"SpatialPolygons"))),]
nrow(pts)
head(pts@data)
plot(pts)

#Calculate the mean drought value across the border, and assign to a column:
drought_values <- pts$pdsi_440
drought.data[[2]][[m]] <- mean(drought_values)
}

我的问题涉及以下代码行:

Map <- readOGR("/Users/fun/Drought shapefiles/", m)

我似乎没有在这里编写正确的代码,以便遍历500个文件。我究竟做错了什么。

作为参考,文件的名称如下:

cru_drought_86_shape.shp
cru_drought_87_shape.shp
cru_drought_88_shape.shp

等等......

如果你想看到两个shapefile,这里是: Drought 303 Drought 304

1 个答案:

答案 0 :(得分:0)

问题是你的for循环中你传递的数字是1:500(m)。如果要执行此操作,则需要使用该数字来索引文件名列表。由于您使用了full.names = T参数,因此您只需传递完整的文件名:

import imaplib

import email

conn = imaplib.IMAP4()

conn.login("username", "password")

status, messages = conn.select('INBOX')    

if status != "OK":

        print ("Incorrect mail box")

        exit()

print (messages)