编写循环以通过多边形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):无法为签名“列表”的函数“ coordinates <-”找到继承的方法

我发现不允许在这种情况下使用gIntersection函数,但是在如下所述的函数之上使用,在这种情况下,gIntersection会修剪点位置,但不会将变量保留在spacedatadataframe中:

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")

终于可以了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章