generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
118 lines (94 loc) · 2.79 KB
/
move.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
package main
import (
"fmt"
"os"
"path"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/tkrajina/gpxgo/gpx"
)
func moveCmd() {
var (
file string
newStart string
newEnd string
output string
indent bool
)
cmd := kingpin.Command("move", "Move track in time")
cmd.Help("When both new-start and new-end are present, the track would be " +
"\nstretched/shrinked to fit in new boundaries." +
"\nOtherwise it would be moved to the touch new-start or new-end.")
cmd.Arg("file", "GPX File to process.").Required().StringVar(&file)
cmd.Flag("new-start", "New time of track start, e.g. 2022-05-28T10:36:34Z.").StringVar(&newStart)
cmd.Flag("new-end", "New time of track end, e.g. 2022-05-28T10:36:34Z.").StringVar(&newEnd)
cmd.Flag("output", "Output file.").Default("<name>.cut.gpx").StringVar(&output)
cmd.Flag("indent", "Indent output file.").BoolVar(&indent)
cmd.Action(func(_ *kingpin.ParseContext) error {
gpxFile, err := gpx.ParseFile(file)
if err != nil {
return fmt.Errorf("error opening gpx file: %w", err)
}
var (
origMin = gpxFile.TimeBounds().StartTime
origDur = gpxFile.TimeBounds().EndTime.Sub(origMin)
newMin time.Time
newDur time.Duration
)
var delta time.Duration
if newStart != "" {
start, err := time.Parse(time.RFC3339, newStart)
if err != nil {
return fmt.Errorf("failed to parse new start time: %w", err)
}
newMin = start
delta = start.Sub(gpxFile.TimeBounds().StartTime)
}
if newEnd != "" {
end, err := time.Parse(time.RFC3339, newEnd)
if err != nil {
return fmt.Errorf("failed to parse new end time: %w", err)
}
if !newMin.IsZero() {
newDur = end.Sub(newMin)
}
delta = end.Sub(gpxFile.TimeBounds().EndTime)
}
if delta == 0 {
fmt.Println("no delta to apply")
return nil
}
fmt.Println("delta to apply:", delta.String())
gpxFile.ExecuteOnAllPoints(func(point *gpx.GPXPoint) {
if newDur != 0 {
point.Timestamp = transformTime(origMin, origDur, newMin, newDur, point.Timestamp)
} else {
point.Timestamp = point.Timestamp.Add(delta)
}
})
fmt.Println(GetGpxElementInfo("", gpxFile))
xx, err := gpxFile.ToXml(gpx.ToXmlParams{Indent: indent})
if err != nil {
return fmt.Errorf("error rendering GPX: %w", err)
}
name := strings.TrimSuffix(file, path.Ext(file))
outName := strings.ReplaceAll(output, "<name>", name)
if err = os.WriteFile(outName, xx, 0o600); err != nil {
return fmt.Errorf("failed to save GPX file: %w", err)
}
return nil
})
}
func transformTime(
origMin time.Time,
origDur time.Duration,
newMin time.Time,
newDur time.Duration,
p time.Time,
) time.Time {
pr := float64(p.Sub(origMin)) / float64(origDur)
cr := pr * float64(newDur) / float64(origDur)
c := newMin.Add(time.Duration(cr * float64(newDur)))
return c
}