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