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