/
plot.go
136 lines (118 loc) · 3.66 KB
/
plot.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
package pkg
import (
"fmt"
sm "github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"image/color"
"math/rand"
"strings"
)
var (
Red = color.RGBA{R: uint8(255), G: uint8(0), B: uint8(0), A: 0xff}
Black = color.RGBA{R: uint8(0), G: uint8(0), B: uint8(0), A: 0xff}
Transparent = color.RGBA{R: uint8(0), G: uint8(0), B: uint8(0), A: 0}
)
func Plot(data *TrackData, config PlotConfig) error {
fmt.Printf("Plotting your map in [Width/Height] [%d, %d]\n", config.ImageWidth, config.ImageHeight)
laps := data.Laps
if config.FastestLapOnly {
// this method is guaranteed to only have a single lap
laps = filterFastestLap(laps)
fmt.Printf("Plotting the fastest Lap [%s]\n", getLapDuration(laps[0]).String())
}
measures := data.GPSMeasurement
if config.UseSmoothedGPSData {
measures = data.FilteredGPSMeasurement
}
gpsErrorStdDevMeters := stddev(measures,
func(measurement GPSMeasurement) float64 {
return measurement.accuracyMeter
})
outputFile := config.OutputFile
pathColor := Black
ctx := newPlotContext(config, "")
for lapNum := 0; lapNum < len(laps); lapNum++ {
if config.PlotLapsSeparately {
ctx = newPlotContext(config, fmt.Sprintf("Lap Time: %s", getLapDuration(laps[lapNum]).String()))
} else {
pathColor = color.RGBA{R: uint8(rand.Intn(255)), G: uint8(rand.Intn(255)), B: uint8(rand.Intn(255)), A: 0xff}
}
addStartEndZone(ctx, data.TrackInformation.startLatLng, gpsErrorStdDevMeters)
addLapPathToContext(laps[lapNum], measures, ctx, pathColor)
if config.PlotLapsSeparately {
outFileLap := fmt.Sprintf("%s_lap_%d.png", outputFile, lapNum)
if lapNum == 0 {
outFileLap = fmt.Sprintf("%s_outlap.png", outputFile)
} else if lapNum == len(laps)-1 {
outFileLap = fmt.Sprintf("%s_inlap.png", outputFile)
}
err := renderAndSave(ctx, outFileLap)
if err != nil {
return err
}
}
}
if !config.PlotLapsSeparately {
err := renderAndSave(ctx, outputFile)
if err != nil {
return err
}
}
return nil
}
func renderAndSave(ctx *sm.Context, outputFile string) error {
img, err := ctx.Render()
if err != nil {
return err
}
if !strings.HasSuffix(strings.ToLower(outputFile), ".png") {
outputFile = outputFile + ".png"
}
if err := gg.SavePNG(outputFile, img); err != nil {
return err
}
fmt.Printf("Saved %s\n", outputFile)
return nil
}
func addLapPathToContext(lap Lap, measures []GPSMeasurement, ctx *sm.Context, color color.RGBA) {
lapSet := MeasuresForLap(lap, measures)
positions := make([]s2.LatLng, len(lapSet))
for i := 0; i < len(lapSet); i++ {
// fmt.Printf("[%f, %f]\n", lapSet[i].latLng[0], lapSet[i].latLng[1])
positions[i] = s2LatLngFromSlice(lapSet[i].latLng)
}
lapPath := sm.NewPath(positions, color, 2.0)
ctx.AddPath(lapPath)
}
func addStartEndZone(ctx *sm.Context, startLatLng []float64, radius float64) {
ctx.AddCircle(&sm.Circle{
Position: s2LatLngFromSlice(startLatLng),
Radius: radius,
Color: Red,
Fill: Transparent,
Weight: 2})
}
func filterFastestLap(laps []Lap) []Lap {
fastestIndex := 0
min := laps[fastestIndex].timeSeconds
for i, lap := range laps {
if lap.timeSeconds < min {
fastestIndex = i
min = lap.timeSeconds
}
}
laps = []Lap{laps[fastestIndex]}
return laps
}
func s2LatLngFromSlice(slice []float64) s2.LatLng {
return s2.LatLngFromDegrees(slice[0], slice[1])
}
func newPlotContext(config PlotConfig, attributionHackString string) *sm.Context {
ctx := sm.NewContext()
ctx.SetSize(config.ImageWidth, config.ImageHeight)
provider := sm.NewTileProviderOpenStreetMaps()
provider.Attribution = fmt.Sprintf("%s | %s", provider.Attribution, attributionHackString)
ctx.SetTileProvider(provider)
return ctx
}