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