Skip to content

Instantly share code, notes, and snippets.

@urschrei
Last active June 18, 2024 15:40
Show Gist options
  • Save urschrei/5355597 to your computer and use it in GitHub Desktop.
Save urschrei/5355597 to your computer and use it in GitHub Desktop.
Extract basic GPS data (lat, lon, elevation, timestamp) from GPX, and dump it into a CSV file. Requires the gpxpy library.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Output Elevation, Lat, Long, and Timestamp series from GPX to CSV
Requires gpxpy
This script expects your input GPX to be located in a subdir named 'data'
"""
import os
import gpxpy
import gpxpy.gpx
import csv
outputdir = "output"
os.path.exists(outputdir) or os.makedirs(outputdir)
# put your GPX file in a subdir named 'data'
try:
gpx_file = open(os.path.join("data", "input.gpx"), "r")
gpx = gpxpy.parse(gpx_file)
except IOError:
print("Couldn't open the input GPX file. Ensure it's in the 'data' dir.")
raise()
def write_csv():
""" coroutine for writing dicts to a CSV as rows """
header_written = False
# create a CSV writer object
with open(os.path.join(outputdir, "output.csv"), "w") as f:
while True:
data = (yield)
# don't bother writing anything unless we have GPS data
dw = csv.DictWriter(f, sorted(data.keys()))
if not header_written:
dw.writeheader()
header_written = True
dw.writerow(data)
# initialise csv output co-routine
output = write_csv()
output.next()
# dump each lat/lon/elevation/timestamp into a dict, with a Unix timestamp
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
output.send({
"Lat.": point.latitude,
"Lon.": point.longitude,
"Elev.": point.elevation,
"Timestamp": point.time.isoformat()
})
@zeevox
Copy link

zeevox commented May 4, 2019

@TiagoCant Use python2 instead of python3

@rivciks
Copy link

rivciks commented Feb 22, 2023

I have the same error. Would it be possible to adjust your python2 script to python3? Thank you.

@zeevox
Copy link

zeevox commented Feb 22, 2023

I have the same error. Would it be possible to adjust your python2 script to python3? Thank you.

Perhaps try replacing output.next() with next(output).

@rivciks
Copy link

rivciks commented Feb 22, 2023

Thanx. It worked. I also had to comment out the line <"Timestamp": point.time.isoformat()> otherwise I was presented with an error:
line 52, in
"Timestamp": point.time.isoformat()
AttributeError: 'NoneType' object has no attribute 'isoformat'

@rivciks
Copy link

rivciks commented Feb 22, 2023

How can you get rid of the string "Elev.,Lat.,Lon." at the beginning of the csv file? My GPS device can't deal with it. Otherwise very nice work! Thank You.

@zeevox
Copy link

zeevox commented Feb 23, 2023

How can you get rid of the string "Elev.,Lat.,Lon." at the beginning of the csv file? My GPS device can't deal with it. Otherwise very nice work! Thank You.

That's called the header of a CSV file, so it might help to comment out dw.writeheader().

@rivciks
Copy link

rivciks commented Feb 23, 2023

OK, Thanx.

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