1 |
# 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 file |
10 |
|
11 |
The tests in this file (test_load.py) are always be the tests for the |
12 |
current version of the thuban file format. Tests for older versions can |
13 |
be found in the version specific test modules, e.g. test_load_0_2 for |
14 |
files created by Thuban 0.2. |
15 |
|
16 |
Maintenance of the test cases: |
17 |
|
18 |
When during a development period the file format is changed with respect |
19 |
to the last released version for the first, the tests here should be |
20 |
copied to the version specific test file. |
21 |
""" |
22 |
|
23 |
__version__ = "$Revision$" |
24 |
# $Source$ |
25 |
# $Id$ |
26 |
|
27 |
import os |
28 |
import unittest |
29 |
|
30 |
import support |
31 |
support.initthuban() |
32 |
|
33 |
from Thuban.Model.load import load_session, parse_color |
34 |
from Thuban.Model.color import Color |
35 |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
36 |
ClassGroupSingleton, ClassGroupDefault |
37 |
|
38 |
|
39 |
def filenames_equal(name1, name2): |
40 |
"""Return true if the filenames name1 and name2 are equal. |
41 |
|
42 |
On systems where it is available, simply use os.path.samefile, |
43 |
otherwise return whether the normalized versions of the filenames |
44 |
according to os.path.normpath are equal. |
45 |
""" |
46 |
if hasattr(os.path, "samefile"): |
47 |
return os.path.samefile(name1, name2) |
48 |
return os.path.normpath(name1) == os.path.normpath(name2) |
49 |
|
50 |
|
51 |
|
52 |
class LoadSessionTest(support.FileLoadTestCase): |
53 |
|
54 |
"""Base class for .thuban file loading tests |
55 |
|
56 |
Basically the same as the FileLoadTestCase, except that all tests |
57 |
use the '.thuban' extension by default and that setUp and tearDown |
58 |
handle sessions. |
59 |
""" |
60 |
|
61 |
file_extension = ".thuban" |
62 |
|
63 |
def setUp(self): |
64 |
"""Create the test files""" |
65 |
support.FileLoadTestCase.setUp(self) |
66 |
self.session = None |
67 |
|
68 |
def tearDown(self): |
69 |
if self.session is not None: |
70 |
self.session.Destroy() |
71 |
self.session = None |
72 |
|
73 |
|
74 |
class ClassificationTest(LoadSessionTest): |
75 |
|
76 |
""" |
77 |
Base class for tests that do some detailed checking of classifications |
78 |
""" |
79 |
|
80 |
def TestLayers(self, layers, expected): |
81 |
TITLE = 0 |
82 |
NUM_GROUPS = 1 |
83 |
CLASSES = 2 |
84 |
GROUP_TYPE = 0 |
85 |
GROUP_DATA = 1 |
86 |
GROUP_LABEL = 2 |
87 |
GROUP_PROPS = 3 |
88 |
|
89 |
eq = self.assertEquals |
90 |
|
91 |
eq(len(layers), len(expected)) |
92 |
|
93 |
for layer, data in zip(layers, expected): |
94 |
eq(layer.Title(), data[TITLE]) |
95 |
|
96 |
clazz = layer.GetClassification() |
97 |
eq(clazz.GetNumGroups(), data[NUM_GROUPS]) |
98 |
eq(clazz.GetNumGroups() + 1, len(data[CLASSES])) |
99 |
|
100 |
i = 0 |
101 |
for group in clazz: |
102 |
props = ClassGroupProperties() |
103 |
props.SetLineColor( |
104 |
parse_color(data[CLASSES][i][GROUP_PROPS][0])) |
105 |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
106 |
props.SetFill( |
107 |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
108 |
|
109 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
110 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
111 |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
112 |
g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0], |
113 |
data[CLASSES][i][GROUP_DATA][1], |
114 |
props, data[CLASSES][i][GROUP_LABEL]) |
115 |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
116 |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
117 |
props, data[CLASSES][i][GROUP_LABEL]) |
118 |
|
119 |
eq(group, g) |
120 |
|
121 |
i += 1 |
122 |
|
123 |
|
124 |
|
125 |
class TestSingleLayer(LoadSessionTest): |
126 |
|
127 |
file_contents = '''\ |
128 |
<?xml version="1.0" encoding="UTF-8"?> |
129 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
130 |
<session title="single map&layer"> |
131 |
<map title="Test Map"> |
132 |
<projection> |
133 |
<parameter value="zone=26"/> |
134 |
<parameter value="proj=utm"/> |
135 |
<parameter value="ellps=clrk66"/> |
136 |
</projection> |
137 |
<layer title="My Layer" stroke_width="1" fill="None" |
138 |
filename="../../Data/iceland/political.shp" |
139 |
stroke="#000000"/> |
140 |
</map> |
141 |
</session> |
142 |
''' |
143 |
|
144 |
def test(self): |
145 |
"""Load a session with a single map with a single layer""" |
146 |
eq = self.assertEquals |
147 |
session = load_session(self.filename()) |
148 |
self.session = session |
149 |
|
150 |
# Check the title |
151 |
eq(session.Title(), "single map&layer") |
152 |
|
153 |
# the session has one map. |
154 |
maps = session.Maps() |
155 |
eq(len(maps), 1) |
156 |
|
157 |
# Check the map's attributes |
158 |
map = maps[0] |
159 |
eq(map.Title(), "Test Map") |
160 |
|
161 |
# the map has a single layer |
162 |
layers = map.Layers() |
163 |
eq(len(layers), 1) |
164 |
|
165 |
# Check the layer attributes |
166 |
layer = layers[0] |
167 |
eq(layer.Title(), "My Layer") |
168 |
self.failUnless(filenames_equal(layer.ShapeStore().FileName(), |
169 |
os.path.join(self.temp_dir(), |
170 |
os.pardir, os.pardir, |
171 |
"Data", "iceland", |
172 |
"political.shp"))) |
173 |
eq(layer.GetClassification().GetDefaultFill(), Color.Transparent) |
174 |
eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000") |
175 |
eq(layer.Visible(), True) |
176 |
|
177 |
self.session.Destroy() |
178 |
self.session = None |
179 |
|
180 |
|
181 |
class TestLayerVisibility(LoadSessionTest): |
182 |
|
183 |
file_contents = '''\ |
184 |
<?xml version="1.0" encoding="UTF-8"?> |
185 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
186 |
<session title="single map&layer"> |
187 |
<map title="Test Map"> |
188 |
<projection> |
189 |
<parameter value="zone=26"/> |
190 |
<parameter value="proj=utm"/> |
191 |
<parameter value="ellps=clrk66"/> |
192 |
</projection> |
193 |
<layer title="My Layer" stroke_width="1" fill="None" |
194 |
filename="../../Data/iceland/political.shp" |
195 |
stroke="#000000" visible="false"> |
196 |
</layer> |
197 |
</map> |
198 |
</session> |
199 |
''' |
200 |
|
201 |
def test(self): |
202 |
"""Test that the visible flag is correctly loaded for a layer.""" |
203 |
eq = self.assertEquals |
204 |
session = load_session(self.filename()) |
205 |
self.session = session |
206 |
maps = session.Maps() |
207 |
eq(len(maps), 1) |
208 |
map = maps[0] |
209 |
layers = map.Layers() |
210 |
eq(len(layers), 1) |
211 |
layer = layers[0] |
212 |
|
213 |
eq(layer.Visible(), False) |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
class TestClassification(ClassificationTest): |
219 |
|
220 |
file_contents = '''\ |
221 |
<?xml version="1.0" encoding="UTF-8"?> |
222 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
223 |
<session title="single map&layer"> |
224 |
<map title="Test Map"> |
225 |
<projection> |
226 |
<parameter value="zone=26"/> |
227 |
<parameter value="proj=utm"/> |
228 |
<parameter value="ellps=clrk66"/> |
229 |
</projection> |
230 |
<layer title="My Layer" stroke_width="1" fill="None" |
231 |
filename="../../Data/iceland/political.shp" |
232 |
stroke="#000000"> |
233 |
<classification field="POPYREG" field_type="string"> |
234 |
<clnull> |
235 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
236 |
</clnull> |
237 |
<clpoint value="1"> |
238 |
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
239 |
</clpoint> |
240 |
<clpoint value="1"> |
241 |
<cldata stroke="#000000" stroke_width="10" fill="None"/> |
242 |
</clpoint> |
243 |
</classification> |
244 |
</layer> |
245 |
<layer title="My Layer 2" stroke_width="1" fill="None" |
246 |
filename="../../Data/iceland/political.shp" |
247 |
stroke="#000000"> |
248 |
<classification field="AREA" field_type="double"> |
249 |
<clnull> |
250 |
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
251 |
</clnull> |
252 |
<clrange min="0" max="1"> |
253 |
<cldata stroke="#111111" stroke_width="1" fill="None"/> |
254 |
</clrange> |
255 |
<clpoint value=".5"> |
256 |
<cldata stroke="#000000" stroke_width="1" fill="#111111"/> |
257 |
</clpoint> |
258 |
<clrange min="-1" max="0"> |
259 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
260 |
</clrange> |
261 |
<clpoint value="-.5"> |
262 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
263 |
</clpoint> |
264 |
</classification> |
265 |
</layer> |
266 |
</map> |
267 |
</session> |
268 |
''' |
269 |
|
270 |
def test(self): |
271 |
"""Load a Thuban session with a map and classified layers.""" |
272 |
session = load_session(self.filename()) |
273 |
self.session = session |
274 |
|
275 |
map = self.session.Maps()[0] # only one map in the sample |
276 |
|
277 |
expected = [("My Layer", 2, |
278 |
[("default", (), "", |
279 |
("#000000", 1, "None")), |
280 |
("single", "1", "", |
281 |
("#000000", 2, "None")), |
282 |
("single", "1", "", |
283 |
("#000000", 10, "None"))]), |
284 |
("My Layer 2", 4, |
285 |
[("default", (), "", |
286 |
("#000000", 2, "None")), |
287 |
("range", (0, 1), "", |
288 |
("#111111", 1, "None")), |
289 |
("single", .5, "", |
290 |
("#000000", 1, "#111111")), |
291 |
("range", (-1, 0), "", |
292 |
("#000000", 1, "None")), |
293 |
("single", -.5, "", |
294 |
("#000000", 1, "None"))])] |
295 |
|
296 |
self.TestLayers(map.Layers(), expected) |
297 |
|
298 |
|
299 |
class TestLabels(ClassificationTest): |
300 |
|
301 |
file_contents = '''\ |
302 |
<?xml version="1.0" encoding="UTF-8"?> |
303 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
304 |
<session title="single map&layer"> |
305 |
<map title="Test Map"> |
306 |
<projection> |
307 |
<parameter value="zone=26"/> |
308 |
<parameter value="proj=utm"/> |
309 |
<parameter value="ellps=clrk66"/> |
310 |
</projection> |
311 |
<layer title="My Layer" stroke_width="1" fill="None" |
312 |
filename="../../Data/iceland/political.shp" |
313 |
stroke="#000000"> |
314 |
<classification field="POPYREG" field_type="string"> |
315 |
<clnull label="hallo"> |
316 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
317 |
</clnull> |
318 |
<clpoint label="welt" value="1"> |
319 |
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
320 |
</clpoint> |
321 |
</classification> |
322 |
</layer> |
323 |
</map> |
324 |
</session> |
325 |
''' |
326 |
|
327 |
def test(self): |
328 |
"""Load a session and test for reading the group labels.""" |
329 |
eq = self.assertEquals |
330 |
session = load_session(self.filename()) |
331 |
self.session = session |
332 |
|
333 |
map = self.session.Maps()[0] # only one map in the sample |
334 |
|
335 |
expected = [("My Layer", 1, |
336 |
[("default", (), "hallo", |
337 |
("#000000", 1, "None")), |
338 |
("single", "1", "welt", |
339 |
("#000000", 2, "None"))])] |
340 |
|
341 |
self.TestLayers(map.Layers(), expected) |
342 |
|
343 |
|
344 |
class TestLayerProjection(LoadSessionTest): |
345 |
|
346 |
file_contents = '''\ |
347 |
<?xml version="1.0" encoding="UTF-8"?> |
348 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
349 |
<session title="single map&layer"> |
350 |
<map title="Test Map"> |
351 |
<projection> |
352 |
<parameter value="zone=26"/> |
353 |
<parameter value="proj=utm"/> |
354 |
<parameter value="ellps=clrk66"/> |
355 |
</projection> |
356 |
<layer title="My Layer" stroke_width="1" fill="None" |
357 |
filename="../../Data/iceland/political.shp" |
358 |
stroke="#000000"> |
359 |
<projection name="hello"> |
360 |
<parameter value="zone=13"/> |
361 |
<parameter value="proj=tmerc"/> |
362 |
<parameter value="ellps=clrk66"/> |
363 |
</projection> |
364 |
<classification field="POPYREG" field_type="string"> |
365 |
<clnull label="hallo"> |
366 |
<cldata stroke="#000000" stroke_width="1" fill="None"/> |
367 |
</clnull> |
368 |
<clpoint label="welt" value="1"> |
369 |
<cldata stroke="#000000" stroke_width="2" fill="None"/> |
370 |
</clpoint> |
371 |
</classification> |
372 |
</layer> |
373 |
<layer title="My Layer" stroke_width="1" fill="None" |
374 |
filename="../../Data/iceland/political.shp" |
375 |
stroke="#000000"> |
376 |
<projection> |
377 |
<parameter value="proj=lcc"/> |
378 |
<parameter value="ellps=clrk66"/> |
379 |
</projection> |
380 |
</layer> |
381 |
</map> |
382 |
</session> |
383 |
''' |
384 |
|
385 |
def test(self): |
386 |
"""Test loading layers with projections""" |
387 |
eq = self.assertEquals |
388 |
neq = self.assertNotEqual |
389 |
|
390 |
session = load_session(self.filename()) |
391 |
self.session = session |
392 |
|
393 |
map = self.session.Maps()[0] # only one map in the sample |
394 |
|
395 |
layers = map.Layers() # two layers in the sample |
396 |
|
397 |
# test layer with a named projection |
398 |
proj = layers[0].GetProjection() |
399 |
neq(proj, None) |
400 |
eq(proj.GetName(), "hello") |
401 |
eq(proj.GetParameter("proj"), "tmerc") |
402 |
eq(proj.GetParameter("zone"), "13") |
403 |
eq(proj.GetParameter("ellps"), "clrk66") |
404 |
|
405 |
# test layer with an unnamed projection |
406 |
proj = layers[1].GetProjection() |
407 |
neq(proj, None) |
408 |
eq(proj.GetName(), "Unknown") |
409 |
eq(proj.GetParameter("proj"), "lcc") |
410 |
eq(proj.GetParameter("ellps"), "clrk66") |
411 |
|
412 |
|
413 |
class TestRasterLayer(LoadSessionTest): |
414 |
|
415 |
file_contents = '''\ |
416 |
<?xml version="1.0" encoding="UTF-8"?> |
417 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
418 |
<session title="single map&layer"> |
419 |
<map title="Test Map"> |
420 |
<rasterlayer title="My RasterLayer" |
421 |
filename="../../Data/iceland/island.tif" |
422 |
visible="false"> |
423 |
</rasterlayer> |
424 |
</map> |
425 |
</session> |
426 |
''' |
427 |
|
428 |
def test(self): |
429 |
eq = self.assertEquals |
430 |
neq = self.assertNotEqual |
431 |
|
432 |
session = load_session(self.filename()) |
433 |
self.session = session |
434 |
|
435 |
map = self.session.Maps()[0] # only one map in the sample |
436 |
|
437 |
layer = map.Layers()[0] # one layer in the sample |
438 |
|
439 |
eq(layer.Title(), "My RasterLayer") |
440 |
self.failIf(layer.Visible()) |
441 |
self.failUnless(filenames_equal(layer.GetImageFilename(), |
442 |
os.path.join(self.temp_dir(), |
443 |
os.pardir, os.pardir, |
444 |
"Data", "iceland", |
445 |
"island.tif"))) |
446 |
|
447 |
if __name__ == "__main__": |
448 |
unittest.main() |