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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 774 - (hide annotations)
Tue Apr 29 14:34:45 2003 UTC (21 years, 10 months ago) by jonathan
Original Path: trunk/thuban/test/test_load.py
File MIME type: text/x-python
File size: 13521 byte(s)
Add new test data contents_test_visible.
(LoadSessionTest.setUp): save test data.
(LoadSessionTest.testLayerVisibility): Test if the visible flag
        is loaded correctly.

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&amp;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&amp;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&amp;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&amp;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&amp;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 bh 292 class LoadSessionTest(unittest.TestCase, support.FileTestMixin):
197    
198     def setUp(self):
199     """Create the test files"""
200     file = open(self.temp_file_name("load_singlelayer.thuban"), "w")
201     file.write(contents_single_map)
202     file.close()
203 jonathan 690
204     file = open(self.temp_file_name("load_classified_v0_2.thuban"), "w")
205     file.write(contents_classified_map_v0_2)
206     file.close()
207    
208     file = open(self.temp_file_name("load_labels.thuban"), "w")
209     file.write(contents_test_labels)
210     file.close()
211 jonathan 746
212     file = open(self.temp_file_name("load_layerproj.thuban"), "w")
213     file.write(contents_test_layer_projection)
214     file.close()
215    
216 jonathan 774 file = open(self.temp_file_name("load_visible.thuban"), "w")
217     file.write(contents_test_visible)
218     file.close()
219    
220 bh 292 self.session = None
221    
222     def tearDown(self):
223     if self.session is not None:
224     self.session.Destroy()
225 bh 765 self.session = None
226 bh 292
227     def testSingleLayer(self):
228     """Load a session with a single map with a single layer"""
229     eq = self.assertEquals
230     session = load_session(self.temp_file_name("load_singlelayer.thuban"))
231     self.session = session
232    
233     # Check the title
234     eq(session.Title(), "single map&layer")
235    
236     # the session has one map.
237     maps = session.Maps()
238     eq(len(maps), 1)
239    
240     # Check the map's attributes
241     map = maps[0]
242     eq(map.Title(), "Test Map")
243    
244     # the map has a single layer
245     layers = map.Layers()
246     eq(len(layers), 1)
247    
248     # Check the layer attributes
249     layer = layers[0]
250     eq(layer.Title(), "My Layer")
251     self.failUnless(filenames_equal(layer.filename,
252     os.path.join(self.temp_dir(),
253     os.pardir, os.pardir,
254     "Data", "iceland",
255     "political.shp")))
256 jonathan 610 eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
257 jonathan 482 eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
258 jonathan 774 eq(layer.Visible(), True)
259 bh 292
260 jonathan 684 self.session.Destroy()
261     self.session = None
262 bh 292
263 jonathan 774 def testLayerVisibility(self):
264     """Test that the visible flag is correctly loaded for a layer."""
265    
266     eq = self.assertEquals
267     session = load_session(self.temp_file_name("load_visible.thuban"))
268     self.session = session
269     maps = session.Maps()
270     eq(len(maps), 1)
271     map = maps[0]
272     layers = map.Layers()
273     eq(len(layers), 1)
274     layer = layers[0]
275    
276     eq(layer.Visible(), False)
277    
278 jonathan 684 def testClassification(self):
279     """Load a session with a map and classified layers."""
280    
281 jonathan 690 session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))
282 jonathan 684 self.session = session
283    
284     map = self.session.Maps()[0] # only one map in the sample
285    
286 jonathan 690 expected = [("My Layer", 2,
287 jonathan 684 [("default", (), "",
288     ("#000000", 1, "None")),
289 jonathan 690 ("single", "1", "",
290     ("#000000", 2, "None")),
291     ("single", "1", "",
292     ("#000000", 10, "None"))]),
293     ("My Layer 2", 4,
294     [("default", (), "",
295     ("#000000", 2, "None")),
296     ("range", (0, 1), "",
297 jonathan 692 ("#111111", 1, "None")),
298 jonathan 690 ("single", .5, "",
299 jonathan 692 ("#000000", 1, "#111111")),
300 jonathan 690 ("range", (-1, 0), "",
301     ("#000000", 1, "None")),
302     ("single", -.5, "",
303     ("#000000", 1, "None"))])]
304 jonathan 684
305 jonathan 690 self.TestLayers(map.Layers(), expected)
306 jonathan 684
307 jonathan 690 def TestLayers(self, layers, expected):
308    
309 jonathan 684 TITLE = 0
310     NUM_GROUPS = 1
311     CLASSES = 2
312     GROUP_TYPE = 0
313     GROUP_DATA = 1
314     GROUP_LABEL = 2
315     GROUP_PROPS = 3
316    
317 jonathan 690 eq = self.assertEquals
318    
319     eq(len(layers), len(expected))
320    
321 jonathan 684 for layer, data in zip(layers, expected):
322     eq(layer.Title(), data[TITLE])
323    
324     clazz = layer.GetClassification()
325     eq(clazz.GetNumGroups(), data[NUM_GROUPS])
326     eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
327    
328     i = 0
329     for group in clazz:
330    
331     props = ClassGroupProperties()
332     props.SetLineColor(
333     parse_color(data[CLASSES][i][GROUP_PROPS][0]))
334     props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
335     props.SetFill(
336     parse_color(data[CLASSES][i][GROUP_PROPS][2]))
337    
338     if data[CLASSES][i][GROUP_TYPE] == "default":
339     g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
340     elif data[CLASSES][i][GROUP_TYPE] == "range":
341     g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
342     data[CLASSES][i][GROUP_DATA][1],
343     props, data[CLASSES][i][GROUP_LABEL])
344     elif data[CLASSES][i][GROUP_TYPE] == "single":
345     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
346     props, data[CLASSES][i][GROUP_LABEL])
347    
348     eq(group, g)
349    
350     i += 1
351    
352 jonathan 690 def testLabels(self):
353     """Load a session and test for reading the group labels."""
354    
355     eq = self.assertEquals
356     session = load_session(self.temp_file_name("load_labels.thuban"))
357     self.session = session
358    
359     map = self.session.Maps()[0] # only one map in the sample
360    
361     expected = [("My Layer", 1,
362     [("default", (), "hallo",
363     ("#000000", 1, "None")),
364     ("single", "1", "welt",
365     ("#000000", 2, "None"))])]
366    
367     self.TestLayers(map.Layers(), expected)
368    
369 jonathan 746 def testLayerProjection(self):
370     eq = self.assertEquals
371     neq = self.assertNotEqual
372    
373     session = load_session(self.temp_file_name("load_layerproj.thuban"))
374     self.session = session
375    
376     map = self.session.Maps()[0] # only one map in the sample
377    
378     layers = map.Layers() # two layers in the sample
379    
380     # test layer with a named projection
381     proj = layers[0].GetProjection()
382     neq(proj, None)
383     eq(proj.GetName(), "hello")
384     eq(proj.GetParameter("proj"), "tmerc")
385     eq(proj.GetParameter("zone"), "13")
386     eq(proj.GetParameter("ellps"), "clrk66")
387    
388     # test layer with an unnamed projection
389     proj = layers[1].GetProjection()
390     neq(proj, None)
391     eq(proj.GetName(), "Unknown")
392     eq(proj.GetParameter("proj"), "lcc")
393     eq(proj.GetParameter("ellps"), "clrk66")
394    
395 bh 292 if __name__ == "__main__":
396     unittest.main()
397 jonathan 684
398    
399    

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26