/
GPXTrackSegment.swift
106 lines (82 loc) · 2.83 KB
/
GPXTrackSegment.swift
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
//
// GPXTrackSegment.swift
// GPXKit
//
// Created by Vincent on 9/12/18.
//
import Foundation
/**
A track segment that holds data on all track points in the particular segment.
Does not hold additional information by default.
*/
public final class GPXTrackSegment: GPXElement, Codable {
/// For Codable use
private enum CodingKeys: String, CodingKey {
case points = "trkpt"
case extensions
}
/// Track points stored in current segment.
@available(*, deprecated, renamed: "points")
public var trackpoints: [GPXTrackPoint] {
return points
}
/// Track points stored in current segment.
public var points = [GPXTrackPoint]()
/// Custom Extensions, if needed.
public var extensions: GPXExtensions?
// MARK:- Instance
/// Default Initializer.
public required init() {
super.init()
}
/// Inits native element from raw parser value
///
/// - Parameters:
/// - raw: Raw element expected from parser
init(raw: GPXRawElement) {
for child in raw.children {
switch child.name {
case "trkpt": self.points.append(GPXTrackPoint(raw: child))
case "extensions": self.extensions = GPXExtensions(raw: child)
default: continue
}
}
}
// MARK:- Public Methods
/// Initializes a new trackpoint to segment, and returns the trackpoint.
public func newTrackpointWith(latitude: Double, longitude: Double) -> GPXTrackPoint {
let trackpoint = GPXTrackPoint(latitude: latitude, longitude: longitude)
self.add(trackpoint: trackpoint)
return trackpoint
}
/// Adds a single track point to this track segment.
public func add(trackpoint: GPXTrackPoint?) {
if let validPoint = trackpoint {
points.append(validPoint)
}
}
/// Adds an array of track points to this track segment.
public func add(trackpoints: [GPXTrackPoint]) {
self.points.append(contentsOf: trackpoints)
}
/// Removes a track point from this track segment.
public func remove(trackpoint: GPXTrackPoint) {
if let index = points.firstIndex(of: trackpoint) {
points.remove(at: index)
}
}
// MARK:- Tag
override func tagName() -> String {
return "trkseg"
}
// MARK:- GPX
override func addChildTag(toGPX gpx: NSMutableString, indentationLevel: Int) {
super.addChildTag(toGPX: gpx, indentationLevel: indentationLevel)
if self.extensions != nil {
self.extensions?.gpx(gpx, indentationLevel: indentationLevel)
}
for trackpoint in points {
trackpoint.gpx(gpx, indentationLevel: indentationLevel)
}
}
}