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

问题描述 投票:0回答:2

我应该断言我在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”)不为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")
}

这会产生:(函数(classes,fdef,mtable)中的错误:无法为签名'“integer”,“CRS”找到函数'spTransform'的继承方法

理想情况下,输出文件将类似于“animal1.shp”“animal2.shp”等。

或者,我在一个文件中有动物1和2。我可以引入这个文件,设置投影,然后为每个唯一的动物ID创建多个子集,并将子集写入.shp文件,但我对空间数据的子集有问题,我认为这是另一个线程的主题。

提前感谢你的帮助。

r loops csv batch-processing shapefile
2个回答
0
投票

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

试试吧。我已经将crs改为CRS,因为sp中的函数是大写的。你的循环范围可以简化为for(i in all.files),我删除了使用oututm_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")
}

编辑:

我必须通过指定图层名称来修改writeOGR线:

writeOGR(utm_out,dsn="/path/Shapefile_test", layer="test", driver="ESRI Shapefile")

1
投票

这是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])
}
© www.soinside.com 2019 - 2024. All rights reserved.