我想使用 R 从由 Google My 曲目创建的 .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“文件名.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")