Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

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="http://www.opengis.net/kml/2.2">
...   <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 f.id, f.properties.name, f.properties.snippet
pm_1, point, Point test
>>>
>>> from shapely.geometry import shape
>>> s = shape(f.geometry)
>>> print s.buffer(1.5).exterior.length
9.4209934708642571

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="http://www.opengis.net/kml/2.2">
...   <Document>
...   </Document>
... </kml>
... """
>>> doc = ElementTree.fromstring(data)
>>> elem = element(doc, f)
>>> print elem
<Element {http://www.opengis.net/kml/2.2}Placemark at ...>
>>> pprint(list(elem))
[<Element {http://www.opengis.net/kml/2.2}name at ...>,
 <Element {http://www.opengis.net/kml/2.2}Snippet at ...>,
 <Element {http://www.opengis.net/kml/2.2}description at ...>,
 <Element {http://www.opengis.net/kml/2.2}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="http://www.opengis.net/kml/2.2">
  <ns0:Document>
    <ns0:Placemark id="pm_2">
      <ns0:name>Number 2</ns0:name>
      <ns0:Snippet>Placemark number 2</ns0:Snippet>
      <ns0:description />
      <ns0:Point>
        <ns0:coordinates>0.000000,0.000000,0.0</ns0:coordinates>
      </ns0:Point>
    </ns0:Placemark>
  </ns0:Document>
</ns0:kml>

KML Helpers

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

>>> from keytree.kml import kml_ns, findall_placemarks
>>> print kml_ns(doc)
{http://www.opengis.net/kml/2.2}
>>> findall_placemarks(doc)
[<Element {http://www.opengis.net/kml/2.2}Placemark at ...>]

Release files for keytree 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keytree 1.1.0
File Size Uploaded
keytree-1.1.0.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keytree 1.1.0
File Interpreter ABI Platform
keytree-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / keytree-1.1.0.tar.gz

Download URL keytree-1.1.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b0db38f599317827bb4ff6de76d18bad95a594ea9c9e6cf44e98ba68aea86430
BLAKE2b-256 checksum
How to use checksums
bc1e313b8a491799dcc1290e6c84b07c95866ebd196f17adbd3c0e8ae9d1a37a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

Release files / keytree-1.1.0-py3-none-any.whl

Download URL keytree-1.1.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4696465136b502e1c6b49b4954ce2ad8938e001b7fccffe3017e5c922f209204
BLAKE2b-256 checksum
How to use checksums
89dd82a7d556cba27d481006f28348aa551e61763e1b3065ada7179a29885eea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page