-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
234 lines (221 loc) · 6.75 KB
/
path.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package path
import (
"errors"
"fmt"
"math"
"strings"
sm "github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/lucasb-eyer/go-colorful"
"github.com/meekmichael/gpxrainbow/colorpath"
"github.com/meekmichael/gpxrainbow/config"
"github.com/meekmichael/gpxrainbow/legend"
"github.com/meekmichael/gpxrainbow/pattern"
"github.com/meekmichael/gpxrainbow/positionregistry"
"github.com/meekmichael/gpxrainbow/tile"
"github.com/tkrajina/gpxgo/gpx"
"github.com/urfave/cli/v2"
)
// Run is the main method for this project
func Run(c *cli.Context) error {
ctx := sm.NewContext()
mConf, err := config.NewConfig(c)
if err != nil {
return err
}
ctx.SetSize(mConf.ImageWidth, mConf.ImageHeight)
ctx.SetTileProvider(tile.ProviderByName(mConf.TileProvider))
gpxFiles := c.Args().Slice()
if len(gpxFiles) == 0 {
return errors.New("no file(s) specified")
}
if mConf.Mode != config.MODE_PROXIMITY {
pathData, err := maxSpeedAndElev(gpxFiles)
if err != nil {
return err
}
mConf.MinElevation = pathData.MinElevation
mConf.MaxElevation = pathData.MaxElevation
mConf.MaxSpeed = pathData.MaxSpeed
}
paths := []*colorpath.ColorPath{}
fmt.Printf("Processing %d GPX files\n", len(gpxFiles))
posRegistry := positionregistry.PositionRegistry{
MaxColors: uint16(len(gpxFiles)),
}
for _, gpxFile := range gpxFiles {
newPaths, err := gpxToColorPath(mConf, gpxFile, &posRegistry)
if err != nil {
return err
}
paths = append(paths, newPaths...)
}
for _, p := range paths {
ctx.AddObject(p)
}
img, err := ctx.Render()
if err != nil {
return err
}
legendOpts := legend.Options{
GradientTable: pattern.GetGradientTable(),
}
switch mConf.Mode {
case config.MODE_PROXIMITY:
legendOpts.MinVal = 1
legendOpts.MaxVal = float64(posRegistry.Tracks)
legendOpts.FormatString = "%2.0f"
legendOpts.Steps = posRegistry.Tracks
legendOpts.Title = "count"
case config.MODE_ELEVATION:
legendOpts.Steps = 250
if mConf.Units == "us" {
legendOpts.MinVal = mConf.MinElevation * 3.2808399
legendOpts.MaxVal = mConf.MaxElevation * 3.2808399
legendOpts.Title = "elevation (ft)"
} else {
legendOpts.MinVal = mConf.MinElevation
legendOpts.MaxVal = mConf.MaxElevation
legendOpts.Title = "elevation (meters)"
}
legendOpts.FormatString = "%2.0f"
case config.MODE_SPEED:
legendOpts.MinVal = 0
if mConf.Units == "us" {
legendOpts.MaxVal = mConf.MaxSpeed * 2.236936 // meters/s -> mph\
legendOpts.Title = "Speed (mph)"
} else {
legendOpts.MaxVal = mConf.MaxSpeed * 3.6 // meters/s -> kph
legendOpts.Title = "speed (kph)"
}
legendOpts.Steps = 250
legendOpts.FormatString = "%2.1f"
}
if mConf.Mode != config.MODE_INPUT {
img, err = legend.Render(legendOpts, img)
}
if err != nil {
return err
}
if strings.HasSuffix(mConf.OutputFile, "png") || strings.HasSuffix(mConf.OutputFile, "PNG") {
if err := gg.SavePNG(mConf.OutputFile, img); err != nil {
return err
}
} else {
if err := gg.SaveJPG(mConf.OutputFile, img, 85); err != nil {
return err
}
}
if err == nil {
fmt.Printf("Saved as %s\n", mConf.OutputFile)
}
return err
}
// AggregatePathData is aggregate information about all paths togethe
type AggregatePathData struct {
MinElevation float64
MaxElevation float64
MaxSpeed float64
}
func maxSpeedAndElev(filenames []string) (AggregatePathData, error) {
minElev := math.Inf(1)
maxElev := 0.0
maxSpeed := 0.0
for _, gpxFile := range filenames {
gpxdata, err := gpx.ParseFile(gpxFile)
if err != nil {
return AggregatePathData{}, fmt.Errorf("likely invalid GPX file %s, error: %v", gpxFile, err)
}
for _, trk := range gpxdata.Tracks {
for _, seg := range trk.Segments {
if err != nil {
return AggregatePathData{}, err
}
for i := range seg.Points {
elev := seg.Points[i].GetElevation()
if elev.NotNull() {
if elev.Value() > maxElev {
maxElev = elev.Value()
}
if elev.Value() < minElev {
minElev = elev.Value()
}
}
if i == 0 {
continue
}
spd := seg.Points[i].SpeedBetween(&seg.Points[i-1], true)
if spd > maxSpeed && spd != math.Inf(1) {
maxSpeed = spd
}
}
}
}
}
return AggregatePathData{
MinElevation: minElev,
MaxElevation: maxElev,
MaxSpeed: maxSpeed,
}, nil
}
// gpxToColorPath iterates through a single GPX file and builds a ColorPath object
// to be later drawn onto a map
func gpxToColorPath(conf config.MapConfig, filename string, posRegistry *positionregistry.PositionRegistry) ([]*colorpath.ColorPath, error) {
gpxdata, err := gpx.ParseFile(filename)
if err != nil {
return []*colorpath.ColorPath{}, err
}
paths := []*colorpath.ColorPath{}
elevDiff := conf.MaxElevation - conf.MinElevation
for _, trk := range gpxdata.Tracks {
if conf.Mode == config.MODE_INPUT || conf.Mode == config.MODE_PROXIMITY {
posRegistry.Tracks++
}
for _, seg := range trk.Segments {
lastColor := pattern.GetGradientTable().GetInterpolatedColorFor(0)
p := colorpath.NewColorPath(float64(conf.LineWidth))
spd := float64(0)
for i := 0; i < len(seg.Points); i++ {
color := colorful.Color{}
if i > 0 {
spd = seg.Points[i].SpeedBetween(&seg.Points[i-1], true) // meters/second
}
elev := seg.Points[i].Elevation
switch conf.Mode {
case config.MODE_INPUT:
color = pattern.GetGradientTable().GetInterpolatedColorFor(float64(posRegistry.Tracks) / float64(posRegistry.MaxColors))
case config.MODE_PROXIMITY:
countNear := uint16(0)
if posRegistry.Tracks > 1 {
countNear = posRegistry.CountNear(s2.LatLngFromDegrees(seg.Points[i].GetLatitude(), seg.Points[i].GetLongitude()), float64(conf.ProximityDistance))
}
color = pattern.GetGradientTable().GetInterpolatedColorFor(float64(countNear) / float64(posRegistry.MaxColors))
case config.MODE_SPEED:
// blend factor to make segments blend together better and not be wild colors
// especially useful for when there are a lot of points close together in a segment
color = pattern.GetGradientTable().GetInterpolatedColorFor(spd/conf.MaxSpeed).BlendHcl(lastColor, 0.7)
case config.MODE_ELEVATION:
if elev.NotNull() {
color = pattern.GetGradientTable().GetInterpolatedColorFor((elev.Value()-conf.MinElevation)/elevDiff).BlendHcl(lastColor, 0.5)
} else {
color = lastColor
}
}
lastColor = color
p.Positions = append(p.Positions, colorpath.Point{
Color: color,
LatLng: s2.LatLngFromDegrees(seg.Points[i].GetLatitude(), seg.Points[i].GetLongitude()),
})
}
paths = append(paths, p)
if conf.Mode == config.MODE_PROXIMITY {
posRegistry.AddFromColorPath(p, posRegistry.Tracks)
}
if conf.Mode == config.MODE_INPUT || conf.Mode == config.MODE_PROXIMITY {
posRegistry.Tracks++
}
}
}
return paths, nil
}