/[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 765 - (show annotations)
Tue Apr 29 12:42:14 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_load.py
File MIME type: text/x-python
File size: 12390 byte(s)
Next step of table implementation. Introduce a transient database
using SQLite that some of the data is copied to on demand. This
allows us to do joins and other operations that require an index
for good performance with reasonable efficiency. Thuban now needs
SQLite 2.8.0 and pysqlite 0.4.1. Older versions may work but I
haven't tested that.

* Thuban/Model/transientdb.py: New. Transient database
implementation.

* test/test_transientdb.py: New. Tests for the transient DB
classes.

* Thuban/Model/session.py (AutoRemoveFile, AutoRemoveDir): New
classes to help automatically remove temporary files and
directories.
(Session.__init__): New instance variables temp_dir for the
temporary directory and transient_db for the SQLite database
(Session.temp_directory): New. Create a temporary directory if not
yet done and return its name. Use AutoRemoveDir to have it
automatically deleted
(Session.TransientDB): Instantiate the transient database if not
done yet and return it.

* Thuban/Model/data.py (ShapefileStore.__init__): Use an
AutoTransientTable so that data is copied to the transient DB on
demand.
(SimpleStore): New class that simply combines a table and a
shapefile

* Thuban/Model/table.py (Table, DBFTable): Rename Table into
DBFTable and update its doc-string to reflect the fact that this
is only the table interface to a DBF file. Table is now an alias
for DBFTable for temporary backwards compatibility.

* Thuban/UI/application.py (ThubanApplication.OnExit): Make sure
the last reference to the session goes away so that the temporary
files are removed properly.

* test/test_load.py (LoadSessionTest.tearDown): Remove the
reference to the session to make sure the temporary files are
removed.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26