Commit 562c8677 authored by Christian Ledermann's avatar Christian Ledermann

add styelUrl to features, add from_element to stylemap, from_element, test for stylemap

parent 3caf41f6
...@@ -13,6 +13,8 @@ href attribute - URL of the web page containing the KML/KMZ file ...@@ -13,6 +13,8 @@ href attribute - URL of the web page containing the KML/KMZ file
These elements are defined in the Atom Syndication Format. The complete These elements are defined in the Atom Syndication Format. The complete
specification is found at http://atompub.org. specification is found at http://atompub.org.
This library only implements a subset of Atom that is useful with KML
""" """
import logging import logging
logger = logging.getLogger('fastkml.atom') logger = logging.getLogger('fastkml.atom')
......
...@@ -214,6 +214,11 @@ class _Feature(object): ...@@ -214,6 +214,11 @@ class _Feature(object):
s = StyleMap(self.ns) s = StyleMap(self.ns)
s.from_element(style) s.from_element(style)
self.append_style(s) self.append_style(s)
style_url = element.find('%sstyleUrl' % self.ns)
if style_url:
s = StyleUrl(self.ns)
s.from_element(style_url)
self.styleUrl = s.url
...@@ -470,7 +475,7 @@ class Placemark(_Feature): ...@@ -470,7 +475,7 @@ class Placemark(_Feature):
if self.geometry is not None: if self.geometry is not None:
element.append(self._etree_geometry()) element.append(self._etree_geometry())
else: else:
logger.warn('Object does not have a geometry') logger.error('Object does not have a geometry')
return element return element
......
...@@ -164,8 +164,42 @@ class StyleMap(_StyleSelector): ...@@ -164,8 +164,42 @@ class StyleMap(_StyleSelector):
super(StyleMap, self).__init__(ns, id) super(StyleMap, self).__init__(ns, id)
pass pass
def from_element(self, element):
super(StyleMap, self).from_element(element)
pairs = element.findall('%sPair' %self.ns)
for pair in pairs:
key = pair.find('%skey' %self.ns)
style = pair.find('%sStyle' %self.ns)
style_url = pair.find('%sstyleUrl' %self.ns)
if key.text == "highlight":
if style is not None:
highlight = Style(self.ns)
highlight.from_element(style)
elif style_url is not None:
highlight = StyleUrl(self.ns)
highlight.from_element(style_url)
else:
raise ValueError
self.highlight = highlight
elif key.text == "normal":
if style is not None:
normal = Style(self.ns)
normal.from_element(style)
elif style_url is not None:
normal = StyleUrl(self.ns)
normal.from_element(style_url)
else:
import ipdb; ipdb.set_trace()
raise ValueError
self.normal = normal
else:
raise ValueError
def etree_element(self): def etree_element(self):
element = super(Style, self).etree_element() element = super(StyleMap, self).etree_element()
if self.normal: if self.normal:
if isinstance(self.normal, (Style, StyleUrl)): if isinstance(self.normal, (Style, StyleUrl)):
pair = etree.SubElement(element, "%sPair" %self.ns) pair = etree.SubElement(element, "%sPair" %self.ns)
......
...@@ -8,6 +8,7 @@ from shapely.geometry import Point, LineString, Polygon ...@@ -8,6 +8,7 @@ from shapely.geometry import Point, LineString, Polygon
from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon
from shapely.geometry.polygon import LinearRing from shapely.geometry.polygon import LinearRing
class BuildKmlTestCase(unittest.TestCase): class BuildKmlTestCase(unittest.TestCase):
""" Build a simple KML File """ """ Build a simple KML File """
def test_kml(self): def test_kml(self):
...@@ -331,6 +332,7 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -331,6 +332,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2 = kml.KML() k2 = kml.KML()
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())
print k.to_string()
def test_iconstyle(self): def test_iconstyle(self):
doc = """<?xml version="1.0" encoding="UTF-8"?> doc = """<?xml version="1.0" encoding="UTF-8"?>
...@@ -362,6 +364,7 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -362,6 +364,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2 = kml.KML() k2 = kml.KML()
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())
print k.to_string()
def test_linestyle(self): def test_linestyle(self):
doc="""<?xml version="1.0" encoding="UTF-8"?> doc="""<?xml version="1.0" encoding="UTF-8"?>
...@@ -389,6 +392,7 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -389,6 +392,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2 = kml.KML() k2 = kml.KML()
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())
print k.to_string()
def test_polystyle(self): def test_polystyle(self):
doc="""<?xml version="1.0" encoding="UTF-8"?> doc="""<?xml version="1.0" encoding="UTF-8"?>
...@@ -416,6 +420,7 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -416,6 +420,7 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2 = kml.KML() k2 = kml.KML()
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())
print k.to_string()
def test_styles(self): def test_styles(self):
doc="""<?xml version="1.0" encoding="UTF-8"?> doc="""<?xml version="1.0" encoding="UTF-8"?>
...@@ -456,6 +461,84 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -456,6 +461,84 @@ class StyleFromStringTestCase( unittest.TestCase ):
k2 = kml.KML() k2 = kml.KML()
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())
print k.to_string()
def test_stylemapurl(self):
doc= """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<StyleMap id="styleMapExample">
<Pair>
<key>normal</key>
<styleUrl>#normalState</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#highlightState</styleUrl>
</Pair>
</StyleMap>
</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.StyleMap))
sm = list(list(k.features()[0].styles()))[0]
self.assertTrue(isinstance(sm.normal, styles.StyleUrl))
self.assertEqual(sm.normal.url, '#normalState')
self.assertTrue(isinstance(sm.highlight, styles.StyleUrl))
self.assertEqual(sm.highlight.url, '#highlightState')
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_stylemapstyles(self):
doc= """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<StyleMap id="styleMapExample">
<Pair>
<key>normal</key>
<Style id="exampleStyleDocument">
<LabelStyle>
<color>ff0000cc</color>
</LabelStyle>
</Style>
</Pair>
<Pair>
<key>highlight</key>
<Style id="examplePolyStyle">
<PolyStyle>
<color>ff0000cc</color>
<colorMode>random</colorMode>
</PolyStyle>
<LineStyle>
<color>ff0000ff</color>
<width>15</width>
</LineStyle>
</Style>
</Pair>
</StyleMap>
</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.StyleMap))
sm = list(list(k.features()[0].styles()))[0]
self.assertTrue(isinstance(sm.normal, styles.Style))
self.assertEqual(len(list(sm.normal.styles())), 1)
self.assertTrue(isinstance(list(sm.normal.styles())[0], styles.LabelStyle))
self.assertTrue(isinstance(sm.highlight, styles.Style))
self.assertTrue(isinstance(sm.highlight, styles.Style))
self.assertEqual(len(list(sm.highlight.styles())), 2)
self.assertTrue(isinstance(list(sm.highlight.styles())[0], styles.LineStyle))
self.assertTrue(isinstance(list(sm.highlight.styles())[1], styles.PolyStyle))
k2 = kml.KML()
k2.from_string(k.to_string())
self.assertEqual(k.to_string(), k2.to_string())
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
...@@ -466,3 +549,4 @@ def test_suite(): ...@@ -466,3 +549,4 @@ def test_suite():
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
from setuptools import setup, find_packages from setuptools import setup, find_packages
import sys, os import sys, os
version = '0.0' version = '0.1'
setup(name='fastkml', setup(name='fastkml',
version=version, version=version,
...@@ -14,7 +14,7 @@ setup(name='fastkml', ...@@ -14,7 +14,7 @@ setup(name='fastkml',
"Programming Language :: Python", "Programming Language :: Python",
'Intended Audience :: Developers', 'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License (GPL)', 'License :: OSI Approved :: GNU General Public License (GPL)',
'Development Status :: 4 - Beta', 'Development Status :: 3 - Alpha',
'Operating System :: OS Independent', 'Operating System :: OS Independent',
], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers ], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='GIS KML Google Maps OpenLayers', keywords='GIS KML Google Maps OpenLayers',
......
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