#!/usr/bin/python
import sys
if int(sys.version[0]) < 3:
print(r"Python 3 is required!")
exit()
import subprocess
import pkg_resources
required = {'moviepy', 'shutils'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
import csv
import PIL
import numbers
import shutil
import os
if len(sys.argv) < 2:
print(r"")
print("\033[91m" + r"openTX/edgeTX Logfile required!" + "\033[0m")
print(r"")
print(r"usage:")
print(r"single file: ./csv2gpx.py YourRadioSDcardLogfile.csv")
print(r"directory: for i in GPS/*.csv ; do ./csv2gpx.py $i ; done")
print(r"")
print(r"if the provided file has " + "\033[91m" + "no GPS data" + "\033[0m" + " at all, the script will " + "\033[91m" + "NOT terminate" + "\033[0m" + "!")
print(r"")
exit()
else:
fopen = sys.argv
inputfile = str(fopen[1])
outputfile = inputfile.rsplit(".",1)[0]+'.gpx'
print(r"Reading: "+inputfile)
global Hdg, Alt, Sats, Date, Time, GSpd, GPS
def find_required_fields(csv_header):
fields = 0
item = csv_header
while fields < len(csv_header):
if "GPS" in item[fields]:
#print('GPS in row: ' + str(fields))
GPS = int(fields)
elif "GSpd" in item[fields]:
#print('Speed in row: ' + str(fields))
GSpd = int(fields)
elif "Time" in item[fields]:
#print('Timestamp in row: ' + str(fields))
Time = int(fields)
elif "Date" in item[fields]:
#print('Date in row: ' + str(fields))
Date = int(fields)
elif "Sats" in item[fields]:
#print('Sats in row: ' + str(fields))
Sats = int(fields)
elif "Alt" in item[fields]:
#print('Alt(m) in row: ' + str(fields))
Alt = int(fields)
elif "Hdg" in item[fields]:
#print('Heading in row: ' + str(fields))
Hdg = int(fields)
fields += 1
return Hdg, Alt, Sats, Date, Time, GSpd, GPS
xmlhead = ''
gpxhead = ''
begingpx = ''
endgpx = ''
with open(outputfile, 'w', encoding='utf-8') as f:
f.write(xmlhead+'\n')
f.write(gpxhead+'\n')
f.write(begingpx+'\n')
with open(inputfile) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
header = []
header = next(csv_file)
header
read_header = header.split(",")
Hdg, Alt, Sats, Date, Time, GSpd, GPS = find_required_fields(read_header)
for row in csv_reader:
tempo = row[GSpd]
koordinaten = str(row[GPS])
filename = row[Date]
spd = round(float(tempo),2)
if 1 <= csv_reader.line_num <= 9: corrected = "000" + str(csv_reader.line_num)
if 10 <= csv_reader.line_num <= 100: corrected = "00" + str(csv_reader.line_num)
if 100 <= csv_reader.line_num <= 1000: corrected = "0" + str(csv_reader.line_num)
gps = koordinaten.split()
speed = ((float(tempo) * 1000)/3600)
if len(gps) < 1:
#print("\033[91m" + r'No GPS Data!' + "\033[0m")
writeGPX = False
else:
writeGPX = True
lat = gps[0]
lon = gps[1]
#unknown '+str(row[23])+'
f.writelines(''+row[Alt]+''+str(round(speed, 2))+''+str(row[Hdg])+'gps'+str(row[Sats])+'\n')
if writeGPX == True:
f.write(endgpx)
print('created: ' + outputfile)
elif writeGPX == False:
os.remove(outputfile)
print('No GPX file created')
exit()