I am in the process of editing gpx files and need to write the files back to .gpx in order to put them in a GPS unit. Basically, I am cleaning up some data and removing some track points. However, when I write the .gpx file, it clears out the timestamp, which makes it very difficult to work with the data later.

I am using the writeOGR function from the rgdal package to export the file. An example that can be reproduced with any .gpx file is as follows:

writeOGR(track, dsn='myfile.gpx', layer="track_points", driver="GPX", dataset_options="GPX_USE_EXTENSIONS=yes", "FORCE_GPX_TRACK=true", overwrite_layer = T)

When you read this back into R using readOGR("myfile.gpx", layer="track_points), the 'time' column is all NA.

I am looking for a way to write the original time information to the new .gpx file.

Here is an example workthrough:


track<-readOGR(system.file("vectors/test_trk2.gpx", package="rgdal"), layer="track_points")

track<-track[is.na(track$time)==F,] #eliminate NAs from track's time column

writeOGR(track, dsn='myfile.gpx', layer="track_points", driver="GPX", dataset_options="GPX_USE_EXTENSIONS=yes", "FORCE_GPX_TRACK=true", overwrite_layer = T)

track<-readOGR("myfile.gpx", layer="track_points") #read track back in


  • You can't really deal with time in GIS-like vector data. I'd avoid going through writeOGR for this if you can, you get equivalent of a location-with-other-columns from readOGR so I'd stick with the tabular form until absolutely needed. In a pinch you might keep the "third coordinate" using sf but must assume types all the same, putting coordinates in a matrix, so it's a non-starter as well. One way or another you'll have to keep a separate data frame indexed by row to each "spatial location" to use these GIS-y tools. There's probably a more GPX-focussed package that could help, though.
    – mdsumner
    Jul 22 '17 at 0:45
  • Can you supply a sample data and illustrate what you are trying to do, or maybe use a supplied GPX file like the one in the rgdal package: system.file("vectors/test_trk2.gpx",package="rgdal")
    – Spacedman
    Jul 23 '17 at 11:00

The answer is quite strange but simple. readOGR() still reads the timestamps as you can see with track$time but it dosn't write it since it's expecting time to be written in capitals. therefore track$TIME <- track$time solves the problem.


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.