Reading multiple .gpx files

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

I can read one file this way:

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

Suppose I want to read a few of them in memory. I could try the 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 the above program gives the following error:

Error in read.table (con <- textConnection (gpsdata), fill = TRUE, ...): no input lines available. 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 - 'has status 1

Please note that I was able to read this file by itself, so it seems that it has nothing to do with the file itself, but with running readGPS in a loop.

, , R , x . , readGPS f = "file1_w_12_f_ddf.gpx": x f = x, f = "x", ? , , GPSBabel...

, .gpx. .

<?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>

. readGPS GPSBabel , PATH.

+5
2

, , . GPSBabel . , "1_San José Baldi_Pernam_14_sep.gpx" , "1_San_José_Baldi_Pernam_14_sep.gpx" .

+4

GPSBabel PATH . test1.gpx_NA_NA test2.gpx_NA_NA 28 . ? , NA , visit.id, .

R?

FWIW, , , . , . ,

files <- dir(pattern = "\\.gpx")
#Replace all space characters with a "_". Replace with the character of your choice.
lapply(files, function(x) file.rename(from = x, to = gsub("\\s+", "_", x)))

#Reread in files with better names:
files <- dir(pattern = "\\.gpx")
out <- lapply(files, function(x) readGPS(i = "gpx", f = x, type = "w"))
names(out) <- files

out 2, data.frame , . - *apply , . for-loop x, temp visit.id, . , lapply , .

+5

All Articles