From 1a28cb83a1ced2c2b3f0d8402758c53b86125030 Mon Sep 17 00:00:00 2001 From: Markus Mueller Date: Mon, 22 Jan 2024 12:03:57 +0100 Subject: [PATCH] Adds speed to TrackPoint --- README.md | 2 +- Sources/GPXKit/GPXFileParser.swift | 14 ++++++++++++-- Sources/GPXKit/TrackPoint.swift | 6 +++++- Tests/GPXKitTests/GPXParserTests.swift | 12 ++++++++---- Tests/GPXKitTests/TestFixtures.swift | 6 ++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1384ce7..37797c5 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ A library for parsing and exporting GPX files with no dependencies besides Found To use the `GPXKit` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file: ```swift -.package(url: "https://github.com/mmllr/GPXKit", from: "2.1.0") +.package(url: "https://github.com/mmllr/GPXKit", from: "2.2.0") ``` ## Usage examples diff --git a/Sources/GPXKit/GPXFileParser.swift b/Sources/GPXKit/GPXFileParser.swift index b3d7965..5e3f6d9 100644 --- a/Sources/GPXKit/GPXFileParser.swift +++ b/Sources/GPXKit/GPXFileParser.swift @@ -38,6 +38,7 @@ internal enum GPXTags: String { case temperature = "atemp" case heartrate = "hr" case cadence = "cad" + case speed } internal enum GPXAttributes: String { @@ -151,7 +152,8 @@ public final class GPXFileParser { power: $0.power, cadence: $0.cadence, heartrate: $0.heartrate, - temperature: $0.temperature + temperature: $0.temperature, + speed: $0.speed ) }, segments) } @@ -175,7 +177,8 @@ public final class GPXFileParser { power: $0.power, cadence: $0.cadence, heartrate: $0.heartrate, - temperature: $0.temperature + temperature: $0.temperature, + speed: $0.speed ) }, nil) } @@ -288,6 +291,7 @@ extension TrackPoint { .flatMap { UInt($0.content) } self.temperature = trackNode.childFor(.extensions)?.childFor(.trackPointExtension)?.childFor(.temperature)? .temperature + self.speed = trackNode.childFor(.extensions)?.childFor(.trackPointExtension)?.childFor(.speed)?.speed } } @@ -332,6 +336,12 @@ extension XMLNode { } } + var speed: Measurement? { + Double(content).flatMap { + Measurement(value: $0, unit: .metersPerSecond) + } + } + func childFor(_ tag: GPXTags) -> XMLNode? { children.first(where: { $0.name.lowercased() == tag.rawValue diff --git a/Sources/GPXKit/TrackPoint.swift b/Sources/GPXKit/TrackPoint.swift index 88ef779..40476cc 100644 --- a/Sources/GPXKit/TrackPoint.swift +++ b/Sources/GPXKit/TrackPoint.swift @@ -14,6 +14,8 @@ public struct TrackPoint: Hashable, Sendable { public var heartrate: UInt? /// Optional temperature value for a given point in a gpx file, which got recorded from a bicycle computer through a temperature sensor. public var temperature: Measurement? + /// Optional speed value for a given point in a gpx file, which got recorded from a bicycle computer through a speed sensor. + public var speed: Measurement? /// Initializer /// You don't need to construct this value by yourself, as it is done by GXPKits track parsing logic. @@ -24,13 +26,15 @@ public struct TrackPoint: Hashable, Sendable { /// - cadence: Optional cadence value for a point. Defaults to nil. /// - heartrate: Optional heartrate value for a point. Defaults to nil. /// - temperature: Optional temperature value for a point. Defaults to nil. - public init(coordinate: Coordinate, date: Date? = nil, power: Measurement? = nil, cadence: UInt? = nil, heartrate: UInt? = nil, temperature: Measurement? = nil) { + /// - speed: Optional speed value for a point. Defaults to nil. + public init(coordinate: Coordinate, date: Date? = nil, power: Measurement? = nil, cadence: UInt? = nil, heartrate: UInt? = nil, temperature: Measurement? = nil, speed: Measurement? = nil) { self.coordinate = coordinate self.date = date self.power = power self.cadence = cadence self.heartrate = heartrate self.temperature = temperature + self.speed = speed } } diff --git a/Tests/GPXKitTests/GPXParserTests.swift b/Tests/GPXKitTests/GPXParserTests.swift index 79721c2..c5d88be 100644 --- a/Tests/GPXKitTests/GPXParserTests.swift +++ b/Tests/GPXKitTests/GPXParserTests.swift @@ -105,7 +105,8 @@ class GPXParserTests: XCTestCase { power: Measurement(value: 42, unit: .watts), cadence: 40, heartrate: 97, - temperature: Measurement(value: 21, unit: .celsius) + temperature: Measurement(value: 21, unit: .celsius), + speed: Measurement(value: 1.23456, unit: .metersPerSecond) ), TrackPoint( coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0), @@ -113,7 +114,8 @@ class GPXParserTests: XCTestCase { power: Measurement(value: 272, unit: .watts), cadence: 45, heartrate: 87, - temperature: Measurement(value: 20.5, unit: .celsius) + temperature: Measurement(value: 20.5, unit: .celsius), + speed: Measurement(value: 0.12345, unit: .metersPerSecond) ), ] @@ -135,7 +137,8 @@ class GPXParserTests: XCTestCase { power: Measurement(value: 166, unit: .watts), cadence: 99, heartrate: 90, - temperature: Measurement(value: 22, unit: .celsius) + temperature: Measurement(value: 22, unit: .celsius), + speed: Measurement(value: 1.23456, unit: .metersPerSecond) ), TrackPoint( coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0), @@ -143,7 +146,8 @@ class GPXParserTests: XCTestCase { power: Measurement(value: 230, unit: .watts), cadence: 101, heartrate: 92, - temperature: Measurement(value: 21, unit: .celsius) + temperature: Measurement(value: 21, unit: .celsius), + speed: Measurement(value: 0.123456, unit: .metersPerSecond) ), ] diff --git a/Tests/GPXKitTests/TestFixtures.swift b/Tests/GPXKitTests/TestFixtures.swift index 3dbe8d5..d76259d 100644 --- a/Tests/GPXKitTests/TestFixtures.swift +++ b/Tests/GPXKitTests/TestFixtures.swift @@ -81,6 +81,7 @@ let testXMLData = """ 21 97 40 + 1.23456 @@ -93,6 +94,7 @@ let testXMLData = """ 20.5 87 45 + 0.12345 @@ -121,6 +123,7 @@ let namespacedTestXMLData = """ 22 90 99 + 1.23456 @@ -133,6 +136,7 @@ let namespacedTestXMLData = """ 21 92 101 + 0.123456 @@ -192,6 +196,7 @@ let testXMLDataContainingWaypoint = """ 21 97 40 + 1.2345 @@ -204,6 +209,7 @@ let testXMLDataContainingWaypoint = """ 20 97 40 + 0.12345678