Skip to content

Instantly share code, notes, and snippets.

@jdunic
Last active January 28, 2016 03:20
Show Gist options
  • Save jdunic/583ec01f74edf61d8915 to your computer and use it in GitHub Desktop.
Save jdunic/583ec01f74edf61d8915 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""GPX_to_csv_waypoints.py: Exporting waypoints in GPX file to a csv file"""
# Author: Jillian Dunic
# Date created: 2014-08-13
#-------------
# Change log #
#-------------
# Load libraries
import distutils.core
import gpxpy
import gpxpy.gpx
import csv
import time
from datetime import datetime, date
def get_user_data():
# User prompts and confirmation that information entered is correct.
username = raw_input("Enter your name: ")
gpx_filename = raw_input("Enter the gpx file path: ")
output_filename = raw_input("Enter the output file path; e.g., PATH/filename1.csv: ")
print("Your name is:", username, "Your input file path is:", gpx_filename, "and your output file path", output_filename)
confirmation = raw_input("Is this information correct? (y/n) ")
go_ahead = distutils.util.strtobool(confirmation)
return(go_ahead, username, gpx_filename, output_filename)
def gpx_to_csv(output_filename, gpx_filename):
# Set time stamp.
today = datetime.now().strftime("%Y-%m-%d-%H:%M")
# Tidying output_filename.
if output_filename[-4] != '.csv':
output_filename = output_filename + '.csv'
output_filename = output_filename[0:-4] + today + output_filename[-4]
# Open and read gpx file.
gpx_file = open(gpx_filename, 'r')
gpx = gpxpy.parse(gpx_file)
# Write waypoints to csv file.
with open(output_filename, "w") as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(['waypoint', 'lat', 'long', 'date', 'time'])
for w in gpx.waypoints:
name = w.name
lat = w.latitude
lon = w.longitude
date = w.time.strftime("%Y-%m-%d")
time = w.time.strftime("%H:%M:%S")
row = [name, lat, lon, date, time]
writer.writerow(row)
go_ahead = True
while True:
go_ahead, username, gpx_filename, output_filename = get_user_data()
if go_ahead == True:
gpx_to_csv(output_filename, gpx_filename)
break
# Exit loop once function is complete.
# raw_input returns the empty string for "enter"
"""
output_csv_filename = "Byrnes_team_dive_lab_waypoints_%s.csv" % date.today()
gpx_file = open('Recently Read from GPSMAP 78sc (GARMIN).GPX', 'r')
gpx = gpxpy.parse(gpx_file)
filename =
with open(output_csv_filename, "w") as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(['waypoint', 'lat', 'long', 'date', 'time'])
for w in gpx.waypoints:
name = w.name
lat = w.latitude
lon = w.longitude
date = w.time.strftime("%Y-%m-%d")
time = w.time.strftime("%H:%M:%S")
row = [name, lat, lon, date, time]
writer.writerow(row)
# 6.28.8.1 Basic example - log to a file
# Here's a simple logging example that just logs to a file. In order, it creates a Logger instance, then a FileHandler and a Formatter. It attaches the Formatter to the FileHandler, then the FileHandler to the Logger. Finally, it sets a debug level for the logger.
import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('/var/tmp/myapp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
We can use this logger object now to write entries to the log file:
logger.error('We have a problem')
logger.info('While this is just chatty')
If we look in the file that was created, we'll see something like this:
2003-07-08 16:49:45,896 ERROR We have a problem
The info message was not written to the file - we called the setLevel method to say we only wanted WARNING or worse, so the info message is discarded.
The timestamp is of the form ``year-month-day hour:minutes:seconds,milliseconds.'' Note that despite the three digits of precision in the milliseconds field, not all systems provide time with this much precision.
"""
@mrmch
Copy link

mrmch commented Aug 17, 2014

Num: #66 add a break command after

Num: #67 you can remove this and the next line because it will automatically get called

@mrmch
Copy link

mrmch commented Aug 17, 2014

also #66: you're not passing the right variables to this function, you should use the variables you got on #64:

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