Commit 8494df50 authored by Christian Ledermann's avatar Christian Ledermann

enable prettyprint, add usage examples to README

parent 562c8677
...@@ -22,3 +22,155 @@ provides (Point, LineString, Polygon, MultiPoint, MultiLineString, ...@@ -22,3 +22,155 @@ provides (Point, LineString, Polygon, MultiPoint, MultiLineString,
MultiPolygon and LinearRing). While KML allows for more abstract MultiPolygon and LinearRing). While KML allows for more abstract
MultiGeometries consisting of a combination of Points, LineStrings MultiGeometries consisting of a combination of Points, LineStrings
and LinearRings, this is not supported in fastkml and LinearRings, this is not supported in fastkml
Usage
=====
You can find more examples in the included tests.py file, here is a
quick overview:
Build a KML from scatch:
------------------------
::
>>> from fastkml import kml
>>> from shapely.geometry import Point, LineString, Polygon
>>> k = kml.KML()
>>> ns = '{http://www.opengis.net/kml/2.2}'
>>> d = kml.Document(ns, 'docid', 'doc name', 'doc description')
>>> f = kml.Folder(ns, 'fid', 'f name', 'f description')
>>> k.append(d)
>>> d.append(f)
>>> nf = kml.Folder(ns, 'nested-fid', 'nested f name', 'nested f description')
>>> f.append(nf)
>>> f2 = kml.Folder(ns, 'id2', 'name2', 'description2')
>>> d.append(f2)
>>> p = kml.Placemark(ns, 'id', 'name', 'description')
>>> p.geometry = Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
>>> f2.append(p)
>>> print k.to_string(prettyprint=True)
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document id="docid">
<ns0:name>doc name</ns0:name>
<ns0:description>doc description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="fid">
<ns0:name>f name</ns0:name>
<ns0:description>f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Folder id="nested-fid">
<ns0:name>nested f name</ns0:name>
<ns0:description>nested f description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
</ns0:Folder>
</ns0:Folder>
<ns0:Folder id="id2">
<ns0:name>name2</ns0:name>
<ns0:description>description2</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Placemark id="id">
<ns0:name>name</ns0:name>
<ns0:description>description</ns0:description>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Polygon>
<ns0:outerBoundaryIs>
<ns0:LinearRing>
<ns0:coordinates>0.000000,0.000000,0.000000
1.000000,1.000000,0.000000
1.000000,0.000000,1.000000
0.000000,0.000000,0.000000
</ns0:coordinates>
</ns0:LinearRing>
</ns0:outerBoundaryIs>
</ns0:Polygon>
</ns0:Placemark>
</ns0:Folder>
</ns0:Document>
</ns0:kml>'
Read a KML file
----------------
You can create a KML object by reading a KML file:
::
>>> from fastkml import kml
>>> doc = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... <name>Document.kml</name>
... <open>1</open>
... <Style id="exampleStyleDocument">
... <LabelStyle>
... <color>ff0000cc</color>
... </LabelStyle>
... </Style>
... <Placemark>
... <name>Document Feature 1</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.371,37.816,0</coordinates>
... </Point>
... </Placemark>
... <Placemark>
... <name>Document Feature 2</name>
... <styleUrl>#exampleStyleDocument</styleUrl>
... <Point>
... <coordinates>-122.370,37.817,0</coordinates>
... </Point>
... </Placemark>
... </Document>
... </kml>"""
>>> k = kml.KML()
>>> k.from_string(doc)
>>> len(k.features())
1
>>> len(k.features()[0].features())
2
>>> k.features()[0].features()[1]
<fastkml.kml.Placemark object at 0x876a16c>
>>> k.features()[0].features()[1].description
>>> k.features()[0].features()[1].name
'Document Feature 2'
>>> k.features()[0].features()[1].name = "ANOTHER NAME"
>>> print k.to_string(prettyprint=True)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document>
<ns0:name>Document.kml</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>1</ns0:open>
<ns0:Style id="exampleStyleDocument">
<ns0:LabelStyle>
<ns0:color>ff0000cc</ns0:color>
<ns0:scale>1.0</ns0:scale>
</ns0:LabelStyle>
</ns0:Style>
<ns0:Placemark>
<ns0:name>Document Feature 1</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.371000,37.816000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
<ns0:Placemark>
<ns0:name>ANOTHER NAME</ns0:name>
<ns0:visibility>1</ns0:visibility>
<ns0:open>0</ns0:open>
<ns0:Point>
<ns0:coordinates>-122.370000,37.817000,0.000000</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
</ns0:Document>
</ns0:kml>
...@@ -27,6 +27,10 @@ except ImportError: ...@@ -27,6 +27,10 @@ except ImportError:
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
LXML = False LXML = False
import re
regex = r"^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\.)*[a-zA-Z]{2,4}$"
check_email = re.compile(regex).match
class Link(object): class Link(object):
""" """
...@@ -159,9 +163,9 @@ class _Person(object): ...@@ -159,9 +163,9 @@ class _Person(object):
uri = etree.SubElement(element, "%suri" %self.ns) uri = etree.SubElement(element, "%suri" %self.ns)
uri.text = self.uri uri.text = self.uri
if self.email: if self.email:
#XXX validate email
email = etree.SubElement(element, "%semail" %self.ns) email = etree.SubElement(element, "%semail" %self.ns)
email.text = self.email if check_email(self.email):
email.text = self.email
...@@ -180,7 +184,8 @@ class _Person(object): ...@@ -180,7 +184,8 @@ class _Person(object):
self.uri = uri.text self.uri = uri.text
email = element.find('%semail' %self.ns) email = element.find('%semail' %self.ns)
if email is not None: if email is not None:
self.email = email.text if check_email(email):
self.email = email.text
class Author(_Person): class Author(_Person):
......
...@@ -76,7 +76,11 @@ class KML(object): ...@@ -76,7 +76,11 @@ class KML(object):
def to_string(self, prettyprint=False): def to_string(self, prettyprint=False):
""" Returm the KML Object as xml """ """ Returm the KML Object as xml """
return etree.tostring(self.etree_element(), encoding='utf-8') if LXML and prettyprint:
return etree.tostring(self.etree_element(), encoding='utf-8',
pretty_print=True)
else:
return etree.tostring(self.etree_element(), encoding='utf-8')
def features(self): def features(self):
""" return a list of features """ """ return a list of features """
......
...@@ -408,6 +408,7 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -408,6 +408,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
</Style> </Style>
</Document> </Document>
</kml>""" </kml>"""
#XXX fil and outline
k = kml.KML() k = kml.KML()
k.from_string(doc) k.from_string(doc)
self.assertEqual(len(k.features()),1) self.assertEqual(len(k.features()),1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment