编写一个循环以通过多边形 shapefile 剪切多个空间数据帧

Writing a loop for cliping multiple spatial data frame by a polygon shapefile

我已经有一些名为 201101.dbf 到 201412.dbf 的 .dbf 文件,并且研究区域 shapefile 已准备就绪。现在,寻找一种通过 shapefile 剪辑(子集)dbf 文件的方法。

#Loading libraries
library(foreign)
library(maptools)
library(rgdal)
library(rgeos)

#set working directory 
setwd('D:/Data1")

#Load the dbf files
Data=dir(,pattern="dbf")

#load study area shape file 
studyarea=readShapeSpatial("D:/Data1/study-area.shp")

#Setting the projection for study area
proje4string(studyarea)=CRS("+init=epsg:32639")


#Looping
for(i in 1:length(Data)){
Data2=read.dbf(Data[i])

#setting coordinates for dbf files
coordinates(Data2)=~longitude+latitude

#Setting the projection for dbf files
proj4string(Data2)=CRS(proj4string(studyarea))

#Clipping the spatial data frames 
Clip-data=gIntersection(Data2,studyarea,byid=TRUE)

#Writing clipped spatial data frames with the names of original dataframes
write.dbf(Clip-Data,Data2=paste("D:/data", paste("Clip-data",Data[i]), sep="/"))}

脚本出现以下错误!

错误(函数(类,fdef,mtable): 无法为签名“"list"”

的函数“坐标<-”找到继承方法

我发现我不允许在这种情况下使用 gIntersection 函数,但是在下面写的函数上,在这种情况下 gIntersection 剪辑点位置但它不保留空间数据帧中的变量:

Clip-data=Data2[studyarea, ]

然后我完成代码的循环部分如下:

#Looping 

for(i in 1:length(Data)){
#reading dataframes
Data2=read.dbf(Data[i])

#Accessing Filename of dataframes in List
Filename=Data[i]


#setting coordinates for the dataframes 
coordinates(Data2)=~longitude+latitude

#Setting the projection 
proj4string(Data2)=CRS(proj4string(studyarea))

#Clipping the spatial data frames 
clipdata=Data2[studyarea, ]

#Defining dsn and layer in writeORG and assigning the original names of dataframes on new derived files

dsn = layer = gsub(".dbf","",Filename)

#writing ESRI Shapefile (spatialpointsdataframe) using Rgdal package. 
writeOGR(clipdata, dsn, layer, driver="ESRI Shapefile")

它终于起作用了。