【问题标题】:R to Extract <when> from Google MyTracks .kml fileR 从 Google MyTracks .kml 文件中提取 <when>
【发布时间】:2013-07-02 00:27:40
【问题描述】:

我想使用 R 从 Google 我的曲目创建的 .kml 文件中读取“何时”值(摘录如下):

 ?xml version="1.0" encoding="UTF-8"?>
 <kml xmlns="http://www.opengis.net/kml/2.2"
 xmlns:gx="http://www.google.com/kml/ext/2.2"
 xmlns:atom="http://www.w3.org/2005/Atom">
 <Document>
 <open>1</open>
 <visibility>1</visibility>
 <name><![CDATA[2013-06-29 1:09pm]]></name>
 <atom:author><atom:name><![CDATA[Created by Google My Tracks on Android.]]></atom:name>     </atom:author>

 ...

 <gx:MultiTrack>
 <altitudeMode>absolute</altitudeMode>
 <gx:interpolate>1</gx:interpolate>
 <gx:Track>
 <when>2013-06-29T17:09:04.564Z</when>
 <gx:coord>-79.305048 43.710639 72.9000015258789</gx:coord>
 <when>2013-06-29T17:09:06.135Z</when>
 <gx:coord>-79.304971 43.710653 67.4000015258789</gx:coord>
 <when>2013-06-29T17:09:08.135Z</when>
 <gx:coord>-79.305193 43.710535 78.19999694824219</gx:coord>
 <when>2013-06-29T17:09:09.135Z</when>

节点“when”是“对应于位置的时间值(在 gx:coord 元素中指定)”。 “gx:coord”是“由经度、纬度和高度三个值组成的坐标值”。 (https://developers.google.com/kml/documentation/kmlreference#gxtrack)

我想要的值的路径是:

 kml/Document/Placemark/gx:MultiTrack/gx:Track/when

来自:xmlstarlet el "filename.kml"

我能够使用以下方法提取坐标和高度:

 coords <- xpathSApply(check, "//gx:coord", xmlValue)
 lat <- sapply(strsplit(as.character(coords)," "), "[",1)
 lon <- sapply(strsplit(as.character(coords)," "), "[",2)
 ele <- sapply(strsplit(as.character(coords)," "), "[",3)

但我当时无法获得。我想从文件中提取的是:

 17:09:04.564
 17:09:06.135
 17:09:08.135
 17:09:09.135

并将它们与坐标和高程对齐。

我试过了:

timeStamp <- xpathSApply(check, "//gx:MultiTrack", xmlValue)

这让我得到一个可能能够被解析的字符串,因为时间以“T”开始并以“Z”结束:

 [1] "absolute12013-06-29T17:09:04.564Z-79.305048 43.710639 72.90000152587892013-06-   29T17:09:06.135Z-79.304971 43.710653 67.40000152587892013-06-29T17:09:08.135Z-79.305193 43.710535 78.199996948242192013-06-29T17:09:09.135Z-79.305164 43.710592 77.699996948242192013-06-29T17:09:10.134Z-79.305097 43.710614 67.52013-06-29T17:09:11.137Z-79.305066 43.710572 

有什么好主意吗?提前致谢。

编辑 ----->

我不优雅的解决方案:

 file_name <- "2013-06-29 1-09pm.kml"
 library(XML)
 # read XML tree schema
 check <-xmlInternalTreeParse(file=file_name)
 library(gsubfn)
 # read kml file into a string 
 z <- xpathSApply(check, "//gx:MultiTrack", xmlValue)
 # find text bounded by (and including) T and Z
 x <- strapply(z,"T.+?Z")
 # unpack the resulting list
 x1 <- unlist(x)
 # get rid of the initial T
 x2 <- gsub("T", "", x1)
 # get rid of the trailing Z
 x3 <- gsub("Z", "", x2)
 # convert it to a time format
 time <- strptime(x3, "%H:%M:%OS")

【问题讨论】:

  • 这里的时间比坐标多,所以他们不会排队。
  • 是的,我的曲目记录和额外的一组整体“时间”是开始和结束。我可以把第一对去掉。

标签: r kml


【解决方案1】:

有更好的 html 解析方法,但我看到没有人发布。这会起作用,但 html 解析通常不赞成使用正则表达式。

library(qdap)
x <- unlist(genXtract(dat, "<when>", "</when>"))
y <- unlist(genXtract(dat, "<gx:coord>", "</gx:coord>"))

x[sapply(x, function(x) !identical(x, character(0)))]
y[sapply(y, function(x) !identical(x, character(0)))]

## > x[sapply(x, function(x) !identical(x, character(0)))]
##       <when>  :  </when>14       <when>  :  </when>16 
## "2013-06-29T17:09:04.564Z" "2013-06-29T17:09:06.135Z" 
##       <when>  :  </when>18       <when>  :  </when>20 
## "2013-06-29T17:09:08.135Z" "2013-06-29T17:09:09.135Z" 
## > y[sapply(y, function(x) !identical(x, character(0)))]
##             <gx:coord>  :  </gx:coord>15 
##  "-79.305048 43.710639 72.9000015258789" 
##             <gx:coord>  :  </gx:coord>17 
##  "-79.304971 43.710653 67.4000015258789" 
##             <gx:coord>  :  </gx:coord>19 
## "-79.305193 43.710535 78.19999694824219" 

【讨论】:

  • 不幸的是,qdap 不适用于 Ubuntu Linux 3.2.0-45-generic-pae 上的 R 版本 2.15.1。包 'qdap' 需要 R >= 3.0.0。谢谢你的建议,我看看能不能得到一个可以一起工作的R版本或qdap版本。
  • 感谢 Tyler,但 qdap 不会与 R 版本 2.15.1 一起安装:* 检查文件 '/tmp/RtmpsVW5Qr/qdap-master/DESCRIPTION' ... OK * 准备 'qdap':*检查DESCRIPTION元信息...OK *检查源文件和make文件中的LF行尾 *检查空目录或不需要的目录 *查看是否应该添加“data/datalist”文件 *构建“qdap_0.2.4. tar.gz' ...错误:此 R 版本为 2.15.1,包 'qdap' 需要 R >= 3.0.0 错误:命令失败 (1)
  • 泰勒感谢您的建议。这可能适用于寻找可以使用 R 版本 3.0 的解决方案的其他人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2012-05-17
  • 2021-02-04
相关资源
最近更新 更多