I have a gpx file where someone exported two different activities in two separate locations to a single track. I need to split them and avoid getting the erroneous line in the track layer that connects the two places where they were using the GPS. I haven't found a method to cut a track at a certain spot, so my approach was to read in the track_points layer, subset for the date I was interested in, and push those points to a spatiallinesdataframe, and write the new track. I manage to write it without getting an error, but when I try to read it back again, it says there are no features in the track layer. I would be open to any solution that would split the track layer, and if anyone that is already answering this question knows how to write a 'tracks', 'track_points', and 'waypoints' layer to the same .gpx file, please include that in your answer as that will be my next step after solving this problem.

Here's what I did so far:

trackpoints<-readOGR(gpxfile, layer="track_points")

trackpoints<-trackpoints[trackpoints$time>'2016/03/16 12:00:59+00',] #subset for the date I'm interested in


lin1<-Lines(list(lin), ID="track")

df<-data.frame(len=sapply(1:length(lin2), function(i) gLength(lin2[i,])))
rownames(df)<-sapply(1:length(lin2), function(i) lin2@lines[[i]]@ID)

lin3<-SpatialLinesDataFrame(lin2, data=df)

proj4string(lin3)<-CRS("+proj=longlat +datum=WGS84")

     dsn="C:/Users/.../newtrack.gpx", layer="tracks", driver="GPX",
     dataset_options="GPX_USE_EXTENSIONS=yes", overwrite_layer = T)
  • Could you post a link to your original data? Your writeOGR function looks correct, what happens if you plot lin3?
    – Simbamangu
    Apr 25 '16 at 18:45
  • When I plot lin3, it plots exactly what I want to see. It's just that when I try to write that to the track layer, it doesn't actually create the feature for some reason. The script should work for any .gpx file, but here is a link to the one that I am working with: dropbox.com/s/45h27qhyywfd1yl/DeerPoint.gpx?dl=0
    – LAD
    Apr 25 '16 at 19:22

Figuring out the GPX driver options for rgdal is headache-inducing.

Writing a linestring as you've done here will cause it to write a route layer - if you write a multilinestring it should create a track layer. According to the documentation you should be able to make it be a track layer regardless using FORCE_GPX_TRACK=true but I've not been able to make that work with this example.

Some experimentation shows that just writing the track points will automatically create the track line as well, creating a track layer:

     driver = "GPX") 

enter image description here

  • I got the FORCE_GPX_TRACK=true to work, but I had to enter it in the argument in a very unintuitive way: writeOGR(lin3, dsn="C:/Users/.../file.gpx", layer="tracks", driver="GPX", dataset_options="GPX_USE_EXTENSIONS=yes","FORCE_GPX_TRACK=true", overwrite_layer = T)
    – LAD
    Apr 26 '16 at 14:00
  • and using "FORCE_GPX_TRACK=true" I can also just write layer="track_points" using my subset of the trackpoints, and it will write both the correct track points layer and the track layer without the need of converting the points into SpatialLines and so forth.
    – LAD
    Apr 26 '16 at 15:44
  • You don't need to use FORCE_GPX_TRACK if you just write track_points, as per my example!
    – Simbamangu
    Apr 26 '16 at 16:05
  • Yes, but if I don't use it and I try to read the newly written file into R with readOGR(file, layer="tracks") , it will say that there is no feature in the track layer. With FORCE_GPX_TRACK , I can run that and also run it again with layer="track_points" and it will read both of them into R.
    – LAD
    Apr 26 '16 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.