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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1410 - (hide annotations)
Mon Jul 14 10:09:11 2003 UTC (21 years, 7 months ago) by frank
Original Path: trunk/thuban/test/test_load_0_8.py
File MIME type: text/x-python
File size: 20294 byte(s)
(TestUnicodeStrings): New, test load of unicode strings from session file:
	session title, map title and projection name.

1 bh 1375 # 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 generated by Thuban 0.8.0 or 0.8.1
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     import dbflib
23    
24     from Thuban.Model.load import load_session, parse_color, LoadError
25     from Thuban.Model.color import Transparent
26     from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
27     ClassGroupSingleton, ClassGroupDefault
28    
29    
30     def filenames_equal(name1, name2):
31     """Return true if the filenames name1 and name2 are equal.
32    
33     On systems where it is available, simply use os.path.samefile,
34     otherwise return whether the normalized versions of the filenames
35     according to os.path.normpath are equal.
36     """
37     if hasattr(os.path, "samefile"):
38     return os.path.samefile(name1, name2)
39     return os.path.normpath(name1) == os.path.normpath(name2)
40    
41    
42    
43     class LoadSessionTest(support.FileLoadTestCase):
44    
45     """Base class for .thuban file loading tests
46    
47     Basically the same as the FileLoadTestCase, except that all tests
48     use the '.thuban' extension by default and that setUp and tearDown
49     handle sessions.
50     """
51    
52     file_extension = ".thuban"
53    
54     def setUp(self):
55     """Create the test files"""
56     support.FileLoadTestCase.setUp(self)
57     self.session = None
58    
59     def tearDown(self):
60     if self.session is not None:
61     self.session.Destroy()
62     self.session = None
63    
64    
65     dtd = "http://thuban.intevation.org/dtds/thuban-0.8.dtd"
66     thubanids = [((dtd, n), (None, "id")) for n in
67     ["fileshapesource", "filetable", "jointable",
68     "derivedshapesource"]]
69     thubanidrefs = [((dtd, n), (None, m)) for n, m in
70     [("layer", "shapestore"),
71     ("jointable", "left"),
72     ("jointable", "right"),
73     ("derivedshapesource", "table"),
74     ("derivedshapesource", "shapesource")]]
75     del n, m, dtd
76    
77    
78     class ClassificationTest(LoadSessionTest):
79    
80     """
81     Base class for tests that do some detailed checking of classifications
82     """
83    
84     def TestLayers(self, layers, expected):
85     TITLE = 0
86     NUM_GROUPS = 1
87     CLASSES = 2
88     GROUP_TYPE = 0
89     GROUP_DATA = 1
90     GROUP_LABEL = 2
91     GROUP_PROPS = 3
92    
93     eq = self.assertEquals
94    
95     eq(len(layers), len(expected))
96    
97     for layer, data in zip(layers, expected):
98     eq(layer.Title(), data[TITLE])
99    
100     clazz = layer.GetClassification()
101     eq(clazz.GetNumGroups(), data[NUM_GROUPS])
102     eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
103    
104     i = 0
105     for group in clazz:
106     props = ClassGroupProperties()
107     props.SetLineColor(
108     parse_color(data[CLASSES][i][GROUP_PROPS][0]))
109     props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
110     props.SetFill(
111     parse_color(data[CLASSES][i][GROUP_PROPS][2]))
112    
113     if data[CLASSES][i][GROUP_TYPE] == "default":
114     g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
115     elif data[CLASSES][i][GROUP_TYPE] == "range":
116     g = ClassGroupRange((data[CLASSES][i][GROUP_DATA][0],
117     data[CLASSES][i][GROUP_DATA][1]),
118     props, data[CLASSES][i][GROUP_LABEL])
119     elif data[CLASSES][i][GROUP_TYPE] == "single":
120     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
121     props, data[CLASSES][i][GROUP_LABEL])
122    
123     eq(group, g)
124    
125     i += 1
126    
127    
128    
129     class TestSingleLayer(LoadSessionTest):
130    
131     file_contents = '''\
132     <?xml version="1.0" encoding="UTF-8"?>
133     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
134     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
135     title="single map&amp;layer">
136     <fileshapesource filetype="shapefile" id="D1"
137     filename="../../Data/iceland/political.shp"/>
138     <map title="Test Map">
139     <projection name="Unknown">
140     <parameter value="zone=26"/>
141     <parameter value="proj=utm"/>
142     <parameter value="ellps=clrk66"/>
143     </projection>
144     <layer shapestore="D1" visible="true"
145     stroke="#000000" title="My Layer" stroke_width="1"
146     fill="None"/>
147     </map>
148     </session>
149     '''
150    
151     def test(self):
152     """Load a session with a single map with a single layer"""
153     eq = self.assertEquals
154     session = load_session(self.filename())
155     self.session = session
156    
157     # Check the title
158     eq(session.Title(), "single map&layer")
159    
160     # the session has one map.
161     maps = session.Maps()
162     eq(len(maps), 1)
163    
164     # Check the map's attributes
165     map = maps[0]
166     eq(map.Title(), "Test Map")
167    
168     # the map has a single layer
169     layers = map.Layers()
170     eq(len(layers), 1)
171    
172     # Check the layer attributes
173     layer = layers[0]
174     eq(layer.Title(), "My Layer")
175     self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
176     os.path.join(self.temp_dir(),
177     os.pardir, os.pardir,
178     "Data", "iceland",
179     "political.shp")))
180     eq(layer.GetClassification().GetDefaultFill(), Transparent)
181     eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
182     eq(layer.Visible(), True)
183    
184     self.session.Destroy()
185     self.session = None
186    
187    
188     class TestLayerVisibility(LoadSessionTest):
189    
190     file_contents = '''\
191     <?xml version="1.0" encoding="UTF-8"?>
192     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
193     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
194     title="single map&amp;layer">
195     <fileshapesource filetype="shapefile" id="D1"
196     filename="../../Data/iceland/political.shp"/>
197     <map title="Test Map">
198     <projection name="Unknown">
199     <parameter value="zone=26"/>
200     <parameter value="proj=utm"/>
201     <parameter value="ellps=clrk66"/>
202     </projection>
203     <layer shapestore="D1" visible="false" stroke="#000000"
204     title="My Layer" stroke_width="1" fill="None"/>
205     </map>
206     </session>
207     '''
208    
209     def test(self):
210     """Test that the visible flag is correctly loaded for a layer."""
211     eq = self.assertEquals
212     session = load_session(self.filename())
213     self.session = session
214     maps = session.Maps()
215     eq(len(maps), 1)
216     map = maps[0]
217     layers = map.Layers()
218     eq(len(layers), 1)
219     layer = layers[0]
220    
221     eq(layer.Visible(), False)
222    
223    
224     class TestClassification(ClassificationTest):
225    
226     file_contents = '''\
227     <?xml version="1.0" encoding="UTF-8"?>
228     <!DOCTYPE session SYSTEM "thuban.dtd">
229     <session title="single map&amp;layer">
230     <map title="Test Map">
231     <projection>
232     <parameter value="zone=26"/>
233     <parameter value="proj=utm"/>
234     <parameter value="ellps=clrk66"/>
235     </projection>
236     <layer title="My Layer" stroke_width="1" fill="None"
237     filename="../../Data/iceland/political.shp"
238     stroke="#000000">
239     <classification field="POPYREG" field_type="string">
240     <clnull>
241     <cldata stroke="#000000" stroke_width="1" fill="None"/>
242     </clnull>
243     <clpoint value="1">
244     <cldata stroke="#000000" stroke_width="2" fill="None"/>
245     </clpoint>
246     <clpoint value="1">
247     <cldata stroke="#000000" stroke_width="10" fill="None"/>
248     </clpoint>
249     </classification>
250     </layer>
251     <layer title="My Layer 2" stroke_width="1" fill="None"
252     filename="../../Data/iceland/political.shp"
253     stroke="#000000">
254     <classification field="AREA" field_type="double">
255     <clnull>
256     <cldata stroke="#000000" stroke_width="2" fill="None"/>
257     </clnull>
258     <clrange min="0" max="1">
259     <cldata stroke="#111111" stroke_width="1" fill="None"/>
260     </clrange>
261     <clpoint value=".5">
262     <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
263     </clpoint>
264     <clrange min="-1" max="0">
265     <cldata stroke="#000000" stroke_width="1" fill="None"/>
266     </clrange>
267     <clpoint value="-.5">
268     <cldata stroke="#000000" stroke_width="1" fill="None"/>
269     </clpoint>
270     </classification>
271     </layer>
272     </map>
273     </session>
274     '''
275    
276     def test(self):
277     """Load a Thuban session with a map and classified layers."""
278     session = load_session(self.filename())
279     self.session = session
280    
281     map = self.session.Maps()[0] # only one map in the sample
282    
283     expected = [("My Layer", 2,
284     [("default", (), "",
285     ("#000000", 1, "None")),
286     ("single", "1", "",
287     ("#000000", 2, "None")),
288     ("single", "1", "",
289     ("#000000", 10, "None"))]),
290     ("My Layer 2", 4,
291     [("default", (), "",
292     ("#000000", 2, "None")),
293     ("range", (0, 1), "",
294     ("#111111", 1, "None")),
295     ("single", .5, "",
296     ("#000000", 1, "#111111")),
297     ("range", (-1, 0), "",
298     ("#000000", 1, "None")),
299     ("single", -.5, "",
300     ("#000000", 1, "None"))])]
301    
302     self.TestLayers(map.Layers(), expected)
303    
304    
305     class TestLabels(ClassificationTest):
306    
307     file_contents = '''\
308     <?xml version="1.0" encoding="UTF-8"?>
309     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
310     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
311     title="single map&amp;layer">
312     <fileshapesource filetype="shapefile" id="D1"
313     filename="../../Data/iceland/political.shp"/>
314     <map title="Test Map">
315     <projection name="Unknown">
316     <parameter value="zone=26"/>
317     <parameter value="proj=utm"/>
318     <parameter value="ellps=clrk66"/>
319     </projection>
320     <layer shapestore="D1" visible="true" stroke="#000000"
321     title="My Layer" stroke_width="1" fill="None">
322     <classification field="POPYREG" field_type="string">
323     <clnull label="hallo">
324     <cldata stroke="#000000" stroke_width="1" fill="None"/>
325     </clnull>
326     <clpoint label="welt" value="1">
327     <cldata stroke="#000000" stroke_width="2" fill="None"/>
328     </clpoint>
329     </classification>
330     </layer>
331     </map>
332     </session>
333     '''
334    
335     def test(self):
336     """Load a session and test for reading the group labels."""
337     eq = self.assertEquals
338     session = load_session(self.filename())
339     self.session = session
340    
341     map = self.session.Maps()[0] # only one map in the sample
342    
343     expected = [("My Layer", 1,
344     [("default", (), "hallo",
345     ("#000000", 1, "None")),
346     ("single", "1", "welt",
347     ("#000000", 2, "None"))])]
348    
349     self.TestLayers(map.Layers(), expected)
350    
351    
352     class TestLayerProjection(LoadSessionTest):
353    
354     file_contents = '''\
355     <?xml version="1.0" encoding="UTF-8"?>
356     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
357     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
358     title="single map&amp;layer">
359     <fileshapesource filetype="shapefile" id="D2"
360     filename="../../Data/iceland/roads-line.shp"/>
361     <fileshapesource filetype="shapefile" id="D4"
362     filename="../../Data/iceland/political.shp"/>
363     <map title="Test Map">
364     <projection name="Unknown">
365     <parameter value="zone=26"/>
366     <parameter value="proj=utm"/>
367     <parameter value="ellps=clrk66"/>
368     </projection>
369     <layer shapestore="D4" visible="true" stroke="#000000"
370     title="My Layer" stroke_width="1" fill="None">
371     <projection name="hello">
372     <parameter value="zone=13"/>
373     <parameter value="proj=tmerc"/>
374     <parameter value="ellps=clrk66"/>
375     </projection>
376     <classification field="POPYREG" field_type="string">
377     <clnull label="hallo">
378     <cldata stroke="#000000" stroke_width="1" fill="None"/>
379     </clnull>
380     <clpoint label="welt" value="1">
381     <cldata stroke="#000000" stroke_width="2" fill="None"/>
382     </clpoint>
383     </classification>
384     </layer>
385     <layer shapestore="D2" visible="true" stroke="#000000"
386     title="My Layer" stroke_width="1" fill="None">
387     <projection name="Unknown">
388     <parameter value="proj=lcc"/>
389     <parameter value="ellps=clrk66"/>
390     </projection>
391     </layer>
392     </map>
393     </session>
394     '''
395    
396     def test(self):
397     """Test loading layers with projections"""
398     eq = self.assertEquals
399     neq = self.assertNotEqual
400    
401     session = load_session(self.filename())
402     self.session = session
403    
404     map = self.session.Maps()[0] # only one map in the sample
405    
406     layers = map.Layers() # two layers in the sample
407    
408     # test layer with a named projection
409     proj = layers[0].GetProjection()
410     neq(proj, None)
411     eq(proj.GetName(), "hello")
412     eq(proj.GetParameter("proj"), "tmerc")
413     eq(proj.GetParameter("zone"), "13")
414     eq(proj.GetParameter("ellps"), "clrk66")
415    
416     # test layer with an unnamed projection
417     proj = layers[1].GetProjection()
418     neq(proj, None)
419     eq(proj.GetName(), "Unknown")
420     eq(proj.GetParameter("proj"), "lcc")
421     eq(proj.GetParameter("ellps"), "clrk66")
422    
423    
424     class TestRasterLayer(LoadSessionTest):
425    
426     file_contents = '''\
427     <?xml version="1.0" encoding="UTF-8"?>
428     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
429     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
430     title="single map&amp;layer">
431     <map title="Test Map">
432     <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
433     title="My RasterLayer"/>
434     </map>
435     </session>
436     '''
437    
438     def test(self):
439     eq = self.assertEquals
440     neq = self.assertNotEqual
441    
442     session = load_session(self.filename())
443     self.session = session
444    
445     map = self.session.Maps()[0] # only one map in the sample
446    
447     layer = map.Layers()[0] # one layer in the sample
448    
449     eq(layer.Title(), "My RasterLayer")
450     self.failIf(layer.Visible())
451     self.failUnless(filenames_equal(layer.GetImageFilename(),
452     os.path.join(self.temp_dir(),
453     os.pardir, os.pardir,
454     "Data", "iceland",
455     "island.tif")))
456    
457     class TestJoinedTable(LoadSessionTest):
458    
459     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
460     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
461     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" title="A Joined Table session">
462     <fileshapesource filetype="shapefile" id="D137227612"
463     filename="../../Data/iceland/roads-line.shp"/>
464     <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
465     title="Some Title"/>
466     <jointable leftcolumn="RDLNTYPE" right="D136171140"
467     title="Joined" rightcolumn="RDTYPE" id="D136169900" left="D137227612"/>
468     <derivedshapesource table="D136169900" shapesource="D137227612"
469     id="D136170932"/>
470     <map title="Test Map">
471     <layer shapestore="D136170932" visible="true" stroke="#000000"
472     title="My Layer" stroke_width="1" fill="None"/>
473     </map>
474     </session>
475     '''
476    
477     def setUp(self):
478     """Extend inherited method to create the dbffile for the join"""
479     LoadSessionTest.setUp(self)
480     dbffile = self.temp_file_name("load_joinedtable.dbf")
481     dbf = dbflib.create(dbffile)
482     dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
483     dbf.add_field("TEXT", dbflib.FTString, 10, 0)
484     dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
485     dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
486     dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
487     dbf.close()
488    
489     def test(self):
490     """Test loading a session containing a joined table"""
491     session = load_session(self.filename())
492     self.session = session
493    
494     tables = session.Tables()
495     self.assertEquals(len(tables), 3)
496     # FIXME: The tests shouldn't assume a certain order of the tables
497     self.assertEquals(tables[0].Title(), "Some Title")
498     self.assertEquals(tables[1].Title(), "Joined")
499     self.assertEquals(tables[1].JoinType(), "INNER")
500    
501    
502     class TestLoadError(LoadSessionTest):
503    
504     file_contents = '''\
505     <?xml version="1.0" encoding="UTF-8"?>
506     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
507     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
508     title="single map&amp;layer">
509     <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
510     <map title="Test Map">
511     <projection name="Unknown">
512     <parameter value="zone=26"/>
513     <parameter value="proj=utm"/>
514     <parameter value="ellps=clrk66"/>
515     </projection>
516     <layer shapestore="D1" visible="true"
517     stroke="#000000" title="My Layer" stroke_width="1"
518     fill="None"/>
519     </map>
520     </session>
521     '''
522    
523     def test(self):
524     """Test loading a session missing a required attribute"""
525     # Don't use assertRaises to make sure that if a session is
526     # actually returned it gets destroyed properly.
527     try:
528     self.session = load_session(self.filename())
529     except LoadError, value:
530     pass
531     else:
532     self.fail("Missing filetype attribute doesn't raise LoadError")
533    
534 frank 1410
535     class TestUnicodeStrings(LoadSessionTest):
536    
537     file_contents = '''\
538     <?xml version="1.0" encoding="UTF-8"?>
539     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
540     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
541     title="Frida: Free Vector Geodata Osnabrück">
542     <fileshapesource filetype="shapefile" id="D1"
543     filename="../../Data/iceland/political.shp"/>
544     <map title="Osnabrück">
545     <projection name="Osnabrück Projection">
546     <parameter value="zone=26"/>
547     <parameter value="proj=utm"/>
548     <parameter value="ellps=clrk66"/>
549     </projection>
550     <layer shapestore="D1" visible="true"
551     stroke="#000000" title="Osnabrück Layer" stroke_width="1"
552     fill="None"/>
553     </map>
554     </session>
555     '''
556    
557     def test(self):
558     """Load a session with unicode strings"""
559     eq = self.assertEquals
560     session = load_session(self.filename())
561     self.session = session
562    
563     # Check the title
564     eq(session.Title(), "Frida: Free Vector Geodata Osnabrück")
565    
566     # Check the map's title
567     maps = session.Maps()
568     map = maps[0]
569     eq(map.Title(), "Osnabrück")
570    
571     # Check the layer's title
572     layers = map.Layers()
573     layer = layers[0]
574     eq(layer.Title(), "Osnabrück Layer")
575    
576     # Check the projection's title
577     projection = map.GetProjection()
578     eq(projection.GetName(), "Osnabrück Projection")
579    
580     self.session.Destroy()
581     self.session = None
582    
583    
584 bh 1375 if __name__ == "__main__":
585     unittest.main()

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26