【发布时间】:2018-07-04 01:23:44
【问题描述】:
我应该先说我在 R 中的循环很糟糕,我认识到这个问题与这篇文章 Batch convert .csv files to .shp files in R 相似。但是,我无法发表评论以查看该用户是否在此线程上找到了解决方案,因为我没有足够的声望点并且建议的解决方案对我没有帮助。
我有多个包含动物 GPS 点的 .csv 文件。我想为空间分析创建多个 shapefile。我尝试创建一个循环来读取 .csv 文件,从具有纬度和经度的 csv 文件制作空间数据,将空间数据帧转换为 UTM 投影,以便我可以计算距离,然后将文件写入 shapefile。这是我尝试过的循环,但我认为我在 out 和 utm_out 中的索引不正确。
这是一些测试数据;记得在写 .csv 之前设置你的工作目录:
#write sample .csv for animal 1
ID1<-rep(1, 3)
Latitude1<-c(25.48146, 25.49211, 25.47954)
Longitude1<-c(-84.66530, -84.64892, -84.69765)
df1<-data.frame(ID1, Latitude1, Longitude1)
colnames(df1)<-c("ID", "Latitude", "Longitude")
write.csv(df1, "df1.csv", row.names = FALSE)
#write sample .csv for animal 2
ID2<-rep(2, 2)
Latitude2<-c(28.48146, 28.49211)
Longitude2<-c(-88.66530, -88.64892)
df2<-data.frame(ID2, Latitude2, Longitude2)
colnames(df2)<-c("ID", "Latitude", "Longitude")
write.csv(df2, "df2.csv", row.names = FALSE)
#create a list of file names in my working directory where .csv files are located
all.files<-list.files(pattern="\\.csv")
#set the points geographic coordinate system
points_crs <- crs("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
#write a loop to read in each file, specify the file as a spatial points data frame & project then write as .shp file
for(i in 1:length(all.files)) {
file<-read.csv(all.files[i], header=TRUE, sep = ",", stringsAsFactors = FALSE) #read files
coords<-file[c("Longitude", "Latitude")] #select coordinates from the file
out<-SpatialPointsDataFrame(
coords = coords,
file,
proj4string = points_crs) #create Spatial Points Data Frame and specify geographic coordinate system = points_crs
utm_out<-spTransform(out, crs("+init=epsg:32616")) #transform to UTM
writeOGR(utm_out[[i]],dsn="C:/Users/Desktop/Shapefile_test",
"*", driver="ESRI Shapefile")
}
这给了我以下信息:错误:inherits(obj, "Spatial") is not TRUE
我也试过了:
for(i in 1:length(all.files)) {
file<-read.csv(all.files[i], header=TRUE, sep = ",", stringsAsFactors = FALSE)
coords<-file[c("Longitude", "Latitude")]
out<-SpatialPointsDataFrame(
coords = coords,
file,
proj4string = points_crs)
utm_out<-spTransform(out[[i]], crs("+init=epsg:32616"))
writeOGR(utm_out[[i]],dsn="C:/Users/Desktop/Shapefile_test", "*", driver="ESRI Shapefile")
}
这会产生: (function (classes, fdef, mtable) 中的错误: 无法找到签名“整数”、“CRS”的函数“spTransform”的继承方法
理想情况下,输出文件将类似于“animal1.shp”“animal2.shp”...等。
或者,我确实将动物 1 和 2 放在一个文件中。我可以引入这个文件,设置投影,然后为每个唯一的动物 ID 创建多个子集,并将子集写入 .shp 文件,但是我在对空间数据进行子集化时遇到问题,我认为这是另一个线程的主题.
提前感谢您的帮助。
【问题讨论】:
-
解决此类问题的最佳方法是摆脱循环,并在单个文件上进行测试,直到一切正常。然后将您的代码包装在一个函数中,或者在最后编写循环。我马上就看到了两个问题,
crs需要大写:CRS。out和utm_out不是列表,你不需要[[]]。 -
另外,请列出您在以后的问题中使用的所有库。
-
另外,如果您的示例需要创建文件,最好提供代码以在之后进行清理。创建一个新文件夹
dir.create("exampleFolder"); setwd("exampleFolder")然后清理它 setwd(".."); unlink("exampleFolder", recursive= TRUE, force= TRUE) -
@Mako212 感谢您的回复。抱歉,我确实忘记发布我使用的包裹。每当我创建一个循环时,我做的第一件事就是逐行运行它以确保它可以正常工作,然后尝试将其包装成一个循环。我的代码在循环之外运行良好—— crs 不需要大写。我还加载了光栅包,所以很可能是因为这个包小写 crs 起作用。我的循环问题似乎总是与索引有关。我不知道 [[ ]] 是特定于列表的,感谢您提供的有用信息!
-
我也不知道 dir.create()...和 unlink(),所以这将是使用 stackoverflow 推进的有用信息,非常感谢
标签: r loops csv batch-processing shapefile