Kml

Kml

class simplekml.Kml(**kwargs)

The main class that represents a KML file.

This class represents a KML file, and the compilation of the KML file will be done through this class. The base feature is a document, all arguments passed to the class on creation are the same as that of a simplekml.Document. To change any properties after creation you can do so through the simplekml.Kml.document property (eg. kml.document.name = “Test”). For a description of what the arguments mean see the KML reference documentation published by Google: http://code.google.com/apis/kml/documentation/kmlreference.html

Simple Example:

from simplekml import Kml
kml = Kml(name='KmlUsage')
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)])  # A simple Point
kml.save("KmlClass.kml")  # Saving
kml.savekmz("KmlClass.kmz", format=False)  # Saving as KMZ
print kml.kml()  # Printing out the kml to screen
addfile(path)

Adds an file to a KMZ and returns the path contained inside of the KMZ (files/…)

This is useful for including images in a KMZ that are referenced from description balloons, as these files are not automatically included in a KMZ.

Usage:

import simplekml
kml = simplekml.Kml()
path = kml.addfile("a/path/to/somefile.file")
pnt = pnt.newpoint()
pnt.description = '<img src="' + path +'" alt="picture" width="400" height="300" align="left" />'

New in version 1.2.0

allcontainers

Returns a list of all the containers that have been attached to the top level document, and all sub containers.

New in version 1.1.0

allfeatures

Returns a list of all the features that have been attached to the top level document, and all sub features.

New in version 1.1.0

allgeometries

Returns a list of all the geometries that have been attached to the top level document, and all sub geometries.

New in version 1.1.0

allstylemaps

Returns a list of all the stylemaps that have been attached to the top level document, and all sub stylemaps.

New in version 1.1.0

allstyles

Returns a list of all the styles that have been attached to the top level document, and all sub styles.

New in version 1.1.0

containers

Returns a list of all the containers that have been attached to to the top level document.

New in version 1.1.0

document

The top level item in the kml document.

0 or 1 top level document is required for a kml document, the default is an instance of simplekml.Document. This property can be set to an instance of simplekml.Document or simplekml.Folder or to remove it completely set it to None

Example:

import simplekml
kml = simplekml.Kml()
kml.document = simplekml.Folder(name = "Top Level Folder")
kml.save('Document Replacement.kml')
features

Returns a list of all the features that have been attached to the top level document.

geometries

Returns a list of all the geometries that have been attached to the top level document.

New in version 1.1.0

hint

Assign a hint attribute to the KML tag.

Possible values to use are:
  • target=moon
  • target=sky
  • target=mars

Usage:

from simplekml import Kml
kml = Kml()
kml.hint = 'target=moon'
print kml.kml()

Result:

<?xml version="1.0" encoding="UTF-8"?>
<kml hint="target=moon" xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1"/>
</kml>

New in version 1.1.0

kml(format=True)

Returns the kml as a string or “prettyprinted” if format = True.

Note

Setting format = False will produce smaller files, as well as decrease the memory required while processing the kml.

PrettyPrinted Example (default):

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name='A Point')
pnt.coords = [(1.0, 2.0)]
print kml.kml()

PrettyPrinted Result:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
        <Placemark id="feat_2">
            <name>A Point</name>
            <Point id="geom_0">
                <coordinates>1.0,2.0,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

Single Line Example:

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name='A Point')
pnt.coords = [(1.0, 2.0)]
print kml.kml(False)

Single Line Result:

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"><Document id="feat_1"><Placemark id="feat_2"><name>A Point</name><Point id="geom_0"><coordinates>1.0,2.0,0.0</coordinates></Point></Placemark></Document></kml>
networklinkcontrol

Accesses/Creates the simplekml.NetworkLinkControl.

See simplekml.NetworkLinkControl for usage example.

New in version 1.1.1

newdocument(**kwargs)

Creates a new simplekml.Document.

The document is attached to this KML document. The arguments are the same as for simplekml.Document. See simplekml.Document for usage.

newfolder(**kwargs)

Creates a new simplekml.Folder.

The folder is attached to this KML document. The arguments are the same as those for simplekml.Folder See simplekml.Folder for usage.

newgroundoverlay(**kwargs)

Creates a new simplekml.GroundOverlay.

The groundoverlay is attached to this KML document. The arguments are the same as those for simplekml.GroundOverlay. See simplekml.GroundOverlay for usage.

newgxmultitrack(**kwargs)

Creates a new simplekml.GxMultiTrack.

The gxmultitrack is attached to this KML document. The arguments are the same as those for simplekml.GxMultiTrack. See simplekml.GxMultiTrack for usage.

newgxtour(**kwargs)

Creates a new simplekml.GxTour.

The tour is attached to this KML document. The arguments are the same as those for simplekml.GxTour See simplekml.GxTour for usage.

newgxtrack(**kwargs)

Creates a new simplekml.GxTrack.

The gxtrack is attached to this KML document. The arguments are the same as those for simplekml.GxTrack See simplekml.GxTrack for usage.

newlinestring(**kwargs)

Creates a new simplekml.LineString.

The linestring is attached to this KML document. The arguments are the same as for simplekml.LineString See simplekml.LineString for usage.

newmodel(**kwargs)

Creates a new simplekml.Model.

The model is attached to this KML document. The arguments are the same as those for simplekml.Model

newmultigeometry(**kwargs)

Creates a new simplekml.MultiGeometry.

The multigeometry is attached to this KML document. The arguments are the same as for simplekml.MultiGeometry. See simplekml.MultiGeometry for usage.

Creates a new simplekml.NetworkLink.

The networklink is attached to this KML document. The arguments are the same as those for simplekml.NetworkLink. See simplekml.NetworkLink for usage.

newphotooverlay(**kwargs)

Creates a new simplekml.PhotoOverlay.

The photooverlay is attached to this KML document. The arguments are the same as those for simplekml.PhotoOverlay. See simplekml.PhotoOverlay for usage.

newplacemark(**kwargs)

Creates a new simplekml.Placemark.

The Placemark is attached to this KML document. The arguments are the same as those for simplekml.Placemark See simplekml.Placemark for usage.

newpoint(**kwargs)

Creates a new simplekml.Point.

The point is attached to this KML document. The arguments are the same as those for simplekml.Point See simplekml.Point for usage.

newpolygon(**kwargs)

Creates a new simplekml.Polygon.

The polygon is attached to this KML document. The arguments are the same as those for simplekml.Polygon See simplekml.Polygon for usage.

newschema(**kwargs)

Creates a new simplekml.Schema.

The schem is attached to this KML document. The arguments are the same as those for simplekml.Schema

newscreenoverlay(**kwargs)

Creates a new simplekml.ScreenOverlay.

The screenoverlay is attached to this KML document. The arguments are the same as those for simplekml.ScreenOverlay. See simplekml.ScreenOverlay for usage.

parsetext(parse=True)

Sets the behavior of how text tags are parsed.

If True the values of the text tags (<name>, <description> and <text>) are escaped, so that the values are rendered properly. If False, the values are left as is. In both cases the CDATA element is left unchanged.

Changed in version 1.1.0

static resetidcounter()

Resets the id counter so that ids count from 0.

New in version 1.3.1

save(path, format=True)

Save the kml to the given file supplied by path.

The KML is saved to a file in one long string if format=False else it gets saved “prettyprinted” (as formatted xml). This works the same as simplekml.Kml.kml()

Note

Setting format = False will produce smaller files, as well as decrease the memory required while processing the kml.

Usage:

import simplekml
kml = simplekml.Kml()
kml.save("Saving.kml")
#kml.save("Saving.kml", False)  # or this
savekmz(path, format=True)

Save the kml as a kmz to the given file supplied by path.

The KML is saved to a file in a long string if format=False else it gets saved “prettyprinted”. This works the same as simplekml.Kml.kml()

Usage:

import simplekml
kml = simplekml.Kml()
kml.savekmz("Saving.kml")
#kml.savekmz("Saving.kml", False)  # or this
stylemaps

Returns a list of all the stylemaps that have been attached to the top level document.

New in version 1.1.0

styles

Returns a list of all the styles that have been attached to the top level document.

New in version 1.1.0

NetworkLinkControl

class simplekml.NetworkLinkControl(minrefreshperiod=None, maxsessionlength=None, cookie=None, message=None, linkname=None, linkdescription=None, linksnippet=None, expires=None, update=None, camera=None, lookat=None, **kwargs)

Controls the behavior of files fetched by a simplekml.NetworkLink.

Arguments are the same as the properties.

Usage:

import simplekml
kml = simplekml.Kml()
kml.document = None  # Removes the default document
kml.networklinkcontrol.minrefreshperiod = 5  # By accessing the networklinkcontrol property one it created
kml.save('NetworkLinkControl.kml')

New in version 1.1.1

camera

Camera that views the scene, accepts simplekml.Camera

New in version 1.1.1

cookie

Use this to append a string to the URL query on the next refresh of the network link, accepts string.

New in version 1.1.1

expires

Date/time at which the link should be refreshed, accepts string.

New in version 1.1.1

linkdescription

Description of the network link, accepts string.

New in version 1.1.1

linkname

Name of the network link, accepts string.

New in version 1.1.1

linksnippet

Short description of the feature, accepts simplekml.LinkSnippet

New in version 1.1.1

lookat

Camera relative to the feature, accepts simplekml.LookAt

New in version 1.1.1

maxsessionlength

Maximum amount of time for which the client simplekml.NetworkLink can remain connected in seconds, accepts int.

New in version 1.1.1

message

A message that appears when the network link is first loaded into Google Earth, accepts string.

New in version 1.1.1

minrefreshperiod

Minimum allowed time between fetches of the file in seconds, accepts int.

New in version 1.1.1

update

Instance of simplekml.Update

New in version 1.1.1

LinkSnippet

class simplekml.LinkSnippet(**kwargs)

A short description of the feature.

Arguments are the same as the properties.

New in version 1.1.1

content

The description to be used in the snippet, accepts string.

maxlines

Number of lines to display, accepts int.