-
Notifications
You must be signed in to change notification settings - Fork 26
/
geoTrack.go
182 lines (157 loc) · 4.47 KB
/
geoTrack.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
package main
import (
"errors"
"math"
"github.com/tkrajina/gpxgo/gpx"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
const gpxParameter = "gpx"
const showRouteParam = "showroute"
func (p *post) hasTrack() bool {
return p.firstParameter(gpxParameter) != ""
}
func (p *post) showTrackRoute() bool {
if param := p.firstParameter(showRouteParam); param == "false" {
return false
}
return true
}
type trackResult struct {
Paths [][]trackPoint
Points []trackPoint
Kilometers string
Hours string
Uphill string
Downhill string
Name string
}
func (t *trackResult) hasPath() bool {
return t.Paths != nil
}
func (t *trackResult) hasMapFeatures() bool {
return t.Points != nil || t.hasPath()
}
func (a *goBlog) getTrack(p *post) (result *trackResult, err error) {
gpxString := p.firstParameter(gpxParameter)
if gpxString == "" {
return nil, errors.New("no gpx parameter in post")
}
// Parse GPX
parseResult, err := trackParseGPX(gpxString)
if err != nil {
// Failed to parse, but just log error
a.error("failed to parse GPX", "err", err)
return nil, nil
}
l, _ := language.Parse(a.getBlogFromPost(p).Lang)
lp := message.NewPrinter(l)
result = &trackResult{
Name: parseResult.gpxData.Name,
}
// Add Paths
result.Paths = parseResult.paths
// Add Points
result.Points = parseResult.points
// Calculate moving statistics
if parseResult.md != nil {
result.Kilometers = lp.Sprintf("%.2f", parseResult.md.MovingDistance/1000)
result.Hours = lp.Sprintf(
"%.0f:%02.0f:%02.0f",
math.Floor(parseResult.md.MovingTime/3600), // Hours
math.Floor(math.Mod(parseResult.md.MovingTime, 3600)/60), // Minutes
math.Floor(math.Mod(parseResult.md.MovingTime, 60)), // Seconds
)
}
if parseResult.ud != nil {
result.Uphill = lp.Sprintf("%.0f", parseResult.ud.Uphill)
result.Downhill = lp.Sprintf("%.0f", parseResult.ud.Downhill)
}
return result, nil
}
type trackPoint [2]float64 // Lat, Lon
func (p *trackPoint) Lat() float64 {
return p[0]
}
func (p *trackPoint) Lon() float64 {
return p[1]
}
type trackParseResult struct {
paths [][]trackPoint
points []trackPoint
gpxData *gpx.GPX
md *gpx.MovingData
ud *gpx.UphillDownhill
}
func trackParseGPX(gpxString string) (result *trackParseResult, err error) {
trunc := func(num float64) float64 {
return float64(int64(num*100000)) / 100000
}
result = &trackParseResult{}
type trackPath struct {
gpxMovingData *gpx.MovingData
gpxUphillDownhill *gpx.UphillDownhill
points []trackPoint
}
result.gpxData, err = gpx.ParseString(gpxString)
if err != nil {
return nil, err
}
paths := make([]trackPath, 0)
for _, track := range result.gpxData.Tracks {
for _, segment := range track.Segments {
md := segment.MovingData()
ud := segment.UphillDownhill()
path := trackPath{
gpxMovingData: &md,
gpxUphillDownhill: &ud,
}
for _, point := range segment.Points {
path.points = append(path.points, trackPoint{
trunc(point.Latitude), trunc(point.Longitude),
})
}
paths = append(paths, path)
}
}
for _, route := range result.gpxData.Routes {
path := trackPath{}
for _, point := range route.Points {
path.points = append(path.points, trackPoint{
trunc(point.Latitude), trunc(point.Longitude),
})
}
paths = append(paths, path)
}
result.paths = make([][]trackPoint, len(paths))
for i, path := range paths {
// Add points
result.paths[i] = path.points
// Combine moving data
if path.gpxMovingData != nil {
if result.md == nil {
result.md = &gpx.MovingData{}
}
result.md.MaxSpeed = math.Max(result.md.MaxSpeed, path.gpxMovingData.MaxSpeed)
result.md.MovingDistance = result.md.MovingDistance + path.gpxMovingData.MovingDistance
result.md.MovingTime = result.md.MovingTime + path.gpxMovingData.MovingTime
result.md.StoppedDistance = result.md.StoppedDistance + path.gpxMovingData.StoppedDistance
result.md.StoppedTime = result.md.StoppedTime + path.gpxMovingData.StoppedTime
}
// Combine uphill/downhill
if path.gpxUphillDownhill != nil {
if result.ud == nil {
result.ud = &gpx.UphillDownhill{}
}
result.ud.Uphill = result.ud.Uphill + path.gpxUphillDownhill.Uphill
result.ud.Downhill = result.ud.Downhill + path.gpxUphillDownhill.Downhill
}
}
result.points = []trackPoint{}
for _, point := range result.gpxData.Waypoints {
result.points = append(result.points, trackPoint{
trunc(point.Latitude), trunc(point.Longitude),
})
}
return result, nil
}