Skip to content

Commit

Permalink
Merge pull request #108 from jovandeginste/improve-fit-support
Browse files Browse the repository at this point in the history
Improve support for fit-files
  • Loading branch information
jovandeginste authored Apr 15, 2024
2 parents 7417be5 + e69a518 commit 8207140
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions pkg/converters/fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package converters
import (
"bytes"
"encoding/xml"
"fmt"
"math"
"strconv"

"github.com/tkrajina/gpxgo/gpx"
Expand All @@ -16,16 +18,19 @@ func ParseFit(fitFile []byte) (*gpx.GPX, error) {
return nil, err
}

gpxFile := &gpx.GPX{
Name: f.FileId.TimeCreated.String(),
Time: &f.FileId.TimeCreated,
Creator: f.FileId.Manufacturer.String(),
}

m, err := f.Activity()
if err != nil {
return nil, err
}

gpxFile := &gpx.GPX{
Creator: "Garmin Connect",
Link: "connect.garmin.com",
LinkText: "Garmin Connect",
Time: &m.Sessions[0].StartTime,
if len(m.Sessions) == 0 {
return nil, fmt.Errorf("no sessions found")
}

gpxFile.AppendTrack(&gpx.GPXTrack{
Expand All @@ -34,26 +39,35 @@ func ParseFit(fitFile []byte) (*gpx.GPX, error) {
})

for _, r := range m.Records {
if r.PositionLat.Invalid() ||
r.PositionLong.Invalid() {
continue
}

p := &gpx.GPXPoint{
Timestamp: r.Timestamp,
Point: gpx.Point{
Latitude: r.PositionLat.Degrees(),
Longitude: r.PositionLong.Degrees(),
Elevation: *gpx.NewNullableFloat64(r.GetEnhancedAltitudeScaled()),
},
Timestamp: r.Timestamp,
Extensions: gpx.Extension{
Nodes: []gpx.ExtensionNode{
{
XMLName: xml.Name{Local: "ns3:TrackPointExtension"},
Nodes: []gpx.ExtensionNode{
{XMLName: xml.Name{Local: "ns3:hr"}, Data: strconv.Itoa(int(r.HeartRate))},
{XMLName: xml.Name{Local: "ns3:cad"}, Data: strconv.Itoa(int(r.Cadence))},
},
},
},
},
}

if a := r.GetEnhancedAltitudeScaled(); !math.IsNaN(a) {
p.Elevation = *gpx.NewNullableFloat64(a)
}

if r.HeartRate != 0xFF {
p.Extensions.Nodes = append(p.Extensions.Nodes, gpx.ExtensionNode{
XMLName: xml.Name{Local: "ns3:hr"}, Data: strconv.Itoa(int(r.HeartRate)),
})
}

if r.Cadence != 0xFF {
p.Extensions.Nodes = append(p.Extensions.Nodes, gpx.ExtensionNode{
XMLName: xml.Name{Local: "ns3:cad"}, Data: strconv.Itoa(int(r.Cadence)),
})
}

gpxFile.AppendPoint(p)
}

Expand Down

0 comments on commit 8207140

Please sign in to comment.