Commit af5d252c authored by Christian Ledermann's avatar Christian Ledermann

comfortable timespan and timestamp handling for features

parent 7bf44fe5
This diff is collapsed.
...@@ -34,8 +34,7 @@ class StyleUrl(_BaseObject): ...@@ -34,8 +34,7 @@ class StyleUrl(_BaseObject):
element.text = self.url element.text = self.url
return element return element
else: else:
logger.critical('No url given for styleUrl') raise ValueError('No url given for styleUrl')
raise ValueError
def from_element(self, element): def from_element(self, element):
super(StyleUrl, self).from_element(element) super(StyleUrl, self).from_element(element)
...@@ -79,7 +78,7 @@ class Style(_StyleSelector): ...@@ -79,7 +78,7 @@ class Style(_StyleSelector):
def styles(self): def styles(self):
for style in self._styles: for style in self._styles:
if isinstance(style, _ColorStyle): if isinstance(style, (_ColorStyle, BalloonStyle)):
yield style yield style
else: else:
raise TypeError raise TypeError
...@@ -106,6 +105,7 @@ class Style(_StyleSelector): ...@@ -106,6 +105,7 @@ class Style(_StyleSelector):
thestyle = LabelStyle(self.ns) thestyle = LabelStyle(self.ns)
thestyle.from_element(style) thestyle.from_element(style)
self.append_style(thestyle) self.append_style(thestyle)
#XXX BalloonStyle
def etree_element(self): def etree_element(self):
element = super(Style, self).etree_element() element = super(Style, self).etree_element()
...@@ -373,4 +373,7 @@ class LabelStyle(_ColorStyle): ...@@ -373,4 +373,7 @@ class LabelStyle(_ColorStyle):
self.scale = float(scale.text) self.scale = float(scale.text)
class BalloonStyle(_BaseObject): class BalloonStyle(_BaseObject):
""" Specifies how the description balloon for placemarks is drawn.
The <bgColor>, if specified, is used as the background color of
the balloon."""
pass pass
...@@ -5,6 +5,7 @@ from fastkml import kml ...@@ -5,6 +5,7 @@ from fastkml import kml
from fastkml import styles from fastkml import styles
from fastkml import base from fastkml import base
from fastkml import config from fastkml import config
import datetime
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
from shapely.geometry import Point, LineString, Polygon from shapely.geometry import Point, LineString, Polygon
from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon
...@@ -43,8 +44,28 @@ class BaseClassesTestCase(unittest.TestCase): ...@@ -43,8 +44,28 @@ class BaseClassesTestCase(unittest.TestCase):
def test_Feature(self): def test_Feature(self):
f = kml._Feature(name='A Feature') f = kml._Feature(name='A Feature')
self.assertRaises(NotImplementedError, f.etree_element)
self.assertEqual(f.name, 'A Feature')
self.assertEqual(f.visibility, 1)
self.assertEqual(f.isopen, 0)
#self.assertEqual(f.atom_author, None)
#self.assertEqual(f.atom_link, None)
#self.assertEqual(f.address, None)
#self.assertEqual(f.phoneNumber, None)
self.assertEqual(f._snippet, None)
self.assertEqual(f.description, None)
self.assertEqual(f._styleUrl, None)
self.assertEqual(f._styles, [])
self.assertEqual(f._time_span, None)
self.assertEqual(f._time_stamp, None)
#self.assertEqual(f.region, None)
#self.assertEqual(f.extended_data, None)
f.__name__ = 'Feature'
f.styleUrl = '#default' f.styleUrl = '#default'
pass self.assertTrue('Feature>' in f.to_string())
self.assertTrue('#default' in f.to_string())
def test_Container(self): def test_Container(self):
pass pass
...@@ -68,7 +89,7 @@ class BuildKmlTestCase(unittest.TestCase): ...@@ -68,7 +89,7 @@ class BuildKmlTestCase(unittest.TestCase):
k = kml.KML() k = kml.KML()
self.assertEqual(len( list(k.features())),0) self.assertEqual(len( list(k.features())),0)
self.assertEqual( k.to_string(), self.assertEqual( k.to_string(),
'<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2"/>') '<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2"/>')
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())
...@@ -631,12 +652,122 @@ class StyleFromStringTestCase( unittest.TestCase ): ...@@ -631,12 +652,122 @@ 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())
class DateTimeTestCase( unittest.TestCase ):
def test_timestamp(self):
now = datetime.datetime.now()
ts = kml.TimeStamp(timestamp=now)
self.assertEqual(ts.timestamp, [now, 'dateTime'])
self.assertTrue('TimeStamp>' in ts.to_string())
self.assertTrue('when>' in ts.to_string())
self.assertTrue(now.isoformat() in ts.to_string())
y2k = datetime.date(2000,1,1)
ts = kml.TimeStamp(timestamp=y2k)
self.assertEqual(ts.timestamp, [y2k, 'date'])
self.assertTrue('2000-01-01' in ts.to_string())
def test_timestamp_resolution(self):
now = datetime.datetime.now()
ts = kml.TimeStamp(timestamp=now)
self.assertTrue(now.isoformat() in ts.to_string())
ts.timestamp[1] = 'date'
self.assertTrue(now.date().isoformat() in ts.to_string())
self.assertFalse(now.isoformat() in ts.to_string())
year = str(now.year)
ym = now.strftime('%Y-%m')
ts.timestamp[1] = 'gYearMonth'
self.assertTrue(ym in ts.to_string())
self.assertFalse(now.date().isoformat() in ts.to_string())
ts.timestamp[1] = 'gYear'
self.assertTrue(year in ts.to_string())
self.assertFalse(ym in ts.to_string())
ts.timestamp = None
self.assertRaises(TypeError, ts.to_string)
def test_timespan(self):
now = datetime.datetime.now()
y2k = datetime.datetime(2000,1,1)
ts = kml.TimeSpan(end=now, begin=y2k)
self.assertEqual(ts.end, [now, 'dateTime'])
self.assertEqual(ts.begin, [y2k, 'dateTime'])
self.assertTrue('TimeSpan>' in ts.to_string())
self.assertTrue('begin>' in ts.to_string())
self.assertTrue('end>' in ts.to_string())
self.assertTrue(now.isoformat() in ts.to_string())
self.assertTrue(y2k.isoformat() in ts.to_string())
ts.end = None
self.assertFalse(now.isoformat() in ts.to_string())
self.assertTrue(y2k.isoformat() in ts.to_string())
ts.begin = None
self.assertRaises(ValueError, ts.to_string)
def test_feature_timestamp(self):
now = datetime.datetime.now()
f = kml.Document()
f.timeStamp = now
self.assertTrue(now.isoformat() in f.to_string())
self.assertTrue('TimeStamp>' in f.to_string())
self.assertTrue('when>' in f.to_string())
f.timeStamp = now.date()
self.assertTrue(now.date().isoformat() in f.to_string())
self.assertFalse(now.isoformat() in f.to_string())
f.timeStamp = None
self.assertFalse('TimeStamp>' in f.to_string())
def test_feature_timespan(self):
now = datetime.datetime.now()
y2k = datetime.date(2000,1,1)
f = kml.Document()
f.begin = y2k
f.end = now
self.assertTrue(now.isoformat() in f.to_string())
self.assertTrue('2000-01-01' in f.to_string())
self.assertTrue('TimeSpan>' in f.to_string())
self.assertTrue('begin>' in f.to_string())
self.assertTrue('end>' in f.to_string())
f.end = None
self.assertFalse(now.isoformat() in f.to_string())
self.assertTrue('2000-01-01' in f.to_string())
self.assertTrue('TimeSpan>' in f.to_string())
self.assertTrue('begin>' in f.to_string())
self.assertFalse('end>' in f.to_string())
f.begin = None
self.assertFalse('TimeSpan>' in f.to_string())
def test_feature_timespan_stamp(self):
now = datetime.datetime.now()
y2k = datetime.date(2000,1,1)
f = kml.Document()
f.begin = y2k
f.end = now
self.assertTrue(now.isoformat() in f.to_string())
self.assertTrue('2000-01-01' in f.to_string())
self.assertTrue('TimeSpan>' in f.to_string())
self.assertTrue('begin>' in f.to_string())
self.assertTrue('end>' in f.to_string())
self.assertFalse('TimeStamp>' in f.to_string())
self.assertFalse('when>' in f.to_string())
f.timeStamp = now
self.assertTrue(now.isoformat() in f.to_string())
self.assertTrue('TimeStamp>' in f.to_string())
self.assertTrue('when>' in f.to_string())
self.assertFalse('2000-01-01' in f.to_string())
self.assertFalse('TimeSpan>' in f.to_string())
self.assertFalse('begin>' in f.to_string())
self.assertFalse('end>' in f.to_string())
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite( KmlFromStringTestCase )) suite.addTest(unittest.makeSuite( KmlFromStringTestCase ))
suite.addTest(unittest.makeSuite( BuildKmlTestCase )) suite.addTest(unittest.makeSuite( BuildKmlTestCase ))
suite.addTest(unittest.makeSuite( StyleFromStringTestCase )) suite.addTest(unittest.makeSuite( StyleFromStringTestCase ))
suite.addTest(unittest.makeSuite(BaseClassesTestCase)) suite.addTest(unittest.makeSuite(BaseClassesTestCase))
suite.addTest(unittest.makeSuite(DateTimeTestCase))
return suite return suite
if __name__ == '__main__': 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