-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
68 lines (61 loc) · 1.36 KB
/
exporter.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
package main
import (
"bufio"
"encoding/csv"
"io"
"strconv"
)
// ExportAsCSV is function that exports all gps point to w in CSV format
func ExportAsCSV(r GPSReader, w io.Writer) error {
csvw := csv.NewWriter(w)
for true {
p, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
record := []string{
strconv.FormatFloat(p.Latitude, 'g', -1, 64),
strconv.FormatFloat(p.Longitude, 'g', -1, 64),
strconv.FormatInt(p.Timestamp.Unix(), 10),
}
csvw.Write(record)
}
csvw.Flush()
return nil
}
// ExportAsKML is function that exports all gps point to w in KML format
//
// TODO: Use xml library
func ExportAsKML(r GPSReader, w io.Writer) error {
writer := bufio.NewWriter(w)
writer.WriteString(`
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<LineString>
<tessellate>1</tessellate>
<coordinates>`)
for true {
p, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
writer.WriteString("\n")
writer.WriteString(strconv.FormatFloat(p.Longitude, 'g', -1, 64))
writer.WriteString(",")
writer.WriteString(strconv.FormatFloat(p.Latitude, 'g', -1, 64))
writer.WriteString(",0")
}
writer.WriteString(`
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>`)
return writer.Flush()
}