1 |
bh |
1247 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Test loading a thuban session from a files generated by Thuban 0.2 and earlier |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
# $Source$ |
14 |
|
|
# $Id$ |
15 |
|
|
|
16 |
|
|
import os |
17 |
|
|
import unittest |
18 |
|
|
|
19 |
|
|
import support |
20 |
|
|
support.initthuban() |
21 |
|
|
|
22 |
|
|
from Thuban.Model.load import load_session, parse_color |
23 |
|
|
from Thuban.Model.color import Color |
24 |
|
|
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
25 |
|
|
ClassGroupSingleton, ClassGroupDefault |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
def filenames_equal(name1, name2): |
29 |
|
|
"""Return true if the filenames name1 and name2 are equal. |
30 |
|
|
|
31 |
|
|
On systems where it is available, simply use os.path.samefile, |
32 |
|
|
otherwise return whether the normalized versions of the filenames |
33 |
|
|
according to os.path.normpath are equal. |
34 |
|
|
""" |
35 |
|
|
if hasattr(os.path, "samefile"): |
36 |
|
|
return os.path.samefile(name1, name2) |
37 |
|
|
return os.path.normpath(name1) == os.path.normpath(name2) |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
class LoadSessionTest(support.FileLoadTestCase): |
42 |
|
|
|
43 |
|
|
"""Base class for .thuban file loading tests |
44 |
|
|
|
45 |
|
|
Basically the same as the FileLoadTestCase, except that all tests |
46 |
|
|
use the '.thuban' extension by default and that setUp and tearDown |
47 |
|
|
handle sessions. |
48 |
|
|
""" |
49 |
|
|
|
50 |
|
|
file_extension = ".thuban" |
51 |
|
|
|
52 |
|
|
def setUp(self): |
53 |
|
|
"""Create the test files""" |
54 |
|
|
support.FileLoadTestCase.setUp(self) |
55 |
|
|
self.session = None |
56 |
|
|
|
57 |
|
|
def tearDown(self): |
58 |
|
|
if self.session is not None: |
59 |
|
|
self.session.Destroy() |
60 |
|
|
self.session = None |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
class ClassificationTest(LoadSessionTest): |
64 |
|
|
|
65 |
|
|
""" |
66 |
|
|
Base class for tests that do some detailed checking of classifications |
67 |
|
|
""" |
68 |
|
|
|
69 |
|
|
def TestLayers(self, layers, expected): |
70 |
|
|
TITLE = 0 |
71 |
|
|
NUM_GROUPS = 1 |
72 |
|
|
CLASSES = 2 |
73 |
|
|
GROUP_TYPE = 0 |
74 |
|
|
GROUP_DATA = 1 |
75 |
|
|
GROUP_LABEL = 2 |
76 |
|
|
GROUP_PROPS = 3 |
77 |
|
|
|
78 |
|
|
eq = self.assertEquals |
79 |
|
|
|
80 |
|
|
eq(len(layers), len(expected)) |
81 |
|
|
|
82 |
|
|
for layer, data in zip(layers, expected): |
83 |
|
|
eq(layer.Title(), data[TITLE]) |
84 |
|
|
|
85 |
|
|
clazz = layer.GetClassification() |
86 |
|
|
eq(clazz.GetNumGroups(), data[NUM_GROUPS]) |
87 |
|
|
eq(clazz.GetNumGroups() + 1, len(data[CLASSES])) |
88 |
|
|
|
89 |
|
|
i = 0 |
90 |
|
|
for group in clazz: |
91 |
|
|
props = ClassGroupProperties() |
92 |
|
|
props.SetLineColor( |
93 |
|
|
parse_color(data[CLASSES][i][GROUP_PROPS][0])) |
94 |
|
|
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
95 |
|
|
props.SetFill( |
96 |
|
|
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
97 |
|
|
|
98 |
|
|
if data[CLASSES][i][GROUP_TYPE] == "default": |
99 |
|
|
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
100 |
|
|
elif data[CLASSES][i][GROUP_TYPE] == "range": |
101 |
|
|
g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0], |
102 |
|
|
data[CLASSES][i][GROUP_DATA][1], |
103 |
|
|
props, data[CLASSES][i][GROUP_LABEL]) |
104 |
|
|
elif data[CLASSES][i][GROUP_TYPE] == "single": |
105 |
|
|
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
106 |
|
|
props, data[CLASSES][i][GROUP_LABEL]) |
107 |
|
|
|
108 |
|
|
eq(group, g) |
109 |
|
|
|
110 |
|
|
i += 1 |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
class TestSingleLayer(LoadSessionTest): |
115 |
|
|
|
116 |
|
|
file_contents = '''\ |
117 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
118 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
119 |
|
|
<session title="single map&layer"> |
120 |
|
|
<map title="Test Map"> |
121 |
|
|
<projection> |
122 |
|
|
<parameter value="zone=26"/> |
123 |
|
|
<parameter value="proj=utm"/> |
124 |
|
|
<parameter value="ellps=clrk66"/> |
125 |
|
|
</projection> |
126 |
|
|
<layer title="My Layer" stroke_width="1" fill="None" |
127 |
|
|
filename="../../Data/iceland/political.shp" |
128 |
|
|
stroke="#000000"/> |
129 |
|
|
</map> |
130 |
|
|
</session> |
131 |
|
|
''' |
132 |
|
|
|
133 |
|
|
def test(self): |
134 |
|
|
"""Load a Thuban 0.2 session with a single map with a single layer""" |
135 |
|
|
eq = self.assertEquals |
136 |
|
|
session = load_session(self.filename()) |
137 |
|
|
self.session = session |
138 |
|
|
|
139 |
|
|
# Check the title |
140 |
|
|
eq(session.Title(), "single map&layer") |
141 |
|
|
|
142 |
|
|
# the session has one map. |
143 |
|
|
maps = session.Maps() |
144 |
|
|
eq(len(maps), 1) |
145 |
|
|
|
146 |
|
|
# Check the map's attributes |
147 |
|
|
map = maps[0] |
148 |
|
|
eq(map.Title(), "Test Map") |
149 |
|
|
|
150 |
|
|
# the map has a single layer |
151 |
|
|
layers = map.Layers() |
152 |
|
|
eq(len(layers), 1) |
153 |
|
|
|
154 |
|
|
# Check the layer attributes |
155 |
|
|
layer = layers[0] |
156 |
|
|
eq(layer.Title(), "My Layer") |
157 |
|
|
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
158 |
|
|
os.path.join(self.temp_dir(), |
159 |
|
|
os.pardir, os.pardir, |
160 |
|
|
"Data", "iceland", |
161 |
|
|
"political.shp"))) |
162 |
|
|
eq(layer.GetClassification().GetDefaultFill(), Color.Transparent) |
163 |
|
|
eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000") |
164 |
|
|
eq(layer.Visible(), True) |
165 |
|
|
|
166 |
|
|
self.session.Destroy() |
167 |
|
|
self.session = None |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
class TestClassification(ClassificationTest): |
171 |
|
|
|
172 |
|
|
file_contents = '''\ |
173 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
174 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
175 |
|
|
<session title="single map&layer"> |
176 |
|
|
<map title="Test Map"> |
177 |
|
|
<projection> |
178 |
|
|
<parameter value="zone=26"/> |
179 |
|
|
<parameter value="proj=utm"/> |
180 |
|
|
<parameter value="ellps=clrk66"/> |
181 |
|
|
</projection> |
182 |
|
|
<layer title="My Layer" stroke_width="1" fill="None" |
183 |
|
|
filename="../../Data/iceland/political.shp" |
184 |
|
|
stroke="#000000"> |
185 |
|
|
<classification field="POPYREG" field_type="string"> |
186 |
|
|
<clnull> |
187 |
|
|
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
188 |
|
|
</clnull> |
189 |
|
|
<clpoint value="1"> |
190 |
|
|
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
191 |
|
|
</clpoint> |
192 |
|
|
<clpoint value="1"> |
193 |
|
|
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
194 |
|
|
</clpoint> |
195 |
|
|
</classification> |
196 |
|
|
</layer> |
197 |
|
|
<layer title="My Layer 2" stroke_width="1" fill="None" |
198 |
|
|
filename="../../Data/iceland/political.shp" |
199 |
|
|
stroke="#000000"> |
200 |
|
|
<classification field="AREA" field_type="double"> |
201 |
|
|
<clnull> |
202 |
|
|
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
203 |
|
|
</clnull> |
204 |
|
|
<clrange min="0" max="1"> |
205 |
|
|
<cldata stroke="#111111" stroke_width="1" fill="None"/> |
206 |
|
|
</clrange> |
207 |
|
|
<clpoint value=".5"> |
208 |
|
|
<cldata stroke="#000000" stroke_width="1" fill="#111111"/> |
209 |
|
|
</clpoint> |
210 |
|
|
<clrange min="-1" max="0"> |
211 |
|
|
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
212 |
|
|
</clrange> |
213 |
|
|
<clpoint value="-.5"> |
214 |
|
|
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
215 |
|
|
</clpoint> |
216 |
|
|
</classification> |
217 |
|
|
</layer> |
218 |
|
|
</map> |
219 |
|
|
</session> |
220 |
|
|
''' |
221 |
|
|
|
222 |
|
|
def test(self): |
223 |
|
|
"""Load a Thuban 0.2 session with a map and classified layers.""" |
224 |
|
|
session = load_session(self.filename()) |
225 |
|
|
self.session = session |
226 |
|
|
|
227 |
|
|
map = self.session.Maps()[0] # only one map in the sample |
228 |
|
|
|
229 |
|
|
expected = [("My Layer", 2, |
230 |
|
|
[("default", (), "", |
231 |
|
|
("#000000", 1, "None")), |
232 |
|
|
("single", "1", "", |
233 |
|
|
("#000000", 2, "None")), |
234 |
|
|
("single", "1", "", |
235 |
|
|
("#000000", 10, "None"))]), |
236 |
|
|
("My Layer 2", 4, |
237 |
|
|
[("default", (), "", |
238 |
|
|
("#000000", 2, "None")), |
239 |
|
|
("range", (0, 1), "", |
240 |
|
|
("#111111", 1, "None")), |
241 |
|
|
("single", .5, "", |
242 |
|
|
("#000000", 1, "#111111")), |
243 |
|
|
("range", (-1, 0), "", |
244 |
|
|
("#000000", 1, "None")), |
245 |
|
|
("single", -.5, "", |
246 |
|
|
("#000000", 1, "None"))])] |
247 |
|
|
|
248 |
|
|
self.TestLayers(map.Layers(), expected) |
249 |
|
|
|
250 |
|
|
|
251 |
|
|
if __name__ == "__main__": |
252 |
|
|
unittest.main() |