Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将多个.csv文件转换为.shp文件_R_Loops_Csv_Batch Processing_Shapefile - Fatal编程技术网

在R中将多个.csv文件转换为.shp文件

在R中将多个.csv文件转换为.shp文件,r,loops,csv,batch-processing,shapefile,R,Loops,Csv,Batch Processing,Shapefile,我应该说我在R中的循环很糟糕,我认识到这个问题和这篇文章类似。然而,我无法留下评论,看看这个用户是否在这个线程上找到了解决方案,因为我没有足够的信誉点,并且建议的解决方案对我没有帮助 我有多个包含动物GPS点的.csv文件。我想为空间分析创建多个shapefile。我尝试创建一个循环来读取.csv文件,从csv文件中生成具有纬度和经度的空间数据,将空间数据帧转换为UTM投影,这样我就可以计算距离,然后将文件作为形状文件写入。这是我尝试过的循环,但我认为我在out和utm_out中的索引是不正确的

我应该说我在R中的循环很糟糕,我认识到这个问题和这篇文章类似。然而,我无法留下评论,看看这个用户是否在这个线程上找到了解决方案,因为我没有足够的信誉点,并且建议的解决方案对我没有帮助

我有多个包含动物GPS点的.csv文件。我想为空间分析创建多个shapefile。我尝试创建一个循环来读取.csv文件,从csv文件中生成具有纬度和经度的空间数据,将空间数据帧转换为UTM投影,这样我就可以计算距离,然后将文件作为形状文件写入。这是我尝试过的循环,但我认为我在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")
}
#为动物1编写sample.csv

ID1扩展我的评论,首先在单个文件上测试这样的批处理操作,然后根据需要调整解决方案以处理批处理,这一点很重要。我对您的问题进行故障排除的第一步是去除
for
循环,并尝试使用第一个文件
all.files[1]
运行代码,但仍然失败,表明至少有一个问题与循环无关

试试这个。我已将
crs
更改为
crs
,因为
sp
中的函数是大写的。您的循环范围可以简化为
for(i in all.files)
,并且我删除了使用
out
utm\u out
访问不存在列表的尝试

require(sp)
require(rgdal)   

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 all.files) {
 file <- read.csv(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
 names<-substr(i, 1, nchar(all.files)-4)
 utm_out <- spTransform(out, CRS("+init=epsg:32616")) #transform to UTM
 writeOGR(utm_out,dsn="/path/Shapefile_test", layer=names, driver="ESRI Shapefile")
}

以下是Mako212解决方案的一个小变化

library(raster)

all.files <- list.files(pattern="\\.csv$")
out.files <- gsub("\\.csv$", ".shp")
crs <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

for(i in 1:length(all.files)) {
    d <- read.csv(all.files[i], stringsAsFactors = FALSE)
    sp <- SpatialPointsDataFrame(coords = d[c("Longitude", "Latitude")], d, proj4string = crs) 
    utm <- spTransform(sp, CRS("+proj=utm +zone=16 +datum=WGS84"))
    shapefile(utm, out.files[i])
}
库(光栅)

处理此类问题的最佳方法是摆脱循环,并在单个文件上进行测试,直到一切正常为止。然后将代码包装到函数中,或者在末尾编写循环。我马上看到两个问题,
crs
需要大写:
crs
out
utm\u out
不是列表,您不需要
[[]]
。此外,请列出您在将来的问题中使用的所有库。此外,如果您的示例需要创建文件,最好提供代码,以便在以后进行清理。创建一个新文件夹
dir.Create(“exampleFolder”);setwd(“exampleFolder”)
然后清理它
setwd(“…”);取消链接(“exampleFolder”,recursive=TRUE,force=TRUE)
@Mako212感谢您的回复。对不起,我忘了把我用过的包裹寄出去了。每当我做一个循环时,我要做的第一件事就是一行一行地运行它以确保它工作,然后尝试将它包装成一个循环。我的代码在循环之外运行良好——crs不需要大写。我还加载了光栅包,因此可能是因为这个包,小写crs才起作用。我的循环问题似乎总是和索引有关。我不知道[[]]是特定于列表的,感谢您提供的有用信息!我也不知道dir.create()…和unlink(),所以这将是使用stackoverflow向前推进的有用信息,非常感谢您的建议,但不幸的是,这不起作用。我确实得到了一个shapefile,但我需要为每个.csv文件创建一个shapefile。因此,对于这个示例,我需要两个名为“animal1.shp”、“animal2.shp”的shapefile文件。对我的循环的建议修订只创建了一个名为“test”的文件。你的任何其他建议都会很有帮助。提前谢谢你!解决方案:在utm_out之后,添加以下行:names@kawilki是的,你明白了。我更新了我的解决方案以反映上述评论。这很好,很简洁,感谢您的建议@RobertH。我尝试了你的代码,但out.files行似乎不起作用。它给了我以下警告:“参数“x”丢失,没有默认值”,然后它在循环中失败。
writeOGR(utm_out,dsn="/path/Shapefile_test", layer="test", driver="ESRI Shapefile")
library(raster)

all.files <- list.files(pattern="\\.csv$")
out.files <- gsub("\\.csv$", ".shp")
crs <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

for(i in 1:length(all.files)) {
    d <- read.csv(all.files[i], stringsAsFactors = FALSE)
    sp <- SpatialPointsDataFrame(coords = d[c("Longitude", "Latitude")], d, proj4string = crs) 
    utm <- spTransform(sp, CRS("+proj=utm +zone=16 +datum=WGS84"))
    shapefile(utm, out.files[i])
}