Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Suppose I have a number of .gpx files (these contain GPX waypoint data from a Garmin eTrex). I want to load them into R with different names and manipulate them.

I can read one file thus:

library(maptools)
gpx.raw <- readGPS(i = "gpx", f = "file1_w_12_f_ddf.gpx", type="w")

Suppose I want to read a number of them into memory. I could try a for loop:

files <- list.files(".",pattern = "*.gpx")
for(x in files){

    #Create new file name
    temp <- strsplit(x,"_",fixed=TRUE)
    visit.id <- sapply(temp,FUN=function(x){paste(x[1],x[4],substr(x[5],1,3),sep="_")})

    #read file with new filename
    assign(visit.id, readGPS(i = "gpx", f = x, type="w"))
}

Running above program yields following error:

Error in read.table(con <- textConnection(gpsdata), fill = TRUE, ...) : no lines available in input In addition: Warning message: running command 'C:\PROGRA~2\GPSBabel\gpsbabel.exe -w -i gpx -f file1_w_12_f_ddf.gpx -o tabsep -F -' had status 1

Note that I was able to read this file on its own, so it would seem it has nothing to do with the file itself but with running readGPS in a loop.

In general I still find it very confusing how R treats variables like x above. I am not sure how to modify the argument to readGPS from the stand alone instance f = "file1_w_12_f_ddf.gpx": Should it be x, or f = x, or f = "x", or what? Or maybe the problem is in the call to GPSBabel...

I include a sample file below so you can copy it to text editor, and save as .gpx. twice with different names and try yourself.

<?xml version="1.0" encoding="UTF-8"?>
<gpx
 version="1.0"
 creator="GPSBabel - http://www.gpsbabel.org"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.topografix.com/GPX/1/0"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
 <time>2010-09-14T18:35:43Z</time>
 <bounds minlat="18.149888897" minlon="-96.747799935" maxlat="50.982883293" maxlon="121.640266674"/>
<wpt lat="38.855549991" lon="-94.799016668">
<ele>325.049072</ele>
 <name>GARMIN</name>
 <cmt>GARMIN</cmt>
 <desc>GARMIN</desc>
 <sym>Flag</sym>
 </wpt>
 <wpt lat="50.982883293" lon="-1.463899976">
 <ele>35.934692</ele>
 <name>GRMEUR</name>
 <cmt>GRMEUR</cmt>
 <desc>GRMEUR</desc>
 <sym>Flag</sym>
 </wpt>
 <wpt lat="25.061783362" lon="121.640266674">
 <ele>38.097656</ele>
 <name>GRMTWN</name>
 <cmt>GRMTWN</cmt>
 <desc>GRMTWN</desc>
 <sym>Flag</sym>
 </wpt>
 </gpx>
share|improve this question

closed as not constructive by Mapperz Jun 19 '11 at 2:38

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
    
it is generally discouraged to cross-post. Since you have already an excepted answer on SO, I have flagged this post to be closed. Another reasonable solution would be to simply close the question yourself. –  Andy W Jun 19 '11 at 2:08
    
Thanks Andy - The dup stackoverflow.com/questions/6397523/read-multiple-gpx-files is outside the GISse domain - Though due to "No eligible question found at that url." Can close with other methods –  Mapperz Jun 19 '11 at 2:38
    
@Andy W - Not sure what is wrong with cross-posting. Two different audiences with different expertise. Unless we are going to get territorial. That said, I want to remove the question since it was really just a problem with file names. How do I do that? –  Fred Jun 19 '11 at 15:17

1 Answer 1

I can't say why it is not working with the readGPS function in the maptools package, but I was able to loop through the files reading in the data using the readOGR function in the rgdal package.

setwd("C:\\Documents and Settings\\Owner\\Desktop\\R_test")
library(rgdal)   

#I just saved my text files in the above directory
myfiles <- list.files()

#this works, note track_points on layer name does not work!
gpx.raw <- readOGR(dsn = myfiles[1], layer="waypoints")

#now making function
for (i in 1:3) {
temp.gpx <- readOGR(dsn = myfiles[i], layer="waypoints")
temp.gpx
}
share|improve this answer
    
Thanks. Actually it works, but I found out that GPSbabel does not like filenames with empty spaces. The name of my first file was something like "file1 w 12_f_ddf.gpx" so it escaped right there. –  Fred Jun 18 '11 at 23:55
    
@Fred , you should either post that as an answer and accept it, or just accept this answer since your problem is solved. –  Andy W Jun 19 '11 at 0:35
    
Was going to post it as answer but I cannot answer my own q within 24 hours... –  Fred Jun 19 '11 at 15:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.