-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpx.go
620 lines (555 loc) · 14.6 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package gpxgo
import (
"bytes"
"encoding/xml"
"fmt"
"golang.org/x/net/html/charset"
"io"
"math"
"os"
)
type Waypoints []Wpt
type Person struct {
XMLName xml.Name `xml:"author"`
Name string `xml:"name,omitempty"`
Email *Email `xml:"email,omitempty"`
Link *Link `xml:"link,omitempty"`
}
type Email struct {
XMLName xml.Name `xml:"email"`
Id string `xml:"id,attr"`
Domain string `xml:"domain,attr"`
}
type Link struct {
XMLName xml.Name `xml:"link"`
Href string `xml:"href,attr"`
Text string `xml:"text,omitempty"`
Type string `xml:"type,omitempty"`
}
type Extensions struct {
XMLName xml.Name `xml:"extensions"`
Info string `xml:",innerxml"`
}
type Copyright struct {
XMLName xml.Name `xml:"copyright,omitempty"`
Author string `xml:"author,attr"`
Year string `xml:"year,omitempty"`
License string `xml:"license,omitempty"`
}
type Bounds struct {
XMLName xml.Name `xml:"bounds"`
MinLat float64 `xml:"minlat,omitempty"`
MinLon float64 `xml:"minlon,omitempty"`
MaxLat float64 `xml:"maxlat,omitempty"`
MaxLon float64 `xml:"maxlon,omitempty"`
}
type Metadata struct {
XMLName xml.Name `xml:"metadata"`
Name string `xml:"name,omitempty"`
Desc string `xml:"desc,omitempty"`
Author *Person `xml:"author,omitempty"`
Copyright *Copyright `xml:"copyright,omitempty"`
Link []Link `xml:"link,omitempty"`
Time string `xml:"time,omitempty"`
Keywords string `xml:"keywords,omitempty"`
Bounds *Bounds `xml:"bounds"`
Extensions *Extensions `xml:"extensions,omitempty"`
}
type Wpt struct {
// XMLName xml.Name `xml:"wpt"`
// attr
Lat float64 `xml:"lat,attr"`
Lon float64 `xml:"lon,attr"`
//
Ele float64 `xml:"ele,omitempty"`
Time string `xml:"time,omitempty"`
Magvar string `xml:"magvar,omitempty"`
Geoidheight string `xml:"geoidheight,omitempty"`
Name string `xml:"name,omitempty"`
Cmt string `xml:"cmt,omitempty"`
Desc string `xml:"desc,omitempty"`
Src string `xml:"src,omitempty"`
Link []Link `xml:"link,omitempty"`
Sym string `xml:"sym,omitempty"`
Type string `xml:"type,omitempty"`
Fix string `xml:"fix,omitempty"`
Sat uint `xml:"sat,omitempty"`
Hdop float64 `xml:"hdop,omitempty"`
Vdop float64 `xml:"vdop,omitempty"`
Pdop float64 `xml:"pdop,omitempty"`
Ageofdgpsdata float64 `xml:"ageofdgpsdata,omitempty"`
Dgpsid int `xml:"dgpsid,omitempty"`
Extensions *Extensions `xml:"extensions,omitempty"`
}
type Rte struct {
XMLName xml.Name `xml:"rte"`
Name string `xml:"name,omitempty"`
Cmt string `xml:"cmt,omitempty"`
Desc string `xml:"desc,omitempty"`
Src string `xml:"src,omitempty"`
Link []Link `xml:"link,omitempty"`
Number uint `xml:"number,omitempty"`
Type string `xml:"type,omitempty"`
Extensions string `xml:"extensions,omitempty"`
Waypoints Waypoints `xml:"rtept,omitempty"`
}
type Trkseg struct {
XMLName xml.Name `xml:"trkseg"`
Waypoints Waypoints `xml:"trkpt"`
Extensions *Extensions `xml:"extensions,omitempty"`
}
type Trk struct {
XMLName xml.Name `xml:"trk"`
Name string `xml:"name,omitempty"`
Cmt string `xml:"cmt,omitempty"`
Desc string `xml:"desc,omitempty"`
Src string `xml:"src,omitempty"`
Link []Link `xml:"link,omitempty"`
Number uint `xml:"number,omitempty"`
Type string `xml:"type,omitempty"`
Extensions string `xml:"extensions,omitempty"`
Segments []Trkseg `xml:"trkseg,omitempty"`
}
type Gpx struct {
XMLName xml.Name `xml:"gpx"`
XMLNs string `xml:"xmlns,attr"`
XMLNsXsi string `xml:"xmlns:xsi,attr"`
XMLSchemaLoc string `xml:"xsi:schemaLocation,attr"`
Version string `xml:"version,attr"`
Creator string `xml:"creator,attr"`
Metadata *Metadata `xml:"metadata,omitempty"`
Waypoints Waypoints `xml:"wpt,omitempty"`
Routes []Rte `xml:"rte,omitempty"`
Tracks []Trk `xml:"trk,omitempty"`
Extensions string `xml:"extensions,omitempty"`
}
type TimeBounds struct {
StartTime float64
EndTime float64
}
/*==========================================================*/
// Static
func ParseWithContent(content []byte) (*Gpx, error) {
gpx := NewGpx()
err := xml.Unmarshal(content, gpx)
if err != nil {
return nil, err
}
return gpx, nil
}
func ParseWithReader(o io.Reader) (*Gpx, error) {
gpx := NewGpx()
d := xml.NewDecoder(o)
d.CharsetReader = charset.NewReaderLabel
err := d.Decode(gpx)
if err != nil {
return nil, err
}
return gpx, nil
}
func ParseWithPath(path string) (*Gpx, error) {
file, err := os.Open(path)
defer file.Close()
if err != nil {
return nil, err
}
return ParseWithReader(file)
}
func NewGpx() *Gpx {
return &Gpx{
XMLNs: "http://www.topografix.com/GPX/1/1",
XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XMLSchemaLoc: "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd",
Version: "1.1",
Creator: "https://github.com/pikeszfish/gpxgo",
}
}
/*==========================================================*/
// Bounds
func minBounds() *Bounds {
return &Bounds{
MaxLat: -math.MaxFloat64,
MinLat: math.MaxFloat64,
MaxLon: -math.MaxFloat64,
MinLon: math.MaxFloat64,
}
}
func (b *Bounds) merge(b2 *Bounds) {
b.MaxLat = math.Max(b.MaxLat, b2.MaxLat)
b.MinLat = math.Min(b.MinLat, b2.MinLat)
b.MaxLon = math.Max(b.MaxLon, b2.MaxLon)
b.MinLon = math.Min(b.MinLon, b2.MinLon)
}
func (b Bounds) String() string {
return fmt.Sprintf("Min: %+v, %+v Max: %+v, %+v",
b.MinLat, b.MinLon, b.MaxLat, b.MaxLon)
}
/*==========================================================*/
// Gpx
func toXML(n interface{}) []byte {
content, _ := xml.MarshalIndent(n, "", " ")
return content
}
func (g *Gpx) ToXML() []byte {
var buffer bytes.Buffer
buffer.WriteString(xml.Header)
buffer.Write(toXML(g))
return buffer.Bytes()
}
func (g *Gpx) Clone() *Gpx {
newgpx := new(Gpx)
newgpx.XMLNs = g.XMLNs
newgpx.XMLNsXsi = g.XMLNsXsi
newgpx.XMLSchemaLoc = g.XMLSchemaLoc
newgpx.Version = g.Version
newgpx.Creator = g.Creator
if g.Metadata != nil {
newgpx.Metadata = &Metadata{
Name: g.Metadata.Name,
Desc: g.Metadata.Desc,
Link: make([]Link, len(g.Metadata.Link)),
Time: g.Metadata.Time,
Keywords: g.Metadata.Keywords,
Extensions: g.Metadata.Extensions,
}
copy(newgpx.Metadata.Link, g.Metadata.Link)
if g.Metadata.Author != nil {
newgpx.Metadata.Author = &Person{
Name: g.Metadata.Author.Name,
}
if g.Metadata.Author.Email != nil {
newgpx.Metadata.Author.Email = &Email{
Id: g.Metadata.Author.Email.Id,
Domain: g.Metadata.Author.Email.Domain,
}
}
if g.Metadata.Author.Link != nil {
newgpx.Metadata.Author.Link = &Link{
Href: g.Metadata.Author.Link.Href,
Text: g.Metadata.Author.Link.Text,
Type: g.Metadata.Author.Link.Type,
}
}
}
if g.Metadata.Copyright != nil {
newgpx.Metadata.Copyright = &Copyright{
Author: g.Metadata.Copyright.Author,
Year: g.Metadata.Copyright.Year,
License: g.Metadata.Copyright.License,
}
}
if g.Metadata.Bounds != nil {
newgpx.Metadata.Bounds = &Bounds{
MaxLat: g.Metadata.Bounds.MaxLat,
MinLat: g.Metadata.Bounds.MinLat,
MaxLon: g.Metadata.Bounds.MaxLon,
MinLon: g.Metadata.Bounds.MinLon,
}
}
}
newgpx.Waypoints = make([]Wpt, len(g.Waypoints))
newgpx.Routes = make([]Rte, len(g.Routes))
newgpx.Tracks = make([]Trk, len(g.Tracks))
copy(newgpx.Waypoints, g.Waypoints)
copy(newgpx.Routes, g.Routes)
copy(newgpx.Tracks, g.Tracks)
return newgpx
}
func (g *Gpx) Bounds() *Bounds {
b := minBounds()
for _, trk := range g.Tracks {
b.merge(trk.Bounds())
}
return b
}
func (g *Gpx) Length2D() float64 {
var length2d float64
for _, trk := range g.Tracks {
length2d += trk.Length2D()
}
return length2d
}
func (g *Gpx) Length3D() float64 {
var length3d float64
for _, trk := range g.Tracks {
length3d += trk.Length3D()
}
return length3d
}
func (g *Gpx) UphillDownhill() (float64, float64) {
var (
uphill float64
downhill float64
)
for _, trk := range g.Tracks {
u, d := trk.UphillDownhill()
uphill += u
downhill += d
}
return uphill, downhill
}
func (g *Gpx) ElevationExtremes() (min float64, max float64) {
var (
elevations []float64
)
for _, trk := range g.Tracks {
min, max = trk.ElevationExtremes()
elevations = append(elevations, min)
elevations = append(elevations, max)
}
min = elevations[0]
max = elevations[0]
for _, elevation := range elevations {
if elevation < min {
min = elevation
}
if elevation > max {
max = elevation
}
}
return min, max
}
func (g *Gpx) RemoveTime() {
for _, trk := range g.Tracks {
trk.RemoveTime()
}
}
func (g *Gpx) RemoveElevation() {
for _, trk := range g.Tracks {
trk.RemoveElevation()
}
}
/*==========================================================*/
// Routes
func (r *Rte) Length2D() float64 {
var length2d float64
for i := 1; i < len(r.Waypoints); i++ {
length2d += r.Waypoints[i].Length2D(&r.Waypoints[i-1])
}
return length2d
}
func (r *Rte) Length3D() float64 {
var length3d float64
for i := 1; i < len(r.Waypoints); i++ {
length3d += r.Waypoints[i].Length3D(&r.Waypoints[i-1])
}
return length3d
}
func (r *Rte) RemoveTime() {
for _, waypoint := range r.Waypoints {
waypoint.RemoveTime()
}
}
func (r *Rte) RemoveElevation() {
for _, waypoint := range r.Waypoints {
waypoint.RemoveElevation()
}
}
/*==========================================================*/
// Tracks
func (t *Trk) Bounds() *Bounds {
b := minBounds()
for _, seg := range t.Segments {
b.merge(seg.Bounds())
}
return b
}
func (t *Trk) Length2D() float64 {
var length2d float64
for _, seg := range t.Segments {
length2d += seg.Length2D()
}
return length2d
}
func (t *Trk) Length3D() float64 {
var length3d float64
for _, seg := range t.Segments {
length3d += seg.Length3D()
}
return length3d
}
func (t *Trk) RemoveTime() {
for _, seg := range t.Segments {
seg.RemoveTime()
}
}
func (t *Trk) UphillDownhill() (float64, float64) {
var (
uphill float64
downhill float64
)
for _, seg := range t.Segments {
u, d := seg.UphillDownhill()
uphill += u
downhill += d
}
return uphill, downhill
}
func (t *Trk) ElevationExtremes() (min float64, max float64) {
var (
elevations []float64
)
for _, seg := range t.Segments {
min, max = seg.ElevationExtremes()
elevations = append(elevations, min)
elevations = append(elevations, max)
}
min = elevations[0]
max = elevations[0]
for _, elevation := range elevations {
if elevation < min {
min = elevation
}
if elevation > max {
max = elevation
}
}
return min, max
}
func (t *Trk) RemoveElevation() {
for _, seg := range t.Segments {
seg.RemoveElevation()
}
}
func (t Trk) String() string {
return fmt.Sprintf("Name: %s Segment Count: %d", t.Name, len(t.Segments))
}
/*==========================================================*/
// Trkseg
func (ts *Trkseg) Bounds() *Bounds {
b := minBounds()
b2 := minBounds()
for _, wp := range ts.Waypoints {
b2.MaxLat = wp.Lat
b2.MaxLon = wp.Lon
b2.MinLat = wp.Lat
b2.MinLon = wp.Lon
b.merge(b2)
}
return b
}
func (ts *Trkseg) Length2D() float64 {
var length2d float64
for i := 1; i < len(ts.Waypoints); i++ {
length2d += ts.Waypoints[i].Length2D(&ts.Waypoints[i-1])
}
return length2d
}
func (ts *Trkseg) Length3D() float64 {
var length3d float64
for i := 1; i < len(ts.Waypoints); i++ {
length3d += ts.Waypoints[i].Length3D(&ts.Waypoints[i-1])
}
return length3d
}
func (ts Trkseg) String() string {
return fmt.Sprintf("Waypoints Count: %d", len(ts.Waypoints))
}
func (ts *Trkseg) UphillDownhill() (uphill float64, downhill float64) {
var (
smoothedElevations []float64
previousEle, currentEle, nextEle float64
f float64
)
if ts.Waypoints == nil || len(ts.Waypoints) <= 1 {
return 0.0, 0.0
}
smoothedElevations = append(smoothedElevations, ts.Waypoints[0])
for i := 0; i < len(ts.Waypoints); i++ {
if i > 0 && i < len(ts.Waypoints)-1 {
previousEle = ts.Waypoints[i-1].Ele
currentEle = ts.Waypoints[i].Ele
nextEle = ts.Waypoints[i+1].Ele
smoothedElevations = append(smoothedElevations, previousEle*0.3+currentEle*0.4+nextEle*0.3)
}
}
smoothedElevations = append(smoothedElevations, ts.Waypoints[len(ts.Waypoints)-1])
for index, ele := range smoothedElevations {
if index == 0 {
continue
}
d = ele - smoothedElevations[index-1]
if d > 0 {
uphill += d
} else {
downhill -= d
}
}
return uphill, downhill
}
func (ts *Trkseg) ElevationExtremes() (min float64, max float64) {
min = 0.0
max = 0.0
for _, wp := range ts.Waypoints {
if wp.Ele < min {
min = wp.Ele
}
if wp.Ele > max {
max = wp.Ele
}
}
return min, max
}
func (ts *Trkseg) RemoveTime() {
for _, wp := range ts.Waypoints {
wp.RemoveTime()
}
}
func (ts *Trkseg) RemoveElevation() {
for _, wp := range ts.Waypoints {
wp.RemoveElevation()
}
}
/*==========================================================*/
// Wpt
func (wp Wpt) String() string {
return fmt.Sprintf("Wpt Lat: %f Lon: %f Name: %s \n", wp.Lat, wp.Lon, wp.Name)
}
func (wp *Wpt) Length2D(wp2 *Wpt) float64 {
return Distance(wp.Lat, wp.Lon, 0.0, wp2.Lat, wp2.Lon, 0.0, false, false)
}
func (wp *Wpt) Length3D(wp2 *Wpt) float64 {
return Distance(wp.Lat, wp.Lon, wp.Ele, wp2.Lat, wp2.Lon, wp2.Ele, true, false)
}
func (wp *Wpt) RemoveTime() {
wp.Time = ""
}
func (wp *Wpt) RemoveElevation() {
wp.Ele = 0.0
}
func (wp *Wpt) DeepCopy() *Wpt {
newWpt := &Wpt{
Lat: wp.Lat,
Lon: wp.Lon,
Ele: wp.Ele,
Time: wp.Time,
Magvar: wp.Magvar,
Geoidheight: wp.Geoidheight,
Name: wp.Name,
Cmt: wp.Cmt,
Desc: wp.Desc,
Src: wp.Src,
Link: make([]Link, len(wp.Link)),
Sym: wp.Sym,
Type: wp.Type,
Fix: wp.Fix,
Sat: wp.Sat,
Hdop: wp.Hdop,
Vdop: wp.Vdop,
Pdop: wp.Pdop,
Ageofdgpsdata: wp.Ageofdgpsdata,
Dgpsid: wp.Dgpsid,
Extensions: wp.Extensions,
}
copy(newWpt.Link, wp.Link)
return newWpt
}
func (wp *Wpt) Move(ld *LocationDelta) {
ld.Move(wp)
}
func (wp *Wpt) DistanceAngle(wp2 *Wpt) *LocationDelta {
return &LocationDelta{
Angle: Bearing(wp.Lat, wp.Lon, wp2.Lat, wp2.Lon),
Distance: Distance(wp.Lat, wp.Lon, 0, wp2.Lat, wp2.Lon, 0, false, false),
}
}