Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save futureshocked/fbec25ed1eedab340801c570f3eae9a8 to your computer and use it in GitHub Desktop.
Save futureshocked/fbec25ed1eedab340801c570f3eae9a8 to your computer and use it in GitHub Desktop.
This script will produce GPX and KML files from the flight data recorder CSV file. These output files can be used in Google Earth Pro, or other programs like Relive.
import csv
import os
import sys
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom.minidom import parseString
from datetime import datetime
# Constants
GROUND_LEVEL_ALTITUDE_YSCN = 70 # Ground level altitude at Camden YSCN in meters
# Function to create GPX element
def create_gpx_element():
gpx = Element('gpx', version="1.1", creator="gpx.py -- https://github.com/tkrajina/gpxpy",
xmlns="http://www.topografix.com/GPX/1/1",
xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance",
xsi_schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd")
return gpx
# Function to create track element in GPX
def create_track_element(gpx):
trk = SubElement(gpx, 'trk')
trkseg = SubElement(trk, 'trkseg')
return trkseg
# Function to add trackpoint to GPX
def add_trackpoint(trkseg, datetime_str, lat, lon, ele):
trkpt = SubElement(trkseg, 'trkpt', lat=str(lat), lon=str(lon))
ele_tag = SubElement(trkpt, 'ele')
ele_tag.text = str(ele)
time_tag = SubElement(trkpt, 'time')
time_tag.text = datetime_str
# Function to create KML element
def create_kml_element():
kml = Element('kml', xmlns="http://www.opengis.net/kml/2.2")
document = SubElement(kml, 'Document')
name = SubElement(document, 'name')
name.text = "Converted GPS Track"
return document
# Function to create track element in KML
def create_kml_track(document):
placemark = SubElement(document, 'Placemark')
name = SubElement(placemark, 'name')
name.text = "Track"
line_string = SubElement(placemark, 'LineString')
tessellate = SubElement(line_string, 'tessellate')
tessellate.text = '1'
altitude_mode = SubElement(line_string, 'altitudeMode')
altitude_mode.text = 'absolute'
coordinates = SubElement(line_string, 'coordinates')
coordinates.text = "" # Initialize the coordinates text
return coordinates
# Function to add coordinates to KML
def add_kml_coordinates(coordinates, lat, lon, ele):
coordinates.text += f"{lon},{lat},{ele} "
# Function to convert CSV to GPX and KML
def convert_csv_to_gpx_kml(csv_file_path, gpx_file_path, kml_file_path):
gpx = create_gpx_element()
trkseg = create_track_element(gpx)
kml_doc = create_kml_element()
kml_coordinates = create_kml_track(kml_doc)
with open(csv_file_path, mode='r') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
time = int(row['time (ms)'])
datetime_str = row['datetime']
latitude = float(row['lat (deg)'])
longitude = float(row['lon (deg)'])
altitude_above_mean_sea_level = float(row['alt (m)'])
altitude_adjusted = altitude_above_mean_sea_level # Use absolute altitude
add_trackpoint(trkseg, datetime_str, latitude, longitude, altitude_adjusted)
add_kml_coordinates(kml_coordinates, latitude, longitude, altitude_adjusted)
# Write GPX file
gpx_string = tostring(gpx, 'utf-8')
parsed_gpx = parseString(gpx_string)
with open(gpx_file_path, 'w') as gpxfile:
gpxfile.write(parsed_gpx.toprettyxml(indent=" "))
# Write KML file
kml_string = tostring(kml_doc, 'utf-8')
parsed_kml = parseString(kml_string)
with open(kml_file_path, 'w') as kmlfile:
kmlfile.write(parsed_kml.toprettyxml(indent=" "))
# Print file locations
print(f"Input CSV file: {csv_file_path}")
print(f"Output GPX file: {gpx_file_path}")
print(f"Output KML file: {kml_file_path}")
# Main function
def main():
if len(sys.argv) != 2:
print("Usage: python script.py <csv_file_path>")
sys.exit(1)
csv_file_path = sys.argv[1]
base_name = os.path.splitext(os.path.basename(csv_file_path))[0]
directory = os.path.dirname(csv_file_path)
gpx_file_path = os.path.join(directory, base_name + '.gpx')
kml_file_path = os.path.join(directory, base_name + '.kml')
convert_csv_to_gpx_kml(csv_file_path, gpx_file_path, kml_file_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment