-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.go
217 lines (197 loc) · 5.53 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"strings"
"time"
"github.com/bradfitz/latlong"
"github.com/tkrajina/gpxgo/gpx"
)
type Segment struct {
gpx *gpx.GPXTrackSegment
filename string
Points Points
// Analysis results
AvgHeading int
HeadingVariation int
Distance float64
AvgSpeed float64
Duration time.Duration
Start time.Time
End time.Time
Mode Mode
// POS pointOfSail
}
func SegmentFromPoints(ps Points, mode Mode, filename string) *Segment {
gpxPoints := make([]gpx.GPXPoint, len(ps))
for i, p := range ps {
gpxPoints[i] = *p.gpx
}
return &Segment{
gpx: &gpx.GPXTrackSegment{Points: gpxPoints},
filename: filename,
Points: ps,
AvgHeading: ps.averageHeading(),
HeadingVariation: ps.headingVariation(),
Distance: ps.distance(),
AvgSpeed: ps.averageSpeed(),
Start: ps.start(),
End: ps.end(),
Duration: ps.duration(),
Mode: mode,
}
}
// gpxPoint returns i-th point of the segment.
func (s *Segment) gpxPoint(i int) *gpx.GPXPoint {
return &s.gpx.Points[i]
}
// gpxEachPair iterates over a segment with pairs of subsequent points.
func (s *Segment) gpxEachPair(f func(prev, next *gpx.GPXPoint)) {
prev := s.gpxPoint(0)
for i := 1; i < len(s.gpx.Points); i++ {
next := s.gpxPoint(i)
f(prev, next)
prev = next
}
}
func (s *Segment) Timezone() *time.Location {
b := s.gpx.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) gpxString() string {
tb := s.gpx.TimeBounds()
return fmt.Sprintf("%s @ %s = %05.2fnm (%d)",
tb.EndTime.Sub(tb.StartTime),
tb.StartTime.In(s.Timezone()).Format(strFormat),
s.gpx.Length2D()/1852,
s.gpx.GetTrackPointsNo(),
)
}
func (s *Segment) String() string {
return fmt.Sprintf("%.0fm/%.0fs @ %.1f kts \u2191 %d\u00b0 %s < %d\u00b0 %s (%d)",
s.Distance, s.Duration.Seconds(), s.AvgSpeed, s.AvgHeading, Direction(s.AvgHeading).String(), s.HeadingVariation, s.Mode, len(s.Points))
}
// gpxSplit segment where the time difference between points is more than @limit.
func (s *Segment) gpxSplit(limit time.Duration) Segments {
limitSeconds := limit.Seconds()
prev := s.gpxPoint(len(s.gpx.Points) - 1)
for i := len(s.gpx.Points) - 2; i >= 0; i-- {
next := s.gpxPoint(i)
if next.TimeDiff(prev) > limitSeconds {
s1, s2 := s.gpx.Split(i)
return append((&Segment{gpx: s1, filename: s.filename}).gpxSplit(limit), &Segment{gpx: s2, filename: s.filename})
}
prev = next
}
return Segments{s}
}
// gpxAnalyze the segment and split it up into runs of points of the same Mode of movement (static, moving, turning).
// The Map is the context to use for the analysis derived from the Track, it is the same for all segments of the track.
func (s *Segment) gpxAnalyze(m *Map, params *AnalysisParameters) Segments {
previous := &Point{gpx: s.gpxPoint(0)}
points := Points{previous}
s.gpxEachPair(func(prev, next *gpx.GPXPoint) {
nextPoint := &Point{
gpx: next,
previous: previous,
Heading: m.Heading(prev, next),
Distance: m.Distance(prev, next, params.distanceUnit),
Speed: m.Speed(prev, next, params.speedUnit),
}
previous.next = nextPoint
previous = nextPoint
points = append(points, nextPoint)
})
for _, p := range points {
p.Analyze(params)
}
segments := Segments{}
points.eachRun(func(run Points, mode Mode) {
segments = append(segments, SegmentFromPoints(run, mode, s.filename))
})
return segments
}
type Segments []*Segment
func gpxGetSegments(g *gpx.GPX, filename string) (s Segments) {
for _, t := range g.Tracks {
for i := range t.Segments {
s = append(s, &Segment{gpx: &t.Segments[i], filename: filename})
}
}
return s
}
func (ss Segments) gpxString() string {
var all []string
for _, s := range ss {
all = append(all, s.gpxString())
}
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].gpx.TimeBounds().StartTime.Before(s[j].gpx.TimeBounds().StartTime)
}
// gpxDedupe removes subsequent segments with the same time bounds
// and segments that have less than @min points.
func (s Segments) gpxDedupe(min int) (t Segments) {
if len(s) == 0 {
return
}
p := s[0]
t = append(t, p)
for _, s := range s[1:] {
if s.gpx.TimeBounds().Equals(p.gpx.TimeBounds()) {
continue
}
if s.gpx.GetTrackPointsNo() > min {
t = append(t, s)
}
p = s
}
return
}
func (s Segments) gpxSplit(limit time.Duration) (t Segments) {
for _, seg := range s {
t = append(t, seg.gpxSplit(limit)...)
}
return t
}
// gpxTracks creates tracks from subsequent segments with time bounds that
// are less than limit time apart.
func (ss Segments) gpxTracks(limit time.Duration) (tracks Tracks) {
if len(ss) == 0 {
return
}
p := ss[0]
t := new(gpx.GPXTrack)
t.AppendSegment(p.gpx)
for _, s := range ss[1:] {
if s.gpx.TimeBounds().StartTime.Sub(p.gpx.TimeBounds().EndTime) > limit {
tracks = append(tracks, Track{gpx: t, filename: s.filename})
t = new(gpx.GPXTrack)
}
t.AppendSegment(s.gpx)
p = s
}
tracks = append(tracks, Track{gpx: t, filename: p.filename})
return
}
func (ss Segments) EachPair(f func(prev *Point, next *Point)) {
var prev *Point
for _, s := range ss {
for _, p := range s.Points {
if prev == nil {
prev = p
continue
}
f(prev, p)
prev = p
}
}
}