Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export also heartrate and cadence to gpx #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/fit2gpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import gpxpy.gpx
import fitdecode
import lxml.etree as mod_etree


# MAIN CONVERTER CLASS
Expand Down Expand Up @@ -133,7 +134,7 @@ def fit_to_dataframes(self, fname: str) -> Tuple[pd.DataFrame, pd.DataFrame]:

# Method adapted from: https://github.com/nidhaloff/gpx-converter/blob/master/gpx_converter/base.py
def dataframe_to_gpx(self, df_points, col_lat='latitude', col_long='longitude', col_time=None, col_alt=None,
gpx_name=None, gpx_desc=None, gpx_link=None, gpx_type=None):
col_hr=None, col_cad=None, gpx_name=None, gpx_desc=None, gpx_link=None, gpx_type=None):
"""
Convert a pandas dataframe to gpx
Parameters:
Expand All @@ -142,6 +143,8 @@ def dataframe_to_gpx(self, df_points, col_lat='latitude', col_long='longitude',
col_time (str): name of the time column
col_long (str): name of the longitudes column
col_lat (str): name of the latitudes column
col_hr (str): name of the heart rate column
col_cad (str): name of the cadence column
gpx_name (str): name for the gpx track (note is not the same as the file name)
gpx_desc (str): description for the gpx track
gpx_type : activity type for the gpx track (can be str, or int)
Expand All @@ -167,6 +170,10 @@ def dataframe_to_gpx(self, df_points, col_lat='latitude', col_long='longitude',
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)

# add extension to be able to add heartrate and cadence
if col_hr or col_cad:
gpx.nsmap = {'gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1'}

# Step 2: Assign GPX track metadata
gpx.tracks[0].name = gpx_name
gpx.tracks[0].type = gpx_type
Expand All @@ -183,6 +190,19 @@ def dataframe_to_gpx(self, df_points, col_lat='latitude', col_long='longitude',
elevation=df_points.loc[idx, col_alt] if col_alt else None
)

# add GPX extensions for heartrate and cadence
if col_hr or col_cad:
namespace = '{gpxtpx}'
root = mod_etree.Element(f'{namespace}TrackPointExtension')
if col_hr:
sub_hr = mod_etree.SubElement(root, f'{namespace}hr')
sub_hr.text = str(df_points.loc[idx, col_hr]) if col_hr else '0'

if col_cad:
sub_cad = mod_etree.SubElement(root, f'{namespace}cad')
sub_cad.text = str(df_points.loc[idx, col_cad]) if col_cad else '0'
track_point.extensions.append(root)

# Append GPX_TrackPoint to segment:
gpx_segment.points.append(track_point)

Expand Down Expand Up @@ -219,6 +239,8 @@ def fit_to_gpx(self, f_in, f_out):
col_long='longitude',
col_time='timestamp',
col_alt='altitude',
col_hr='heart_rate',
col_cad='cadence',
)

# Step 3: Save file
Expand Down Expand Up @@ -378,6 +400,8 @@ def strava_fit_to_gpx(self):
col_long='longitude',
col_time='timestamp',
col_alt='altitude',
col_hr='heart_rate',
col_cad='cadence',
**strava_args
)

Expand Down