forked from stilldavid/gopro-utils
/
gopro2gpx.go
95 lines (78 loc) · 1.84 KB
/
gopro2gpx.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
package main
import (
"flag"
"fmt"
"io"
"os"
"time"
"github.com/stilldavid/gopro-utils/telemetry"
"github.com/tkrajina/gpxgo/gpx"
)
func main() {
gpxData := new(gpx.GPX)
inName := flag.String("i", "", "Required: telemetry file to read")
outName := flag.String("o", "", "Required: gpx file to write")
flag.Parse()
if *inName == "" {
flag.Usage()
return
}
telemFile, err := os.Open(*inName)
if err != nil {
fmt.Printf("Cannot access telemetry file %s.\n", *inName)
os.Exit(1)
}
defer telemFile.Close()
t := &telemetry.TELEM{}
t_prev := &telemetry.TELEM{}
track := new(gpx.GPXTrack)
segment := new(gpx.GPXTrackSegment)
for {
t, err = telemetry.Read(telemFile)
if err != nil && err != io.EOF {
fmt.Println("Error reading telemetry file", err)
os.Exit(1)
} else if err == io.EOF || t == nil {
break
}
// first full, guess it's about a second
if t_prev.IsZero() {
*t_prev = *t
t.Clear()
continue
}
// process until t.Time
t_prev.FillTimes(t.Time.Time)
telems := t_prev.ShitJson()
for i, _ := range telems {
segment.AppendPoint(
&gpx.GPXPoint{
Point: gpx.Point{
Latitude: telems[i].Latitude,
Longitude: telems[i].Longitude,
Elevation: *gpx.NewNullableFloat64(telems[i].Altitude),
},
Timestamp: time.Unix(telems[i].TS/1000/1000, telems[i].TS%(1000*1000)*1000),
},
)
}
*t_prev = *t
t = &telemetry.TELEM{}
}
track.AppendSegment(segment)
gpxData.AppendTrack(track)
gpxFile, err := os.Create(*outName)
if err != nil {
fmt.Printf("Cannot make output file %s.\n", *outName)
os.Exit(1)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("Cannot close gpx file %s: %s", file.Name(), err)
os.Exit(1)
}
}(gpxFile)
xml, err := gpxData.ToXml(gpx.ToXmlParams{Version: "1.1", Indent: true})
gpxFile.Write(xml)
}