【问题标题】:convert multiple .csv files to .shp files in R在 R 中将多个 .csv 文件转换为 .shp 文件
【发布时间】: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


【解决方案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])
}

【讨论】:

  • 这很简洁,感谢您的建议@RobertH。我试过你的代码,但 out.files 行似乎不起作用。它给了我以下警告:“缺少参数“x”,没有默认值”,然后它在循环中失败。
【解决方案2】:

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

试试这个。我已将crs 更改为CRS,因为sp 中的函数是大写的。您的循环范围可以简化为for(i in all.files),并且我删除了使用out 和utm_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")

【讨论】:

  • 感谢您的建议,但不幸的是,这不起作用。我确实得到了一个 shapefile,但我需要每个 .csv 文件的 shapefile。因此,对于本示例,我需要 2 个名为“animal1.shp”、“animal2.shp”的 shapefile 文件。对我的循环的建议修订只创建一个名为“test”的文件。您的任何其他建议都会很有帮助。提前谢谢!
  • 解决方案:在 utm_out 之后,添加以下行:names
  • @kawilki 是的,你明白了。我更新了我的解决方案以反映上述评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
相关资源
最近更新 更多