35 |
support.initthuban() |
support.initthuban() |
36 |
|
|
37 |
from xmlsupport import sax_eventlist |
from xmlsupport import sax_eventlist |
38 |
|
|
39 |
|
import dbflib |
40 |
|
|
41 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import save_session |
42 |
from Thuban.Model.load import load_session, parse_color |
from Thuban.Model.load import load_session, parse_color, LoadError |
43 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
44 |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
45 |
ClassGroupSingleton, ClassGroupDefault |
ClassGroupSingleton, ClassGroupDefault |
79 |
self.session.Destroy() |
self.session.Destroy() |
80 |
self.session = None |
self.session = None |
81 |
|
|
82 |
|
|
83 |
|
dtd = "http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
84 |
|
thubanids = [((dtd, n), (None, "id")) for n in |
85 |
|
["fileshapesource", "filetable", "jointable", |
86 |
|
"derivedshapesource"]] |
87 |
|
thubanidrefs = [((dtd, n), (None, m)) for n, m in |
88 |
|
[("layer", "shapestore"), |
89 |
|
("jointable", "left"), |
90 |
|
("jointable", "right"), |
91 |
|
("derivedshapesource", "table"), |
92 |
|
("derivedshapesource", "shapesource")]] |
93 |
|
del n, m, dtd |
94 |
|
|
95 |
def check_format(self): |
def check_format(self): |
96 |
"""Check whether the file we loaded from matches the one that |
"""Check whether the file we loaded from matches the one that |
97 |
would be written. Call this from each test case after loading |
would be written. Call this from each test case after loading |
99 |
""" |
""" |
100 |
filename = self.temp_file_name(self.id() + ".roundtrip.thuban") |
filename = self.temp_file_name(self.id() + ".roundtrip.thuban") |
101 |
save_session(self.session, filename) |
save_session(self.session, filename) |
102 |
el1 = sax_eventlist(filename = filename) |
el1 = sax_eventlist(filename = filename, ids = self.thubanids, |
103 |
el2 = sax_eventlist(filename = self.filename()) |
idrefs = self.thubanidrefs) |
104 |
self.assertEquals(el1, el2) |
el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids, |
105 |
|
idrefs = self.thubanidrefs) |
106 |
|
if 0: |
107 |
|
for a, b in zip(el1, el2): |
108 |
|
print a != b and "***************" or "" |
109 |
|
print a |
110 |
|
print b |
111 |
|
self.assertEquals(el1, el2, |
112 |
|
"loaded file not equivalent to the saved file") |
113 |
|
|
114 |
|
|
115 |
class ClassificationTest(LoadSessionTest): |
class ClassificationTest(LoadSessionTest): |
167 |
|
|
168 |
file_contents = '''\ |
file_contents = '''\ |
169 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
170 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
171 |
<session title="single map&layer"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
172 |
<map title="Test Map"> |
title="single map&layer"> |
173 |
<projection name="Unknown"> |
<fileshapesource filetype="shapefile" id="D1" |
174 |
<parameter value="zone=26"/> |
filename="../../Data/iceland/political.shp"/> |
175 |
<parameter value="proj=utm"/> |
<map title="Test Map"> |
176 |
<parameter value="ellps=clrk66"/> |
<projection name="Unknown"> |
177 |
</projection> |
<parameter value="zone=26"/> |
178 |
<layer title="My Layer" stroke_width="1" fill="None" |
<parameter value="proj=utm"/> |
179 |
filename="../../Data/iceland/political.shp" |
<parameter value="ellps=clrk66"/> |
180 |
stroke="#000000" visible="true"/> |
</projection> |
181 |
</map> |
<layer shapestore="D1" visible="true" |
182 |
|
stroke="#000000" title="My Layer" stroke_width="1" |
183 |
|
fill="None"/> |
184 |
|
</map> |
185 |
</session> |
</session> |
186 |
''' |
''' |
187 |
|
|
228 |
|
|
229 |
file_contents = '''\ |
file_contents = '''\ |
230 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
231 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
232 |
<session title="single map&layer"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
233 |
<map title="Test Map"> |
title="single map&layer"> |
234 |
<projection name="Unknown"> |
<fileshapesource filetype="shapefile" id="D1" |
235 |
<parameter value="zone=26"/> |
filename="../../Data/iceland/political.shp"/> |
236 |
<parameter value="proj=utm"/> |
<map title="Test Map"> |
237 |
<parameter value="ellps=clrk66"/> |
<projection name="Unknown"> |
238 |
</projection> |
<parameter value="zone=26"/> |
239 |
<layer title="My Layer" stroke_width="1" fill="None" |
<parameter value="proj=utm"/> |
240 |
filename="../../Data/iceland/political.shp" |
<parameter value="ellps=clrk66"/> |
241 |
stroke="#000000" visible="false"> |
</projection> |
242 |
</layer> |
<layer shapestore="D1" visible="false" stroke="#000000" |
243 |
|
title="My Layer" stroke_width="1" fill="None"/> |
244 |
</map> |
</map> |
245 |
</session> |
</session> |
246 |
''' |
''' |
347 |
|
|
348 |
file_contents = '''\ |
file_contents = '''\ |
349 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
350 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
351 |
<session title="single map&layer"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
352 |
<map title="Test Map"> |
title="single map&layer"> |
353 |
<projection name="Unknown"> |
<fileshapesource filetype="shapefile" id="D1" |
354 |
<parameter value="zone=26"/> |
filename="../../Data/iceland/political.shp"/> |
355 |
<parameter value="proj=utm"/> |
<map title="Test Map"> |
356 |
<parameter value="ellps=clrk66"/> |
<projection name="Unknown"> |
357 |
</projection> |
<parameter value="zone=26"/> |
358 |
<layer title="My Layer" stroke_width="1" fill="None" |
<parameter value="proj=utm"/> |
359 |
filename="../../Data/iceland/political.shp" |
<parameter value="ellps=clrk66"/> |
360 |
stroke="#000000" visible="true"> |
</projection> |
361 |
|
<layer shapestore="D1" visible="true" stroke="#000000" |
362 |
|
title="My Layer" stroke_width="1" fill="None"> |
363 |
<classification field="POPYREG" field_type="string"> |
<classification field="POPYREG" field_type="string"> |
364 |
<clnull label="hallo"> |
<clnull label="hallo"> |
365 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
395 |
|
|
396 |
file_contents = '''\ |
file_contents = '''\ |
397 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
398 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
399 |
<session title="single map&layer"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
400 |
<map title="Test Map"> |
title="single map&layer"> |
401 |
<projection name="Unknown"> |
<fileshapesource filetype="shapefile" id="D2" |
402 |
<parameter value="zone=26"/> |
filename="../../Data/iceland/roads-line.shp"/> |
403 |
<parameter value="proj=utm"/> |
<fileshapesource filetype="shapefile" id="D4" |
404 |
<parameter value="ellps=clrk66"/> |
filename="../../Data/iceland/political.shp"/> |
405 |
</projection> |
<map title="Test Map"> |
406 |
<layer title="My Layer" stroke_width="1" fill="None" |
<projection name="Unknown"> |
407 |
filename="../../Data/iceland/political.shp" |
<parameter value="zone=26"/> |
408 |
stroke="#000000" visible="true"> |
<parameter value="proj=utm"/> |
409 |
<projection name="hello"> |
<parameter value="ellps=clrk66"/> |
410 |
<parameter value="zone=13"/> |
</projection> |
411 |
<parameter value="proj=tmerc"/> |
<layer shapestore="D4" visible="true" stroke="#000000" |
412 |
<parameter value="ellps=clrk66"/> |
title="My Layer" stroke_width="1" fill="None"> |
413 |
</projection> |
<projection name="hello"> |
414 |
|
<parameter value="zone=13"/> |
415 |
|
<parameter value="proj=tmerc"/> |
416 |
|
<parameter value="ellps=clrk66"/> |
417 |
|
</projection> |
418 |
<classification field="POPYREG" field_type="string"> |
<classification field="POPYREG" field_type="string"> |
419 |
<clnull label="hallo"> |
<clnull label="hallo"> |
420 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
424 |
</clpoint> |
</clpoint> |
425 |
</classification> |
</classification> |
426 |
</layer> |
</layer> |
427 |
<layer title="My Layer" stroke_width="1" fill="None" |
<layer shapestore="D2" visible="true" stroke="#000000" |
428 |
filename="../../Data/iceland/political.shp" |
title="My Layer" stroke_width="1" fill="None"> |
429 |
stroke="#000000" visible="true"> |
<projection name="Unknown"> |
430 |
<projection name="Unknown"> |
<parameter value="proj=lcc"/> |
431 |
<parameter value="proj=lcc"/> |
<parameter value="ellps=clrk66"/> |
432 |
<parameter value="ellps=clrk66"/> |
</projection> |
|
</projection> |
|
433 |
</layer> |
</layer> |
434 |
</map> |
</map> |
435 |
</session> |
</session> |
469 |
|
|
470 |
file_contents = '''\ |
file_contents = '''\ |
471 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
472 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
473 |
<session title="single map&layer"> |
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
474 |
<map title="Test Map"> |
title="single map&layer"> |
475 |
<rasterlayer title="My RasterLayer" |
<map title="Test Map"> |
476 |
filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
477 |
visible="false"> |
title="My RasterLayer"/> |
|
</rasterlayer> |
|
478 |
</map> |
</map> |
479 |
</session> |
</session> |
480 |
''' |
''' |
499 |
"island.tif"))) |
"island.tif"))) |
500 |
self.check_format() |
self.check_format() |
501 |
|
|
502 |
|
|
503 |
|
class TestJoinedTable(LoadSessionTest): |
504 |
|
|
505 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
506 |
|
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
507 |
|
<session title="A Joined Table session"> |
508 |
|
<filetable filename="load_joinedtable.dbf" |
509 |
|
title="Some Title" |
510 |
|
filetype="DBF" id="D141881756"/> |
511 |
|
<fileshapesource filename="../../Data/iceland/roads-line.shp" |
512 |
|
filetype="shapefile" id="D142197204"/> |
513 |
|
<jointable id="D142180284" |
514 |
|
title="Joined" |
515 |
|
leftcolumn="RDLNTYPE" left="D142197204" |
516 |
|
rightcolumn="RDTYPE" right="D141881756"/> |
517 |
|
<derivedshapesource id="D141915644" |
518 |
|
table="D142180284" shapesource="D142197204"/> |
519 |
|
<map title="Test Map"> |
520 |
|
<layer title="My Layer" |
521 |
|
shapestore="D141915644" visible="true" |
522 |
|
stroke="#000000" stroke_width="1" fill="None"/> |
523 |
|
</map> |
524 |
|
</session>''' |
525 |
|
|
526 |
|
def setUp(self): |
527 |
|
"""Extend inherited method to create the dbffile for the join""" |
528 |
|
LoadSessionTest.setUp(self) |
529 |
|
dbffile = self.temp_file_name("load_joinedtable.dbf") |
530 |
|
dbf = dbflib.create(dbffile) |
531 |
|
dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0) |
532 |
|
dbf.add_field("TEXT", dbflib.FTString, 10, 0) |
533 |
|
dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"}) |
534 |
|
dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"}) |
535 |
|
dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"}) |
536 |
|
dbf.close() |
537 |
|
|
538 |
|
def test(self): |
539 |
|
"""Test loading a session containing a joined table""" |
540 |
|
session = load_session(self.filename()) |
541 |
|
self.session = session |
542 |
|
|
543 |
|
tables = session.Tables() |
544 |
|
self.assertEquals(len(tables), 3) |
545 |
|
# FIXME: The tests shouldn't assume a certain order of the tables |
546 |
|
self.assertEquals(tables[0].Title(), "Some Title") |
547 |
|
self.assertEquals(tables[1].Title(), "Joined") |
548 |
|
|
549 |
|
|
550 |
|
class TestLoadError(LoadSessionTest): |
551 |
|
|
552 |
|
file_contents = '''\ |
553 |
|
<?xml version="1.0" encoding="UTF-8"?> |
554 |
|
<!DOCTYPE session SYSTEM "thuban-0.8.dtd"> |
555 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
556 |
|
title="single map&layer"> |
557 |
|
<fileshapesource id="D1" filename="../../Data/iceland/political.shp"/> |
558 |
|
<map title="Test Map"> |
559 |
|
<projection name="Unknown"> |
560 |
|
<parameter value="zone=26"/> |
561 |
|
<parameter value="proj=utm"/> |
562 |
|
<parameter value="ellps=clrk66"/> |
563 |
|
</projection> |
564 |
|
<layer shapestore="D1" visible="true" |
565 |
|
stroke="#000000" title="My Layer" stroke_width="1" |
566 |
|
fill="None"/> |
567 |
|
</map> |
568 |
|
</session> |
569 |
|
''' |
570 |
|
|
571 |
|
def test(self): |
572 |
|
"""Test loading a session missing a required attribute""" |
573 |
|
# Don't use assertRaises to make sure that if a session is |
574 |
|
# actually returned it gets destroyed properly. |
575 |
|
try: |
576 |
|
self.session = load_session(self.filename()) |
577 |
|
except LoadError, value: |
578 |
|
pass |
579 |
|
else: |
580 |
|
self.fail("Missing filetype attribute doesn't raise LoadError") |
581 |
|
|
582 |
if __name__ == "__main__": |
if __name__ == "__main__": |
583 |
unittest.main() |
unittest.main() |