/[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 714 by jonathan, Wed Apr 23 08:47:05 2003 UTC revision 1813 by bh, Mon Oct 13 14:01:37 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 xmlsupport
20  import support  import support
21  support.initthuban()  support.initthuban()
22    
23  from Thuban.Model.proj import Projection, ProjFile  from Thuban import _
24    from Thuban.Model.proj import Projection, ProjFile, \
25         PROJ_UNITS_METERS, PROJ_UNITS_DEGREES
26    
27  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
28    
29  from test_save import sax_eventlist  from xmlsupport import sax_eventlist
30    
31    from xml.sax import SAXParseException
32    
33    
34  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
# Line 32  class TestProjection(unittest.TestCase, Line 38  class TestProjection(unittest.TestCase,
38    
39      def test(self):      def test(self):
40          """Test Projection"""          """Test Projection"""
41          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          params = ["zone=26", "proj=utm", "ellps=clrk66"]
42          self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(params)
43            self.assertEquals(proj.params, params)
44    
45          # It's not clear whether this value is really the correct one          # It's not clear whether this value is really the correct one
46          # but a test failure here probably still means a bug somewhere          # but a test failure here probably still means a bug somewhere
# Line 48  class TestProjection(unittest.TestCase, Line 55  class TestProjection(unittest.TestCase,
55                                    3875381.8535437919, 252962.10480170773),                                    3875381.8535437919, 252962.10480170773),
56                                   epsilon = 1e-5)                                   epsilon = 1e-5)
57    
58            # GetName()
59            self.assertEquals(proj.GetName(), _("Unknown"))
60    
61            # GetParameter()
62          self.assertEquals(proj.GetParameter("zone"), "26")          self.assertEquals(proj.GetParameter("zone"), "26")
63          self.assertEquals(proj.GetParameter("proj"), "utm")          self.assertEquals(proj.GetParameter("proj"), "utm")
64          self.assertEquals(proj.GetParameter("ellps"), "clrk66")          self.assertEquals(proj.GetParameter("ellps"), "clrk66")
65          self.assertEquals(proj.GetParameter("hallo"), "")          self.assertEquals(proj.GetParameter("hallo"), "")
66    
67            # GetAllParameters()
68            self.assertEquals(proj.GetAllParameters(), params)
69    
70            # GetName()
71            proj = Projection(params, "MyName")
72            self.assertEquals(proj.GetName(), "MyName")
73    
74        def test_get_parameter_without_equals_sign(self):
75            """Test Projection.GetParameter() for a parameter without '=' sign"""
76            proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"])
77            # The Projection class pretends that for parameters specified
78            # without a value the value is the same as the parameter name.
79            self.assertEquals(proj.GetParameter("south"), "south")
80    
81        def test_get_projection_units_geo(self):
82            """Test Projection.GetProjectedUnits() for geographic projection"""
83            proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
84                               "ellps=clrk66"])
85            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
86    
87        def test_get_projection_units_normal(self):
88            """Test Projection.GetProjectedUnits() for normal projection"""
89            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
90            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)
91    
92    
93  sample_projfile = '''\  sample_projfile = '''\
94  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
95  <!DOCTYPE projfile SYSTEM "thuban.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
96  <projectionlist>  <projectionlist>
97      <projection name="Transverse Mercartor">      <projection name="Transverse Mercator">
98          <parameter value="proj=tmerc"/>          <parameter value="proj=tmerc"/>
99          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
100          <parameter value="lat_0=90w"/>          <parameter value="lat_0=90w"/>
101          <parameter value="lon_0=90w"/>          <parameter value="lon_0=90w"/>
102          <parameter value="k=1"/>          <parameter value="k=1"/>
103      </projection>      </projection>
104      <projection name="Transverse Mercartor">      <projection name="Transverse Mercator">
105          <parameter value="proj=tmerc"/>          <parameter value="proj=tmerc"/>
106          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
107          <parameter value="lat_0=30w"/>          <parameter value="lat_0=30w"/>
108          <parameter value="lon_0=30w"/>          <parameter value="lon_0=30w"/>
109          <parameter value="k=1"/>          <parameter value="k=1"/>
110      </projection>      </projection>
111      <projection name="Universal Transverse Mercartor">      <projection name="Universal Transverse Mercator">
112          <parameter value="proj=utm"/>          <parameter value="proj=utm"/>
113          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
114          <parameter value="zone=1"/>          <parameter value="zone=1"/>
# Line 80  sample_projfile = '''\ Line 116  sample_projfile = '''\
116  </projectionlist>  </projectionlist>
117  '''  '''
118    
119  sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc",  sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
120                                                    "ellps=clrk66",                                                    "ellps=clrk66",
121                                                    "lat_0=90w",                                                    "lat_0=90w",
122                                                    "lon_0=90w",                                                    "lon_0=90w",
123                                                    "k=1"]),                                                    "k=1"]),
124                          ("Transverse Mercartor", ["proj=tmerc",                          ("Transverse Mercator", ["proj=tmerc",
125                                                    "ellps=clrk66",                                                    "ellps=clrk66",
126                                                    "lat_0=30w",                                                    "lat_0=30w",
127                                                    "lon_0=30w",                                                    "lon_0=30w",
128                                                    "k=1"]),                                                    "k=1"]),
129                          ("Universal Transverse Mercartor", ["proj=utm",                          ("Universal Transverse Mercator", ["proj=utm",
130                                                              "ellps=clrk66",                                                              "ellps=clrk66",
131                                                              "zone=1"])]                                                              "zone=1"])]
132    
133  sample_projfile2 = '''\  sample_projfile2 = '''\
134  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
135  <!DOCTYPE projfile SYSTEM "thuban.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
136  <projectionlist>  <projectionlist>
137  </projectionlist>  </projectionlist>
138  '''  '''
139    
140  sample_projfile_data2 = []  sample_projfile_data2 = []
141    
142  class TestProjFile(unittest.TestCase, support.FileTestMixin):  class ProjFileTest(unittest.TestCase, support.FileTestMixin):
143    
144        """Base class for the proj file tests"""
145    
146        def filename(self):
147            """Return the filename for the test"""
148            return self.temp_file_name(self.id() + ".proj")
149    
150    
151    class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):
152    
153      """Test cases for reading and writing projection files.      """Test cases for reading and writing projection files.
154      """      """
155    
156      def compare_xml(self, xml1, xml2):      def test(self):
157          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          """Test ProjFile"""
158    
159      def testRead(self):          proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
160          """Test ReadProjFile"""          proj1 = Projection(["proj=utm", "ellps=clrk66"])
161            proj2 = Projection(["proj=lcc", "ellps=clrk66",
162                                "lat_1=0", "lat_2=20"])
163    
164            eq = self.assertEquals
165    
166    
167            #
168            # __init__()
169            # GetFilename()
170            # SetFilename()
171            #
172            for name in ["", "hello_world"]:
173                projFile = ProjFile(name)
174                eq(projFile.GetFilename(), name)
175    
176                projFile.SetFilename("XXX")
177                projFile.SetFilename(name)
178                eq(projFile.GetFilename(), name)
179    
180            # initial number of projections should be 0
181            eq(len(projFile.GetProjections()), 0)
182    
183            #
184            # Add()
185            # Remove()
186            #
187            projFile.Add(proj0)
188            eq(len(projFile.GetProjections()), 1)
189            projFile.Remove(proj0)
190            eq(len(projFile.GetProjections()), 0)
191    
192            # try to remove something that doesn't exist
193            self.assertRaises(ValueError, projFile.Remove, None)
194            self.assertRaises(ValueError, projFile.Remove, proj0)
195    
196            projFile.Add(proj0)
197            projFile.Add(proj1)
198            projFile.Add(proj2)
199            eq(len(projFile.GetProjections()), 3)
200    
201            # GetProjections() -- tests order
202            projs = projFile.GetProjections()
203            eq(projs[0], proj0)
204            eq(projs[1], proj1)
205            eq(projs[2], proj2)
206    
207            projFile.Remove(proj2)
208            projFile.Remove(proj1)
209    
210            # Replace()
211            projFile.Replace(proj0, proj1)
212            projs = projFile.GetProjections()
213            eq(projs[0], proj1)
214    
215            # replace a non-existent projection
216            self.assertRaises(ValueError, projFile.Replace, None, proj2)
217            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
218    
219        def testRead(self):
220            """Test read_proj_file"""
221          self.doTestRead(sample_projfile_data, sample_projfile)          self.doTestRead(sample_projfile_data, sample_projfile)
222          self.doTestRead(sample_projfile_data2, sample_projfile2)          self.doTestRead(sample_projfile_data2, sample_projfile2)
223    
224      def testWrite(self):      def test_read_non_existing_file(self):
225          """Test WriteProjFile"""          """Test read_proj_file with non-existing file"""
226            self.assertRaises(IOError,
227                              resource.read_proj_file,
228                              self.temp_file_name("nonexistent.proj"))
229    
230        def test_read_unreadable_file(self):
231            """Test read_proj_file with unreadable file
232    
233            As currently written this only works on unix-like systems and
234            not e.g. on MS Windows.
235            """
236            filename = self.filename()
237            file = open(filename, "w")
238            file.close()
239            os.chmod(filename, 0200) # write-only
240            self.assertRaises(IOError, resource.read_proj_file, filename)
241            os.chmod(filename, 0600) # read/write so we reuse the file
242    
243        def test_read_empty_file(self):
244            """Test read_proj_file with empty file"""
245            filename = self.filename()
246            file = open(filename, "w")
247            file.close()
248    
249            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
250    
251          self.doTestWrite(sample_projfile_data, sample_projfile)      def doTestRead(self, data, input):
         self.doTestWrite(sample_projfile_data2, sample_projfile2)  
252    
253      def doTestWrite(self, data, expected):          filename = self.filename()
254            file = open(filename, "w")
255            file.write(input)
256            file.close()
257    
258            pf, warnings = resource.read_proj_file(filename)
259            self.assertEquals(warnings, [])
260    
261            eq = self.assertEquals
262    
263            eq(pf.GetFilename(), filename)
264    
265            for proj, d in zip(pf.GetProjections(), data):
266                eq(proj.GetName(), d[0])
267                for param in proj.GetAllParameters():
268                    self.assert_(param in d[1])
269    
270        def test_get_system_proj_file(self):
271            """Test resource.get_system_proj_file()
272    
273            This is primarily to test whether the system proj file contains
274            invalid projection paramers and whether the proj file is not
275            empty
276            """
277            projfile, warnings = resource.get_system_proj_file()
278            self.assertEquals(warnings, [])
279            self.assert_(len(projfile.GetProjections()) > 0)
280    
281          filename = self.temp_file_name("projfile.proj")  
282    class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):
283    
284        """Test cases for writing proj files"""
285    
286        def compare_xml(self, xml1, xml2):
287            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
288    
289        def doTestWrite(self, data, expected):
290            filename = self.filename()
291    
292          pf = ProjFile(filename)          pf = ProjFile(filename)
293          for proj in data:          for proj in data:
294              pf.Add(Projection(proj[1], proj[0]))              pf.Add(Projection(proj[1], proj[0]))
295    
296          resource.WriteProjFile(pf)          resource.write_proj_file(pf)
297    
298          file = open(filename)          file = open(filename)
299          written_contents = file.read()          written_contents = file.read()
300          file.close()          file.close()
301          self.compare_xml(written_contents, expected)          self.compare_xml(written_contents, expected)
302            self.validate_data(written_contents)
303            self.validate_data(expected)
304    
305      def doTestRead(self, data, input):      def test_write(self):
306            """Test write_proj_file"""
307            self.doTestWrite(sample_projfile_data, sample_projfile)
308    
309          filename = self.temp_file_name("projfile.proj")      def test_write_empty_file(self):
310          file = open(filename, "w")          """Test write empty ProjFile"""
311          file.write(input)          self.doTestWrite(sample_projfile_data2, sample_projfile2)
         file.close()  
312    
         pf = resource.ReadProjFile(filename)  
313    
314          eq = self.assertEquals  class TestProjFileWithInvalidParameters(unittest.TestCase,
315                                            support.FileLoadTestCase):
316    
317          eq(pf.GetFileName(), filename)      file_extension = ".proj"
318        file_contents = '''\
319    <?xml version="1.0" encoding="UTF-8"?>
320    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
321    <projectionlist>
322        <projection name="Universal Transverse Mercator">
323            <parameter value="proj=utm"/>
324            <parameter value="ellps=clrk66"/>
325            <!-- an invalid zone number to trigger the parameter checking
326                 in the proj library -->
327            <parameter value="zone=1000"/>
328        </projection>
329        <projection name="Transverse Mercator">
330            <parameter value="proj=tmerc"/>
331            <parameter value="ellps=clrk66"/>
332            <parameter value="lat_0=90w"/>
333            <parameter value="lon_0=90w"/>
334            <parameter value="k=1"/>
335        </projection>
336    </projectionlist>
337    '''
338    
339          for proj, d in zip(pf.GetProjections(), data):      def setUp(self):
340              eq(proj.GetName(), d[0])          support.FileLoadTestCase.setUp(self)
341              for param in proj.GetAllParameters():  
342                  self.assert_(param in d[1])      def test(self):
343            """Test reading a proj file with invalid parameters"""
344            projfile, warnings = resource.read_proj_file(self.filename())
345            projs = projfile.GetProjections()
346            self.assertEquals(len(projs), 1)
347            params = projs[0].GetAllParameters()[:]
348            params.sort()
349            self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
350                                       'lon_0=90w', 'proj=tmerc'])
351            self.assertEquals(warnings,
352                           ['Error in projection "Universal Transverse Mercator":'
353                            ' invalid UTM zone number'])
354    
355    
356  if __name__ == "__main__":  if __name__ == "__main__":

Legend:
Removed from v.714  
changed lines
  Added in v.1813

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26