forked from Maelkum/gpxgo
/
converters.go
617 lines (546 loc) · 17.1 KB
/
converters.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
// Copyright 2013, 2014 Peter Vasil, Tomo Krajina. All
// rights reserved. Use of this source code is governed
// by a BSD-style license that can be found in the
// LICENSE file.
package gpx
import (
"strings"
)
//defaultCreator contains the original repo path
const defaultCreator = "https://github.com/tkrajina/gpxgo"
// ----------------------------------------------------------------------------------------------------
// Gpx 1.0 Stuff
// ----------------------------------------------------------------------------------------------------
func convertToGpx10Models(gpxDoc *GPX) *gpx10Gpx {
gpx10Doc := &gpx10Gpx{}
//gpx10Doc.XMLNs = gpxDoc.XMLNs
gpx10Doc.XMLNs = "http://www.topografix.com/GPX/1/0"
gpx10Doc.XmlNsXsi = gpxDoc.XmlNsXsi
gpx10Doc.XmlSchemaLoc = gpxDoc.XmlSchemaLoc
gpx10Doc.Version = "1.0"
if len(gpxDoc.Creator) == 0 {
gpx10Doc.Creator = defaultCreator
} else {
gpx10Doc.Creator = gpxDoc.Creator
}
gpx10Doc.Name = gpxDoc.Name
gpx10Doc.Desc = gpxDoc.Description
gpx10Doc.Author = gpxDoc.AuthorName
gpx10Doc.Email = gpxDoc.AuthorEmail
if len(gpxDoc.AuthorLink) > 0 || len(gpxDoc.AuthorLinkText) > 0 {
// TODO
}
if len(gpxDoc.Link) > 0 || len(gpxDoc.LinkText) > 0 {
gpx10Doc.Url = gpxDoc.Link
gpx10Doc.UrlName = gpxDoc.LinkText
}
if gpxDoc.Time != nil {
gpx10Doc.Time = formatGPXTime(gpxDoc.Time)
}
gpx10Doc.Keywords = gpxDoc.Keywords
if gpxDoc.Waypoints != nil {
gpx10Doc.Waypoints = make([]*gpx10GpxPoint, len(gpxDoc.Waypoints))
for waypointNo, waypoint := range gpxDoc.Waypoints {
gpx10Doc.Waypoints[waypointNo] = convertPointToGpx10(&waypoint)
}
}
if gpxDoc.Routes != nil {
gpx10Doc.Routes = make([]*gpx10GpxRte, len(gpxDoc.Routes))
for routeNo, route := range gpxDoc.Routes {
r := new(gpx10GpxRte)
r.Name = route.Name
r.Cmt = route.Comment
r.Desc = route.Description
r.Src = route.Source
// TODO
//r.Links = route.Links
r.Number = route.Number
r.Type = route.Type
// TODO
//r.RoutePoints = route.RoutePoints
gpx10Doc.Routes[routeNo] = r
if route.Points != nil {
r.Points = make([]*gpx10GpxPoint, len(route.Points))
for pointNo, point := range route.Points {
r.Points[pointNo] = convertPointToGpx10(&point)
}
}
}
}
if gpxDoc.Tracks != nil {
gpx10Doc.Tracks = make([]*gpx10GpxTrk, len(gpxDoc.Tracks))
for trackNo, track := range gpxDoc.Tracks {
gpx10Track := new(gpx10GpxTrk)
gpx10Track.Name = track.Name
gpx10Track.Cmt = track.Comment
gpx10Track.Desc = track.Description
gpx10Track.Src = track.Source
gpx10Track.Number = track.Number
gpx10Track.Type = track.Type
if track.Segments != nil {
gpx10Track.Segments = make([]*gpx10GpxTrkSeg, len(track.Segments))
for segmentNo, segment := range track.Segments {
gpx10Segment := new(gpx10GpxTrkSeg)
if segment.Points != nil {
gpx10Segment.Points = make([]*gpx10GpxPoint, len(segment.Points))
for pointNo, point := range segment.Points {
gpx10Point := convertPointToGpx10(&point)
// TODO
//gpx10Point.Speed = point.Speed
//gpx10Point.Speed = point.Speed
gpx10Segment.Points[pointNo] = gpx10Point
}
}
gpx10Track.Segments[segmentNo] = gpx10Segment
}
}
gpx10Doc.Tracks[trackNo] = gpx10Track
}
}
return gpx10Doc
}
func convertFromGpx10Models(gpx10Doc *gpx10Gpx) *GPX {
gpxDoc := new(GPX)
gpxDoc.XMLNs = gpx10Doc.XMLNs
gpxDoc.XmlNsXsi = gpx10Doc.XmlNsXsi
gpxDoc.XmlSchemaLoc = gpx10Doc.XmlSchemaLoc
gpxDoc.Creator = gpx10Doc.Creator
gpxDoc.Version = gpx10Doc.Version
gpxDoc.Name = gpx10Doc.Name
gpxDoc.Description = gpx10Doc.Desc
gpxDoc.AuthorName = gpx10Doc.Author
gpxDoc.AuthorEmail = gpx10Doc.Email
if len(gpx10Doc.Url) > 0 || len(gpx10Doc.UrlName) > 0 {
gpxDoc.Link = gpx10Doc.Url
gpxDoc.LinkText = gpx10Doc.UrlName
}
if len(gpx10Doc.Time) > 0 {
gpxDoc.Time, _ = parseGPXTime(gpx10Doc.Time)
}
gpxDoc.Keywords = gpx10Doc.Keywords
if gpx10Doc.Waypoints != nil {
waypoints := make([]GPXPoint, len(gpx10Doc.Waypoints))
for waypointNo, waypoint := range gpx10Doc.Waypoints {
waypoints[waypointNo] = *convertPointFromGpx10(waypoint)
}
gpxDoc.Waypoints = waypoints
}
if gpx10Doc.Routes != nil {
gpxDoc.Routes = make([]GPXRoute, len(gpx10Doc.Routes))
for routeNo, route := range gpx10Doc.Routes {
r := new(GPXRoute)
r.Name = route.Name
r.Comment = route.Cmt
r.Description = route.Desc
r.Source = route.Src
// TODO
//r.Links = route.Links
r.Number = route.Number
r.Type = route.Type
// TODO
//r.RoutePoints = route.RoutePoints
if route.Points != nil {
r.Points = make([]GPXPoint, len(route.Points))
for pointNo, point := range route.Points {
r.Points[pointNo] = *convertPointFromGpx10(point)
}
}
gpxDoc.Routes[routeNo] = *r
}
}
if gpx10Doc.Tracks != nil {
gpxDoc.Tracks = make([]GPXTrack, len(gpx10Doc.Tracks))
for trackNo, track := range gpx10Doc.Tracks {
gpxTrack := new(GPXTrack)
gpxTrack.Name = track.Name
gpxTrack.Comment = track.Cmt
gpxTrack.Description = track.Desc
gpxTrack.Source = track.Src
gpxTrack.Number = track.Number
gpxTrack.Type = track.Type
if track.Segments != nil {
gpxTrack.Segments = make([]GPXTrackSegment, len(track.Segments))
for segmentNo, segment := range track.Segments {
gpxSegment := GPXTrackSegment{}
if segment.Points != nil {
gpxSegment.Points = make([]GPXPoint, len(segment.Points))
for pointNo, point := range segment.Points {
gpxSegment.Points[pointNo] = *convertPointFromGpx10(point)
}
}
gpxTrack.Segments[segmentNo] = gpxSegment
}
}
gpxDoc.Tracks[trackNo] = *gpxTrack
}
}
return gpxDoc
}
func convertPointToGpx10(original *GPXPoint) *gpx10GpxPoint {
result := new(gpx10GpxPoint)
result.Lat = original.Latitude
result.Lon = original.Longitude
result.Ele = original.Elevation
result.Timestamp = formatGPXTime(&original.Timestamp)
result.MagVar = original.MagneticVariation
result.GeoIdHeight = original.GeoidHeight
result.Name = original.Name
result.Cmt = original.Comment
result.Desc = original.Description
result.Src = original.Source
// TODO
//w.Links = original.Links
result.Sym = original.Symbol
result.Type = original.Type
result.Fix = original.TypeOfGpsFix
if original.Satellites.NotNull() {
value := original.Satellites.Value()
result.Sat = &value
}
if original.HorizontalDilution.NotNull() {
value := original.HorizontalDilution.Value()
result.Hdop = &value
}
if original.VerticalDilution.NotNull() {
value := original.VerticalDilution.Value()
result.Vdop = &value
}
if original.PositionalDilution.NotNull() {
value := original.PositionalDilution.Value()
result.Pdop = &value
}
if original.AgeOfDGpsData.NotNull() {
value := original.AgeOfDGpsData.Value()
result.AgeOfDGpsData = &value
}
if original.DGpsId.NotNull() {
value := original.DGpsId.Value()
result.DGpsId = &value
}
return result
}
func convertPointFromGpx10(original *gpx10GpxPoint) *GPXPoint {
result := new(GPXPoint)
result.Latitude = original.Lat
result.Longitude = original.Lon
result.Elevation = original.Ele
time, _ := parseGPXTime(original.Timestamp)
if time != nil {
result.Timestamp = *time
}
result.MagneticVariation = original.MagVar
result.GeoidHeight = original.GeoIdHeight
result.Name = original.Name
result.Comment = original.Cmt
result.Description = original.Desc
result.Source = original.Src
// TODO
//w.Links = original.Links
result.Symbol = original.Sym
result.Type = original.Type
result.TypeOfGpsFix = original.Fix
if original.Sat != nil {
result.Satellites = *NewNullableInt(*original.Sat)
}
if original.Hdop != nil {
result.HorizontalDilution = *NewNullableFloat64(*original.Hdop)
}
if original.Vdop != nil {
result.VerticalDilution = *NewNullableFloat64(*original.Vdop)
}
if original.Pdop != nil {
result.PositionalDilution = *NewNullableFloat64(*original.Pdop)
}
if original.AgeOfDGpsData != nil {
result.AgeOfDGpsData = *NewNullableFloat64(*original.AgeOfDGpsData)
}
if original.DGpsId != nil {
result.DGpsId = *NewNullableInt(*original.DGpsId)
}
return result
}
// ----------------------------------------------------------------------------------------------------
// Gpx 1.1 Stuff
// ----------------------------------------------------------------------------------------------------
func convertToGpx11Models(gpxDoc *GPX) *gpx11Gpx {
gpx11Doc := &gpx11Gpx{}
gpx11Doc.Version = "1.1"
gpx11Doc.XMLNs = "http://www.topografix.com/GPX/1/1"
gpx11Doc.XmlNsXsi = gpxDoc.XmlNsXsi
gpx11Doc.XmlSchemaLoc = gpxDoc.XmlSchemaLoc
if len(gpxDoc.Creator) == 0 {
gpx11Doc.Creator = defaultCreator
} else {
gpx11Doc.Creator = gpxDoc.Creator
}
gpx11Doc.Name = gpxDoc.Name
gpx11Doc.Desc = gpxDoc.Description
gpx11Doc.AuthorName = gpxDoc.AuthorName
if len(gpxDoc.AuthorEmail) > 0 {
parts := strings.Split(gpxDoc.AuthorEmail, "@")
if len(parts) == 1 {
gpx11Doc.AuthorEmail = new(gpx11GpxEmail)
gpx11Doc.AuthorEmail.Id = parts[0]
} else if len(parts) > 1 {
gpx11Doc.AuthorEmail = new(gpx11GpxEmail)
gpx11Doc.AuthorEmail.Id = parts[0]
gpx11Doc.AuthorEmail.Domain = parts[1]
}
}
if len(gpxDoc.AuthorLink) > 0 || len(gpxDoc.AuthorLinkText) > 0 || len(gpxDoc.AuthorLinkType) > 0 {
gpx11Doc.AuthorLink = new(gpx11GpxLink)
gpx11Doc.AuthorLink.Href = gpxDoc.AuthorLink
gpx11Doc.AuthorLink.Text = gpxDoc.AuthorLinkText
gpx11Doc.AuthorLink.Type = gpxDoc.AuthorLinkType
}
if len(gpxDoc.Copyright) > 0 || len(gpxDoc.CopyrightYear) > 0 || len(gpxDoc.CopyrightLicense) > 0 {
gpx11Doc.Copyright = new(gpx11GpxCopyright)
gpx11Doc.Copyright.Author = gpxDoc.Copyright
gpx11Doc.Copyright.Year = gpxDoc.CopyrightYear
gpx11Doc.Copyright.License = gpxDoc.CopyrightLicense
}
if len(gpxDoc.Link) > 0 || len(gpxDoc.LinkText) > 0 || len(gpxDoc.LinkType) > 0 {
gpx11Doc.Link = new(gpx11GpxLink)
gpx11Doc.Link.Href = gpxDoc.Link
gpx11Doc.Link.Text = gpxDoc.LinkText
gpx11Doc.Link.Type = gpxDoc.LinkType
}
if gpxDoc.Time != nil {
gpx11Doc.Timestamp = formatGPXTime(gpxDoc.Time)
}
gpx11Doc.Keywords = gpxDoc.Keywords
if gpxDoc.Waypoints != nil {
gpx11Doc.Waypoints = make([]*gpx11GpxPoint, len(gpxDoc.Waypoints))
for waypointNo, waypoint := range gpxDoc.Waypoints {
gpx11Doc.Waypoints[waypointNo] = convertPointToGpx11(&waypoint)
}
}
if gpxDoc.Routes != nil {
gpx11Doc.Routes = make([]*gpx11GpxRte, len(gpxDoc.Routes))
for routeNo, route := range gpxDoc.Routes {
r := new(gpx11GpxRte)
r.Name = route.Name
r.Cmt = route.Comment
r.Desc = route.Description
r.Src = route.Source
// TODO
//r.Links = route.Links
r.Number = route.Number
r.Type = route.Type
// TODO
//r.RoutePoints = route.RoutePoints
gpx11Doc.Routes[routeNo] = r
if route.Points != nil {
r.Points = make([]*gpx11GpxPoint, len(route.Points))
for pointNo, point := range route.Points {
r.Points[pointNo] = convertPointToGpx11(&point)
}
}
}
}
if gpxDoc.Tracks != nil {
gpx11Doc.Tracks = make([]*gpx11GpxTrk, len(gpxDoc.Tracks))
for trackNo, track := range gpxDoc.Tracks {
gpx11Track := new(gpx11GpxTrk)
gpx11Track.Name = track.Name
gpx11Track.Cmt = track.Comment
gpx11Track.Desc = track.Description
gpx11Track.Src = track.Source
gpx11Track.Number = track.Number
gpx11Track.Type = track.Type
if track.Segments != nil {
gpx11Track.Segments = make([]*gpx11GpxTrkSeg, len(track.Segments))
for segmentNo, segment := range track.Segments {
gpx11Segment := new(gpx11GpxTrkSeg)
if segment.Points != nil {
gpx11Segment.Points = make([]*gpx11GpxPoint, len(segment.Points))
for pointNo, point := range segment.Points {
gpx11Segment.Points[pointNo] = convertPointToGpx11(&point)
}
}
gpx11Track.Segments[segmentNo] = gpx11Segment
}
}
gpx11Doc.Tracks[trackNo] = gpx11Track
}
}
return gpx11Doc
}
func convertFromGpx11Models(gpx11Doc *gpx11Gpx) *GPX {
gpxDoc := new(GPX)
gpxDoc.XMLNs = gpx11Doc.XMLNs
gpxDoc.XmlNsXsi = gpx11Doc.XmlNsXsi
gpxDoc.XmlSchemaLoc = gpx11Doc.XmlSchemaLoc
gpxDoc.Creator = gpx11Doc.Creator
gpxDoc.Version = gpx11Doc.Version
gpxDoc.Name = gpx11Doc.Name
gpxDoc.Description = gpx11Doc.Desc
gpxDoc.AuthorName = gpx11Doc.AuthorName
if gpx11Doc.AuthorEmail != nil {
gpxDoc.AuthorEmail = gpx11Doc.AuthorEmail.Id + "@" + gpx11Doc.AuthorEmail.Domain
}
if gpx11Doc.AuthorLink != nil {
gpxDoc.AuthorLink = gpx11Doc.AuthorLink.Href
gpxDoc.AuthorLinkText = gpx11Doc.AuthorLink.Text
gpxDoc.AuthorLinkType = gpx11Doc.AuthorLink.Type
}
/* TODO
if gpx11Doc.Extensions != nil {
gpxDoc.Extensions = &gpx11Doc.Extensions.Bytes
}
*/
if len(gpx11Doc.Timestamp) > 0 {
gpxDoc.Time, _ = parseGPXTime(gpx11Doc.Timestamp)
}
if gpx11Doc.Copyright != nil {
gpxDoc.Copyright = gpx11Doc.Copyright.Author
gpxDoc.CopyrightYear = gpx11Doc.Copyright.Year
gpxDoc.CopyrightLicense = gpx11Doc.Copyright.License
}
if gpx11Doc.Link != nil {
gpxDoc.Link = gpx11Doc.Link.Href
gpxDoc.LinkText = gpx11Doc.Link.Text
gpxDoc.LinkType = gpx11Doc.Link.Type
}
gpxDoc.Keywords = gpx11Doc.Keywords
if gpx11Doc.Waypoints != nil {
waypoints := make([]GPXPoint, len(gpx11Doc.Waypoints))
for waypointNo, waypoint := range gpx11Doc.Waypoints {
waypoints[waypointNo] = *convertPointFromGpx11(waypoint)
}
gpxDoc.Waypoints = waypoints
}
if gpx11Doc.Routes != nil {
gpxDoc.Routes = make([]GPXRoute, len(gpx11Doc.Routes))
for routeNo, route := range gpx11Doc.Routes {
r := new(GPXRoute)
r.Name = route.Name
r.Comment = route.Cmt
r.Description = route.Desc
r.Source = route.Src
// TODO
//r.Links = route.Links
r.Number = route.Number
r.Type = route.Type
// TODO
//r.RoutePoints = route.RoutePoints
if route.Points != nil {
r.Points = make([]GPXPoint, len(route.Points))
for pointNo, point := range route.Points {
r.Points[pointNo] = *convertPointFromGpx11(point)
}
}
gpxDoc.Routes[routeNo] = *r
}
}
if gpx11Doc.Tracks != nil {
gpxDoc.Tracks = make([]GPXTrack, len(gpx11Doc.Tracks))
for trackNo, track := range gpx11Doc.Tracks {
gpxTrack := new(GPXTrack)
gpxTrack.Name = track.Name
gpxTrack.Comment = track.Cmt
gpxTrack.Description = track.Desc
gpxTrack.Source = track.Src
gpxTrack.Number = track.Number
gpxTrack.Type = track.Type
if track.Segments != nil {
gpxTrack.Segments = make([]GPXTrackSegment, len(track.Segments))
for segmentNo, segment := range track.Segments {
gpxSegment := GPXTrackSegment{}
if segment.Points != nil {
gpxSegment.Points = make([]GPXPoint, len(segment.Points))
for pointNo, point := range segment.Points {
gpxSegment.Points[pointNo] = *convertPointFromGpx11(point)
}
}
gpxTrack.Segments[segmentNo] = gpxSegment
}
}
gpxDoc.Tracks[trackNo] = *gpxTrack
}
}
return gpxDoc
}
func convertPointToGpx11(original *GPXPoint) *gpx11GpxPoint {
result := new(gpx11GpxPoint)
result.Lat = original.Latitude
result.Lon = original.Longitude
result.Ele = original.Elevation
result.Timestamp = formatGPXTime(&original.Timestamp)
result.MagVar = original.MagneticVariation
result.GeoIdHeight = original.GeoidHeight
result.Name = original.Name
result.Cmt = original.Comment
result.Desc = original.Description
result.Src = original.Source
// TODO
//w.Links = original.Links
result.Sym = original.Symbol
result.Type = original.Type
result.Fix = original.TypeOfGpsFix
if original.Satellites.NotNull() {
value := original.Satellites.Value()
result.Sat = &value
}
if original.HorizontalDilution.NotNull() {
value := original.HorizontalDilution.Value()
result.Hdop = &value
}
if original.VerticalDilution.NotNull() {
value := original.VerticalDilution.Value()
result.Vdop = &value
}
if original.PositionalDilution.NotNull() {
value := original.PositionalDilution.Value()
result.Pdop = &value
}
if original.AgeOfDGpsData.NotNull() {
value := original.AgeOfDGpsData.Value()
result.AgeOfDGpsData = &value
}
if original.DGpsId.NotNull() {
value := original.DGpsId.Value()
result.DGpsId = &value
}
return result
}
func convertPointFromGpx11(original *gpx11GpxPoint) *GPXPoint {
result := new(GPXPoint)
result.Latitude = original.Lat
result.Longitude = original.Lon
result.Elevation = original.Ele
time, _ := parseGPXTime(original.Timestamp)
if time != nil {
result.Timestamp = *time
}
result.MagneticVariation = original.MagVar
result.GeoidHeight = original.GeoIdHeight
result.Name = original.Name
result.Comment = original.Cmt
result.Description = original.Desc
result.Source = original.Src
// TODO
//w.Links = original.Links
result.Symbol = original.Sym
result.Type = original.Type
result.TypeOfGpsFix = original.Fix
if original.Sat != nil {
result.Satellites = *NewNullableInt(*original.Sat)
}
if original.Hdop != nil {
result.HorizontalDilution = *NewNullableFloat64(*original.Hdop)
}
if original.Vdop != nil {
result.VerticalDilution = *NewNullableFloat64(*original.Vdop)
}
if original.Pdop != nil {
result.PositionalDilution = *NewNullableFloat64(*original.Pdop)
}
if original.AgeOfDGpsData != nil {
result.AgeOfDGpsData = *NewNullableFloat64(*original.AgeOfDGpsData)
}
if original.DGpsId != nil {
result.DGpsId = *NewNullableInt(*original.DGpsId)
}
return result
}