-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (119 loc) · 3.27 KB
/
main.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
package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"os"
"time"
)
type gpx struct {
TrackList []trk `xml:"trk"`
}
type trk struct {
SegmentList []trkseg `xml:"trkseg"`
}
type trkseg struct {
PointList []trkpt `xml:"trkpt"`
}
type trkpt struct {
Latitude float64 `xml:"lat,attr"`
Longitude float64 `xml:"lon,attr"`
Timestamp time.Time `xml:"time"`
}
func readGpx(filename string) (gpx, error) {
fmt.Printf("Opening %s...\n", filename)
empty := gpx{}
xmlFile, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return empty, err
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
var q gpx
xml.Unmarshal(b, &q)
// fmt.Println(q)
return q, nil
}
func isEqual(x trkpt, test trkpt) bool {
return test.Timestamp.Equal(x.Timestamp)
}
func isBefore(x trkpt, test trkpt) bool {
return test.Timestamp.Before(x.Timestamp)
}
func isAfter(x trkpt, test trkpt) bool {
return test.Timestamp.After(x.Timestamp)
}
func isInside(x trkpt, y trkpt, test trkpt) bool {
if isAfter(x, test) && isBefore(y, test) {
return true
} else {
return false
}
}
func main() {
aFile := flag.String("a", "", "A File")
bFile := flag.String("b", "", "B File")
outFilename := flag.String("o", "", "Out File")
flag.Parse()
aSet, aErr := readGpx(*aFile)
if aErr != nil {
fmt.Println(aErr)
os.Exit(1)
}
bSet, bErr := readGpx(*bFile)
if bErr != nil {
fmt.Println(bErr)
os.Exit(2)
}
fmt.Println(bSet)
outSet := aSet
for _, test_track := range bSet.TrackList {
for _, test_segment := range test_track.SegmentList {
for i, test_point := range test_segment.PointList {
fmt.Printf("Considering bSet index %d\n", i)
x := trkpt{}
y := trkpt{}
changed := false
OuterLoop:
for a, known_track := range outSet.TrackList {
for b, known_segment := range known_track.SegmentList {
for c, known_point := range known_segment.PointList {
x = y
y = known_point
if isInside(x, y, test_point) {
fmt.Printf("test_point IS inside a pairing. To insert at %d,%d,%d\n", a, b, c)
outSet.TrackList[a].SegmentList[b].PointList = append(known_segment.PointList[:c], append([]trkpt{test_point}, known_segment.PointList[c:]...)...)
changed = true
}
if isEqual(x, test_point) {
fmt.Printf("test_point appears at an equal time. Insert near %d,%d,%d\n", a, b, c)
outSet.TrackList[a].SegmentList[b].PointList = append(known_segment.PointList[:c], append([]trkpt{test_point}, known_segment.PointList[c:]...)...)
changed = true
}
if changed {
break OuterLoop
}
}
}
}
if changed == false && isAfter(y, test_point) {
fmt.Printf("test_point IS at the end. Work out the index now.\n")
a := len(outSet.TrackList) - 1
b := len(outSet.TrackList[a].SegmentList) - 1
outSet.TrackList[a].SegmentList[b].PointList = append(outSet.TrackList[a].SegmentList[b].PointList[:], test_point)
}
}
}
}
fmt.Println("Marshalling up...")
aOut, _ := xml.MarshalIndent(aSet, "", "\t")
// bOut, _ := xml.MarshalIndent(bSet, "", "\t")
// fmt.Printf("%s", bytesOut)
fmt.Println("Writing out...")
out, err := os.Create(*outFilename)
fmt.Println(err)
fmt.Fprintf(out, "%s%s", xml.Header, aOut)
defer out.Close()
}