Skip to content

Commit

Permalink
Join TCX tracks and GPX segments
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Mar 21, 2022
1 parent 182b69b commit c1e2279
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
39 changes: 22 additions & 17 deletions gpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,46 @@ func parseGPX(r io.Reader) error {
sport = s
}
}
if !includeSport(sport) {
if len(t.Segments) == 0 || !includeSport(sport) {
continue
}

act := &activity{
sport: sport,
records: make([]*record, 0, len(t.Segments[0].Points)),
}

var p0, p1 gpx.GPXPoint
for _, s := range t.Segments {
if len(s.Points) == 0 {
continue
}

p0, p1 := s.Points[0], s.Points[len(s.Points)-1]
if !includeTimestamp(p0.Timestamp, p1.Timestamp) ||
!includeDuration(p1.Timestamp.Sub(p0.Timestamp)) {
continue
}

act := &activity{
sport: sport,
records: make([]*record, len(s.Points)),
}
for i, p := range s.Points {
act.records[i] = &record{
if len(act.records) == 0 {
p0 = p
}
p1 = p
act.records = append(act.records, &record{
ts: p.Timestamp,
lat: degreesToRadians(p.Latitude),
lon: degreesToRadians(p.Longitude),
}
})
if i > 0 {
r0, r1 := act.records[i-1], act.records[i]
act.distance += haversineDistance(r0.lat, r0.lon, r1.lat, r1.lon)
}
}
if !includeDistance(act.distance) {
continue
}
activities = append(activities, act)
}

if len(act.records) == 0 ||
!includeTimestamp(p0.Timestamp, p1.Timestamp) ||
!includeDuration(p1.Timestamp.Sub(p0.Timestamp)) ||
!includeDistance(act.distance) {
continue
}

activities = append(activities, act)
}

return nil
Expand Down
50 changes: 29 additions & 21 deletions tcx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,47 @@ func parseTCX(r io.Reader) error {
}

for _, a := range f.Activities {
if !includeSport(a.Sport) {
if len(a.Laps) == 0 || !includeSport(a.Sport) {
continue
}

act := &activity{
sport: a.Sport,
records: make([]*record, 0, len(a.Laps[0].Track)),
}

var t0, t1 tcx.Trackpoint
for _, l := range a.Laps {
if len(l.Track) == 0 {
continue
}

t0, t1 := l.Track[0], l.Track[len(l.Track)-1]
if !includeTimestamp(t0.Time, t1.Time) ||
!includeDuration(t1.Time.Sub(t0.Time)) ||
!includeDistance(l.DistanceInMeters) {
continue
}
act := &activity{
sport: a.Sport,
distance: l.DistanceInMeters,
records: make([]*record, 0, len(l.Track)),
}
act.distance += l.DistanceInMeters

for _, t := range l.Track {
if t.LatitudeInDegrees != 0 && t.LongitudeInDegrees != 0 {
act.records = append(act.records, &record{
ts: t.Time,
lat: degreesToRadians(t.LatitudeInDegrees),
lon: degreesToRadians(t.LongitudeInDegrees),
})
if t.LatitudeInDegrees == 0 || t.LongitudeInDegrees == 0 {
continue
}
if len(act.records) == 0 {
t0 = t
}
t1 = t
act.records = append(act.records, &record{
ts: t.Time,
lat: degreesToRadians(t.LatitudeInDegrees),
lon: degreesToRadians(t.LongitudeInDegrees),
})
}
if len(act.records) > 0 {
activities = append(activities, act)
}
}

if len(act.records) == 0 ||
!includeTimestamp(t0.Time, t1.Time) ||
!includeDuration(t1.Time.Sub(t0.Time)) ||
!includeDistance(act.distance) {
continue
}

activities = append(activities, act)
}

return nil
Expand Down

0 comments on commit c1e2279

Please sign in to comment.