import osmnx as ox
import pandas as pd
import gpxpy
import gpxpy.gpx
import matplotlib.pyplot as plt
import polyline
Strava Route Plot using OSMnx involves visualizing and analyzing running or cycling routes recorded with the Strava app on maps generated with the OSMnx library. OSMnx is a Python package that allows users to download, model, and visualize street networks from OpenStreetMap’s data.
1. Read Route
= "Princesa_Toa_.gpx"
path with open(path, 'r') as gpx_file:
= gpxpy.parse(gpx_file)
gpx
# extract in gpx file latitude, longitude and elevation
= [{'latitude': point.latitude,'longitude': point.longitude,'elevation': point.elevation}
route_info for track in gpx.tracks
for segment in track.segments
for point in segment.points ]
# route_df Dataframe
= pd.DataFrame(route_info)
route_df route_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 |
2. Center point for Graph in ox
Link osmnx graph_from_point fuction https://osmnx.readthedocs.io/en/stable/osmnx.html?highlight=graph_from_point#osmnx.graph.graph_from_point
# center point to extract graph latitude , longitude
= (-0.26428048222240524, -78.4202862684383)
center_point = ox.graph_from_point(center_point, dist=12000, retain_all=True, simplify = False, network_type='all')
G #bike all
3. Plot Route
= ox.plot_graph(G, node_size=0,
fig, ax = (11, 16),
figsize = 300,
dpi = False,
save = "#FFFFFF",
bgcolor = "#253951",
edge_color = 0.2 ,
edge_alpha = False)
show ## Plot activity in graph
'longitude'] , route_df['latitude'] ,
plt.plot( route_df[= "#E64E25" ,
color = 2.0)
linewidth ## Plot activity in graph
plt.show()=0)
fig.tight_layout(pad## path name to save
= path.split(".")[0] + ".png"
path_save # fig.savefig( path_save, dpi=300, format="png", bbox_inches='tight',
# facecolor=fig.get_facecolor(), transparent=False)
# save figure
'activity.png') fig.savefig(
4. Plot Elevation
= route_df.elevation
elevation = range(len(elevation))
x_plot = min(elevation) - 25 min_elev
# Set the figure size
= plt.subplots(figsize=(11, 2))
fig, ax # Area plot
="#E64E25")
ax.fill_between(x_plot, elevation, color# Set the minimum and maximum values for the y-axis
4000)
ax.set_ylim(min_elev, 'off')
plt.axis(# Save the figure
= path.split(".")[0] +"_elevation" ".png"
path_save =True, bbox_inches='tight', pad_inches=0 )
plt.savefig(path_save , transparent# Display the plot
plt.show()
'perfil.png') fig.savefig(