Keytree

Keytree (also known as pleiades.keytree in our repository) is a simple package of ElementTree helpers for parsing KML. I needed it, didn't see anything else like it out there, and put in a little extra work to make it more generally useful. If you're already using ElementTree and are curious, give it a try. It plays well with Shapely, of course:

>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse(open('archaic.kml', 'rb'))
>>> kmlns = tree.getroot().tag.split('}')[0][1:]
>>> placemarks = tree.findall('*/{%s}Placemark' % kmlns)
>>> p0 = placemarks[0]
>>> import keytree
>>> f = keytree.feature(p0)
>>> from shapely.geometry import asShape
>>> shape = asShape(f.geometry)
>>> shape.wkt
'POINT (21.9725000000000001 32.8962999999999965)'

Keytree parses out points, line strings, and polygons, but not yet geometry collections. I'll make a real release of it after I get a chance to work on compatibility with lxml.

Comments

Re: Keytree

Author: Matt Priour

>>> from xml.etree import ElementTree >>> tree = ElementTree.parse(open('archaic.kml', 'rb')) >>> kmlns = tree.getroot().tag.split('}')[0][1:] >>> placemarks = tree.findall('*/{%s}Placemark' % kmlns) >>> p0 = placemarks[0]
Is it possible to have keytree just automatically do the kmlns & placemarks operations you have done here? It seems that is keytree is for KML then it should automatically know those 2 things and they could be implemented as helper/convience(sic) functions rather than always having to manually do that everytime you want to start working with KML feature extraction/import.

Re: Keytree

Author: Sean

Indeed. If KML docs were constrained to a Document/Folder/Placemark structure, I would use the WorldMill model. But KML is more loosely structured than that -- you can have folders of folders, and placemarks and folders as siblings -- and for now I'm leaving the how of getting Placemark elements up to the user (me, for all I know).