Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit a19d26e

Browse files
committed
add elevationGain property
1 parent f5a066b commit a19d26e

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/main/kotlin/be/yellowduck/gpx/Segment.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ data class Segment(
2525
return Distance(distance)
2626
}
2727

28+
/**
29+
* Returns the total elevation gain of the segment
30+
*/
31+
val elevationGain: Double
32+
get() {
33+
var total = 0.0
34+
points.forEachIndexed { index, point ->
35+
if (index > 0) {
36+
val ele = points[index].ele - points[index - 1].ele
37+
if (ele > 0) {
38+
total += ele
39+
}
40+
}
41+
}
42+
return total
43+
}
44+
2845
/**
2946
* Returns the segment as an encoded polyline string.
3047
*

src/main/kotlin/be/yellowduck/gpx/Track.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ data class Track(
2121
return Distance(segments.sumOf { it.distance.meters })
2222
}
2323

24+
/**
25+
* Returns the total elevation gain of the track
26+
*/
27+
val elevationGain: Double
28+
get() {
29+
return segments.sumOf { it.elevationGain }
30+
}
31+
2432
/**
2533
* Returns the segment as an encoded polyline string.
2634
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package be.yellowduck.gpx
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
class TrackTests {
7+
@Test
8+
fun testElevationGain() {
9+
val path = "src/test/resources/Gravelgrinders_Gent_4.gpx"
10+
val gpx = GPX.parse(path)
11+
assertThat(gpx.tracks.firstOrNull()?.elevationGain).isEqualTo(469.7999999999836)
12+
}
13+
}

0 commit comments

Comments
 (0)