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="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 ...>]

Project details


Download files

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

Source Distribution

keytree-1.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

keytree-1.0.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file keytree-1.0.0.tar.gz.

File metadata

  • Download URL: keytree-1.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for keytree-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6f59d426182d4b59238c3b77cdded8359bc74068fc6cf7b6b7de677b50cf3280
MD5 f1058d59a3e6dbcf8f575e07870c2d13
BLAKE2b-256 71de640534f6c358bb5abcabad4954b522926bb6140a699f795f3fd02fe47e12

See more details on using hashes here.

File details

Details for the file keytree-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: keytree-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for keytree-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27f6fdbe8ebe2b1d8177e89b9f4ad70ea7a176983bded9d42119c3399c61014a
MD5 3f2feaafef3400bd610c0f88163fecab
BLAKE2b-256 7bec04a819dc579441b6dcfe6d38c578da0d138e6c1da5dd7959a4023661bcd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page