Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
N1029676 committed Aug 13, 2020
0 parents commit 816538a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Flight plan KML to CSV conversion
This quick project exists to convert a specifically formatted KML file containing pair's of coordinates into a CSV file where they are displayed in Degrees, DegreesMinutes, and DegreesMinutesSeconds.

# Procedures
## Converting KML to CSV
1. Open command prompt or powershell and navigate to the folder the script/executable is stored in
2. Call the script where the first argument is the `inputFile` and the optional second argument is the `outputFile`
- As script: `python parser.py [inputFile] [outputFile]`
- As executable: `parser.py [inputFile] [outputFile]`
- If the `outputFile` argument is not specified it will default to using the original path and filename while appending `.csv` to the end
3. After a few seconds the output file should appear where directed while also displaying to the screen

## Created the executable from script
Only needed if developing the script
1. Using pyinstaller run `pyinstaller --onefile parser.py`

# Caveats
1. This seems to accurately convert coordinates for the given formats, but you should absolutely check that yourself before relying on this
2. If you want to have different formats in the output, you will have to modify the source code yourself
3. Because Excel gets weird with the UTF-8 encoding when you open a CSV, I've removed some characters that caused issues but this may affect readability
44 changes: 44 additions & 0 deletions parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import untangle
import re
import csv
from pygeodesy import LatLon_, latlonDMS
import sys


inputFilename = sys.argv[1]

try:
outputFilename = sys.argv[2]
except IndexError:
outputFilename = inputFilename+'.csv'

print('DEBUG: Reading', inputFilename)
print('DEBUG: Saving to', outputFilename)

latlonre = re.compile(r"""(-*\d+\.\d+)""") # Extract the two pairs of coords from the rest of the text

mykml = untangle.parse(inputFilename) # Parse the XML input file

coords = [] # Prepare to store coordinates

for folder in mykml.kml.Document.Folder.Placemark: # For each 'folder'...
tmpcoordpair = latlonre.findall(folder.LineString.coordinates.cdata) # Find all of the Regular Expression string matches

for i in range(2): # Since there are '2' pairs do the following...
coord = LatLon_(tmpcoordpair[i*2], tmpcoordpair[i*2+1])
coords.append({
"folder": folder.name.cdata,
"coord_deg": str(latlonDMS(coord, form='-d')).replace('°', ''),
"coord_degmin": str(latlonDMS(coord, form='-dm')).replace('°', ' ').replace('′', ''),
"coord_degminsec": str(latlonDMS(coord, form='-dms')).replace('°', ' ').replace('′', ' ').replace('″', ''),
})

for coord in coords:
print(coord) # For debugging really...

csvHeaders = coords[0].keys()
with open(outputFilename, 'w', newline='', encoding='utf-8') as output_file:
csv.register_dialect('easyout', delimiter=',', quoting=csv.QUOTE_ALL)
dict_writer = csv.DictWriter(output_file, csvHeaders, dialect='easyout')
dict_writer.writeheader()
dict_writer.writerows(coords)

0 comments on commit 816538a

Please sign in to comment.