Skip to main content

Python KML library

Project description

=========
PyKML
=========
PyKML is a Python package for parsing and authoring KML documents. It is based
on the lxml.objectify API (http://codespeak.net/lxml/objectify.html) which
provides Pythonic access to XML documents.

------------
Dependencies
------------
* lxml

------------
Installation
------------
PyKML can be installed from the Python Package Index, using either easy_install
or pip:

$ sudo easy_install pykml

or

$ sudo pip install pykml

------
Usage
------

KML documents can be constructed by using element factory objects. The
following example uses two factory objects, corresponding to the OGC KML and
ATOM namespaces:

>>> from pykml.kml_ogc.factory import KML_ElementMaker as KML
>>> from pykml.kml_ogc.factory import ATOM_ElementMaker as ATOM
>>> doc = KML.kml(
... KML.Document(
... ATOM.author(
... ATOM.name("J. K. Rowling")
... ),
... ATOM.link(href="http://www.harrypotter.com"),
... KML.Placemark(
... KML.name("Hogwarts"),
... KML.Point(
... KML.coordinates("1,1")
... )
... )
... )
... )

Constructed documents can be converted to a string respresentation:

>>> from lxml import etree
>>> etree.tostring(doc)

and can be validated against the official KML XML Schema:

>>> from pykml.kml_ogc import schema
>>> print schema.validate(doc)

Existing KML documents can also be parsed:

>>> import urllib2
>>> from pykml.kml_ogc.parser import parse
>>> url = 'http://code.google.com/apis/kml/documentation/KML_Samples.kml'
>>> fileobject = urllib2.urlopen(url)
>>> tree = parse(fileobject, validate=True)

Documents that make use of the Google Extension namespace elements can be
created by importing the appropriate factory objects:

>>> from pykml.kml_gx import schema
>>> from pykml.kml_gx.factory import KML_ElementMaker as K
>>> from pykml.kml_gx.factory import ATOM_ElementMaker as ATOM
>>> from pykml.kml_gx.factory import GX_ElementMaker as GX
>>> doc = K.kml(
... GX.Tour(
... GX.Playlist(
... GX.SoundCue(
... K.href("http://dev.keyhole.com/codesite/cntowerfacts.mp3")
... ),
... GX.Wait(
... GX.duration(10)
... ),
... GX.FlyTo(
... GX.duration(5),
... GX.flyToMode("bounce"),
... K.LookAt(
... K.longitude(-79.387),
... K.latitude(43.643),
... K.altitude(0),
... K.heading(-172.3),
... K.tilt(10),
... K.range(1200),
... K.altitudeMode("relativeToGround"),
... )
... )
... )
... )
... )
>>> print schema.validate(doc)

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

pykml-0.0.2.tar.gz (16.0 kB view details)

Uploaded Source

File details

Details for the file pykml-0.0.2.tar.gz.

File metadata

  • Download URL: pykml-0.0.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pykml-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a4bbdd462eca35dbbff724082f3309f084fc09f03c14208ed1baee6f041840e1
MD5 64de07c35f4b06f9b82aedf09429178a
BLAKE2b-256 849d2349684a7c08bea794c275d178b6bd6b35d1872d475c138025f209ea410a

See more details on using hashes here.

Supported by

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