Skip to main content

KML utilities for the ElementTree API

Project description

Keytree provides functions for reading and writing KML using the ElementTree API.

Reading KML

KML Placemark elements can be adapted to the Python geo interface and then used with packages like Shapely:

>>> data = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="">
...   <Document>
...     <Placemark id="pm_1">
...       <name>point</name>
...       <Snippet>Point test</Snippet>
...       <Point>
...         <coordinates>
...           -122.364383,37.824664,0
...         </coordinates>
...       </Point>
...     </Placemark>
...   </Document>
... </kml>
... """
>>> from xml.etree import ElementTree
>>> doc = ElementTree.fromstring(data)
>>> kmlns = doc.tag.split('}')[0][1:]
>>> placemarks = doc.findall('*/{%s}Placemark' % kmlns)
>>> p0 = placemarks[0]
>>> import keytree
>>> f = keytree.feature(p0)
>>> print,,
pm_1, point, Point test
>>> from shapely.geometry import shape
>>> s = shape(f.geometry)
>>> print s.buffer(1.5).exterior.length

Writing KML

Objects that provide the Python geo interface can also be converted to ElementTree API Elements:

>>> f = {
...     'id': 'pm_2',
...     'geometry': {
...         'type': 'Point',
...         'coordinates': (-122.364383, 37.824663999999999) },
...     'properties': {
...         'title': 'Feature 2',
...         'description': 'The second feature', }

The first argument to the keytree.element function is an XML context, the created element will have the same namespace as that element:

>>> data = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="">
...   <Document>
...   </Document>
... </kml>
... """
>>> doc = ElementTree.fromstring(data)
>>> elem = element(doc, f)
>>> print elem
<Element {}Placemark at ...>
>>> pprint(list(elem))
[<Element {}name at ...>,
 <Element {}Snippet at ...>,
 <Element {}description at ...>,
 <Element {}Point at ...>]

The created element is not automatically added to the KML context and must be appended to its proper Document or Folder:

>>> doc[0].append(elem)
>>> print etree.tostring(doc)
<ns0:kml xmlns:ns0="">
    <ns0:Placemark id="pm_2">
      <ns0:name>Number 2</ns0:name>
      <ns0:Snippet>Placemark number 2</ns0:Snippet>
      <ns0:description />

KML Helpers

The keytree.kml module contains a few useful utility functions:

>>> from keytree.kml import kml_ns, findall_placemarks
>>> print kml_ns(doc)
>>> findall_placemarks(doc)
[<Element {}Placemark at ...>]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for keytree, version 1.1.0
Filename, size File type Python version Upload date Hashes
Filename, size keytree-1.1.0.tar.gz (7.6 kB) File type Source Python version None Upload date Hashes View
Filename, size keytree-1.1.0-py3-none-any.whl (8.8 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page