Skip to content

Instantly share code, notes, and snippets.

@deparkes
Created May 2, 2016 13:18
Show Gist options
  • Save deparkes/9a0b45c69beb9a614f54d13bc7c551b5 to your computer and use it in GitHub Desktop.
Save deparkes/9a0b45c69beb9a614f54d13bc7c551b5 to your computer and use it in GitHub Desktop.
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")

@angelomalfitano
Copy link

Hello philshem, thank you for your code. I've a problem with line 5. I named correctly the colums in file in.csv but I receive this message:
Traceback (most recent call last):
File "C:\Users\angelo\Desktop\map\gpslatlong.py", line 5, in
df = df[['latitude','longitude']] # change to your column names, assume the columns are sorted by time
File "C:\Users\angelo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3511, in getitem
indexer = self.columns._get_indexer_strict(key, "columns")[1]
File "C:\Users\angelo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 5782, in _get_indexer_strict
self._raise_if_missing(keyarr, indexer, axis_name)
File "C:\Users\angelo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 5842, in _raise_if_missing
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['latitude', 'longitude'], dtype='object')] are in the [columns]"

Do you have any ideas?
Thank you
Angelo

@philshem
Copy link

Hi @angelomalfitano - it seems like it's an issue with your CSV. Can you upload it?

@angelomalfitano
Copy link

Hello @philshem recreate .csv and it works without any issue! thank you Angelo

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