#!/usr/bin/env python
from fastkml import kml
# Setup the string which contains the KML file we want to read
doc = """
Document.kml
1
Document Feature 1
#exampleStyleDocument
-122.371,37.816,0
Document Feature 2
#exampleStyleDocument
-122.370,37.817,0
"""
# Create the KML object to store the parsed result
# Read in the KML string
k = kml.KML.from_string(doc.encode("utf-8"))
# Next we perform some simple sanity checks
# Check that the number of features is correct
# This corresponds to the single ``Document``
features = k.features
print(len(features))
# Check that we can access the features as a generator
# (The two Placemarks of the Document)
print(features[0].features)
f2 = features[0].features
print(len(f2))
# Check specifics of the first Placemark in the Document
print(f2[0])
print(f2[0].description)
print(f2[0].name)
# Check specifics of the second Placemark in the Document
print(f2[1].name)
f2[1].name = "ANOTHER NAME"
print(f2[1].name)
# Verify that we can print back out the KML object as a string
print(k.to_string(prettyprint=True))
expected = """
Document.kml
1
1
ff0000cc
1.0
Document Feature 1
1
0
-122.371000,37.816000,0.000000
ANOTHER NAME
1
0
-122.370000,37.817000,0.000000
"""