Use for questions about the pyKML Python module used for creating, parsing, manipulating, and validating KML files.
pyKML is a Python package for creating, parsing, manipulating, and validating KML, a language for encoding and annotating geographic data.
pyKML is based on the lxml.objectify API which provides a Pythonic API for working with XML documents. pyKML adds additional functionality specific to the KML language.
Flat learning curve:
Concise, readable and expressive syntax, easy to learn for python developers
Hello World
Example code to create a KML object then output it to a file.
from pykml.factory import KML_ElementMaker as KML
from lxml import etree
kml = KML.Placemark(
KML.name("Hello World!"),
KML.Point(
KML.coordinates("-64.5253,18.4607")
)
)
with open('hello.kml', 'wb') as output:
output.write(etree.tostring(kml, pretty_print=True, xml_declaration = True))
This is what is generated:
<?xml version='1.0' encoding='UTF-8'?>
<Placemark xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">
<name>Hello World!</name>
<Point>
<coordinates>-64.5253,18.4607</coordinates>
</Point>
</Placemark>
Asking Questions:
- Before asking the question, make sure you have gone through the Examples and Tutorials. It covers all the basic functionality of pyKML.