Batch convert .csv files to .shp files in R -


i trying convert large number (>500) of text files shapefiles. can convert single .csv projected shapefile. , can lapply , 'for' loops work when loading, cleaning up, , exporting text files. code fails when add in steps convert shapefiles within loops. below 2 ways i've tried tackling problem , associated error messages:

general processing/definitions-

    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 input .csv (x,y utm coords columns 4,5)      setwd("c:/.../testdata/out")     all.the.filenames <- list.files(pattern= "\\.csv")  #create list of files batch process  head(exampledata,2)        point            location                 time easting northing 1 trackpoint 14 s 661117 3762441 12/1/2008 5:57:02  661117  3762441 2 trackpoint 14 s 661182 3762229 12/1/2008 5:58:02  661182  3762229 

batch conversion 'for' loop

    names <- substr(all.the.filenames, 1, nchar(all.the.filenames)-4)   #clean 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 returns error message:

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

if end 'for' loop after 'assign' line, imports .csv files separate objects in r. problem seems function 'coordinates' not seeing coords numeric, , same error message no matter how explicitly try define them such (e.g., coordinates(i) <- c(as.numeric("easting","northing")) also, these lines of code work when applied single .csv file, problem when subset within loop.

batch conversion using lapply

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) 

which returns error message:

error in (function (classes, fdef, mtable)  :    unable find inherited method function ‘proj4string<-’ signature ‘"data.frame", "crs"’  #double-check if function 'coordinates' worked     class(files.to.process) == "spatialpoints"     [1] false 

conclusion/problem

with both approaches problem seems in 'coordinates' step make spatial object. doing wrong in loops? help! seth h.

in first attempt, object inside loop character object. so,

coordinates(get(i)) 

would work better; don't have batch of csv files test on. in second attempt using lapply(), i'm not sure what's going on,

class(files.to.process) 

should "list", want

lapply(files.to.process,class) 

and tell if objects of class spatialpoints. i'm guessing data.frames, , need 1 more step in between.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -