/[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 1687 - (hide annotations)
Fri Aug 29 10:02:16 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_load_0_8.py
File MIME type: text/x-python
File size: 20476 byte(s)
Add some missing parameters to projections. Proj complains about
them on windows but for some reason not on Linux.

* test/test_save.py (SaveSessionTest.testLayerProjection): Add
missing required projection parameters

* test/test_proj.py (TestProjFile.test): Add missing required
projection parameters

* test/test_load_0_8.py (TestLayerProjection.file_contents)
(TestLayerProjection.test): Add missing required projection
parameters and tests for them

* test/test_load.py (TestLayerProjection.file_contents)
(TestLayerProjection.test): Add missing required projection
parameters and tests for them

* test/test_layer.py (TestLayer.test_base_layer): Add missing
required projection parameters

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 bh 1687 <parameter value="lat_1=10"/>
390     <parameter value="lat_2=20"/>
391 bh 1375 <parameter value="ellps=clrk66"/>
392     </projection>
393     </layer>
394     </map>
395     </session>
396     '''
397    
398     def test(self):
399     """Test loading layers with projections"""
400     eq = self.assertEquals
401     neq = self.assertNotEqual
402    
403     session = load_session(self.filename())
404     self.session = session
405    
406     map = self.session.Maps()[0] # only one map in the sample
407    
408     layers = map.Layers() # two layers in the sample
409    
410     # test layer with a named projection
411     proj = layers[0].GetProjection()
412     neq(proj, None)
413     eq(proj.GetName(), "hello")
414     eq(proj.GetParameter("proj"), "tmerc")
415     eq(proj.GetParameter("zone"), "13")
416     eq(proj.GetParameter("ellps"), "clrk66")
417    
418     # test layer with an unnamed projection
419     proj = layers[1].GetProjection()
420     neq(proj, None)
421     eq(proj.GetName(), "Unknown")
422     eq(proj.GetParameter("proj"), "lcc")
423 bh 1687 eq(proj.GetParameter("lat_1"), "10")
424     eq(proj.GetParameter("lat_2"), "20")
425 bh 1375 eq(proj.GetParameter("ellps"), "clrk66")
426    
427    
428     class TestRasterLayer(LoadSessionTest):
429    
430     file_contents = '''\
431     <?xml version="1.0" encoding="UTF-8"?>
432     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
433     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
434     title="single map&amp;layer">
435     <map title="Test Map">
436     <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
437     title="My RasterLayer"/>
438     </map>
439     </session>
440     '''
441    
442     def test(self):
443     eq = self.assertEquals
444     neq = self.assertNotEqual
445    
446     session = load_session(self.filename())
447     self.session = session
448    
449     map = self.session.Maps()[0] # only one map in the sample
450    
451     layer = map.Layers()[0] # one layer in the sample
452    
453     eq(layer.Title(), "My RasterLayer")
454     self.failIf(layer.Visible())
455     self.failUnless(filenames_equal(layer.GetImageFilename(),
456     os.path.join(self.temp_dir(),
457     os.pardir, os.pardir,
458     "Data", "iceland",
459     "island.tif")))
460    
461     class TestJoinedTable(LoadSessionTest):
462    
463     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
464     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
465     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd" title="A Joined Table session">
466     <fileshapesource filetype="shapefile" id="D137227612"
467     filename="../../Data/iceland/roads-line.shp"/>
468     <filetable filetype="DBF" filename="load_joinedtable.dbf" id="D136171140"
469     title="Some Title"/>
470     <jointable leftcolumn="RDLNTYPE" right="D136171140"
471     title="Joined" rightcolumn="RDTYPE" id="D136169900" left="D137227612"/>
472     <derivedshapesource table="D136169900" shapesource="D137227612"
473     id="D136170932"/>
474     <map title="Test Map">
475     <layer shapestore="D136170932" visible="true" stroke="#000000"
476     title="My Layer" stroke_width="1" fill="None"/>
477     </map>
478     </session>
479     '''
480    
481     def setUp(self):
482     """Extend inherited method to create the dbffile for the join"""
483     LoadSessionTest.setUp(self)
484     dbffile = self.temp_file_name("load_joinedtable.dbf")
485     dbf = dbflib.create(dbffile)
486     dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
487     dbf.add_field("TEXT", dbflib.FTString, 10, 0)
488     dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
489     dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
490     dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
491     dbf.close()
492    
493     def test(self):
494     """Test loading a session containing a joined table"""
495     session = load_session(self.filename())
496     self.session = session
497    
498     tables = session.Tables()
499     self.assertEquals(len(tables), 3)
500     # FIXME: The tests shouldn't assume a certain order of the tables
501     self.assertEquals(tables[0].Title(), "Some Title")
502     self.assertEquals(tables[1].Title(), "Joined")
503     self.assertEquals(tables[1].JoinType(), "INNER")
504    
505    
506     class TestLoadError(LoadSessionTest):
507    
508     file_contents = '''\
509     <?xml version="1.0" encoding="UTF-8"?>
510     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
511     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
512     title="single map&amp;layer">
513     <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
514     <map title="Test Map">
515     <projection name="Unknown">
516     <parameter value="zone=26"/>
517     <parameter value="proj=utm"/>
518     <parameter value="ellps=clrk66"/>
519     </projection>
520     <layer shapestore="D1" visible="true"
521     stroke="#000000" title="My Layer" stroke_width="1"
522     fill="None"/>
523     </map>
524     </session>
525     '''
526    
527     def test(self):
528     """Test loading a session missing a required attribute"""
529     # Don't use assertRaises to make sure that if a session is
530     # actually returned it gets destroyed properly.
531     try:
532     self.session = load_session(self.filename())
533     except LoadError, value:
534     pass
535     else:
536     self.fail("Missing filetype attribute doesn't raise LoadError")
537    
538 frank 1410
539     class TestUnicodeStrings(LoadSessionTest):
540    
541     file_contents = '''\
542     <?xml version="1.0" encoding="UTF-8"?>
543     <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
544     <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
545     title="Frida: Free Vector Geodata Osnabrück">
546     <fileshapesource filetype="shapefile" id="D1"
547     filename="../../Data/iceland/political.shp"/>
548     <map title="Osnabrück">
549     <projection name="Osnabrück Projection">
550     <parameter value="zone=26"/>
551     <parameter value="proj=utm"/>
552     <parameter value="ellps=clrk66"/>
553     </projection>
554     <layer shapestore="D1" visible="true"
555     stroke="#000000" title="Osnabrück Layer" stroke_width="1"
556     fill="None"/>
557     </map>
558     </session>
559     '''
560    
561     def test(self):
562     """Load a session with unicode strings"""
563     eq = self.assertEquals
564     session = load_session(self.filename())
565     self.session = session
566    
567     # Check the title
568     eq(session.Title(), "Frida: Free Vector Geodata Osnabrück")
569    
570     # Check the map's title
571     maps = session.Maps()
572     map = maps[0]
573     eq(map.Title(), "Osnabrück")
574    
575     # Check the layer's title
576     layers = map.Layers()
577     layer = layers[0]
578     eq(layer.Title(), "Osnabrück Layer")
579    
580     # Check the projection's title
581     projection = map.GetProjection()
582     eq(projection.GetName(), "Osnabrück Projection")
583    
584     self.session.Destroy()
585     self.session = None
586    
587    
588 bh 1375 if __name__ == "__main__":
589     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