From 7afe4283b0dbc73127237ba832c91e05d7bd4c59 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 2 Sep 2022 18:11:20 +0300 Subject: [PATCH] Transform to Mercator projection so maps aren't squashed away from equator --- src/stravavis/plot_map.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/stravavis/plot_map.py b/src/stravavis/plot_map.py index f466b5e..fb1a075 100644 --- a/src/stravavis/plot_map.py +++ b/src/stravavis/plot_map.py @@ -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"): @@ -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