Skip to content

Commit

Permalink
Merge pull request #21 from hugovk/unsquash-map
Browse files Browse the repository at this point in the history
Fix squashed maps
  • Loading branch information
marcusvolz authored Sep 4, 2022
2 parents 2b66523 + 7afe428 commit 159e30d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/stravavis/plot_map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
from math import log, pi, tan

import matplotlib.pyplot as plt
from rich.progress import track

# Dummy units
MAP_WIDTH = 1
MAP_HEIGHT = 1


def convert_x(lon):
# Get x value
x = (lon + 180) * (MAP_WIDTH / 360)
return x


def convert_y(lat):
# Convert from degrees to radians
lat_rad = lat * pi / 180

# Get y value
mercator_n = log(tan((pi / 4) + (lat_rad / 2)))
y = (MAP_HEIGHT / 2) + (MAP_WIDTH * mercator_n / (2 * pi))
return y


def 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"):
Expand Down Expand Up @@ -28,6 +50,11 @@ def plot_map(df, lon_min=None, lon_max= None, lat_min=None, lat_max=None,
for activity in track(activities, "Plotting activities"):
x = df[df['name'] == activity]['lon']
y = df[df['name'] == activity]['lat']

# Transform to Mercator projection so maps aren't squashed away from equator
x = x.transform(convert_x)
y = y.transform(convert_y)

plt.plot(x, y, color='black', alpha=alpha, linewidth=linewidth)

# Update plot aesthetics
Expand Down

0 comments on commit 159e30d

Please sign in to comment.