/
GPXRoute.swift
179 lines (143 loc) · 5.57 KB
/
GPXRoute.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
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
//
// GPXRoute.swift
// GPXKit
//
// Created by Vincent on 8/12/18.
//
import Foundation
/**
Value type that represents a route, or `rteType` in GPX v1.1 schema.
The route can represent the planned route of a specific trip.
*/
public final class GPXRoute: GPXElement, Codable, GPXRouteType {
/// For Codable use
private enum CodingKeys: String, CodingKey {
case name
case comment = "cmt"
case desc
case source = "src"
case links = "link"
case type
case extensions
}
/// Name of the route.
public var name: String?
/// Additional comment of the route.
public var comment: String?
/// Description of the route.
public var desc: String?
/// Source of the route.
public var source: String?
/// A value type for link properties (see `GPXLink`)
///
/// Intended for additional information about current route through a web link.
@available(*, deprecated, message: "CoreGPX now support multiple links.", renamed: "links.first")
public var link: GPXLink? {
return links.first
}
/// A value type for link properties (see `GPXLink`)
///
/// Intended for additional information about current route through web links.
public var links = [GPXLink]()
/// Type of route.
public var type: String?
/// Extensions
public var extensions: GPXExtensions?
/// Route points in the route.
///
/// All route points joined represents a route.
@available(*, deprecated, renamed: "points")
public var routepoints: [GPXRoutePoint] {
return points
}
/// Route points in the route.
///
/// All route points joined represents a route.
public var points = [GPXRoutePoint]()
/// Number of route (possibly a tag for the route)
public var number: Int?
// 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 "link": self.links.append(GPXLink(raw: child))
case "rtept": self.points.append(GPXRoutePoint(raw: child))
case "name": self.name = child.text
case "cmt": self.comment = child.text
case "desc": self.desc = child.text
case "src": self.source = child.text
case "type": self.type = child.text
case "number": self.number = Convert.toInt(from: child.text)
case "extensions": self.extensions = GPXExtensions(raw: child)
default: continue
}
}
}
// MARK: Public Methods
/// Creates a `GPXLink` which is added to the route and also returned.
///
/// Not recommended for use. Init `GPXRoutePoint` manually, then adding it to route, instead.
func newLink(withHref href: String) -> GPXLink {
let link: GPXLink = GPXLink(withHref: href)
self.links.append(link)
return link
}
/// Creates a `GPXRoutePoint` which is added to the route and also returned.
///
/// Not recommended for use. Init `GPXRoutePoint` manually, then adding it to route, instead.
func newRoutePointWith(latitude: Double, longitude: Double) -> GPXRoutePoint {
let routepoint = GPXRoutePoint(latitude: latitude, longitude: longitude)
self.add(routepoint: routepoint)
return routepoint
}
/// Adds a singular route point to the route.
func add(routepoint: GPXRoutePoint?) {
if let validPoint = routepoint {
points.append(validPoint)
}
}
/// Adds an array of route points to the route.
func add(routepoints: [GPXRoutePoint]) {
self.points.append(contentsOf: routepoints)
}
/// Removes a route point from the route.
func remove(routepoint: GPXRoutePoint) {
let contains = points.contains(routepoint)
if contains == true {
if let index = points.firstIndex(of: routepoint) {
points.remove(at: index)
}
}
}
// MARK:- Tag
override func tagName() -> String {
return "rte"
}
// MARK:- GPX
override func addChildTag(toGPX gpx: NSMutableString, indentationLevel: Int) {
super.addChildTag(toGPX: gpx, indentationLevel: indentationLevel)
self.addProperty(forValue: name, gpx: gpx, tagName: "name", indentationLevel: indentationLevel)
self.addProperty(forValue: comment, gpx: gpx, tagName: "comment", indentationLevel: indentationLevel)
self.addProperty(forValue: desc, gpx: gpx, tagName: "desc", indentationLevel: indentationLevel)
self.addProperty(forValue: source, gpx: gpx, tagName: "src", indentationLevel: indentationLevel)
for link in links {
link.gpx(gpx, indentationLevel: indentationLevel)
}
self.addProperty(forIntegerValue: number, gpx: gpx, tagName: "number", indentationLevel: indentationLevel)
self.addProperty(forValue: type, gpx: gpx, tagName: "type", indentationLevel: indentationLevel)
if self.extensions != nil {
self.extensions?.gpx(gpx, indentationLevel: indentationLevel)
}
for routepoint in points {
routepoint.gpx(gpx, indentationLevel: indentationLevel)
}
}
}