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:
library(rgdal) gpxfile<-"C:/Users/.../file.gpx" trackpoints<-readOGR(gpxfile, layer="track_points") library(lubridate) trackpoints$time<-ymd_hms(trackpoints$time) trackpoints<-trackpoints[trackpoints$time>'2016/03/16 12:00:59+00',] #subset for the date I'm interested in coord<-as.data.frame(coordinates(trackpoints)) lin<-Line(coord) lin1<-Lines(list(lin), ID="track") lin2<-SpatialLines(list(lin1)) 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") names(lin3)<-"name" writeOGR(lin3, dsn="C:/Users/.../newtrack.gpx", layer="tracks", driver="GPX", dataset_options="GPX_USE_EXTENSIONS=yes", overwrite_layer = T)