Commit a0cdbb8d authored by Christian Ledermann's avatar Christian Ledermann

add get_style_by_url to document

parent 95a282da
...@@ -26,6 +26,8 @@ http://schemas.opengis.net/kml/. ...@@ -26,6 +26,8 @@ http://schemas.opengis.net/kml/.
""" """
import urlparse
from .geometry import Point, LineString, Polygon from .geometry import Point, LineString, Polygon
from .geometry import MultiPoint, MultiLineString, MultiPolygon from .geometry import MultiPoint, MultiLineString, MultiPolygon
from .geometry import LinearRing from .geometry import LinearRing
...@@ -534,6 +536,12 @@ class Document(_Container): ...@@ -534,6 +536,12 @@ class Document(_Container):
feature.from_element(placemark) feature.from_element(placemark)
self.append(feature) self.append(feature)
def get_style_by_url(self, styleUrl):
id = urlparse.urlparse(styleUrl).fragment
for style in self.styles():
if style.id == id:
return style
class Folder(_Container): class Folder(_Container):
""" """
......
...@@ -771,6 +771,48 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -771,6 +771,48 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2.from_string(k.to_string()) k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string()) self.assertEqual(k.to_string(), k2.to_string())
def test_get_style_by_url(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>
<StyleMap id="styleMapExample">
<Pair>
<key>normal</key>
<styleUrl>#normalState</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#highlightState</styleUrl>
</Pair>
</StyleMap>
<Style id="linestyleExample">
<LineStyle>
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
</Style>
</Document>
</kml>"""
k = kml.KML()
k.from_string(doc)
self.assertEqual(len(list(k.features())),1)
document = list(k.features())[0]
style = document.get_style_by_url('http://localhost:8080/somepath#exampleStyleDocument')
self.assertTrue(isinstance(list(style.styles())[0], styles.LabelStyle))
style = document.get_style_by_url('somepath#linestyleExample')
self.assertTrue(isinstance(list(style.styles())[0], styles.LineStyle))
style = document.get_style_by_url('#styleMapExample')
self.assertTrue(isinstance(style, styles.StyleMap))
class DateTimeTestCase( unittest.TestCase ): class DateTimeTestCase( unittest.TestCase ):
def test_timestamp(self): def test_timestamp(self):
......
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