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

convert multiple .csv files to .shp files in R

我应该先说我在 R 中的循环很糟糕,我认识到这个问题类似于这个 post 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")
}

这会产生:错误(函数(类、fdef、mtable): 无法为签名“"integer"、"CRS"”

找到函数“spTransform”的继承方法

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

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

在此先感谢您的帮助。

扩展我的评论,重要的是首先在单个文件上测试像这样的批处理操作,然后根据需要调整您的解决方案以处理批处理。我解决您的问题的第一步是去除 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")

这里是 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])
}