0

我需要解析 strava gpx 文件,并且我正在使用库 marcusvolz/strava 可以很好地解析坐标、ele、时间,但问题是当您尝试解析更改函数的扩展路径时。

我更改了函数,包括:hr <- as.numeric(XML::xpathSApply(pfile, path = "//trkpt/extensions/gpxtpx:TrackPointExtension/gpxtpx:hr", XML::xmlValue))

但返回:

XPath 错误:未定义的命名空间前缀 XPath 错误:无效表达式 xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, : 评估 xpath 表达式时出错 //trkpt/extensions/gpxtpx:TrackPointExtension/gpxtpx:hr

    file<-c("test.xml")

    # Parse GPX file and generate R structure representing XML tree

    pfile <- XML::htmlTreeParse(file = file,
                                error = function (...) {},
                                useInternalNodes = TRUE)

    coords <- XML::xpathSApply(pfile, path = "//trkpt", XML::xmlAttrs)

    # extract the activity type from file name

    type <- str_match(file, ".*-(.*).gpx")[[2]]

    # Check for empty file.

    if (length(coords) == 0) return(NULL)

    # dist_to_prev computation requires that there be at least two coordinates.

    if (ncol(coords) < 2) return(NULL)

    lat <- as.numeric(coords["lat", ])

    lon <- as.numeric(coords["lon", ])

    ele <- as.numeric(XML::xpathSApply(pfile, path = "//trkpt/ele", XML::xmlValue))

    time <- XML::xpathSApply(pfile, path = "//trkpt/time", XML::xmlValue)


    hr <- as.numeric(XML::xpathSApply(pfile, path = "//trkpt/extensions/gpxtpx:TrackPointExtension/gpxtpx:hr", XML::xmlValue))
    result <- data.frame(lat = lat, lon = lon, time = time, type = type)

    result <- result %>%
      dplyr::mutate(dist_to_prev = c(0, sp::spDists(x = as.matrix(.[, c("lon", "lat")]), longlat = TRUE, segments = TRUE)),
                    cumdist = cumsum(dist_to_prev),
                    time = as.POSIXct(.$time, tz = "GMT", format = "%Y-%m-%dT%H:%M:%OS")) %>%
      dplyr::mutate(time_diff_to_prev = as.numeric(difftime(time, dplyr::lag(time, default = .$time[1]))),
                    cumtime = cumsum(time_diff_to_prev))
    result

Aquí el xml:

<?xml version="1.0" encoding="utf-8"?>
<gpx version="1.1" creator="Strava GPX downloader (https://chrome.google.com/webstore/detail/strava-gpx-downloader/pnglhfabfkchkadgnkfacoakincdpeeg)" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1">
<metadata>
<time>1970-01-01T00:00:00.000Z</time>
<link href="https://www.strava.com/activities/1241375734/">
<text>Strava</text>
</link>
</metadata>
<trk>
<name></name>
<type></type>
<trkseg>
    <trkpt lat="39.461234" lon="-0.337053">
    <ele>-22.4</ele>
    <time>1970-01-01T00:00:00.000Z</time>
    <extensions>
       <gpxtpx:TrackPointExtension>
           <gpxtpx:hr>107</gpxtpx:hr>
           <gpxtpx:cad>66</gpxtpx:cad>
           <gpxtpx:atemp>27</gpxtpx:atemp>
       </gpxtpx:TrackPointExtension>
   </extensions>
   </trkpt>>
</gpx>

我需要在 df 中包含 hr 的值

4

1 回答 1

0

我使用 trackeR 解决了它

更多信息: https ://cran.r-project.org/web/packages/trackeR/vignettes/trackeR.pdf

于 2019-09-20T10:49:02.347 回答