在R中将.csv文件批量转换为.shp文件 [英] Batch convert .csv files to .shp files in R

查看:857
本文介绍了在R中将.csv文件批量转换为.shp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大量(> 500)文本文件转换为shapefile.我可以成功地将单个.csv转换为投影的shapefile.在加载,清理和导出文本文件时,我可以使用lapply和'for'循环.但是,当我在循环中添加一些步骤以转换为shapefile时,代码将失败.以下是我尝试解决问题和相关错误消息的两种方法:

I am trying to convert a large number (>500) of text files into shapefiles. I can successfully convert a single .csv into a projected shapefile. And I can get lapply and 'for' loops to work when just loading, cleaning up, and exporting the text files. But the code fails when I add in steps to convert to shapefiles within the loops. Below are two ways I've tried tackling the problem and the associated error messages:

    library(rgdal)
    library(sp)

    crs.geo<-CRS("+proj=utm +zone=14 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")  #define projection
    coltype=c("character","character","character","numeric","numeric")   #define data types for input .csv (x,y UTM coords are columns 4,5)

    setwd("C:/.../testdata/out")
    all.the.filenames <- list.files(pattern= "\\.csv")  #create list of files to batch process

head(exampledata,2)
       Point            Location                 Time easting northing
1 Trackpoint 14 S 661117 3762441 12/1/2008 5:57:02 AM  661117  3762441
2 Trackpoint 14 S 661182 3762229 12/1/2008 5:58:02 AM  661182  3762229

带有"for"循环的批量转换

    names <- substr(all.the.filenames, 1, nchar(all.the.filenames)-4)   #clean up file names

    for(i in names) {
      filepath <- file.path("../out",paste(i,".csv",sep=""))
      assign(i, read.table(filepath, colClasses=coltype, header=TRUE, sep=",", na.strings=c("NA",""))) 
      coordinates(i) <- c(4,5)  #coords in columns 4,5
      proj4string(i) <- crs.geo
      writeOGR(i,"C:/Users/Seth/Documents/testdata/out","*",driver="ESRI Shapefile") }

R返回此错误消息:

Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘coordinates<-’ for signature ‘"character"’

如果我在'assign'行之后结束'for'循环,它将成功将所有.csv文件导入为R中的单独对象.问题似乎是函数'coordinates'没有将坐标视为数字,并且无论我尝试如何明确地定义它们,我都会收到相同的错误消息(例如,coordinates(i) <- c(as.numeric("easting","northing")).此外,当将这些代码行应用于单个.csv文件时,这些代码行也可以成功工作,问题出在我在循环中进行子集化时.

If I end the 'for' loop after the 'assign' line, it successfully imports all .csv files as separate objects in R. The problem seems to be that function 'coordinates' is not seeing the coords as numeric, and I get the same error message no matter how explicitly I try to define them as such (e.g., coordinates(i) <- c(as.numeric("easting","northing")) Also, these lines of code work successfully when applied to a single .csv file, the problem is when I subset within a loop.

files.to.process <- lapply(all.the.filenames, function(x) read.csv(x, colClasses=coltype, header=TRUE))
lapply(files.to.process, function(c) coordinates(c)<-c("easting","northing"))
[[1]]
[1] "easting"  "northing"
[[2]]
[1] "easting"  "northing"
[[3]]
[1] "easting"  "northing"
[[4]]
[1] "easting"  "northing"
[[5]]
[1] "easting"  "northing"

lapply(files.to.process, function(p) proj4string(p) <- crs.geo)

返回错误消息:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘proj4string<-’ for signature ‘"data.frame", "CRS"’

#Double-check if function 'coordinates' worked
    class(files.to.process) == "SpatialPoints"
    [1] FALSE

结论/问题

使用这两种方法时,问题似乎出在制造空间物体的坐标"步骤中.我在循环中做错了什么?非常感谢您的帮助! 塞思·H.

Conclusion/problem

With both approaches the problem seems to be in the 'coordinates' step to make a spatial object. What am I doing wrong in the loops? Thanks much for any help! Seth H.

推荐答案

在您的第一次尝试中,循环内的对象i是字符对象.因此,

In your first attempt, the object i inside the loop is a character object. So,

coordinates(get(i))

会更好地工作;我没有一批csv文件可以对其进行测试. 在第二次尝试使用lapply()时,我不确定是怎么回事,但是

would work better; I don't have a batch of csv files to test it on. In the second attempt using lapply(), I'm not exactly sure what's going on, but

class(files.to.process)

应为列表",所以您要做的是

should be "list", so what you want to do is

lapply(files.to.process,class)

,它将告诉您对象是否属于类空间点.我猜它们是data.frames,在这之间还需要一步.

and that will tell you if the objects are of class spatialpoints. I'm guessing they are data.frames, and you need one more step in between.

这篇关于在R中将.csv文件批量转换为.shp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆