-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx.go
64 lines (53 loc) · 1.21 KB
/
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
package main
import (
"encoding/xml"
"io/ioutil"
"strconv"
"github.com/golang/geo/s2"
)
type Waypoint struct {
Latitude string `xml:"lat,attr"`
Longitude string `xml:"lon,attr"`
}
type Segment struct {
Waypoints []Waypoint `xml:"trkpt"`
}
type Track struct {
Segments []Segment `xml:"trkseg"`
}
type Gpx struct {
Version string `xml:"version,attr"`
Tracks []Track `xml:"trk"`
}
// GetPoints returns an iterator over all points in the GPX file.
func (g *Gpx) GetPoints() <-chan s2.LatLng {
ch := make(chan s2.LatLng)
go func() {
for _, track := range g.Tracks {
for _, segment := range track.Segments {
for _, waypoint := range segment.Waypoints {
lat, _ := strconv.ParseFloat(waypoint.Latitude, 64)
lng, _ := strconv.ParseFloat(waypoint.Longitude, 64)
latlng := s2.LatLngFromDegrees(lat, lng)
ch <- latlng
}
}
}
close(ch)
}()
return ch
}
// ParseGpx returns track and waypoint information from a GPX file given by its
// filename.
func ParseGpx(filename string) (Gpx, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return Gpx{}, err
}
parsed := Gpx{}
err = xml.Unmarshal(data, &parsed)
if err != nil {
return Gpx{}, err
}
return parsed, nil
}