-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_gpx.go
153 lines (131 loc) · 2.97 KB
/
format_gpx.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
150
151
152
153
package gogpslib
import (
"bytes"
"encoding/xml"
"html/template"
"io/ioutil"
"log"
"os"
"time"
"github.com/nolka/gogpslib/writer"
)
type link struct {
Href string `xml:"href,attr"`
Text string `xml:"text"`
Type string `xml:"type"`
}
type author struct {
Name string `xml:"name"`
Link link `xml:"link"`
}
type metadata struct {
Name string `xml:"name"`
Author author `xml:"author"`
Copyright string `xml:"copyright"`
Link link `xml:"link"`
Time time.Time `xml:"time"`
}
type trkpt struct {
Lat string `xml:"lat,attr"`
Lon string `xml:"lon,attr"`
Ele string `xml:"ele"`
Time string `xml:"time"`
}
type trkseg struct {
Trkpt []trkpt `xml:"trkpt"`
}
type trk struct {
Name string `xml:"name"`
Trkseg trkseg `xml:"trkseg"`
}
type gpx struct {
Medatada metadata `xml:"metadata"`
Trk []trk `xml:"trk"`
}
type GpxFormat struct {
Gpx gpx
Segments []TrackSegment
}
func (s *GpxFormat) GetSegments() []TrackSegment {
var segs []TrackSegment
for _, _trk := range s.Gpx.Trk {
var points []GeoPoint
for _, pt := range _trk.Trkseg.Trkpt {
point := GeoPoint{}
point.Lat = StrToFloat(pt.Lat)
point.Lon = StrToFloat(pt.Lon)
if pt.Ele != "" {
point.Altitude = StrToFloat(pt.Ele)
}
timeFormat := "2006-01-02T03:04:05Z"
dt, err := time.Parse(timeFormat, pt.Time)
if err == nil {
point.DateTime = dt
} else {
log.Println(err)
}
points = append(points, point)
}
var seg TrackSegment
seg.Name = _trk.Name
seg.GeoPoints = points
segs = append(segs, seg)
}
return segs
}
func (s *GpxFormat) SetSegments(segments []TrackSegment) {
s.Segments = segments
}
func (s *GpxFormat) GetGeoPointsCount() int {
var count int = 0
for _, s := range s.GetSegments() {
count += s.GetPointsCount()
}
return count
}
func (s *GpxFormat) Read(fileName string) {
file, err := os.Open(fileName)
if err != nil {
log.Print(err)
return
}
defer file.Close()
xmlBytes, _ := ioutil.ReadAll(file)
err = xml.Unmarshal(xmlBytes, &s.Gpx)
if err != nil {
log.Fatal(err)
}
}
func (s *GpxFormat) WriteSegments(w writer.Writer) {
tpl, err := template.New("gpxfile").Parse(GpxTemplate)
if err != nil {
log.Print(err)
return
}
var b bytes.Buffer
err = tpl.Execute(&b, s)
if err != nil {
log.Print(err)
return
}
w.Append(b.String())
}
const GpxTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<gpx
version="1.0"
creator="` + LibName + `"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<metadata>
</metadata>{{range .GetSegments}}
<trk>
<name>{{.Name}}</name>
<trkseg>{{range .GeoPoints}}
<trkpt lat="{{.Lat}}" lon="{{.Lon}}">
{{if gt .Altitude 0.0}}<ele>{{.Altitude}}</ele>{{end}}
<time>{{.DateTime.Format "2006-01-02T03:04:05Z"}}</time>
</trkpt>{{end}}
</trkseg>
</trk>{{end}}
</gpx>`