首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用R解析strava gpx <gpxtpx:hr>

使用R解析strava gpx <gpxtpx:hr>
EN

Stack Overflow用户
提问于 2019-09-20 17:30:59
回答 1查看 349关注 0票数 0

我需要解析strava gpx文件,我使用的是marcusvolz/strava库,它可以很好地解析坐标,元素,时间,但问题是当你试图解析扩展路径改变函数的时候。

我修改了函数,包括: hr <- as.numeric(XML::xpathSApply(pfile,path =xmlValue))

但返回:

XPath错误:未定义的命名空间前缀XPath错误:xpathApply.XMLInternalDocument中的表达式错误(doc,path,fun,...,namespaces = namespaces,:计算xpath表达式名称时出错

代码语言:javascript
复制
    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:

代码语言:javascript
复制
<?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的值。

EN

回答 1

Stack Overflow用户

发布于 2019-09-20 18:49:02

我用trackeR解决了这个问题

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58025630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档