Skip to content

Commit

Permalink
Adds speed to TrackPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmllr committed Jan 22, 2024
1 parent 147179d commit 1a28cb8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions Sources/GPXKit/GPXFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal enum GPXTags: String {
case temperature = "atemp"
case heartrate = "hr"
case cadence = "cad"
case speed
}

internal enum GPXAttributes: String {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -332,6 +336,12 @@ extension XMLNode {
}
}

var speed: Measurement<UnitSpeed>? {
Double(content).flatMap {
Measurement<UnitSpeed>(value: $0, unit: .metersPerSecond)
}
}

func childFor(_ tag: GPXTags) -> XMLNode? {
children.first(where: {
$0.name.lowercased() == tag.rawValue
Expand Down
6 changes: 5 additions & 1 deletion Sources/GPXKit/TrackPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnitTemperature>?
/// 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<UnitSpeed>?

/// Initializer
/// You don't need to construct this value by yourself, as it is done by GXPKits track parsing logic.
Expand All @@ -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<UnitPower>? = nil, cadence: UInt? = nil, heartrate: UInt? = nil, temperature: Measurement<UnitTemperature>? = nil) {
/// - speed: Optional speed value for a point. Defaults to nil.
public init(coordinate: Coordinate, date: Date? = nil, power: Measurement<UnitPower>? = nil, cadence: UInt? = nil, heartrate: UInt? = nil, temperature: Measurement<UnitTemperature>? = nil, speed: Measurement<UnitSpeed>? = nil) {
self.coordinate = coordinate
self.date = date
self.power = power
self.cadence = cadence
self.heartrate = heartrate
self.temperature = temperature
self.speed = speed
}
}

Expand Down
12 changes: 8 additions & 4 deletions Tests/GPXKitTests/GPXParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ class GPXParserTests: XCTestCase {
power: Measurement<UnitPower>(value: 42, unit: .watts),
cadence: 40,
heartrate: 97,
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius)
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius),
speed: Measurement(value: 1.23456, unit: .metersPerSecond)
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"),
power: Measurement<UnitPower>(value: 272, unit: .watts),
cadence: 45,
heartrate: 87,
temperature: Measurement<UnitTemperature>(value: 20.5, unit: .celsius)
temperature: Measurement<UnitTemperature>(value: 20.5, unit: .celsius),
speed: Measurement(value: 0.12345, unit: .metersPerSecond)
),
]

Expand All @@ -135,15 +137,17 @@ class GPXParserTests: XCTestCase {
power: Measurement<UnitPower>(value: 166, unit: .watts),
cadence: 99,
heartrate: 90,
temperature: Measurement<UnitTemperature>(value: 22, unit: .celsius)
temperature: Measurement<UnitTemperature>(value: 22, unit: .celsius),
speed: Measurement<UnitSpeed>(value: 1.23456, unit: .metersPerSecond)
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"),
power: Measurement<UnitPower>(value: 230, unit: .watts),
cadence: 101,
heartrate: 92,
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius)
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius),
speed: Measurement<UnitSpeed>(value: 0.123456, unit: .metersPerSecond)
),
]

Expand Down
6 changes: 6 additions & 0 deletions Tests/GPXKitTests/TestFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ let testXMLData = """
<gpxtpx:atemp>21</gpxtpx:atemp>
<gpxtpx:hr>97</gpxtpx:hr>
<gpxtpx:cad>40</gpxtpx:cad>
<gpxtpx:speed>1.23456</gpxtpx:speed>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>
Expand All @@ -93,6 +94,7 @@ let testXMLData = """
<gpxtpx:atemp>20.5</gpxtpx:atemp>
<gpxtpx:hr>87</gpxtpx:hr>
<gpxtpx:cad>45</gpxtpx:cad>
<gpxtpx:speed>0.12345</gpxtpx:speed>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>
Expand Down Expand Up @@ -121,6 +123,7 @@ let namespacedTestXMLData = """
<ns3:atemp>22</ns3:atemp>
<ns3:hr>90</ns3:hr>
<ns3:cad>99</ns3:cad>
<ns3:speed>1.23456</ns3:speed>
</ns3:TrackPointExtension>
</extensions>
</trkpt>
Expand All @@ -133,6 +136,7 @@ let namespacedTestXMLData = """
<ns3:atemp>21</ns3:atemp>
<ns3:hr>92</ns3:hr>
<ns3:cad>101</ns3:cad>
<ns3:speed>0.123456</ns3:speed>
</ns3:TrackPointExtension>
</extensions>
</trkpt>
Expand Down Expand Up @@ -192,6 +196,7 @@ let testXMLDataContainingWaypoint = """
<gpxtpx:atemp>21</gpxtpx:atemp>
<gpxtpx:hr>97</gpxtpx:hr>
<gpxtpx:cad>40</gpxtpx:cad>
<gpxtpx:speed>1.2345</gpxtpx:speed>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>
Expand All @@ -204,6 +209,7 @@ let testXMLDataContainingWaypoint = """
<gpxtpx:atemp>20</gpxtpx:atemp>
<gpxtpx:hr>97</gpxtpx:hr>
<gpxtpx:cad>40</gpxtpx:cad>
<gpxtpx:speed>0.12345678</gpxtpx:speed>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>
Expand Down

0 comments on commit 1a28cb8

Please sign in to comment.