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

Diff of /branches/WIP-pyshapelib-bramz/test/test_proj.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 333 by bh, Fri Sep 20 15:47:07 2002 UTC revision 1687 by bh, Fri Aug 29 10:02:16 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002 by Intevation GmbH  # Copyright (c) 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 14  __version__ = "$Revision$" Line 14  __version__ = "$Revision$"
14  # $Id$  # $Id$
15    
16  import unittest  import unittest
17    import os
18    
19  import support  import support
20  support.initthuban()  support.initthuban()
21    
22  from Thuban.Model.proj import Projection  from Thuban import _
23    from Thuban.Model.proj import Projection, ProjFile
24    
25    import Thuban.Model.resource as resource
26    
27    from xmlsupport import sax_eventlist
28    
29    from xml.sax import SAXParseException
30    
31    
32  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
# Line 28  class TestProjection(unittest.TestCase, Line 36  class TestProjection(unittest.TestCase,
36    
37      def test(self):      def test(self):
38          """Test Projection"""          """Test Projection"""
39          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          params = ["zone=26", "proj=utm", "ellps=clrk66"]
40          self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(params)
41            self.assertEquals(proj.params, params)
42    
43          # It's not clear whether this value is really the correct one          # It's not clear whether this value is really the correct one
44          # but a test failure here probably still means a bug somewhere          # but a test failure here probably still means a bug somewhere
# Line 44  class TestProjection(unittest.TestCase, Line 53  class TestProjection(unittest.TestCase,
53                                    3875381.8535437919, 252962.10480170773),                                    3875381.8535437919, 252962.10480170773),
54                                   epsilon = 1e-5)                                   epsilon = 1e-5)
55    
56            # GetName()
57            self.assertEquals(proj.GetName(), _("Unknown"))
58    
59            # GetParameter()
60            self.assertEquals(proj.GetParameter("zone"), "26")
61            self.assertEquals(proj.GetParameter("proj"), "utm")
62            self.assertEquals(proj.GetParameter("ellps"), "clrk66")
63            self.assertEquals(proj.GetParameter("hallo"), "")
64    
65            # GetAllParameters()
66            self.assertEquals(proj.GetAllParameters(), params)
67    
68            # GetName()
69            proj = Projection(params, "MyName")
70            self.assertEquals(proj.GetName(), "MyName")
71    
72    
73    sample_projfile = '''\
74    <?xml version="1.0" encoding="UTF-8"?>
75    <!DOCTYPE projfile SYSTEM "thuban.dtd">
76    <projectionlist>
77        <projection name="Transverse Mercartor">
78            <parameter value="proj=tmerc"/>
79            <parameter value="ellps=clrk66"/>
80            <parameter value="lat_0=90w"/>
81            <parameter value="lon_0=90w"/>
82            <parameter value="k=1"/>
83        </projection>
84        <projection name="Transverse Mercartor">
85            <parameter value="proj=tmerc"/>
86            <parameter value="ellps=clrk66"/>
87            <parameter value="lat_0=30w"/>
88            <parameter value="lon_0=30w"/>
89            <parameter value="k=1"/>
90        </projection>
91        <projection name="Universal Transverse Mercartor">
92            <parameter value="proj=utm"/>
93            <parameter value="ellps=clrk66"/>
94            <parameter value="zone=1"/>
95        </projection>
96    </projectionlist>
97    '''
98    
99    sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc",
100                                                      "ellps=clrk66",
101                                                      "lat_0=90w",
102                                                      "lon_0=90w",
103                                                      "k=1"]),
104                            ("Transverse Mercartor", ["proj=tmerc",
105                                                      "ellps=clrk66",
106                                                      "lat_0=30w",
107                                                      "lon_0=30w",
108                                                      "k=1"]),
109                            ("Universal Transverse Mercartor", ["proj=utm",
110                                                                "ellps=clrk66",
111                                                                "zone=1"])]
112    
113    sample_projfile2 = '''\
114    <?xml version="1.0" encoding="UTF-8"?>
115    <!DOCTYPE projfile SYSTEM "thuban.dtd">
116    <projectionlist>
117    </projectionlist>
118    '''
119    
120    sample_projfile_data2 = []
121    
122    class TestProjFile(unittest.TestCase, support.FileTestMixin):
123    
124        """Test cases for reading and writing projection files.
125        """
126    
127        def compare_xml(self, xml1, xml2):
128            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
129    
130        def test(self):
131            """Test ProjFile"""
132    
133            proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
134            proj1 = Projection(["proj=utm", "ellps=clrk66"])
135            proj2 = Projection(["proj=lcc", "ellps=clrk66",
136                                "lat_1=0", "lat_2=20"])
137    
138            eq = self.assertEquals
139    
140    
141            #
142            # __init__()
143            # GetFilename()
144            # SetFilename()
145            #
146            for name in ["", "hello_world"]:
147                projFile = ProjFile(name)
148                eq(projFile.GetFilename(), name)
149    
150                projFile.SetFilename("XXX")
151                projFile.SetFilename(name)
152                eq(projFile.GetFilename(), name)
153    
154            # initial number of projections should be 0
155            eq(len(projFile.GetProjections()), 0)
156    
157            #
158            # Add()
159            # Remove()
160            #
161            projFile.Add(proj0)
162            eq(len(projFile.GetProjections()), 1)
163            projFile.Remove(proj0)
164            eq(len(projFile.GetProjections()), 0)
165    
166            # try to remove something that doesn't exist
167            self.assertRaises(ValueError, projFile.Remove, None)
168            self.assertRaises(ValueError, projFile.Remove, proj0)
169    
170            projFile.Add(proj0)
171            projFile.Add(proj1)
172            projFile.Add(proj2)
173            eq(len(projFile.GetProjections()), 3)
174    
175            # GetProjections() -- tests order
176            projs = projFile.GetProjections()
177            eq(projs[0], proj0)
178            eq(projs[1], proj1)
179            eq(projs[2], proj2)
180    
181            projFile.Remove(proj2)
182            projFile.Remove(proj1)
183    
184            # Replace()
185            projFile.Replace(proj0, proj1)
186            projs = projFile.GetProjections()
187            eq(projs[0], proj1)
188    
189            # replace a non-existent projection
190            self.assertRaises(ValueError, projFile.Replace, None, proj2)
191            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
192    
193        def testRead(self):
194            """Test read_proj_file"""
195    
196            self.doTestRead(sample_projfile_data, sample_projfile)
197            self.doTestRead(sample_projfile_data2, sample_projfile2)
198    
199            #
200            # file doesn't exist
201            #
202            self.assertRaises(IOError,
203                resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
204    
205            #
206            # file isn't readable
207            #
208            filename = self.temp_file_name("projfile.proj")
209            file = open(filename, "w")
210            file.close()
211            os.chmod(filename, 0200) # write-only
212            self.assertRaises(IOError, resource.read_proj_file, filename)
213            os.chmod(filename, 0600) # read/write so we reuse the file
214    
215            #
216            # file has invalid XML (or none at all)
217            #
218            filename = self.temp_file_name("projfile.proj")
219            file = open(filename, "w")
220            file.close()
221    
222            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
223    
224        def testWrite(self):
225            """Test write_proj_file"""
226    
227            self.doTestWrite(sample_projfile_data, sample_projfile)
228            self.doTestWrite(sample_projfile_data2, sample_projfile2)
229    
230        def doTestWrite(self, data, expected):
231    
232            filename = self.temp_file_name("projfile.proj")
233    
234            pf = ProjFile(filename)
235            for proj in data:
236                pf.Add(Projection(proj[1], proj[0]))
237    
238            resource.write_proj_file(pf)
239    
240            file = open(filename)
241            written_contents = file.read()
242            file.close()
243            self.compare_xml(written_contents, expected)
244    
245        def doTestRead(self, data, input):
246    
247            filename = self.temp_file_name("projfile.proj")
248            file = open(filename, "w")
249            file.write(input)
250            file.close()
251    
252            pf = resource.read_proj_file(filename)
253    
254            eq = self.assertEquals
255    
256            eq(pf.GetFilename(), filename)
257    
258            for proj, d in zip(pf.GetProjections(), data):
259                eq(proj.GetName(), d[0])
260                for param in proj.GetAllParameters():
261                    self.assert_(param in d[1])
262    
263    
264  if __name__ == "__main__":  if __name__ == "__main__":

Legend:
Removed from v.333  
changed lines
  Added in v.1687

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26