0

Strava gpx ファイルを解析する必要があり、coords、ele、time を解析するために正常に動作するライブラリ marcusvolz/strava を使用していますが、関数を変更して拡張パスを解析しようとすると問題が発生します。

次の関数を変更しました: 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

取得済み 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 に答える