/[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 746 - (hide annotations)
Fri Apr 25 10:27:05 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: 12356 byte(s)
(LoadSessionTest.testLayerProjection): New.  Tests a file where a layer has
        a projection.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26