-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
76 lines (62 loc) · 1.47 KB
/
merge.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
package gopx
import (
"errors"
"sort"
"time"
)
// MergeByTimestamp merges gpxs based on there timetamp
func MergeByTimestamp(gpxs []*Gpx) (*Gpx, error) {
points, err := getPoints(gpxs)
if err != nil {
return nil, err
}
for _, point := range points {
_, err := time.Parse(time.RFC3339Nano, point.Timestamp)
if err != nil {
return nil, err
}
}
sort.Sort(sortByTimestamp(points))
return points.createGpx("merged"), nil
}
// MergeByDistance merges gpxs based on distance
func MergeByDistance(gpxs []*Gpx, start Point) (*Gpx, error) {
points, err := getPoints(gpxs)
if err != nil {
return nil, err
}
currentPoint := start
var sorted Points
for len(points) > 0 {
closest := points[0]
closestIndex := 0
closestDist := currentPoint.Distance(closest)
// Find the next closest point
for i, p := range points {
pDist := currentPoint.Distance(p)
if pDist < closestDist {
closest = p
closestIndex = i
closestDist = pDist
}
}
points = append(points[:closestIndex], points[closestIndex+1:]...)
sorted = append(sorted, closest)
currentPoint = closest
}
return sorted.createGpx("merged"), nil
}
// getPoints get points from gpxs.
func getPoints(gpxs []*Gpx) (Points, error) {
if len(gpxs) == 0 {
return nil, errors.New("No gpxs to merge")
}
var points Points
for _, gpx := range gpxs {
points = append(points, gpx.GetPoints()...)
}
if len(points) == 0 {
return nil, errors.New("No Points found to merge")
}
return points, nil
}