summaryrefslogtreecommitdiff
path: root/ride/ride.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2024-07-28 17:50:29 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2024-07-28 17:50:29 +0200
commit203b9bdf17bd4742b5aab2c35df47b59af0652f1 (patch)
treeb6bc8ddee21bcaa022ef68b3769f61100117b190 /ride/ride.go
parent171366f337c3422331b84b6c7416e1b1d6bd36a0 (diff)
test: Add some test to fix behavior
Diffstat (limited to 'ride/ride.go')
-rw-r--r--ride/ride.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/ride/ride.go b/ride/ride.go
new file mode 100644
index 0000000..8206793
--- /dev/null
+++ b/ride/ride.go
@@ -0,0 +1,46 @@
+package ride
+
+import (
+ "fmt"
+
+ "github.com/martinlehoux/kagamigo/kcore"
+ "github.com/tkrajina/gpxgo/gpx"
+)
+
+type Ride struct {
+ points []Point
+}
+
+func (r *Ride) check() {
+ kcore.Assert(len(r.points) > 0, "no points in ride")
+}
+
+func FromGPX(content *gpx.GPX) Ride {
+ segment := content.Tracks[0].Segments[0]
+ points := make([]Point, len(segment.Points))
+ distance := 0.0
+ for i, p := range segment.Points {
+ if i != 0 {
+ distance += p.Distance2D(&segment.Points[i-1])
+ }
+ kcore.Assert(i == 0 || distance > 0, "zero distance")
+ kcore.Assert(p.Elevation.NotNull(), "points without elevation")
+ points[i] = Point{distance: distance, elevation: p.Elevation.Value()}
+ }
+ ride := Ride{points}
+ ride.check()
+ return ride
+}
+
+func FromPoints(points []Point) Ride {
+ ride := Ride{points}
+ ride.check()
+ return ride
+}
+
+func (r *Ride) String(climb Climb) string {
+ start := r.points[climb.start]
+ end := r.points[climb.end]
+ score := Score(r.points, climb.start, climb.end)
+ return fmt.Sprintf("%.1fkm-%.1fkm: %.1fkm at %.1f%% (%d pts - %s)", start.distance/1000, end.distance/1000, (end.distance-start.distance)/1000, Slope(start, end)*100, int(score), Category(score))
+}