1 |
# Copyright (c) 2002, 2003, 2004 by Intevation GmbH |
# Copyright (c) 2002, 2003, 2004, 2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
40 |
import dbflib |
import dbflib |
41 |
import shapelib |
import shapelib |
42 |
|
|
43 |
|
from Thuban import internal_from_unicode |
44 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import save_session |
45 |
from Thuban.Model.load import load_session, parse_color, LoadError, \ |
from Thuban.Model.load import load_session, parse_color, LoadError, \ |
46 |
LoadCancelled |
LoadCancelled |
54 |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
55 |
ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE |
ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE |
56 |
|
|
|
|
|
57 |
def filenames_equal(name1, name2): |
def filenames_equal(name1, name2): |
58 |
"""Return true if the filenames name1 and name2 are equal. |
"""Return true if the filenames name1 and name2 are equal. |
59 |
|
|
124 |
print a != b and "***************" or "" |
print a != b and "***************" or "" |
125 |
print a |
print a |
126 |
print b |
print b |
127 |
|
|
128 |
self.assertEquals(el1, el2, |
self.assertEquals(el1, el2, |
129 |
"loaded file not equivalent to the saved file") |
"loaded file not equivalent to the saved file") |
130 |
|
|
163 |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
164 |
props.SetFill( |
props.SetFill( |
165 |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
166 |
|
if len(data[CLASSES][i][GROUP_PROPS]) > 3: |
167 |
|
props.SetSize(data[CLASSES][i][GROUP_PROPS][3]) |
168 |
|
|
169 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
if data[CLASSES][i][GROUP_TYPE] == "default": |
170 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
215 |
self.session = session |
self.session = session |
216 |
|
|
217 |
# Check the title |
# Check the title |
218 |
eq(session.Title(), "Stra\xdfen & Landmarken") |
eq(session.Title(), internal_from_unicode(u"Stra\xdfen & Landmarken")) |
219 |
|
|
220 |
# the session has one map. |
# the session has one map. |
221 |
maps = session.Maps() |
maps = session.Maps() |
223 |
|
|
224 |
# Check the map's attributes |
# Check the map's attributes |
225 |
map = maps[0] |
map = maps[0] |
226 |
eq(map.Title(), "\xdcbersicht") |
eq(map.Title(), internal_from_unicode(u"\xdcbersicht")) |
227 |
proj = map.GetProjection() |
proj = map.GetProjection() |
228 |
eq(proj.GetName(), "WGS 84 / UTM zone 27N") |
eq(proj.GetName(), "WGS 84 / UTM zone 27N") |
229 |
eq(proj.EPSGCode(), "32627") |
eq(proj.EPSGCode(), "32627") |
238 |
|
|
239 |
# Check the layer attributes |
# Check the layer attributes |
240 |
layer = layers[0] |
layer = layers[0] |
241 |
eq(layer.Title(), "K\xfcste") |
eq(layer.Title(), internal_from_unicode(u"K\xfcste")) |
242 |
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
243 |
os.path.join(self.temp_dir(), |
os.path.join(self.temp_dir(), |
244 |
os.pardir, os.pardir, |
os.pardir, os.pardir, |
391 |
self.check_format() |
self.check_format() |
392 |
|
|
393 |
|
|
394 |
|
class TestSymbolSize(ClassificationTest): |
395 |
|
|
396 |
|
file_contents = '''\ |
397 |
|
<?xml version="1.0" encoding="UTF-8"?> |
398 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
399 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="Thuban sample session"> |
400 |
|
<fileshapesource filetype="shapefile" id="D813968480" filename="../../Data/iceland/cultural_landmark-point.shp"/> |
401 |
|
<map title="Iceland map"> |
402 |
|
<layer title="cultural_landmark-point" stroke_width="1" shapestore="D813968480" visible="true" stroke="#000000" fill="#000000"> |
403 |
|
<classification field="CLPTLABEL" field_type="string"> |
404 |
|
<clnull label=""> |
405 |
|
<cldata stroke="#000000" stroke_width="1" size="3" fill="#000000"/> |
406 |
|
</clnull> |
407 |
|
<clpoint label="" value="RUINS"> |
408 |
|
<cldata stroke="#000000" stroke_width="1" size="6" fill="#ffffff"/> |
409 |
|
</clpoint> |
410 |
|
<clpoint label="" value="FARM"> |
411 |
|
<cldata stroke="#000000" stroke_width="1" size="9" fill="#ffff00"/> |
412 |
|
</clpoint> |
413 |
|
</classification> |
414 |
|
</layer> |
415 |
|
</map> |
416 |
|
</session> |
417 |
|
''' |
418 |
|
|
419 |
|
def test(self): |
420 |
|
"""Test that the size attribute for point symbols is correctly |
421 |
|
loaded for a layer.""" |
422 |
|
eq = self.assertEquals |
423 |
|
session = load_session(self.filename()) |
424 |
|
self.session = session |
425 |
|
|
426 |
|
map = session.Maps()[0] # only one map in the sample |
427 |
|
|
428 |
|
expected = [("cultural_landmark-point", 2, |
429 |
|
[("default", (), "", |
430 |
|
("#000000", 1, "#000000", 3)), |
431 |
|
("single", "RUINS", "", |
432 |
|
("#000000", 1, "#ffffff", 6)), |
433 |
|
("single", "FARM", "", |
434 |
|
("#000000", 1, "#ffff00", 9))])] |
435 |
|
|
436 |
|
self.TestLayers(map.Layers(), expected) |
437 |
|
|
438 |
|
self.check_format() |
439 |
|
|
440 |
|
|
441 |
class TestClassification(ClassificationTest): |
class TestClassification(ClassificationTest): |
442 |
|
|
443 |
file_contents = '''\ |
file_contents = '''\ |
511 |
("#000000", 2, "None")), |
("#000000", 2, "None")), |
512 |
("single", "1", "", |
("single", "1", "", |
513 |
("#000000", 10, "None")), |
("#000000", 10, "None")), |
514 |
("single", "\xe4\xf6\xfc", "\xdcml\xe4uts", |
("single", internal_from_unicode(u"\xe4\xf6\xfc"), |
515 |
|
internal_from_unicode(u"\xdcml\xe4uts"), |
516 |
("#000000", 1, "None"))]), |
("#000000", 1, "None"))]), |
517 |
("My Layer 2", 4, |
("My Layer 2", 4, |
518 |
[("default", (), "", |
[("default", (), "", |
666 |
title="single map&layer"> |
title="single map&layer"> |
667 |
<map title="Test Map"> |
<map title="Test Map"> |
668 |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
<rasterlayer visible="false" filename="../../Data/iceland/island.tif" |
669 |
title="My RasterLayer"/> |
title="My RasterLayer" opacity="0.4" masktype="alpha"/> |
670 |
</map> |
</map> |
671 |
</session> |
</session> |
672 |
''' |
''' |
683 |
layer = map.Layers()[0] # one layer in the sample |
layer = map.Layers()[0] # one layer in the sample |
684 |
|
|
685 |
eq(layer.Title(), "My RasterLayer") |
eq(layer.Title(), "My RasterLayer") |
686 |
|
eq(layer.Opacity(), 0.4) |
687 |
|
eq(layer.MaskType(), layer.MASK_ALPHA) |
688 |
|
|
689 |
self.failIf(layer.Visible()) |
self.failIf(layer.Visible()) |
690 |
self.failUnless(filenames_equal(layer.GetImageFilename(), |
self.failUnless(filenames_equal(layer.GetImageFilename(), |
691 |
os.path.join(self.temp_dir(), |
os.path.join(self.temp_dir(), |
794 |
|
|
795 |
label_layer = self.session.Maps()[0].LabelLayer() |
label_layer = self.session.Maps()[0].LabelLayer() |
796 |
expected_labels = [(-21.5, 64.25, "RUINS", ALIGN_LEFT, ALIGN_CENTER), |
expected_labels = [(-21.5, 64.25, "RUINS", ALIGN_LEFT, ALIGN_CENTER), |
797 |
(-15.125, 64.75, "H\xfctte", ALIGN_RIGHT, ALIGN_TOP), |
(-15.125, 64.75, internal_from_unicode(u"H\xfctte"), |
798 |
|
ALIGN_RIGHT, ALIGN_TOP), |
799 |
] |
] |
800 |
for label, values in zip(label_layer.Labels(), expected_labels): |
for label, values in zip(label_layer.Labels(), expected_labels): |
801 |
self.assertEquals((label.x, label.y, label.text, label.halign, |
self.assertEquals((label.x, label.y, label.text, label.halign, |
986 |
else: |
else: |
987 |
self.fail("Missing filetype attribute doesn't raise LoadError") |
self.fail("Missing filetype attribute doesn't raise LoadError") |
988 |
|
|
989 |
|
class Shapefile_CallBack: |
990 |
|
|
991 |
|
def __init__(self, params): |
992 |
|
"""Initialize the callback return values. |
993 |
|
|
994 |
|
params must be a dictionary of the potential CB modes (keys), |
995 |
|
with lists of tuples of return values as values. |
996 |
|
Depending on the test the callback can be called multiple, |
997 |
|
each time a return value is poped from the list |
998 |
|
""" |
999 |
|
|
1000 |
|
self.params = params |
1001 |
|
|
1002 |
|
|
1003 |
|
def s_cb(self, filename, mode = None, second_try= 0): |
1004 |
|
if self.params.has_key(mode): |
1005 |
|
return self.params[mode].pop(0) |
1006 |
|
else: |
1007 |
|
raise LoadError |
1008 |
|
|
1009 |
|
class TestAltPath(LoadSessionTest): |
1010 |
|
|
1011 |
|
"""Test the various cases in the alternative path feature. |
1012 |
|
|
1013 |
|
The test checks the reasonable cases: |
1014 |
|
- First recognition of a path error, fixed with user interaction. |
1015 |
|
- First recognition of a path error, load cancelled. |
1016 |
|
- Path error fixed from list, confirmed by user. |
1017 |
|
- Path error fixed from list, changed by user. |
1018 |
|
- Path error fixed from list, cancelled by user. |
1019 |
|
- Path error wrongly fixed from list, manual fix forced. |
1020 |
|
""" |
1021 |
|
|
1022 |
|
file_contents = '''\ |
1023 |
|
<?xml version="1.0" encoding="UTF-8"?> |
1024 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
1025 |
|
<session xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" title="AltPath Test session"> |
1026 |
|
<fileshapesource filetype="shapefile" id="D1108450956" filename="../../Data/iceland/political.shp"/> |
1027 |
|
<fileshapesource filetype="shapefile" id="D1108900076" filename="../Data/iceland/roads-line.shp"/> |
1028 |
|
<fileshapesource filetype="shapefile" id="D1108947244" filename="../../Data/iceland/cultural_landmark-point.shp"/> |
1029 |
|
<map title="not the iceland map"> |
1030 |
|
<layer title="political" stroke_width="1" shapestore="D1108450956" visible="true" stroke="#000000" fill="#c0c0c0"/> |
1031 |
|
<layer title="roads-line" stroke_width="1" shapestore="D1108900076" visible="true" stroke="#000000" fill="None"/> |
1032 |
|
<layer title="something else" stroke_width="1" shapestore="D1108947244" visible="true" stroke="#000000" fill="None"/> |
1033 |
|
</map> |
1034 |
|
</session> |
1035 |
|
''' |
1036 |
|
|
1037 |
|
def checkSession(self, session): |
1038 |
|
"""Check if session has been loaded successfully.""" |
1039 |
|
|
1040 |
|
eq = self.assertEquals |
1041 |
|
|
1042 |
|
map = session.Maps()[0] |
1043 |
|
layers = map.Layers() |
1044 |
|
|
1045 |
|
eq("AltPath Test session", session.Title()) |
1046 |
|
eq("not the iceland map", map.Title()) |
1047 |
|
eq(3,len(layers)) |
1048 |
|
eq("political",layers[0].Title()) |
1049 |
|
eq("roads-line",layers[1].Title()) |
1050 |
|
eq("something else",layers[2].Title()) |
1051 |
|
|
1052 |
|
def test_01_single_path_error_fix(self): |
1053 |
|
"""Test single file path error fix.""" |
1054 |
|
# The usual initial case |
1055 |
|
s_cb = Shapefile_CallBack({ |
1056 |
|
"search": [("../Data/iceland/roads-line.shp",0)], |
1057 |
|
"check": [(None, None)]}) |
1058 |
|
self.session = load_session(self.filename(), |
1059 |
|
shapefile_callback =s_cb.s_cb) |
1060 |
|
self.checkSession(self.session) |
1061 |
|
|
1062 |
|
def test_02_path_error_fix_from_list(self): |
1063 |
|
"""Test single file path error fix.""" |
1064 |
|
# This represents the usual case for "from_list" |
1065 |
|
s_cb = Shapefile_CallBack({ |
1066 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1067 |
|
"check": [(os.path.abspath("../Data/iceland/roads-line.shp"),1)] |
1068 |
|
}) |
1069 |
|
self.session = load_session(self.filename(), |
1070 |
|
shapefile_callback =s_cb.s_cb) |
1071 |
|
self.checkSession(self.session) |
1072 |
|
|
1073 |
|
def test_03_single_path_error_cancelled(self): |
1074 |
|
"""Test alternative path cancelled.""" |
1075 |
|
s_cb = Shapefile_CallBack({ |
1076 |
|
"search": [(None,0)], |
1077 |
|
"check": [(None, None)]}) |
1078 |
|
self.assertRaises(LoadCancelled, |
1079 |
|
load_session, self.filename(), None, s_cb.s_cb) |
1080 |
|
|
1081 |
|
def test_04_path_error_fix_from_list_cancelled(self): |
1082 |
|
"""Test alternative path from list cancelled.""" |
1083 |
|
s_cb = Shapefile_CallBack({ |
1084 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1085 |
|
"check": [(None,1)] |
1086 |
|
}) |
1087 |
|
self.assertRaises(LoadCancelled, |
1088 |
|
load_session, self.filename(), None, s_cb.s_cb) |
1089 |
|
|
1090 |
|
def test_05_path_error_fix_from_list_changed(self): |
1091 |
|
"""Test alternative path from list changed.""" |
1092 |
|
s_cb = Shapefile_CallBack({ |
1093 |
|
"search": [("../Data/iceland/roads-line.shp",1)], |
1094 |
|
"check": [("../Data/iceland/roads-line.shp",0)] |
1095 |
|
}) |
1096 |
|
self.session = load_session(self.filename(), |
1097 |
|
shapefile_callback =s_cb.s_cb) |
1098 |
|
self.checkSession(self.session) |
1099 |
|
|
1100 |
|
def test_06_path_error_fix_from_list_fails(self): |
1101 |
|
"""Test alternative path recovery from list.""" |
1102 |
|
s_cb = Shapefile_CallBack({ |
1103 |
|
"search": [("../wrong/iceland/roads-line.shp",1), |
1104 |
|
("../Data/iceland/roads-line.shp",0)], |
1105 |
|
"check": [(None,None)] |
1106 |
|
}) |
1107 |
|
self.session = load_session(self.filename(), |
1108 |
|
shapefile_callback =s_cb.s_cb) |
1109 |
|
self.assertRaises(IndexError, |
1110 |
|
s_cb.s_cb, None, "search") |
1111 |
|
|
1112 |
|
|
1113 |
|
|
1114 |
if __name__ == "__main__": |
if __name__ == "__main__": |
1115 |
support.run_tests() |
support.run_tests() |