/[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 691 - (hide annotations)
Wed Apr 16 14:06:17 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: 9977 byte(s)
removed unnecessary extra <clnull> in the classification test data.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26