/[thuban]/branches/WIP-pyshapelib-bramz/test/test_load.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/test_load.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 957 - (show annotations)
Wed May 21 17:12:22 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/test/test_load.py
File MIME type: text/x-python
File size: 14159 byte(s)
Remove unused imports and restructure the
test code
(LoadSessionTest): Split into one class for each test and turn
LoadSessionTest itself into the base class for all such session
tests.
(ClassificationTest): New base class for load tests that test
classifications
(TestSingleLayer, TestLayerVisibility, TestClassification)
(TestLabels, TestLayerProjection, TestRasterLayer): New classes
for the individual tests

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
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&amp;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 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.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 TestLayerVisibility(LoadSessionTest):
171
172 file_contents = '''\
173 <?xml version="1.0" encoding="UTF-8"?>
174 <!DOCTYPE session SYSTEM "thuban.dtd">
175 <session title="single map&amp;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" visible="false">
185 </layer>
186 </map>
187 </session>
188 '''
189
190 def test(self):
191 """Test that the visible flag is correctly loaded for a layer."""
192 eq = self.assertEquals
193 session = load_session(self.filename())
194 self.session = session
195 maps = session.Maps()
196 eq(len(maps), 1)
197 map = maps[0]
198 layers = map.Layers()
199 eq(len(layers), 1)
200 layer = layers[0]
201
202 eq(layer.Visible(), False)
203
204
205
206
207 class TestClassification(ClassificationTest):
208
209 file_contents = '''\
210 <?xml version="1.0" encoding="UTF-8"?>
211 <!DOCTYPE session SYSTEM "thuban.dtd">
212 <session title="single map&amp;layer">
213 <map title="Test Map">
214 <projection>
215 <parameter value="zone=26"/>
216 <parameter value="proj=utm"/>
217 <parameter value="ellps=clrk66"/>
218 </projection>
219 <layer title="My Layer" stroke_width="1" fill="None"
220 filename="../../Data/iceland/political.shp"
221 stroke="#000000">
222 <classification field="POPYREG" field_type="string">
223 <clnull>
224 <cldata stroke="#000000" stroke_width="1" fill="None"/>
225 </clnull>
226 <clpoint value="1">
227 <cldata stroke="#000000" stroke_width="2" fill="None"/>
228 </clpoint>
229 <clpoint value="1">
230 <cldata stroke="#000000" stroke_width="10" fill="None"/>
231 </clpoint>
232 </classification>
233 </layer>
234 <layer title="My Layer 2" stroke_width="1" fill="None"
235 filename="../../Data/iceland/political.shp"
236 stroke="#000000">
237 <classification field="AREA" field_type="double">
238 <clnull>
239 <cldata stroke="#000000" stroke_width="2" fill="None"/>
240 </clnull>
241 <clrange min="0" max="1">
242 <cldata stroke="#111111" stroke_width="1" fill="None"/>
243 </clrange>
244 <clpoint value=".5">
245 <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
246 </clpoint>
247 <clrange min="-1" max="0">
248 <cldata stroke="#000000" stroke_width="1" fill="None"/>
249 </clrange>
250 <clpoint value="-.5">
251 <cldata stroke="#000000" stroke_width="1" fill="None"/>
252 </clpoint>
253 </classification>
254 </layer>
255 </map>
256 </session>
257 '''
258
259 def test(self):
260 """Load a Thuban 0.2 session with a map and classified layers."""
261 session = load_session(self.filename())
262 self.session = session
263
264 map = self.session.Maps()[0] # only one map in the sample
265
266 expected = [("My Layer", 2,
267 [("default", (), "",
268 ("#000000", 1, "None")),
269 ("single", "1", "",
270 ("#000000", 2, "None")),
271 ("single", "1", "",
272 ("#000000", 10, "None"))]),
273 ("My Layer 2", 4,
274 [("default", (), "",
275 ("#000000", 2, "None")),
276 ("range", (0, 1), "",
277 ("#111111", 1, "None")),
278 ("single", .5, "",
279 ("#000000", 1, "#111111")),
280 ("range", (-1, 0), "",
281 ("#000000", 1, "None")),
282 ("single", -.5, "",
283 ("#000000", 1, "None"))])]
284
285 self.TestLayers(map.Layers(), expected)
286
287
288 class TestLabels(ClassificationTest):
289
290 file_contents = '''\
291 <?xml version="1.0" encoding="UTF-8"?>
292 <!DOCTYPE session SYSTEM "thuban.dtd">
293 <session title="single map&amp;layer">
294 <map title="Test Map">
295 <projection>
296 <parameter value="zone=26"/>
297 <parameter value="proj=utm"/>
298 <parameter value="ellps=clrk66"/>
299 </projection>
300 <layer title="My Layer" stroke_width="1" fill="None"
301 filename="../../Data/iceland/political.shp"
302 stroke="#000000">
303 <classification field="POPYREG" field_type="string">
304 <clnull label="hallo">
305 <cldata stroke="#000000" stroke_width="1" fill="None"/>
306 </clnull>
307 <clpoint label="welt" value="1">
308 <cldata stroke="#000000" stroke_width="2" fill="None"/>
309 </clpoint>
310 </classification>
311 </layer>
312 </map>
313 </session>
314 '''
315
316 def test(self):
317 """Load a session and test for reading the group labels."""
318 eq = self.assertEquals
319 session = load_session(self.filename())
320 self.session = session
321
322 map = self.session.Maps()[0] # only one map in the sample
323
324 expected = [("My Layer", 1,
325 [("default", (), "hallo",
326 ("#000000", 1, "None")),
327 ("single", "1", "welt",
328 ("#000000", 2, "None"))])]
329
330 self.TestLayers(map.Layers(), expected)
331
332
333 class TestLayerProjection(LoadSessionTest):
334
335 file_contents = '''\
336 <?xml version="1.0" encoding="UTF-8"?>
337 <!DOCTYPE session SYSTEM "thuban.dtd">
338 <session title="single map&amp;layer">
339 <map title="Test Map">
340 <projection>
341 <parameter value="zone=26"/>
342 <parameter value="proj=utm"/>
343 <parameter value="ellps=clrk66"/>
344 </projection>
345 <layer title="My Layer" stroke_width="1" fill="None"
346 filename="../../Data/iceland/political.shp"
347 stroke="#000000">
348 <projection name="hello">
349 <parameter value="zone=13"/>
350 <parameter value="proj=tmerc"/>
351 <parameter value="ellps=clrk66"/>
352 </projection>
353 <classification field="POPYREG" field_type="string">
354 <clnull label="hallo">
355 <cldata stroke="#000000" stroke_width="1" fill="None"/>
356 </clnull>
357 <clpoint label="welt" value="1">
358 <cldata stroke="#000000" stroke_width="2" fill="None"/>
359 </clpoint>
360 </classification>
361 </layer>
362 <layer title="My Layer" stroke_width="1" fill="None"
363 filename="../../Data/iceland/political.shp"
364 stroke="#000000">
365 <projection>
366 <parameter value="proj=lcc"/>
367 <parameter value="ellps=clrk66"/>
368 </projection>
369 </layer>
370 </map>
371 </session>
372 '''
373
374 def test(self):
375 """Test loading layers with projections"""
376 eq = self.assertEquals
377 neq = self.assertNotEqual
378
379 session = load_session(self.filename())
380 self.session = session
381
382 map = self.session.Maps()[0] # only one map in the sample
383
384 layers = map.Layers() # two layers in the sample
385
386 # test layer with a named projection
387 proj = layers[0].GetProjection()
388 neq(proj, None)
389 eq(proj.GetName(), "hello")
390 eq(proj.GetParameter("proj"), "tmerc")
391 eq(proj.GetParameter("zone"), "13")
392 eq(proj.GetParameter("ellps"), "clrk66")
393
394 # test layer with an unnamed projection
395 proj = layers[1].GetProjection()
396 neq(proj, None)
397 eq(proj.GetName(), "Unknown")
398 eq(proj.GetParameter("proj"), "lcc")
399 eq(proj.GetParameter("ellps"), "clrk66")
400
401
402 class TestRasterLayer(LoadSessionTest):
403
404 file_contents = '''\
405 <?xml version="1.0" encoding="UTF-8"?>
406 <!DOCTYPE session SYSTEM "thuban.dtd">
407 <session title="single map&amp;layer">
408 <map title="Test Map">
409 <rasterlayer title="My RasterLayer"
410 filename="../../Data/iceland/island.tif"
411 visible="false">
412 </rasterlayer>
413 </map>
414 </session>
415 '''
416
417 def test(self):
418 eq = self.assertEquals
419 neq = self.assertNotEqual
420
421 session = load_session(self.filename())
422 self.session = session
423
424 map = self.session.Maps()[0] # only one map in the sample
425
426 layer = map.Layers()[0] # one layer in the sample
427
428 eq(layer.Title(), "My RasterLayer")
429 self.failIf(layer.Visible())
430 self.failUnless(filenames_equal(layer.GetImageFilename(),
431 os.path.join(self.temp_dir(),
432 os.pardir, os.pardir,
433 "Data", "iceland",
434 "island.tif")))
435
436 if __name__ == "__main__":
437 unittest.main()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26