Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import gpxpy
import gpxpy.gpx
import folium
gpx_file = open('path_to_gpx_file.gpx', 'r')
gpx = gpxpy.parse(gpx_file)
points = []
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
points.append(tuple([point.latitude, point.longitude]))
print(points)
ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)
# Load map centred on average coordinates
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)
#add a markers
for each in points:
folium.Marker(each).add_to(my_map)
#fadd lines
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
# Save map
my_map.save("./gpx_berlin_withmarker.html")
@philshem
Copy link

philshem commented Feb 1, 2020

great code!

if you have a csv with latitude/longitude columns sorted by timestamp:

import pandas as pd
import folium

df = pd.read_csv('in.csv')
df = df[['latitude','longitude']] # change to your column names, assume the columns are sorted by time
points = [tuple(x) for x in df.to_numpy()]

ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)

# Load map centred on average coordinates
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)

#add a markers
for each in points:  
    folium.Marker(each).add_to(my_map)

#add lines
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)

# Save map
my_map.save("./out.html")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment