Strava GPX Plotter: Mapping Beautiful Journeys with Folium in Python

news
code
visualization
Author

Marco Aguirre

Published

August 6, 2023

“Strava GPX Plotter: Mapping Beautiful Journeys with Folium in Python” is a tool that allows users to visualize their Strava activities on interactive maps. By using Python and the Folium library, this tool reads GPX files from Strava and plots the routes on a map, highlighting the beauty of each journey. Users can customize the maps, adding markers and different styles to enhance the visual appeal. This tool is perfect for athletes and outdoor enthusiasts who want to see their activities in a new light, making it easy to share and analyze their adventures.

import pandas as pd
import gpxpy
import gpxpy.gpx
import matplotlib.pyplot as plt
import os
import folium

Read gpx File

# path from gpx file
path = "Princesa_Toa_.gpx"
# load data from gpx strava activity and parse
with open(path, 'r') as gpx_file:
    gpx = gpxpy.parse(gpx_file)

Extract latitude, longitude and elevation

# extract in gpx file latitude, longitude  and elevation
route_info = [{'latitude': point.latitude,'longitude': point.longitude,'elevation': point.elevation} 
              for track   in gpx.tracks  
              for segment in track.segments 
              for point   in segment.points ]
# route_df Dataframe
df = pd.DataFrame(route_info)
df.head()
latitude longitude elevation
0 -0.216083 -78.436892 2388.8
1 -0.216085 -78.436895 2388.8
2 -0.216141 -78.436849 2388.9
3 -0.216161 -78.436832 2388.9
4 -0.216211 -78.436803 2389.0
# Calculate the middle index
middle_index = len(df) // 2
# Get the middle element using iloc
point  = df.iloc[middle_index]

Plot Folium Map Strava default Tile

my_map = folium.Map(location=[point['latitude'], point['longitude']] , zoom_start=12  )
points = df[['latitude','longitude']].values.tolist()
folium.PolyLine(points, color="red", weight=1.5, opacity=2.5).add_to(my_map)
<folium.vector_layers.PolyLine at 0x213f3814e20>
my_map
Make this Notebook Trusted to load map: File -> Trust Notebook

Plot Folium Map Strava stamen_terrain Tile

tiles='https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}{r}.png'
attr='&copy; <a href="https://www.stadiamaps.com/" target="_blank">Stadia Maps</a> &copy; <a href="https://www.stamen.com/" target="_blank">Stamen Design</a> &copy; <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
my_map = folium.Map(location=[point['latitude'], point['longitude']],tiles= tiles,attr=attr,zoom_start=12  )
points = df[['latitude','longitude']].values.tolist()
folium.PolyLine(points, color="red", weight=1.5, opacity=2.5).add_to(my_map)
<folium.vector_layers.PolyLine at 0x213ed8dc8e0>
my_map
Make this Notebook Trusted to load map: File -> Trust Notebook