Created
February 4, 2011 06:34
-
-
Save tylerritchie/810816 to your computer and use it in GitHub Desktop.
For converting from Key Track Pro to GPS Babel universal csv format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
ktpro2kml.py v0.01 | |
Version by Tyler Ritchie: http://tylerritchie.com | |
License: http://creativecommons.org/licenses/BSD/ | |
""" | |
from datetime import datetime | |
import os | |
import codecs | |
import sys | |
import math | |
import optparse | |
import xml.etree.ElementTree as etree | |
def dms_to_deg(degrees, min, sec, card): | |
dms = float(degrees) + float(min)/60 + float(sec)/3600 | |
if card.lower() == (u'w' or u's'): | |
return dms*-1 | |
else: | |
return dms | |
def get_cardinal(string): | |
cardinals = ['N','S','E','W'] | |
cardinal = '' | |
for i in cardinals: | |
if i in string: | |
cardinal = string[string.index(i)] | |
return cardinal | |
def text_between(string,start_char,end_char): | |
return string[string.find(start_char)+1:string.find(end_char)] | |
def string_to_dms(string): | |
deg = text_between(string, ' ', u'\xb0') | |
min = text_between(string, u'\xb0', "'") | |
sec = text_between(string, "'", '"') | |
card = get_cardinal(string) | |
return (deg, min, sec, card) | |
def parse_ktpro(string): | |
'''This assumes that the line orientation is | |
"date,time,lat,lon,speed,bearing,elevation"''' | |
values = {} | |
vals = [ v.strip() for v in string.split(",") ] | |
if len(vals) > 1: | |
print vals | |
values['datetime'] = datetime.strptime(vals[0] + " " + vals[1], "%m-%d-%Y %H:%M:%S") | |
values['lat'] = dms_to_deg(*string_to_dms(vals[2])) | |
values['lon'] = dms_to_deg(*string_to_dms(vals[3])) | |
values['velocity'] = vals[4] | |
values['bearing'] = vals[5][:vals[5].find(u'\xb0')] | |
values['elevation'] = vals[6] | |
return values | |
else: | |
return None | |
def write_to_file(data, filename): | |
with open( filename, "w" ) as file: | |
file.write( data ) | |
print "File created successfully as %s" % filename | |
def main(): | |
print "huzzah!" | |
usage = '''usage: %prog -i input.txt [-o output.unicsv]''' | |
parser = OptionParser(usage) | |
parser.add_option("-i", "--input", dest="infile", help="input INFILE with waypoints.", metavar="INFILE") | |
parser.add_option("-o", "--output", dest="outfile", help="optional output OUTFILE", metavar="OUTFILE") | |
parser.add_option("-d", "--decdegrees", action="store_true", dest="degree", help="enable decimal degrees DD.0000") | |
(options, args) = parser.parse_args() | |
parser.check_required("-i") | |
output_file = ['date,time,lat,lon,ele,speed,cour'] | |
for line in codecs.open(options.infile, 'r', encoding="latin-1"): | |
parsed_data = parse_ktpro(line) | |
if parsed_data == None: | |
pass | |
else: | |
formatted_data = [] | |
date = parsed_data['datetime'].strftime('%Y/%m/%d') | |
time = parsed_data['datetime'].strftime('%H:%M:%S') | |
formatted_data.append(date) | |
formatted_data.append(time) | |
formatted_data.append('%.5f' % parsed_data['lat']) | |
formatted_data.append('%.5f' % parsed_data['lon']) | |
formatted_data.append(parsed_data['elevation']) | |
formatted_data.append(parsed_data['velocity']) | |
formatted_data.append(parsed_data['bearing']) | |
output_file.append(",".join(formatted_data)) | |
output_file = '\n'.join(output_file) | |
output_file = output_file.encode('ascii', 'ignore') | |
if options.outfile: | |
write_to_file(output_file, options.outfile ) | |
else: | |
filename = options.infile.split(".")[0] + ".unicsv" | |
if str(options.infile) == filename: | |
print "Error: source same as destination file, specify an output file and try again." | |
sys.exit() | |
else: | |
write_to_file(output_file, filename) | |
sys.exit() | |
class OptionParser (optparse.OptionParser): | |
def check_required (self, opt): | |
option = self.get_option(opt) | |
# Assumes the option's 'default' is set to None! | |
if getattr(self.values, option.dest) is None: | |
self.error("%s option not supplied" % option) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment