Open GPS tracks (GPX files) with Geopandas

It is easy to open a complete folder of GPS Tracks (GPX files) in Geopandas, e.g. for plotting maps or to export them as shapefile.

If you are programming in python, you might want to import your GPS tracks (taken with a GPS device or your mobile phone and tracking app) into a GeoDataFrame. It’s easy to import the tracks of all GPX files of a given folder.

Jupyter Notebook on GitHub

import geopandas as gpd
import os
folder = "gpx/"

I want a GeoDataFrame with 2 columns: 1) file name and 2) geometry of the track.

# Create empty GeoDataFrame
track = gpd.GeoDataFrame(columns=['name', 'geometry'], 
     geometry='geometry')

Try to open all gpx files in folder. Geopandas uses fiona to open/parse gpx. The layer “tracks” only contains the track
without waypoints/timestamps (good enough for plotting maps).

for file in os.listdir(folder):
    if file.endswith(('.gpx')):
        try:
            gdf = gpd.read_file(folder + file, layer='tracks')
            track = track.append(gdf[['name', 'geometry']])
        except:
            print("Error", file)

track.sort_values(by="name", inplace=True)
track.reset_index(inplace=True, drop=True)

Now you can plot, edit or export your GeoDataframe.

# Save tracks as Shapefile
track.to_file(folder + 'track.shp')

# Simple plot
track.plot()

You can also try PyGMT to plot pretty maps of your GPS track.