/[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 690 - (hide annotations)
Wed Apr 16 13:47:22 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: 10104 byte(s)
Rework the classification test to test that we can load old files.
(testLabels): Test a file where the groups have labels.

1 bh 292 # Copyright (c) 2002 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 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="1" fill="None"/>
94     </clnull>
95     <clnull>
96     <cldata stroke="#000000" stroke_width="2" fill="None"/>
97     </clnull>
98     <clrange min="0" max="1">
99     <cldata stroke="#000000" stroke_width="1" fill="None"/>
100     </clrange>
101     <clpoint value=".5">
102     <cldata stroke="#000000" stroke_width="1" fill="None"/>
103     </clpoint>
104     <clrange min="-1" max="0">
105     <cldata stroke="#000000" stroke_width="1" fill="None"/>
106     </clrange>
107     <clpoint value="-.5">
108     <cldata stroke="#000000" stroke_width="1" fill="None"/>
109     </clpoint>
110     </classification>
111     </layer>
112     </map>
113     </session>
114     '''
115 bh 292
116 jonathan 690 contents_test_labels = '''\
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     <classification field="POPYREG" field_type="string">
130     <clnull label="hallo">
131     <cldata stroke="#000000" stroke_width="1" fill="None"/>
132     </clnull>
133     <clpoint label="welt" value="1">
134     <cldata stroke="#000000" stroke_width="2" fill="None"/>
135     </clpoint>
136     </classification>
137     </layer>
138     </map>
139     </session>
140     '''
141    
142 bh 292 class LoadSessionTest(unittest.TestCase, support.FileTestMixin):
143    
144     def setUp(self):
145     """Create the test files"""
146     file = open(self.temp_file_name("load_singlelayer.thuban"), "w")
147     file.write(contents_single_map)
148     file.close()
149 jonathan 690
150     file = open(self.temp_file_name("load_classified_v0_2.thuban"), "w")
151     file.write(contents_classified_map_v0_2)
152     file.close()
153    
154     file = open(self.temp_file_name("load_labels.thuban"), "w")
155     file.write(contents_test_labels)
156     file.close()
157 bh 292 self.session = None
158    
159     def tearDown(self):
160     if self.session is not None:
161     self.session.Destroy()
162    
163     def testSingleLayer(self):
164     """Load a session with a single map with a single layer"""
165     eq = self.assertEquals
166     session = load_session(self.temp_file_name("load_singlelayer.thuban"))
167     self.session = session
168    
169     # Check the title
170     eq(session.Title(), "single map&layer")
171    
172     # the session has one map.
173     maps = session.Maps()
174     eq(len(maps), 1)
175    
176     # Check the map's attributes
177     map = maps[0]
178     eq(map.Title(), "Test Map")
179    
180     # the map has a single layer
181     layers = map.Layers()
182     eq(len(layers), 1)
183    
184     # Check the layer attributes
185     layer = layers[0]
186     eq(layer.Title(), "My Layer")
187     self.failUnless(filenames_equal(layer.filename,
188     os.path.join(self.temp_dir(),
189     os.pardir, os.pardir,
190     "Data", "iceland",
191     "political.shp")))
192 jonathan 610 eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
193 jonathan 482 eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
194 bh 292
195 jonathan 684 self.session.Destroy()
196     self.session = None
197 bh 292
198 jonathan 684 def testClassification(self):
199     """Load a session with a map and classified layers."""
200    
201 jonathan 690 session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))
202 jonathan 684 self.session = session
203    
204     map = self.session.Maps()[0] # only one map in the sample
205    
206 jonathan 690 expected = [("My Layer", 2,
207 jonathan 684 [("default", (), "",
208     ("#000000", 1, "None")),
209 jonathan 690 ("single", "1", "",
210     ("#000000", 2, "None")),
211     ("single", "1", "",
212     ("#000000", 10, "None"))]),
213     ("My Layer 2", 4,
214     [("default", (), "",
215     ("#000000", 2, "None")),
216     ("range", (0, 1), "",
217 jonathan 684 ("#000000", 1, "None")),
218 jonathan 690 ("single", .5, "",
219     ("#000000", 1, "None")),
220     ("range", (-1, 0), "",
221     ("#000000", 1, "None")),
222     ("single", -.5, "",
223     ("#000000", 1, "None"))])]
224 jonathan 684
225 jonathan 690 self.TestLayers(map.Layers(), expected)
226 jonathan 684
227 jonathan 690 def TestLayers(self, layers, expected):
228    
229 jonathan 684 TITLE = 0
230     NUM_GROUPS = 1
231     CLASSES = 2
232     GROUP_TYPE = 0
233     GROUP_DATA = 1
234     GROUP_LABEL = 2
235     GROUP_PROPS = 3
236    
237 jonathan 690 eq = self.assertEquals
238    
239     eq(len(layers), len(expected))
240    
241 jonathan 684 for layer, data in zip(layers, expected):
242     eq(layer.Title(), data[TITLE])
243    
244     clazz = layer.GetClassification()
245     eq(clazz.GetNumGroups(), data[NUM_GROUPS])
246     eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
247    
248     i = 0
249     for group in clazz:
250    
251     props = ClassGroupProperties()
252     props.SetLineColor(
253     parse_color(data[CLASSES][i][GROUP_PROPS][0]))
254     props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
255     props.SetFill(
256     parse_color(data[CLASSES][i][GROUP_PROPS][2]))
257    
258     if data[CLASSES][i][GROUP_TYPE] == "default":
259     g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
260     elif data[CLASSES][i][GROUP_TYPE] == "range":
261     g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
262     data[CLASSES][i][GROUP_DATA][1],
263     props, data[CLASSES][i][GROUP_LABEL])
264     elif data[CLASSES][i][GROUP_TYPE] == "single":
265     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
266     props, data[CLASSES][i][GROUP_LABEL])
267    
268     eq(group, g)
269    
270     i += 1
271    
272 jonathan 690 def testLabels(self):
273     """Load a session and test for reading the group labels."""
274    
275     eq = self.assertEquals
276     session = load_session(self.temp_file_name("load_labels.thuban"))
277     self.session = session
278    
279     map = self.session.Maps()[0] # only one map in the sample
280    
281     expected = [("My Layer", 1,
282     [("default", (), "hallo",
283     ("#000000", 1, "None")),
284     ("single", "1", "welt",
285     ("#000000", 2, "None"))])]
286    
287     self.TestLayers(map.Layers(), expected)
288    
289 bh 292 if __name__ == "__main__":
290     unittest.main()
291 jonathan 684
292    
293    

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26