Commit 504510ab authored by Christian Ledermann's avatar Christian Ledermann

add tests for styles

parent 30f898b1
......@@ -245,14 +245,14 @@ class IconStyle(_ColorStyle):
__name__ = "IconStyle"
scale = 1.0
# Resizes the icon. (float)
heading = 0.0
heading = None
# Direction (that is, North, South, East, West), in degrees.
# Default=0 (North).
icon_href = None
# An HTTP address or a local file specification used to load an icon.
def __init__(self, ns=None, id=None, color=None, colorMode=None,
scale=1.0, heading=0.0, icon_href=None):
scale=1.0, heading=None, icon_href=None):
super(IconStyle, self).__init__(ns, id, color, colorMode)
self.scale = scale
self.heading = heading
......@@ -282,7 +282,7 @@ class IconStyle(_ColorStyle):
self.heading = float(heading.text)
icon = element.find('%sIcon' %self.ns)
if icon is not None:
href = element.find('%shref' %self.ns)
href = icon.find('%shref' %self.ns)
if href is not None:
self.icon_href = href.text
......@@ -323,6 +323,7 @@ class PolyStyle(_ColorStyle):
extrusions (which look like the walls of buildings) and line
extrusions (which look like solid fences).
"""
__name__ = "PolyStyle"
fill = 1
# Boolean value. Specifies whether to fill the polygon.
outline = 1
......
......@@ -2,6 +2,7 @@
import unittest
from fastkml import kml
from fastkml import styles
import xml.etree.ElementTree as etree
from shapely.geometry import Point, LineString, Polygon
from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon
......@@ -303,11 +304,176 @@ class KmlFromStringTestCase( unittest.TestCase ):
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
class StyleFromStringTestCase( unittest.TestCase ):
def test_labelstyle(self):
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>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(k.features()),1)
self.assertTrue(isinstance(
list(k.features()[0].styles())[0], styles.Style))
style = list(list(k.features()[0].styles())[0].styles())[0]
self.assertTrue(isinstance(style, styles.LabelStyle))
self.assertEqual(style.color, 'ff0000cc')
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_iconstyle(self):
doc = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="randomColorIcon">
<IconStyle>
<color>ff00ff00</color>
<colorMode>random</colorMode>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/icon21.png</href>
</Icon>
</IconStyle>
</Style>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(k.features()),1)
self.assertTrue(isinstance(
list(k.features()[0].styles())[0], styles.Style))
style = list(list(k.features()[0].styles())[0].styles())[0]
self.assertTrue(isinstance(style, styles.IconStyle))
self.assertEqual(style.color, 'ff00ff00')
self.assertEqual(style.scale, 1.1)
self.assertEqual(style.colorMode, 'random')
self.assertEqual(style.icon_href, 'http://maps.google.com/icon21.png')
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_linestyle(self):
doc="""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>LineStyle.kml</name>
<open>1</open>
<Style id="linestyleExample">
<LineStyle>
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
</Style>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(k.features()),1)
self.assertTrue(isinstance(
list(k.features()[0].styles())[0], styles.Style))
style = list(list(k.features()[0].styles())[0].styles())[0]
self.assertTrue(isinstance(style, styles.LineStyle))
self.assertEqual(style.color, '7f0000ff')
self.assertEqual(style.width, 4)
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_polystyle(self):
doc="""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>PolygonStyle.kml</name>
<open>1</open>
<Style id="examplePolyStyle">
<PolyStyle>
<color>ff0000cc</color>
<colorMode>random</colorMode>
</PolyStyle>
</Style>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(k.features()),1)
self.assertTrue(isinstance(
list(k.features()[0].styles())[0], styles.Style))
style = list(list(k.features()[0].styles())[0].styles())[0]
self.assertTrue(isinstance(style, styles.PolyStyle))
self.assertEqual(style.color, 'ff0000cc')
self.assertEqual(style.colorMode, 'random')
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_styles(self):
doc="""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<!-- Begin Style Definitions -->
<Style id="myDefaultStyles">
<IconStyle>
<color>a1ff00ff</color>
<scale>1.399999976158142</scale>
<Icon>
<href>http://myserver.com/icon.jpg</href>
</Icon>
</IconStyle>
<LabelStyle>
<color>7fffaaff</color>
<scale>1.5</scale>
</LabelStyle>
<LineStyle>
<color>ff0000ff</color>
<width>15</width>
</LineStyle>
<PolyStyle>
<color>7f7faaaa</color>
<colorMode>random</colorMode>
</PolyStyle>
</Style>
<!-- End Style Definitions -->
<!-- Placemark #1 -->
<Placemark>
<name>Google Earth - New Polygon</name>
<description>Here is some descriptive text</description>
<styleUrl>#myDefaultStyles</styleUrl>
. . .
</Placemark>
<!-- Placemark #2 -->
<Placemark>
<name>Google Earth - New Path</name>
<styleUrl>#myDefaultStyles</styleUrl>
. . . .
</Placemark>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(k.features()),1)
self.assertTrue(isinstance(
list(k.features()[0].styles())[0], styles.Style))
style = list(list(k.features()[0].styles())[0].styles())
self.assertEqual(len(style), 4)
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite( KmlFromStringTestCase ))
suite.addTest(unittest.makeSuite( BuildKmlTestCase ))
suite.addTest(unittest.makeSuite( StyleFromStringTestCase ))
return suite
if __name__ == '__main__':
......
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