#!/usr/bin/env python3

from lxml import etree
from lxml.builder import E
from zipfile import ZipFile

def makeXMLObject(name, description, coordinates):
    return E.Object(
        {'name': name},
        E.Group('SAGs'),
        E.Comment(description),
        E.Table('/'),
        E.Symbol('w'),
        E.Latitude(coordinates.split(',')[1]),
        E.Longitude(coordinates.split(',')[0]),
        E.Interval('10'),
        E.Enabled('1'),
        E.ISEnabled('1'),
        E.RFEnabled('1'),
        E.RFPath('WIDE1-1,WIDE2-1'),
    )

with ZipFile('2018 VT 50.kmz') as f:
    xml = etree.fromstring(f.read('doc.kml'))

for placemark in xml.findall('.//{*}Placemark'):
    point = placemark.find('{*}Point')
    if point is not None:
        name = placemark.find('{*}name').text.strip()
        description = placemark.find('{*}description')
        description = description.text.strip() if description is not None else ''
        coordinates = point.find('{*}coordinates').text.strip()
        print(etree.tostring(makeXMLObject(name, description, coordinates),
              pretty_print=True, encoding='unicode'))

