-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
137 lines (122 loc) · 2.93 KB
/
segment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"strings"
"time"
"github.com/bradfitz/latlong"
"github.com/tkrajina/gpxgo/gpx"
)
type Segment struct {
*gpx.GPXTrackSegment
}
// Point returns i-th point of the segment.
func (s Segment) Point(i int) *gpx.GPXPoint {
return &s.Points[i]
}
// EachPair iterates over a segment with pairs of subsequent points.
func (s Segment) EachPair(f func(prev, next *gpx.GPXPoint)) {
prev := s.Point(0)
for i := 1; i < len(s.Points); i++ {
next := s.Point(i)
f(prev, next)
prev = next
}
}
func (s Segment) Timezone() *time.Location {
b := s.Bounds()
var err error
tz, err := time.LoadLocation(latlong.LookupZoneName(b.MinLatitude, b.MinLongitude))
if err != nil {
tz = time.UTC
}
return tz
}
func (s Segment) String() string {
tb := s.TimeBounds()
return fmt.Sprintf("%s @ %s = %05.2fnm (%d)",
tb.EndTime.Sub(tb.StartTime),
tb.StartTime.In(s.Timezone()).Format(strFormat),
s.Length2D()/1852,
s.GetTrackPointsNo(),
)
}
func (s Segment) Split(limit time.Duration) Segments {
limitSeconds := limit.Seconds()
prev := s.Point(len(s.Points) - 1)
for i := len(s.Points) - 2; i >= 0; i-- {
next := s.Point(i)
if next.TimeDiff(prev) > limitSeconds {
s1, s2 := s.GPXTrackSegment.Split(i)
return append(Segment{s1}.Split(limit), Segment{s2})
}
prev = next
}
return Segments{s}
}
type Segments []Segment
func GetSegments(g *gpx.GPX) (s Segments) {
for _, t := range g.Tracks {
for i := range t.Segments {
s = append(s, Segment{&t.Segments[i]})
}
}
return s
}
func (ss Segments) String() string {
var all []string
for _, s := range ss {
all = append(all, s.String())
}
return strings.Join(all, "\n")
}
// Sort segments by start time
func (s Segments) Len() int { return len(s) }
func (s Segments) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Segments) Less(i, j int) bool {
return s[i].TimeBounds().StartTime.Before(s[j].TimeBounds().StartTime)
}
// Dedupe removes subsequent segments with the same time bounds
// and segments that have less than @min points.
func (s Segments) Dedupe(min int) (t Segments) {
if len(s) == 0 {
return
}
p := s[0]
t = append(t, p)
for _, s := range s[1:] {
if s.TimeBounds().Equals(p.TimeBounds()) {
continue
}
if s.GetTrackPointsNo() > min {
t = append(t, s)
}
p = s
}
return
}
func (s Segments) Split(limit time.Duration) (t Segments) {
for _, seg := range s {
t = append(t, seg.Split(limit)...)
}
return t
}
// Tracks creates tracks from subsequent segments with time bounds that
// are less than limit time apart.
func (s Segments) Tracks(limit time.Duration) (tracks Tracks) {
if len(s) == 0 {
return
}
p := s[0]
t := new(gpx.GPXTrack)
t.AppendSegment(p.GPXTrackSegment)
for _, s := range s[1:] {
if s.TimeBounds().StartTime.Sub(p.TimeBounds().EndTime) > limit {
tracks = append(tracks, Track{t, nil})
t = new(gpx.GPXTrack)
}
t.AppendSegment(s.GPXTrackSegment)
p = s
}
tracks = append(tracks, Track{t, nil})
return
}