问题描述
假设我有许多 .gpx 文件(这些文件包含Garmin eTrex的GPX航点数据).我想用不同的名称将它们加载到R中并对其进行操作.
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")
假设我想将其中一些读入内存.我可以尝试 for 循环:
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:
请注意,我能够自行读取此文件,因此似乎与文件本身无关,但与循环运行readGPS无关.
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.
总的来说,我仍然感到非常困惑, R 如何处理上面的 x 之类的变量.我不确定如何修改独立实例f = "file1_w_12_f_ddf.gpx"
中的readGPS参数:应该是x
还是f = x
或f = "x"
,还是什么?也许问题出在给GPSBabel打电话...
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...
我在下面提供了一个示例文件,因此您可以将其复制到文本编辑器中,并使用不同的名称另存为 .gpx..
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>
注意:要运行 readGPS ,您将需要安装开源GPSBabel程序,并在PATH变量中引用该程序.
NOTE: To run readGPS you will need the open source GPSBabel program installed and referenced in your PATH variable.
推荐答案
原来的代码很好,问题出在文件名上. GPSBabel不喜欢带有空格的名称.因此,"1_SanJoséBaldi_Pernam_14_sep.gpx"是一个问题,而1_San_José_Baldi_Pernam_14_sep.gpx"不是问题.
Turns out the code was fine, the problem is with the file names. GPSBabel does not like names with white spaces. So "1_San José Baldi_Pernam_14_sep.gpx" is a problem, "1_San_José_Baldi_Pernam_14_sep.gpx" is not.
这篇关于读取多个.gpx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!