speendo speendo - 3 years ago 154
R Question

Import gpx track using the XML library

I want to analyse a gpx track in R. To import the data, I try to use the XML-package.

I found a tutorial that explained how to import each individual data vector and then combine them to a data frame.

However, in my usecase this does not work, because for some nodes there was no heart rate

collected, so the length of the vectors would not match.

Therefore I try to import all relevant data at once.

What I manage to do so far is


filename <- "sample.gpx"
download.file("https://owncloud.yeara.net/index.php/s/Io4uOq6sfFuCCdq/download", filename) # downloads a sample file from my server

gpx.raw <- xmlTreeParse(filename, useInternalNodes = TRUE)

rootNode <- xmlRoot(gpx.raw)

print(rootNode) # output seems okay

Now, instead of the rootNode, I'd like to import the content of
into a dataframe. It should be designed in the following way:

  • Each
    should be a row

  • trkpt lon
    trkpt lat
    should be in corresponding columns

  • If there is a value
    it should also go into the corresponding column

Can you help me to achieve this?

Answer Source

This is the code I ended up with. Thanks to all of you (especially @lukeA) for your help.


filename <- "Downloads/activity(1).gpx"

gpx.raw <- xmlTreeParse(filename, useInternalNodes = TRUE)

rootNode <- xmlRoot(gpx.raw)

gpx.rawlist <- xmlToList(rootNode)$trk

gpx.list <- unlist(gpx.rawlist[names(gpx.rawlist) == "trkseg"], recursive = FALSE)

gpx <- do.call(rbind.fill, lapply(gpx.list, function(x) as.data.frame(t(unlist(x)), stringsAsFactors=F)))
names(gpx) <- c("ele", "time", "hr", "lon", "lat")

I had some trouble with multiple trksegs as I could not access them by name (because they all have the same name in the list: trkseg) I could solve this with the unlist command and the tricky selection of elements in gpx.rawlist.

I wonder if there is a more elegant way, but at least this seems to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download