wbaeckelmans
wbaeckelmans

Reputation: 407

Subscript out of bounds - Loading .gpx file

At this moment I am experimenting with reading GPX files, so far I have exported runs from my sports tracker app and I have managed to load these correctly and plotted them on a map.

Now I have drawn a new route in my app and exported this into a .gpx file, however when I try loading this in R I get the following error:

Error in nm[[1]] : subscript out of bounds

R code :

library(plotKML)
library(leaflet)
route <- readGPX("../GPS data Serge/Testing.gpx", tracks = TRUE)

Testing.gpx file :

<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="Endomondo.com" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata>
    <author>
      <name>Wouter Baeckelmans</name>
    </author>
    <link href="http://www.endomondo.com">
      <text>Endomondo</text>
    </link>
    <time>2015-12-02T15:53:31Z</time>
  </metadata>
  <trk>
    <name>Test</name>
    <src>http://www.endomondo.com/</src>
    <link href="https://www.endomondo.com/routes/639589549">
      <text>Test</text>
    </link>
    <type>RUNNING</type>
    <trkseg>
      <trkpt lat="8.39087" lon="-12.94756"/>
      <trkpt lat="8.39022" lon="-12.94777"/>
      <trkpt lat="8.38974" lon="-12.94861"/>
      <trkpt lat="8.38307" lon="-12.94227"/>
      <trkpt lat="8.38248" lon="-12.94181"/>
      <trkpt lat="8.3822" lon="-12.94171"/>
      <trkpt lat="8.38104" lon="-12.94155"/>
      <trkpt lat="8.3765" lon="-12.94101"/>
      <trkpt lat="8.37593" lon="-12.94086"/>
      <trkpt lat="8.37556" lon="-12.94065"/>
      <trkpt lat="8.37522" lon="-12.9404"/>
      <trkpt lat="8.3742" lon="-12.93948"/>
      <trkpt lat="8.37367" lon="-12.93893"/>
      <trkpt lat="8.37305" lon="-12.93819"/>
    </trkseg>
  </trk>
</gpx>

Any idea's on what could be causing this? I noticed this .gpx files looks different than the other .gpx file that I could load succesfully. That file contained a trkseg tag for each trkpt tag... But I think it should be possible to load this file as well, I just don't seem to find how.

Any help on this is greatly appreciated!

Upvotes: 0

Views: 409

Answers (1)

Spacedman
Spacedman

Reputation: 94277

Tried readOGR from the rgdal package?

> route = readOGR("Testing.gpx","tracks")
OGR data source with driver: GPX 
Source: "Testing.gpx", layer: "tracks"
with 1 features
It has 12 fields
> plot(route)

and there it is.

route is now a SpatialLinesDataFrame, so read up on the sp package to understand what you can do with it. SpatialLinesDataFrame features can be complex things, such as entire branching river systems, so access to the raw coordinates can be correspondingly complex. If you know your data is a simple single line, then you can get the coordinates as a 2-column matrix:

> coordinates(route)[[1]][[1]]
           [,1]    [,2]
 [1,] -12.94756 8.39087
 [2,] -12.94777 8.39022
 [3,] -12.94861 8.38974
 [4,] -12.94227 8.38307
 [5,] -12.94181 8.38248
 [6,] -12.94171 8.38220
 [7,] -12.94155 8.38104
 [8,] -12.94101 8.37650
 [9,] -12.94086 8.37593
[10,] -12.94065 8.37556
[11,] -12.94040 8.37522
[12,] -12.93948 8.37420
[13,] -12.93893 8.37367
[14,] -12.93819 8.37305

This is because you have one feature and one line in that feature.

Looking at the readGPX code I suspect a bug in its handling of names in the GPX file....

Upvotes: 1

Related Questions