简体   繁体   English

读取多个.gpx文件

[英]Read multiple .gpx files

Suppose I have a number of .gpx files (these contain GPX waypoint data from a Garmin eTrex). 假设我有许多.gpx文件(这些文件包含来自Garmin eTrex的GPX航路点数据)。 I want to load them into R with different names and manipulate them. 我想用不同的名称将它们加载到R中并操纵它们。

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: 我可以尝试for循环:

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 read.table出错(con < - textConnection(gpsdata),fill = TRUE,...):输入中没有可用的行另外:警告消息:运行命令'C:\\ PROGRA~2 \\ GPSBabel \\ gpsbabel.exe - w -i gpx -f file1_w_12_f_ddf.gpx -o tabsep -F - '状态为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. 请注意,我能够自己读取此文件,因此它似乎与文件本身无关,而是在循环中运行readGPS。

In general I still find it very confusing how R treats variables like x above. 一般来说,我仍然觉得R处理像上面的x这样的变量非常困惑。 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? 我不确定如何从独立实例f = "file1_w_12_f_ddf.gpx"修改readGPS的参数:它应该是x ,还是f = x ,或者f = "x" ,还是什么? Or maybe the problem is in the call to GPSBabel... 或者问题可能在于对GPSBabel的调用......

I include a sample file below so you can copy it to text editor, and save as .gpx. 我在下面包含一个示例文件,因此您可以将其复制到文本编辑器,并保存为.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>

NOTE: To run readGPS you will need the open source GPSBabel program installed and referenced in your PATH variable. 注意:要运行readGPS,您需要在PATH变量中安装和引用开源GPSBabel程序。

Fred, 弗雷德,

After installing GPSBabel and updating the PATH variable, your code snippet ran fine. 安装GPSBabel并更新PATH变量后,您的代码段运行正常。 I have two objects names test1.gpx_NA_NA and test2.gpx_NA_NA with three observations of 28 variables. 我有两个对象名称test1.gpx_NA_NAtest2.gpx_NA_NA有三个28个变量的观察值。 Is that right? 是对的吗? I assume the NA bit in the file names is due to how you are defining visit.id and my test file names not fitting into that paradigm. 我假设文件名中的NA位是由于你如何定义visit.id和我的测试文件名不符合该范例。

Have you tried this on a fresh instance of R? 你是否在一个新的R实例上试过这个?

FWIW, I would probably read all of these files into a single list object. FWIW,我可能会将所有这些文件读入单个列表对象。 I find dealing with a list object easier than having lots of different objects floating around. 我发现处理列表对象比放置许多不同的对象更容易。 For example, 例如,

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

and out is now a list of 2, where each object is a data.frame with the name of the file that it was associated with previously. out现在的2的列表,其中的每个对象是与它与之前相关的文件的名称data.frame。 Using something from the *apply family has the benefit leaving a clean working space behind as well. 使用来自*apply系列的东西也有利于留下干净的工作空间。 Using the for-loop results in x , temp , and visit.id hanging out afterwords. 使用for循环会导致xtempvisit.id You could wrap them up into a function call, but just using lapply will be more straight forward I think. 你可以将它们包装成一个函数调用,但我认为只使用lapply会更直接。

Turns out the code was fine, the problem is with the file names. 原来代码很好,问题是文件名。 GPSBabel does not like names with white spaces. GPSBabel不喜欢带有空格的名字。 So "1_San José Baldi_Pernam_14_sep.gpx" is a problem, "1_San_José_Baldi_Pernam_14_sep.gpx" is not. 所以“1_SanJoséBaldi_Pernam_14_sep.gpx”是一个问题,“1_San_José_Baldi_Pernam_14_sep.gpx”不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM