
# Extract coordinates from kml to CSV
# Markus Opitz 2026

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("input.kml")
root = tree.getroot()

ns = {
   "kml": "http://www.opengis.net/kml/2.2",
   "gx": "http://www.google.com/kml/ext/2.2"
}

coords_list = []

for coord in root.findall(".//gx:coord", ns):
    lon, lat, alt= map(float, coord.text.strip().split())
    coords_list.append((lon, lat, alt))

# save CSV
with open("coords.csv", "w", newline="") as f:
     writer = csv.writer(f)
     writer.writerow(["lon", "lat", "alt"])	#header
     writer.writerows(coords_list)

print("saved as coords.csv")
