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

Elevations #5

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ A map of activities viewed in plan.

![map](https://github.com/marcusvolz/strava_py/blob/main/plots/map001.png "A map of activities viewed in plan.")

### Elevations

A plot of activity elevation profiles as small multiples.

![map](https://github.com/marcusvolz/strava_py/blob/main/plots/elevations001.png "A plot of activity elevation profiles as small multiples.")

## How to use

### Bulk export from Strava
Expand Down Expand Up @@ -51,3 +57,9 @@ plot_facets(df, output_file = 'plot.png')
plot_map(df, lon_min=None, lon_max= None, lat_min=None, lat_max=None,
alpha=0.3, linewidth=0.3, output_file="map.png")
```

### Plot elevations

```python
plot_elevations(df, output_file = 'elevations.png')
```
Binary file added plots/elevations001.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/strava_py/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def main():

# Normally imports go at the top, but scientific libraries can be slow to import
# so let's validate arguments first
from strava_py.plot_elevations import plot_elevations
from strava_py.plot_map import plot_map
from strava_py.plot_facets import plot_facets
from strava_py.process_data import process_data
Expand All @@ -42,6 +43,10 @@ def main():
plot_facets(df, output_file=args.output_file)
print(f"Saved to {args.output_file}")

print("Plotting elevations...")
plot_elevations(df, output_file=args.output_file)
print(f"Saved to {args.output_file}")


if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions src/strava_py/plot_elevations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import math
import matplotlib.pyplot as plt
import seaborn as sns

def plot_elevations(df, output_file = 'elevations.png'):

# Compute activity start times (for facet ordering)
start_times = df.groupby('name').agg({'time': 'min'}).reset_index().sort_values('time')
ncol = math.ceil(math.sqrt(len(start_times)))

# Create facets
p = sns.FacetGrid(
data = df,
col = 'name',
col_wrap = ncol,
col_order = start_times['name'],
sharex = False,
sharey = True,
)

# Add activities
p = p.map(
plt.plot, "dist", "ele", color = 'black', linewidth = 4
)

# Update plot aesthetics
p.set(xlabel = None)
p.set(ylabel = None)
p.set(xticks = [])
p.set(yticks = [])
p.set(xticklabels = [])
p.set(yticklabels = [])
p.set_titles(col_template = '', row_template = '')
sns.despine(left = True, bottom = True)
plt.subplots_adjust(left = 0.05, bottom = 0.05, right = 0.95, top = 0.95)
plt.savefig(output_file)
35 changes: 32 additions & 3 deletions src/strava_py/process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fit2gpx
import gpxpy
import math
import pandas as pd
from rich.progress import track

Expand All @@ -20,9 +21,13 @@ def process_gpx(gpxfile):
ele = []
time = []
name = []
dist = []

for track in activity.tracks:
for segment in track.segments:
x0 = activity.tracks[0].segments[0].points[0].longitude
y0 = activity.tracks[0].segments[0].points[0].latitude
d0 = 0
for point in segment.points:
x = point.longitude
y = point.latitude
Expand All @@ -33,10 +38,15 @@ def process_gpx(gpxfile):
ele.append(z)
time.append(t)
name.append(gpxfile)
d = d0 + math.sqrt(math.pow(x - x0, 2) + math.pow(y - y0, 2))
dist.append(d)
x0 = x
y0 = y
d0 = d

df = pd.DataFrame(
list(zip(lon, lat, ele, time, name)),
columns = ['lon', 'lat', 'ele', 'time', 'name']
list(zip(lon, lat, ele, time, name, dist)),
columns = ['lon', 'lat', 'ele', 'time', 'name', 'dist']
)

return df
Expand All @@ -48,7 +58,26 @@ def process_fit(fitfile):
df_lap, df = conv.fit_to_dataframes(fname = fitfile)

df['name'] = fitfile
df = df[['longitude', 'latitude', 'altitude', 'timestamp', 'name']]

dist = []

for i in range(len(df.index)):
if i < 1:
x0 = df['longitude'][0]
y0 = df['latitude'][0]
d0 = 0
dist.append(d0)
else:
x = df['longitude'][i]
y = df['latitude'][i]
d = d0 + math.sqrt(math.pow(x - x0, 2) + math.pow(y - y0, 2))
dist.append(d)
x0 = x
y0 = y
d0 = d

df = df.join(pd.DataFrame({'dist': dist}))
df = df[['longitude', 'latitude', 'altitude', 'timestamp', 'name', 'dist']]
df = df.rename(columns = {'longitude': 'lon', 'latitude': 'lat', 'altitude': 'ele', 'timestamp': 'time'})

return df
Expand Down