-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
101 lines (84 loc) · 1.89 KB
/
path.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
package pathfilter
import (
"encoding/csv"
"os"
"strconv"
"sync"
"github.com/pkg/errors"
)
const (
earthRadius = 6371.0 * 1000 // in meters
)
var (
errInvalidRecord = errors.New("invalid record")
)
type point struct {
Lat float64
Lon float64
Timestamp int64
}
// Path is a collection of points
type Path struct {
l sync.RWMutex
points []*point
}
// NewPathFromCSV returns a path from a CSV file
// the returned *Path maintains the original order
// from the input file.
func NewPathFromCSV(filePath string) (*Path, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
return nil, err
}
var p Path
p.points = make([]*point, len(records))
for i, rec := range records {
if len(rec) != 3 {
return nil, errors.Wrap(errInvalidRecord, "length must be exactly 3")
}
var (
lat, lon float64
t int64
)
if lat, err = strconv.ParseFloat(rec[0], 64); err != nil {
return nil, errors.Wrap(errInvalidRecord, err.Error())
}
if lon, err = strconv.ParseFloat(rec[1], 64); err != nil {
return nil, errors.Wrap(errInvalidRecord, err.Error())
}
if t, err = strconv.ParseInt(rec[2], 10, 64); err != nil {
return nil, errors.Wrap(errInvalidRecord, err.Error())
}
p.points[i] = &point{
Lat: lat,
Lon: lon,
Timestamp: t,
}
}
return &p, nil
}
// ExportCSV saves a CSV version of the path
func (p *Path) ExportCSV(filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
csvWriter := csv.NewWriter(f)
for _, point := range p.points {
csvWriter.Write(
[]string{
strconv.FormatFloat(point.Lat, 'f', 9, 64),
strconv.FormatFloat(point.Lon, 'f', 9, 64),
strconv.FormatInt(point.Timestamp, 10),
})
}
csvWriter.Flush()
return csvWriter.Error()
}