forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoTrack.go
172 lines (150 loc) · 4.21 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
package main
import (
"encoding/json"
"errors"
"log"
"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
PathsJSON string
Points []*trackPoint
PointsJSON string
Kilometers string
Hours string
Name string
MapAttribution string
MinZoom, MaxZoom int
}
func (t *trackResult) hasMapFeatures() bool {
return t.Points != nil || t.Paths != nil
}
func (a *goBlog) getTrack(p *post, withMapFeatures bool) (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
log.Printf("failed to parse GPX: %v", err)
return nil, nil
}
l, _ := language.Parse(a.cfg.Blogs[p.Blog].Lang)
lp := message.NewPrinter(l)
result = &trackResult{
Name: parseResult.gpxData.Name,
}
if withMapFeatures {
// Add Paths
pathsJSON, err := json.Marshal(parseResult.paths)
if err != nil {
return nil, err
}
result.Paths = parseResult.paths
result.PathsJSON = string(pathsJSON)
// Add Points
pointsJSON, err := json.Marshal(parseResult.points)
if err != nil {
return nil, err
}
result.Points = parseResult.points
result.PointsJSON = string(pointsJSON)
// Map settings
result.MapAttribution = a.getMapAttribution()
result.MinZoom = a.getMinZoom()
result.MaxZoom = a.getMaxZoom()
}
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
)
}
return result, nil
}
type trackPoint struct {
Lat, Lon float64
}
type trackParseResult struct {
paths [][]*trackPoint
points []*trackPoint
gpxData *gpx.GPX
md *gpx.MovingData
}
func trackParseGPX(gpxString string) (result *trackParseResult, err error) {
result = &trackParseResult{}
type trackPath struct {
gpxMovingData *gpx.MovingData
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()
path := &trackPath{
gpxMovingData: &md,
}
for _, point := range segment.Points {
path.points = append(path.points, &trackPoint{
Lat: point.GetLatitude(), Lon: point.GetLongitude(),
})
}
paths = append(paths, path)
}
}
for _, route := range result.gpxData.Routes {
path := &trackPath{}
for _, point := range route.Points {
path.points = append(path.points, &trackPoint{
Lat: point.GetLatitude(), Lon: point.GetLongitude(),
})
}
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
}
}
result.points = []*trackPoint{}
for _, point := range result.gpxData.Waypoints {
result.points = append(result.points, &trackPoint{
Lat: point.GetLatitude(), Lon: point.GetLongitude(),
})
}
return result, nil
}