Skip to content

Commit

Permalink
feat: elevation gain - strava/garmin(gpx/tcx/fit)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-29 committed Oct 29, 2023
1 parent 2ff0529 commit abe1c46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions run_page/generator/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def randomword():
"summary_polyline",
"average_heartrate",
"average_speed",
"total_elevation_gain",
]


Expand All @@ -53,6 +54,7 @@ class Activity(Base):
summary_polyline = Column(String)
average_heartrate = Column(Float)
average_speed = Column(Float)
total_elevation_gain = Column(Float)
streak = None

def to_dict(self):
Expand Down Expand Up @@ -106,6 +108,7 @@ def update_or_create_activity(session, run_activity):
location_country=location_country,
average_heartrate=run_activity.average_heartrate,
average_speed=float(run_activity.average_speed),
total_elevation_gain=float(run_activity.total_elevation_gain),
summary_polyline=(
run_activity.map and run_activity.map.summary_polyline or ""
),
Expand All @@ -120,6 +123,7 @@ def update_or_create_activity(session, run_activity):
activity.type = run_activity.type
activity.average_heartrate = run_activity.average_heartrate
activity.average_speed = float(run_activity.average_speed)
activity.total_elevation_gain = float(run_activity.total_elevation_gain)
activity.summary_polyline = (
run_activity.map and run_activity.map.summary_polyline or ""
)
Expand Down
15 changes: 15 additions & 0 deletions run_page/gpxtrackposter/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self):
self.length = 0
self.special = False
self.average_heartrate = None
self.total_elevation_gain = None
self.moving_dict = {}
self.run_id = 0
self.start_latlng = []
Expand Down Expand Up @@ -157,6 +158,7 @@ def _load_tcx_data(self, tcx, file_name):
except:
pass
self.polyline_str = polyline.encode(polyline_container)
self.total_elevation_gain = tcx.ascent
self.moving_dict = {
"distance": self.length,
"moving_time": datetime.timedelta(seconds=moving_time),
Expand Down Expand Up @@ -220,6 +222,9 @@ def _load_gpx_data(self, gpx):
sum(heart_rate_list) / len(heart_rate_list) if heart_rate_list else None
)
self.moving_dict = self._get_moving_data(gpx)
self.total_elevation_gain = (
gpx.get_uphill_downhill().uphill if gpx.has_elevations() else None
)

def _load_fit_data(self, fit: FitFile):
_polylines = []
Expand Down Expand Up @@ -250,6 +255,9 @@ def _load_fit_data(self, fit: FitFile):
self.average_heartrate = (
message.avg_heart_rate if message.avg_heart_rate != 0 else None
)
self.total_elevation_gain = (
message.total_ascent if message.total_ascent != 0 else None
)
self.type = Sport(message.sport).name.lower()

# moving_dict
Expand Down Expand Up @@ -292,6 +300,10 @@ def append(self, other):
)
self.file_names.extend(other.file_names)
self.special = self.special or other.special
self.average_heartrate = self.average_heartrate or other.average_heartrate
self.total_elevation_gain = (
self.total_elevation_gain if self.total_elevation_gain else 0
) + (other.total_elevation_gain if other.total_elevation_gain else 0)
except:
print(
f"something wrong append this {self.end_time},in files {str(self.file_names)}"
Expand Down Expand Up @@ -325,6 +337,9 @@ def to_namedtuple(self):
"average_heartrate": int(self.average_heartrate)
if self.average_heartrate
else None,
"total_elevation_gain": int(self.total_elevation_gain)
if self.total_elevation_gain
else None,
"map": run_map(self.polyline_str),
"start_latlng": self.start_latlng,
}
Expand Down

0 comments on commit abe1c46

Please sign in to comment.