Package sld :: Package test
[hide private]
[frames] | no frames]

Source Code for Package sld.test

  1  #!/usr/bin/env python 
  2  """ 
  3  Test suite for the StyledLayerDescriptor python library. 
  4   
  5  License 
  6  ======= 
  7  Copyright 2011-2014 David Zwarg <U{david.a@zwarg.com}> 
  8   
  9  Licensed under the Apache License, Version 2.0 (the "License"); 
 10  you may not use this file except in compliance with the License. 
 11  You may obtain a copy of the License at 
 12   
 13  U{http://www.apache.org/licenses/LICENSE-2.0} 
 14   
 15  Unless required by applicable law or agreed to in writing, software 
 16  distributed under the License is distributed on an "AS IS" BASIS, 
 17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 18  See the License for the specific language governing permissions and 
 19  limitations under the License. 
 20   
 21  @author: David Zwarg 
 22  @contact: david.a@zwarg.com 
 23  @copyright: 2011-2014, Azavea 
 24  @license: Apache 2.0 
 25  @version: 1.0.10 
 26  """ 
 27  import sld 
 28  import unittest 
 29  import copy 
 30  from lxml import etree 
 31   
 32   
33 -class SLD_Test(unittest.TestCase):
34 """ 35 All tests for django-sld are contained in this TestCase class. 36 """ 37 38 _sld0 = None 39 """Store a parsed SLD, with known styles and structure""" 40 41 _sld1 = None 42 """Store a dynamically generated SLD""" 43
44 - def setUp(self):
45 """ 46 Set up the test fixture. 47 """ 48 if SLD_Test._sld0 is None: 49 SLD_Test._sld0 = sld.StyledLayerDescriptor('test/style.sld') 50 SLD_Test._sld1 = sld.StyledLayerDescriptor()
51
52 - def test_constructor1(self):
53 """ 54 Test an empty constructor, and make sure the SLD is valid. 55 """ 56 sld_doc = sld.StyledLayerDescriptor() 57 58 self.assertTrue('sld' in sld_doc._nsmap) 59 60 expected = """<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0"/>""" 61 actual = etree.tostring(sld_doc._node, with_tail=False) 62 self.assertEqual(actual, expected.encode('utf-8')) 63 64 sld_doc.normalize() 65 66 self.assertTrue(sld_doc.validate())
67
68 - def test_constructor2(self):
69 """ 70 Test a constructor on a bogus file. 71 """ 72 try: 73 sld.StyledLayerDescriptor('junk') 74 self.fail("Error") 75 except: 76 # Good, failure on a junk file. 77 pass
78
79 - def test_sld_version(self):
80 """ 81 Test the SLD version on the root element. 82 """ 83 self.assertEqual(self._sld0.version, "1.0.0")
84
85 - def test_sld_ns(self):
86 """ 87 Test the namespace on the root element. 88 """ 89 self.assertEqual(self._sld0.xmlns, 'http://www.opengis.net/sld')
90
91 - def test_sld_namedlayer1(self):
92 """ 93 Test the object type of the NamedLayer property. 94 """ 95 self.assertTrue(isinstance(self._sld0.NamedLayer, sld.NamedLayer), "NamedLayer property is not the proper class.")
96
97 - def test_sld_namedlayer2(self):
98 """ 99 Test the creation and construction of a NamedLayer element. 100 """ 101 self.assertTrue(self._sld1.NamedLayer is None) 102 103 sld_doc = copy.deepcopy(self._sld1) 104 105 sld_doc.create_namedlayer('test named layer') 106 self.assertFalse(sld_doc.NamedLayer is None) 107 108 sld_doc.normalize() 109 self.assertTrue(sld_doc.validate())
110
111 - def test_namedlayer_name(self):
112 """ 113 Test the proper parsing of the name of the NamedLayer. 114 """ 115 expected = 'poptot' 116 self.assertEqual(self._sld0.NamedLayer.Name, expected, "NamedLayer was named '%s', not '%s'" % (self._sld0.NamedLayer.Name, expected,))
117
119 """ 120 Test the object type of the UserStyle property. 121 """ 122 self.assertTrue(isinstance(self._sld0.NamedLayer.UserStyle, sld.UserStyle), "UserStyle property is not the proper class.")
123
125 """ 126 Test the proper parsing of the UserStyle property. 127 """ 128 sld_doc = copy.deepcopy(self._sld1) 129 130 sld_doc.create_namedlayer('test named layer') 131 132 self.assertTrue(sld_doc.NamedLayer.UserStyle is None) 133 134 sld_doc.NamedLayer.create_userstyle() 135 136 self.assertFalse(sld_doc.NamedLayer.UserStyle is None) 137 138 sld_doc.normalize() 139 self.assertFalse(sld_doc.validate())
140
141 - def test_userstyle_title1(self):
142 """ 143 Test the parsing of the UserStyle Title, and proper rendering. 144 """ 145 sld_doc = copy.deepcopy(self._sld0) 146 us = sld_doc.NamedLayer.UserStyle 147 expected = 'Population' 148 self.assertEqual(us.Title, expected, "UserStyle Title was '%s', not '%s'" % (us.Title, expected,)) 149 150 expected = 'Consternation' 151 us.Title = expected 152 self.assertEqual(us.Title, expected, "UserStyle Title was '%s', not '%s'" % (us.Title, expected,)) 153 154 us._node.remove(us._node[2]) 155 156 expected = """<UserStyle xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 157 <Title>%s</Title> 158 <Abstract>A grayscale style showing the population numbers in a given geounit.</Abstract> 159 </UserStyle>""" % expected 160 actual = etree.tostring(us._node, with_tail=False) 161 self.assertEqual(len(actual), len(expected)) 162 self.assertEqual(actual, expected.encode('utf-8'), "UserStyle was not serialized correctly.\n%s" % actual) 163 164 sld_doc.normalize() 165 self.assertFalse(sld_doc.validate())
166
167 - def test_userstyle_title2(self):
168 """ 169 Test the construction of the UserStyle Title, and proper rendering. 170 """ 171 sld_doc = copy.deepcopy(self._sld1) 172 sld_doc.create_namedlayer('test named layer') 173 sld_doc.NamedLayer.create_userstyle() 174 175 us = sld_doc.NamedLayer.UserStyle 176 self.assertTrue(us.Title is None, "UserStyle Title was not None") 177 178 expected = 'Consternation' 179 us.Title = expected 180 self.assertEqual(us.Title, expected, "UserStyle Title was '%s', not '%s'" % (us.Title, expected,)) 181 182 expected = """<sld:UserStyle xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Title>%s</sld:Title></sld:UserStyle>""" % expected 183 actual = etree.tostring(us._node, with_tail=False) 184 self.assertEqual(len(actual), len(expected)) 185 self.assertEqual(actual, expected.encode('utf-8'), "UserStyle was not serialized correctly.\n%s" % actual) 186 187 sld_doc.normalize() 188 self.assertFalse(sld_doc.validate())
189
190 - def test_userstyle_abstract1(self):
191 """ 192 Test the parsing of the UserStyle Abstract, and proper rendering. 193 """ 194 sld_doc = copy.deepcopy(self._sld0) 195 us = sld_doc.NamedLayer.UserStyle 196 expected = 'A grayscale style showing the population numbers in a given geounit.' 197 self.assertEqual(us.Abstract, expected, "UserStyle Abstract was '%s', not '%s'" % (us.Abstract, expected,)) 198 199 expected = 'Something completely different' 200 us.Abstract = expected 201 self.assertEqual(us.Abstract, expected, "UserStyle Abstract was '%s', not '%s'" % (us.Abstract, expected,)) 202 203 us._node.remove(us._node[2]) 204 205 expected = """<UserStyle xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 206 <Title>Population</Title> 207 <Abstract>%s</Abstract> 208 </UserStyle>""" % expected 209 actual = etree.tostring(us._node, with_tail=False) 210 self.assertEqual(len(actual), len(expected)) 211 self.assertEqual(actual, expected.encode('utf-8'), "UserStyle was not serialized correctly.\n%s" % actual) 212 213 sld_doc.normalize() 214 self.assertFalse(sld_doc.validate())
215
216 - def test_userstyle_abstract2(self):
217 """ 218 Test the construction of the UserStyle Abstract, and proper rendering. 219 """ 220 sld_doc = copy.deepcopy(self._sld1) 221 sld_doc.create_namedlayer('test named layer') 222 sld_doc.NamedLayer.create_userstyle() 223 224 us = sld_doc.NamedLayer.UserStyle 225 self.assertTrue(us.Abstract is None, "UserStyle Abstract was not None") 226 227 expected = 'Something completely different' 228 us.Abstract = expected 229 self.assertEqual(us.Abstract, expected, "UserStyle Abstract was '%s', not '%s'" % (us.Abstract, expected,)) 230 231 expected = """<sld:UserStyle xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Abstract>%s</sld:Abstract></sld:UserStyle>""" % expected 232 actual = etree.tostring(us._node, with_tail=False) 233 self.assertEqual(len(actual), len(expected)) 234 self.assertEqual(actual, expected.encode('utf-8'), "UserStyle was not serialized correctly.\n%s" % actual) 235 236 sld_doc.normalize() 237 self.assertFalse(sld_doc.validate())
238
240 """ 241 Test the object type of the FeatureTypeStyle property. 242 """ 243 self.assertTrue(isinstance(self._sld0.NamedLayer.UserStyle.FeatureTypeStyle, sld.FeatureTypeStyle), "FeatureTypeStyle property is not the proper class.")
244
246 """ 247 Test the construction of a new FeatureTypeStyle property. 248 """ 249 sld_doc = copy.deepcopy(self._sld1) 250 sld_doc.create_namedlayer('test named layer') 251 sld_doc.NamedLayer.create_userstyle() 252 253 self.assertTrue(sld_doc.NamedLayer.UserStyle.FeatureTypeStyle is None) 254 255 sld_doc.NamedLayer.UserStyle.create_featuretypestyle() 256 257 self.assertFalse(sld_doc.NamedLayer.UserStyle.FeatureTypeStyle is None) 258 259 sld_doc.normalize() 260 self.assertFalse(sld_doc.validate())
261
263 """ 264 Test the parsing of the Rules property. 265 """ 266 rules = self._sld0.NamedLayer.UserStyle.FeatureTypeStyle.Rules 267 self.assertEqual(len(rules), 6) 268 self.assertTrue(isinstance(rules[0], sld.Rule), "Rule item in list is not the proper class.")
269
271 """ 272 Test the construction of the Rules property. 273 """ 274 sld_doc = copy.deepcopy(self._sld1) 275 sld_doc.create_namedlayer('test named layer') 276 sld_doc.NamedLayer.create_userstyle() 277 sld_doc.NamedLayer.UserStyle.create_featuretypestyle() 278 279 rules = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules 280 self.assertEqual(len(rules), 0) 281 282 sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.create_rule('test rule', sld.PointSymbolizer) 283 rules = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules 284 285 self.assertEqual(len(rules), 1) 286 287 sld_doc.normalize() 288 self.assertTrue(sld_doc.validate())
289
290 - def test_rule_title1(self):
291 """ 292 Test the parsing of the individual Rule properties. 293 """ 294 sld_doc = copy.deepcopy(self._sld0) 295 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 296 297 expected = "> 880" 298 self.assertEqual(rule.Title, expected) 299 300 expected = "> 999" 301 rule.Title = expected 302 self.assertEqual(rule.Title, expected) 303 304 expected = """<Rule xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 305 <Title>&gt; 999</Title> 306 <ogc:Filter> 307 <ogc:PropertyIsGreaterThanOrEqualTo> 308 <ogc:PropertyName>number</ogc:PropertyName> 309 <ogc:Literal>880</ogc:Literal> 310 </ogc:PropertyIsGreaterThanOrEqualTo> 311 </ogc:Filter> 312 <MaxScaleDenominator>20000</MaxScaleDenominator> 313 <PolygonSymbolizer> 314 <Fill> 315 <CssParameter name="fill">#252525</CssParameter> 316 </Fill> 317 </PolygonSymbolizer> 318 </Rule>""" 319 actual = etree.tostring(rule._node, with_tail=False) 320 self.assertEqual(actual, expected.encode('utf-8'), actual) 321 322 sld_doc.normalize() 323 self.assertTrue(sld_doc.validate())
324
325 - def test_rule_title2(self):
326 """ 327 Test the construction of new Rule properties. 328 """ 329 sld_doc = copy.deepcopy(self._sld1) 330 sld_doc.create_namedlayer('test named layer') 331 sld_doc.NamedLayer.create_userstyle() 332 sld_doc.NamedLayer.UserStyle.create_featuretypestyle() 333 sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.create_rule('test rule', sld.PointSymbolizer) 334 335 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 336 337 expected = "> 999" 338 rule.Title = expected 339 self.assertEqual(rule.Title, expected) 340 341 expected = """<sld:Rule xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Title>&gt; 999</sld:Title><sld:PointSymbolizer><sld:Graphic><sld:Mark><sld:WellKnownName>square</sld:WellKnownName><sld:Fill><sld:CssParameter name="fill">#ff0000</sld:CssParameter></sld:Fill></sld:Mark></sld:Graphic></sld:PointSymbolizer></sld:Rule>""" 342 actual = etree.tostring(rule._node, with_tail=False) 343 self.assertEqual(actual, expected.encode('utf-8'), actual) 344 345 sld_doc.normalize() 346 self.assertTrue(sld_doc.validate())
347
348 - def test_rule_filter1(self):
349 """ 350 Test the parsing of the Filter property. 351 """ 352 sld_doc = copy.deepcopy(self._sld0) 353 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 354 355 self.assertFalse(rule.Filter.PropertyIsGreaterThanOrEqualTo is None) 356 357 self.assertEqual(rule.Filter.PropertyIsGreaterThanOrEqualTo.PropertyName, 'number') 358 self.assertEqual(rule.Filter.PropertyIsGreaterThanOrEqualTo.Literal, '880') 359 360 sld_doc.normalize() 361 self.assertTrue(sld_doc.validate())
362
363 - def test_rule_filter_none(self):
364 """ 365 Test the construction of the Filter property. 366 """ 367 sld_doc = copy.deepcopy(self._sld1) 368 namedlayer = sld_doc.create_namedlayer('test named layer') 369 userstyle = namedlayer.create_userstyle() 370 featuretypestyle = userstyle.create_featuretypestyle() 371 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 372 rfilter = rule.create_filter() 373 374 self.assertTrue(rfilter is None) 375 sld_doc.normalize() 376 self.assertTrue(sld_doc.validate())
377
378 - def test_filter_eq(self):
379 """ 380 Test the construction of an equality filter. 381 """ 382 sld_doc = copy.deepcopy(self._sld1) 383 namedlayer = sld_doc.create_namedlayer('test named layer') 384 userstyle = namedlayer.create_userstyle() 385 featuretypestyle = userstyle.create_featuretypestyle() 386 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 387 rfilter = rule.create_filter('valueA', '==', '5000') 388 389 self.assertTrue(rfilter.PropertyIsNotEqualTo is None) 390 self.assertTrue(rfilter.PropertyIsLessThan is None) 391 self.assertTrue(rfilter.PropertyIsLessThanOrEqualTo is None) 392 self.assertTrue(rfilter.PropertyIsGreaterThan is None) 393 self.assertTrue(rfilter.PropertyIsGreaterThanOrEqualTo is None) 394 self.assertTrue(rfilter.PropertyIsLike is None) 395 self.assertFalse(rfilter.PropertyIsEqualTo is None) 396 self.assertEqual(rfilter.PropertyIsEqualTo.PropertyName, 'valueA') 397 self.assertEqual(rfilter.PropertyIsEqualTo.Literal, '5000') 398 399 sld_doc.normalize() 400 self.assertTrue(sld_doc.validate())
401
402 - def test_filter_lte(self):
403 """ 404 Test the construction of a less-than-or-equal Filter. 405 """ 406 sld_doc = copy.deepcopy(self._sld1) 407 namedlayer = sld_doc.create_namedlayer('test named layer') 408 userstyle = namedlayer.create_userstyle() 409 featuretypestyle = userstyle.create_featuretypestyle() 410 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 411 rfilter = rule.create_filter('valueB', '<=', '5000') 412 413 self.assertTrue(rfilter.PropertyIsEqualTo is None) 414 self.assertTrue(rfilter.PropertyIsNotEqualTo is None) 415 self.assertTrue(rfilter.PropertyIsLessThan is None) 416 self.assertTrue(rfilter.PropertyIsGreaterThan is None) 417 self.assertTrue(rfilter.PropertyIsGreaterThanOrEqualTo is None) 418 self.assertTrue(rfilter.PropertyIsLike is None) 419 self.assertFalse(rfilter.PropertyIsLessThanOrEqualTo is None) 420 self.assertEqual(rfilter.PropertyIsLessThanOrEqualTo.PropertyName, 'valueB') 421 self.assertEqual(rfilter.PropertyIsLessThanOrEqualTo.Literal, '5000') 422 423 sld_doc.normalize() 424 self.assertTrue(sld_doc.validate())
425
426 - def test_filter_lt(self):
427 """ 428 Test the construction of a less-than Filter. 429 """ 430 sld_doc = copy.deepcopy(self._sld1) 431 namedlayer = sld_doc.create_namedlayer('test named layer') 432 userstyle = namedlayer.create_userstyle() 433 featuretypestyle = userstyle.create_featuretypestyle() 434 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 435 rfilter = rule.create_filter('valueC', '<', '500') 436 437 self.assertTrue(rfilter.PropertyIsEqualTo is None) 438 self.assertTrue(rfilter.PropertyIsLessThanOrEqualTo is None) 439 self.assertTrue(rfilter.PropertyIsNotEqualTo is None) 440 self.assertTrue(rfilter.PropertyIsGreaterThan is None) 441 self.assertTrue(rfilter.PropertyIsGreaterThanOrEqualTo is None) 442 self.assertTrue(rfilter.PropertyIsLike is None) 443 self.assertFalse(rfilter.PropertyIsLessThan is None) 444 self.assertEqual(rfilter.PropertyIsLessThan.PropertyName, 'valueC') 445 self.assertEqual(rfilter.PropertyIsLessThan.Literal, '500') 446 447 sld_doc.normalize() 448 self.assertTrue(sld_doc.validate())
449
450 - def test_filter_gte(self):
451 """ 452 Test the construction of a greater-than-or-equal Filter. 453 """ 454 sld_doc = copy.deepcopy(self._sld1) 455 namedlayer = sld_doc.create_namedlayer('test named layer') 456 userstyle = namedlayer.create_userstyle() 457 featuretypestyle = userstyle.create_featuretypestyle() 458 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 459 rfilter = rule.create_filter('valueD', '>=', '100') 460 461 self.assertTrue(rfilter.PropertyIsEqualTo is None) 462 self.assertTrue(rfilter.PropertyIsLessThanOrEqualTo is None) 463 self.assertTrue(rfilter.PropertyIsLessThan is None) 464 self.assertTrue(rfilter.PropertyIsNotEqualTo is None) 465 self.assertTrue(rfilter.PropertyIsGreaterThan is None) 466 self.assertTrue(rfilter.PropertyIsLike is None) 467 self.assertFalse(rfilter.PropertyIsGreaterThanOrEqualTo is None) 468 self.assertEqual(rfilter.PropertyIsGreaterThanOrEqualTo.PropertyName, 'valueD') 469 self.assertEqual(rfilter.PropertyIsGreaterThanOrEqualTo.Literal, '100') 470 471 sld_doc.normalize() 472 self.assertTrue(sld_doc.validate())
473
474 - def test_filter_gt(self):
475 """ 476 Test the construction of a greater-than Filter. 477 """ 478 sld_doc = copy.deepcopy(self._sld1) 479 namedlayer = sld_doc.create_namedlayer('test named layer') 480 userstyle = namedlayer.create_userstyle() 481 featuretypestyle = userstyle.create_featuretypestyle() 482 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 483 rfilter = rule.create_filter('valueE', '>', '10') 484 485 self.assertTrue(rfilter.PropertyIsEqualTo is None) 486 self.assertTrue(rfilter.PropertyIsLessThanOrEqualTo is None) 487 self.assertTrue(rfilter.PropertyIsLessThan is None) 488 self.assertTrue(rfilter.PropertyIsGreaterThanOrEqualTo is None) 489 self.assertTrue(rfilter.PropertyIsNotEqualTo is None) 490 self.assertTrue(rfilter.PropertyIsLike is None) 491 self.assertFalse(rfilter.PropertyIsGreaterThan is None) 492 self.assertEqual(rfilter.PropertyIsGreaterThan.PropertyName, 'valueE') 493 self.assertEqual(rfilter.PropertyIsGreaterThan.Literal, '10') 494 495 sld_doc.normalize() 496 self.assertTrue(sld_doc.validate())
497
498 - def test_filter_neq(self):
499 """ 500 Test the construction of an inequality Filter. 501 """ 502 sld_doc = copy.deepcopy(self._sld1) 503 namedlayer = sld_doc.create_namedlayer('test named layer') 504 userstyle = namedlayer.create_userstyle() 505 featuretypestyle = userstyle.create_featuretypestyle() 506 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 507 rfilter = rule.create_filter('valueF', '!=', '0.01') 508 509 self.assertTrue(rfilter.PropertyIsEqualTo is None) 510 self.assertTrue(rfilter.PropertyIsLessThan is None) 511 self.assertTrue(rfilter.PropertyIsLessThanOrEqualTo is None) 512 self.assertTrue(rfilter.PropertyIsGreaterThan is None) 513 self.assertTrue(rfilter.PropertyIsGreaterThanOrEqualTo is None) 514 self.assertTrue(rfilter.PropertyIsLike is None) 515 self.assertFalse(rfilter.PropertyIsNotEqualTo is None) 516 self.assertEqual(rfilter.PropertyIsNotEqualTo.PropertyName, 'valueF') 517 self.assertEqual(rfilter.PropertyIsNotEqualTo.Literal, '0.01') 518 519 sld_doc.normalize() 520 self.assertTrue(sld_doc.validate())
521
522 - def test_filter_and(self):
523 """ 524 Test the construction of a logical-and Filter. 525 """ 526 sld_doc = copy.deepcopy(self._sld1) 527 namedlayer = sld_doc.create_namedlayer('test named layer') 528 userstyle = namedlayer.create_userstyle() 529 featuretypestyle = userstyle.create_featuretypestyle() 530 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 531 532 filter1 = sld.Filter(rule) 533 filter1.PropertyIsGreaterThan = sld.PropertyCriterion(filter1, 'PropertyIsGreaterThan') 534 filter1.PropertyIsGreaterThan.PropertyName = 'number' 535 filter1.PropertyIsGreaterThan.Literal = '-10' 536 537 filter2 = sld.Filter(rule) 538 filter2.PropertyIsLessThanOrEqualTo = sld.PropertyCriterion(filter2, 'PropertyIsLessThanOrEqualTo') 539 filter2.PropertyIsLessThanOrEqualTo.PropertyName = 'number' 540 filter2.PropertyIsLessThanOrEqualTo.Literal = '10' 541 542 rule.Filter = filter1 + filter2 543 544 expected = """<sld:Rule xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Title>test rule</sld:Title><sld:PointSymbolizer><sld:Graphic><sld:Mark><sld:WellKnownName>square</sld:WellKnownName><sld:Fill><sld:CssParameter name="fill">#ff0000</sld:CssParameter></sld:Fill></sld:Mark></sld:Graphic></sld:PointSymbolizer><ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>-10</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:And></ogc:Filter></sld:Rule>""" 545 actual = etree.tostring(rule._node, with_tail=False) 546 self.assertEqual(actual, expected.encode('utf-8')) 547 548 sld_doc.normalize() 549 self.assertTrue(sld_doc.validate())
550
551 - def test_filter_or(self):
552 """ 553 Test the construction of a logical-or Filter. 554 """ 555 sld_doc = copy.deepcopy(self._sld1) 556 namedlayer = sld_doc.create_namedlayer('test named layer') 557 userstyle = namedlayer.create_userstyle() 558 featuretypestyle = userstyle.create_featuretypestyle() 559 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 560 561 filter1 = sld.Filter(rule) 562 filter1.PropertyIsGreaterThan = sld.PropertyCriterion(filter1, 'PropertyIsGreaterThan') 563 filter1.PropertyIsGreaterThan.PropertyName = 'number' 564 filter1.PropertyIsGreaterThan.Literal = '10' 565 566 filter2 = sld.Filter(rule) 567 filter2.PropertyIsLessThan = sld.PropertyCriterion(filter2, 'PropertyIsLessThan') 568 filter2.PropertyIsLessThan.PropertyName = 'number' 569 filter2.PropertyIsLessThan.Literal = '-10' 570 571 rule.Filter = filter1 | filter2 572 573 expected = """<sld:Rule xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Title>test rule</sld:Title><sld:PointSymbolizer><sld:Graphic><sld:Mark><sld:WellKnownName>square</sld:WellKnownName><sld:Fill><sld:CssParameter name="fill">#ff0000</sld:CssParameter></sld:Fill></sld:Mark></sld:Graphic></sld:PointSymbolizer><ogc:Filter><ogc:Or><ogc:PropertyIsGreaterThan><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:PropertyIsLessThan><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>-10</ogc:Literal></ogc:PropertyIsLessThan></ogc:Or></ogc:Filter></sld:Rule>""" 574 actual = etree.tostring(rule._node, with_tail=False) 575 self.assertEqual(actual, expected.encode('utf-8')) 576 577 sld_doc.normalize() 578 self.assertTrue(sld_doc.validate())
579
580 - def test_filter_and_or(self):
581 """ 582 Test the construction of a logical-and combined with a logical-or Filter. 583 """ 584 sld_doc = copy.deepcopy(self._sld1) 585 namedlayer = sld_doc.create_namedlayer('test named layer') 586 userstyle = namedlayer.create_userstyle() 587 featuretypestyle = userstyle.create_featuretypestyle() 588 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 589 590 filter1 = sld.Filter(rule) 591 filter1.PropertyIsGreaterThan = sld.PropertyCriterion(filter1, 'PropertyIsGreaterThan') 592 filter1.PropertyIsGreaterThan.PropertyName = 'number' 593 filter1.PropertyIsGreaterThan.Literal = '10' 594 595 filter2 = sld.Filter(rule) 596 filter2.PropertyIsLessThan = sld.PropertyCriterion(filter2, 'PropertyIsLessThan') 597 filter2.PropertyIsLessThan.PropertyName = 'number' 598 filter2.PropertyIsLessThan.Literal = '-10' 599 600 filter3 = sld.Filter(rule) 601 filter3.PropertyIsEqualTo = sld.PropertyCriterion(filter3, 'PropertyIsEqualTo') 602 filter3.PropertyIsEqualTo.PropertyName = 'value' 603 filter3.PropertyIsEqualTo.Literal = 'yes' 604 605 rule.Filter = filter1 + (filter2 | filter3) 606 607 expected = """<sld:Rule xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc"><sld:Title>test rule</sld:Title><sld:PointSymbolizer><sld:Graphic><sld:Mark><sld:WellKnownName>square</sld:WellKnownName><sld:Fill><sld:CssParameter name="fill">#ff0000</sld:CssParameter></sld:Fill></sld:Mark></sld:Graphic></sld:PointSymbolizer><ogc:Filter><ogc:And><ogc:PropertyIsGreaterThan><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>10</ogc:Literal></ogc:PropertyIsGreaterThan><ogc:Or><ogc:PropertyIsLessThan><ogc:PropertyName>number</ogc:PropertyName><ogc:Literal>-10</ogc:Literal></ogc:PropertyIsLessThan><ogc:PropertyIsEqualTo><ogc:PropertyName>value</ogc:PropertyName><ogc:Literal>yes</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:And></ogc:Filter></sld:Rule>""" 608 actual = etree.tostring(rule._node, with_tail=False, pretty_print=False) 609 self.assertEqual(actual, expected.encode('utf-8')) 610 611 sld_doc.normalize() 612 self.assertTrue(sld_doc.validate())
613
615 """ 616 Test the parsing of the PolygonSymbolizer property. 617 """ 618 sld_doc = copy.deepcopy(self._sld0) 619 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 620 621 self.assertFalse(rule.PolygonSymbolizer is None) 622 623 sld_doc.normalize() 624 self.assertTrue(sld_doc.validate())
625
627 """ 628 Test the construction of a PolygonSymbolizer property. 629 """ 630 sld_doc = copy.deepcopy(self._sld1) 631 namedlayer = sld_doc.create_namedlayer('test named layer') 632 userstyle = namedlayer.create_userstyle() 633 featuretypestyle = userstyle.create_featuretypestyle() 634 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 635 symbolizer = rule.create_symbolizer('Polygon') 636 637 self.assertTrue(symbolizer.Fill is None) 638 self.assertTrue(symbolizer.Stroke is None) 639 self.assertTrue(symbolizer.Font is None) 640 641 sld_doc.normalize() 642 self.assertTrue(sld_doc.validate())
643
645 """ 646 Test the parsing of a Fill property. 647 """ 648 sld_doc = copy.deepcopy(self._sld0) 649 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 650 651 self.assertFalse(rule.PolygonSymbolizer.Fill is None) 652 653 del rule.PolygonSymbolizer.Fill 654 655 self.assertTrue(rule.PolygonSymbolizer.Fill is None) 656 657 sld_doc.normalize() 658 self.assertTrue(sld_doc.validate())
659
661 """ 662 Test the construction of a Fill property. 663 """ 664 sld_doc = copy.deepcopy(self._sld1) 665 namedlayer = sld_doc.create_namedlayer('test named layer') 666 userstyle = namedlayer.create_userstyle() 667 featuretypestyle = userstyle.create_featuretypestyle() 668 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 669 symbolizer = rule.create_symbolizer('Polygon') 670 fill = symbolizer.create_fill() 671 672 self.assertFalse(fill.CssParameters is None) 673 self.assertEqual(len(fill.CssParameters), 0) 674 675 sld_doc.normalize() 676 self.assertTrue(sld_doc.validate())
677
678 - def test_fill_cssparameter1(self):
679 """ 680 Test the parsing of the CssParameter property. 681 """ 682 fill = self._sld0.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0].PolygonSymbolizer.Fill 683 684 self.assertFalse(fill.CssParameters is None) 685 self.assertEqual(len(fill.CssParameters), 1) 686 687 fill = self._sld0.NamedLayer.UserStyle.FeatureTypeStyle.Rules[5].LineSymbolizer.Stroke 688 689 self.assertFalse(fill.CssParameters is None) 690 self.assertEqual(len(fill.CssParameters), 2)
691
692 - def test_fill_cssparameter2(self):
693 """ 694 Test the construction of the CssParameter property. 695 """ 696 sld_doc = copy.deepcopy(self._sld1) 697 namedlayer = sld_doc.create_namedlayer('test named layer') 698 userstyle = namedlayer.create_userstyle() 699 featuretypestyle = userstyle.create_featuretypestyle() 700 rule = featuretypestyle.create_rule('test rule', sld.PointSymbolizer) 701 symbolizer = rule.create_symbolizer('Polygon') 702 fill = symbolizer.create_fill() 703 fill.create_cssparameter('fill', '#ffffff') 704 705 self.assertEqual(len(fill.CssParameters), 1) 706 self.assertEqual(fill.CssParameters[0].Name, 'fill') 707 self.assertEqual(fill.CssParameters[0].Value, '#ffffff') 708 709 sld_doc.normalize() 710 self.assertTrue(sld_doc.validate())
711
713 """ 714 Test the parsing of the MaxScaleDenominator & MinScaleDenominator properties. 715 """ 716 sld_doc = copy.deepcopy(self._sld0) 717 rule = sld_doc.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0] 718 719 self.assertTrue(rule.MinScaleDenominator is None) 720 self.assertFalse(rule.MaxScaleDenominator is None) 721 722 expected = '20000' 723 self.assertEqual(rule.MaxScaleDenominator, expected, "MaxScaleDominator was '%s', not '%s'" % (rule.MaxScaleDenominator, expected,)) 724 expected = '15000' 725 rule.MaxScaleDenominator = expected 726 self.assertEqual(rule.MaxScaleDenominator, expected, "MaxScaleDominator was '%s', not '%s'" % (rule.MaxScaleDenominator, expected,)) 727 728 del rule.MaxScaleDenominator 729 self.assertTrue(rule.MaxScaleDenominator is None) 730 731 expected = '15000' 732 rule.MinScaleDenominator = expected 733 self.assertEqual(rule.MinScaleDenominator, expected, "MinScaleDenominator was '%s', not '%s'" % (rule.MinScaleDenominator, expected,)) 734 735 sld_doc.normalize() 736 self.assertTrue(sld_doc.validate())
737
738 - def test_scale_denominators(self):
739 """ 740 Test the construction of the MaxScaleDenominator & MinScaleDenominator properties. 741 """ 742 sld_doc = copy.deepcopy(self._sld1) 743 namedlayer = sld_doc.create_namedlayer('test named layer') 744 userstyle = namedlayer.create_userstyle() 745 featuretypestyle = userstyle.create_featuretypestyle() 746 747 rule1 = featuretypestyle.create_rule('test rule 1', sld.PointSymbolizer) 748 self.assertTrue(rule1.MinScaleDenominator is None) 749 self.assertTrue(rule1.MaxScaleDenominator is None) 750 751 rule2 = featuretypestyle.create_rule('test rule 2', sld.PointSymbolizer, '10000', '20000') 752 self.assertFalse(rule2.MinScaleDenominator is None) 753 self.assertFalse(rule2.MaxScaleDenominator is None) 754 self.assertEqual(rule2.MinScaleDenominator, '10000') 755 self.assertEqual(rule2.MaxScaleDenominator, '20000') 756 757 sld_doc.normalize() 758 self.assertTrue(sld_doc.validate())
759 760 761 if __name__ == '__main__': 762 unittest.main() 763