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, 2016 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, 2016 at 19:22

2 Answers 2


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, 2016 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, 2016 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, 2016 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, 2016 at 17:19

I tried over and over again to use the above question and answer to export a data frame I had created as a track in a gpx file. I could only get a gpx file that had waypoints (not track points or tracks). The problem I discovered is that a track requries information about the track and segments (track_fid, track_seg_id, track_seg_point_id), not just geographic data.

Here is an example in case someone else is trying to convert a very simple data frame to tracks and track points.

#Example DataFrame
Long<-seq(-87.111, -87.121, length.out=100)
Lat<-seq(36.333, 36.323, length.out=100)
Time<-seq(1200,1300, length.out=100)

GeoData<-data.frame(Long,Lat,Time) #Starting DF
GeoData[,"track_fid"]<-0 #Assign ID to tracks
GeoData[,"track_seg_id"]<-0 #Assign_seg_id to segments of tracks
GeoData[,"track_seg_point_id"]<-0:(nrow(GeoData)-1) #Assign point IDs, begin at zero and go in consecutive order. If data is not in order, it will need sorted first.

#Make DF a spatial point data frame
#For the data argument only include columns you wish to maintain in the final gpx file
GeoDataSpatialPoint<-SpatialPointsDataFrame(GeoData[,c("Long","Lat")], data = GeoData[,c(4:6)], proj4string = CRS("+proj=longlat +datum=WGS84"))

writeOGR(GeoDataSpatialPoint, dsn = "FinalTrackFile.gpx", layer = "track_points",
         dataset_options="GPX_USE_EXTENSIONS=yes", driver = "GPX", overwrite_layer = T)

#make sure the file saved and has the expected geometry types (in this case, 100 track points and 1 track)

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.