-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpl.go
89 lines (79 loc) · 2 KB
/
simpl.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
package main
import (
"gopkg.in/urfave/cli.v1"
log "github.com/Sirupsen/logrus"
"github.com/ptrv/go-gpx"
"errors"
"github.com/and-hom/gpx-cli/util"
)
func trksimpl(c *cli.Context) error {
if (len(c.Args()) == 0) {
log.Warn("No input files - exiting")
return errors.New("Input is missing")
}
if (len(c.Args()) > 1) {
log.Warn("Can not process more then one file")
return errors.New("Too much files")
}
target, err := util.GetTarget(c.String("out"))
if (err != nil) {
return err
}
defer target.Close()
minPoints := int(c.Uint("min-points"))
if minPoints == 0 {
minPoints = 20
}
maxDist := c.Int("max-dist")
if maxDist == 0 {
maxDist = 20
}
log.Infof("Removing clusters greater then %d points with distance not more then %d meters\n", minPoints, maxDist)
util.WithGpxFiles(c.Args(), func(fileName string, gpxData *gpx.Gpx) {
util.ModifyWaypointsBySegment(gpxData, target, func(wpts *[]gpx.Wpt) (*[]gpx.Wpt, bool) {
size := len(*wpts)
if (size == 0) {
return wpts, false
}
resultPts := make([]gpx.Wpt, size)
var count = 1
var clusterSize = 0;
var changed = false
resultPts[0] = (*wpts)[0]
for i := 1; i < size; i++ {
jLim := size - i
clusterSize = 0
for j := 1; j < jLim; j++ {
lastPoint := (j == jLim - 1)
if (*wpts)[i].Distance2D(&((*wpts)[i + j])) < float64(maxDist) && !lastPoint {
clusterSize++
} else {
if (lastPoint) {
clusterSize++
}
lastIdx := (i + j - 2) // -2 to keep last point of the cluster
if clusterSize >= minPoints {
log.Infof("Drop points from [%f %f] %s to %s: %d\n",
(*wpts)[i].Lat,
(*wpts)[i].Lon,
(*wpts)[i].Timestamp,
(*wpts)[lastIdx].Timestamp,
clusterSize)
i = lastIdx
changed = true
}
break
}
}
resultPts[count] = (*wpts)[i]
count++
}
finalPts := make([]gpx.Wpt, count)
for i := 0; i < count; i++ {
finalPts[i] = resultPts[i]
}
return &finalPts, changed
})
})
return nil
}