/[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

trunk/thuban/test/test_proj.py revision 1818 by bh, Mon Oct 13 15:54:14 2003 UTC branches/WIP-pyshapelib-bramz/test/test_proj.py revision 2734 by bramz, Thu Mar 1 12:42:59 2007 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003 by Intevation GmbH  # Copyright (c) 2002, 2003, 2006 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4    # Bernhard Reiter <[email protected]>
5  #  #
6  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
7  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 14  __version__ = "$Revision$" Line 15  __version__ = "$Revision$"
15  # $Id$  # $Id$
16    
17  import unittest  import unittest
18    import locale
19  import os  import os
20    
21    import localessupport
22  import xmlsupport  import xmlsupport
23  import support  import support
24  support.initthuban()  support.initthuban()
# Line 23  support.initthuban() Line 26  support.initthuban()
26  from Thuban import _  from Thuban import _
27  from Thuban.Model.proj import Projection, ProjFile, \  from Thuban.Model.proj import Projection, ProjFile, \
28       PROJ_UNITS_METERS, PROJ_UNITS_DEGREES       PROJ_UNITS_METERS, PROJ_UNITS_DEGREES
29    from Thuban.Model.messages import PROJECTION_ADDED, PROJECTION_REMOVED, \
30         PROJECTION_REPLACED
31  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
32    
33  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
# Line 54  class TestProjection(unittest.TestCase, Line 58  class TestProjection(unittest.TestCase,
58                                   (3620891.3077618643, 0.0,                                   (3620891.3077618643, 0.0,
59                                    3875381.8535437919, 252962.10480170773),                                    3875381.8535437919, 252962.10480170773),
60                                   epsilon = 1e-5)                                   epsilon = 1e-5)
61            self.assertFloatSeqEqual(proj.InverseBBox((3620891.3077618643, 0.0,
62                                                       3875381.8535437919,
63                                                       252962.10480170773)),
64                                     (-0.018341599754143501, 0.0,
65                                      2.017992992681688, 2.0377390677846736),
66                                     epsilon = 1e-5)
67    
68          # GetName()          # GetName()
69          self.assertEquals(proj.GetName(), _("Unknown"))          self.assertEquals(proj.GetName(), _("Unknown"))
# Line 79  class TestProjection(unittest.TestCase, Line 89  class TestProjection(unittest.TestCase,
89          self.assertEquals(proj.GetParameter("south"), "south")          self.assertEquals(proj.GetParameter("south"), "south")
90    
91      def test_get_projection_units_geo(self):      def test_get_projection_units_geo(self):
92          """Test Projection.GetProjectedUnits() for geographic projection"""          """Test Projection.GetProjectedUnits() for geographic projection.
93            Test for the alias 'longlat' as well.
94            """
95          proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",          proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
96                             "ellps=clrk66"])                             "ellps=clrk66"])
97          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
98            proj = Projection(["proj=longlat", "to_meter=0.017453292519943295",
99                               "ellps=clrk66"])
100            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
101    
102        def test_lc_numeric_robustness(self):
103            """Test if an LC_NUMERIC local with comma as decimal_point will work.
104    
105            Some versions of proj are not robust against this.
106            Starting with Python 2.4 there is a different behaviour when
107            calling C extensions and now they will see changes locales settings
108            which might tickle the bug in proj.
109            """
110            params = ["proj=latlong", "to_meter=0.01745", "ellps=clrk66"]
111    
112            oldlocale = localessupport.setdecimalcommalocale()
113            if oldlocale == None:
114                raise support.SkipTest(
115                        "No locale with comma as decimal_point found.")
116    
117            proj = Projection(params)
118            #print proj.work_around_broken_proj
119            result1 =  proj.Forward(1.2,3.2)
120    
121            locale.setlocale(locale.LC_NUMERIC, "C")
122            proj = Projection(params)
123            result2= proj.Forward(1.2,3.2)
124    
125            locale.setlocale(locale.LC_NUMERIC, oldlocale)
126            self.assertFloatSeqEqual(result1, result2, epsilon = 1e-5 )
127    
128      def test_get_projection_units_normal(self):      def test_get_projection_units_normal(self):
129          """Test Projection.GetProjectedUnits() for normal projection"""          """Test Projection.GetProjectedUnits() for normal projection"""
# Line 114  class TestProjection(unittest.TestCase, Line 155  class TestProjection(unittest.TestCase,
155          self.assertEquals(proj.EPSGCode(), "42")          self.assertEquals(proj.EPSGCode(), "42")
156    
157    
 class ProjFileTest(unittest.TestCase, support.FileTestMixin):  
158    
159      """Base class for the proj file tests"""  class TestProjFileSimple:
160    
161      def filename(self):      def test_init(self):
162          """Return the filename for the test"""          """Test ProjFile coinstructor"""
163          return self.temp_file_name(self.id() + ".proj")          proj_file = ProjFile("some_filename")
164            self.assertEquals(proj_file.GetFilename(), "some_filename")
165            self.assertEquals(len(proj_file.GetProjections()), 0)
166    
167        def test_set_filename(self):
168            """Test ProjFile.SetFilename()"""
169            proj_file = ProjFile("some_filename")
170            proj.SetFilename("other_name")
171            self.assertEquals(proj_file.GetFilename(), "other_name")
172    
 class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):  
173    
174      """Test cases for reading and writing projection files.  class TestProjFile(unittest.TestCase, support.SubscriberMixin):
     """  
175    
176      def test(self):      """Test cases for ProjFile objects"""
         """Test ProjFile"""  
   
         proj0 = Projection(["proj=tmerc", "ellps=clrk66"])  
         proj1 = Projection(["proj=utm", "ellps=clrk66"])  
         proj2 = Projection(["proj=lcc", "ellps=clrk66",  
                             "lat_1=0", "lat_2=20"])  
   
         eq = self.assertEquals  
   
   
         #  
         # __init__()  
         # GetFilename()  
         # SetFilename()  
         #  
         for name in ["", "hello_world"]:  
             projFile = ProjFile(name)  
             eq(projFile.GetFilename(), name)  
   
             projFile.SetFilename("XXX")  
             projFile.SetFilename(name)  
             eq(projFile.GetFilename(), name)  
   
         # initial number of projections should be 0  
         eq(len(projFile.GetProjections()), 0)  
   
         #  
         # Add()  
         # Remove()  
         #  
         projFile.Add(proj0)  
         eq(len(projFile.GetProjections()), 1)  
         projFile.Remove(proj0)  
         eq(len(projFile.GetProjections()), 0)  
   
         # try to remove something that doesn't exist  
         self.assertRaises(ValueError, projFile.Remove, None)  
         self.assertRaises(ValueError, projFile.Remove, proj0)  
   
         projFile.Add(proj0)  
         projFile.Add(proj1)  
         projFile.Add(proj2)  
         eq(len(projFile.GetProjections()), 3)  
   
         # GetProjections() -- tests order  
         projs = projFile.GetProjections()  
         eq(projs[0], proj0)  
         eq(projs[1], proj1)  
         eq(projs[2], proj2)  
177    
178          projFile.Remove(proj2)      def setUp(self):
179          projFile.Remove(proj1)          self.clear_messages()
180            self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
181            self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
182            self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
183                                     "lat_1=0", "lat_2=20"])
184            self.proj_file = ProjFile("some_filename")
185            for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]:
186                self.proj_file.Subscribe(msg, self.subscribe_with_params, msg)
187    
188        def tearDown(self):
189            self.clear_messages()
190            self.proj_file.Destroy()
191    
192        def test_add_remove(self):
193            """Test ProjFile.Add() and ProjFile.Remove()"""
194            self.proj_file.Add(self.proj0)
195            self.proj_file.Add(self.proj1)
196            self.assertEquals(self.proj_file.GetProjections(),
197                              [self.proj0, self.proj1])
198            self.check_messages([(self.proj0, PROJECTION_ADDED),
199                                 (self.proj1, PROJECTION_ADDED)])
200            self.clear_messages()
201    
202            self.proj_file.Remove(self.proj0)
203            self.assertEquals(self.proj_file.GetProjections(), [self.proj1])
204            self.check_messages([(self.proj0, PROJECTION_REMOVED)])
205    
206        def test_remove_non_existing(self):
207            """Test ProjFile.Remove(<proj not in projfile>)"""
208            self.assertRaises(ValueError, self.proj_file.Remove, self.proj0)
209            # Nothing happened, so no messages should have been sent
210            self.check_messages([])
211    
212        def test_replace(self):
213            """Test ProjFile.Replace()"""
214            self.proj_file.Add(self.proj0)
215            self.proj_file.Add(self.proj1)
216            self.clear_messages()
217    
218          # Replace()          # Replace()
219          projFile.Replace(proj0, proj1)          self.proj_file.Replace(self.proj0, self.proj2)
220          projs = projFile.GetProjections()          self.assertEquals(self.proj_file.GetProjections(),
221          eq(projs[0], proj1)                            [self.proj2, self.proj1])
222            self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)])
223          # replace a non-existent projection  
224          self.assertRaises(ValueError, projFile.Replace, None, proj2)      def test_replace_non_existing(self):
225          self.assertRaises(ValueError, projFile.Replace, proj0, proj2)          """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
226            self.proj_file.Add(self.proj0)
227            self.proj_file.Add(self.proj1)
228            self.clear_messages()
229            self.assertRaises(ValueError,
230                              self.proj_file.Replace, self.proj2, self.proj0)
231            # All projections should still be there
232            self.assertEquals(self.proj_file.GetProjections(),
233                              [self.proj0, self.proj1])
234            # Nothing happened, so no messages should have been sent
235            self.check_messages([])
236    
237    
238    class ProjFileTest(unittest.TestCase, support.FileTestMixin,
239                       xmlsupport.ValidationTest):
240    
241        """Base class for the proj file tests that read or write files"""
242    
243        def filename(self):
244            """Return the filename for the test"""
245            return self.temp_file_name(self.id() + ".proj")
246    
247    
248    class ProjFileReadTests(ProjFileTest):
249    
250        """Test read ProjFile objects from files
251    
252        The files only cover error handling and the system projection file.
253        """
254    
255      def test_read_non_existing_file(self):      def test_read_non_existing_file(self):
256          """Test read_proj_file with non-existing file"""          """Test read_proj_file with non-existing file"""
# Line 203  class TestProjFile(ProjFileTest, xmlsupp Line 264  class TestProjFile(ProjFileTest, xmlsupp
264          As currently written this only works on unix-like systems and          As currently written this only works on unix-like systems and
265          not e.g. on MS Windows.          not e.g. on MS Windows.
266          """          """
267            if os.name != "posix":
268                raise support.SkipTest("Test only works on posix systems")
269          filename = self.filename()          filename = self.filename()
270          file = open(filename, "w")          file = open(filename, "w")
271          file.close()          file.close()
# Line 218  class TestProjFile(ProjFileTest, xmlsupp Line 281  class TestProjFile(ProjFileTest, xmlsupp
281          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
282    
283      def test_get_system_proj_file(self):      def test_get_system_proj_file(self):
284          """Test resource.get_system_proj_file()          """Test resource.get_system_proj_file(DEFAULT_PROJ_FILE)
285    
286          This is primarily to test whether the system proj file contains          This is primarily to test whether the system proj file contains
287          invalid projection paramers and whether the proj file is not          invalid projection paramers and whether the proj file is not
288          empty          empty
289          """          """
290          projfile, warnings = resource.get_system_proj_file()          projfile, warnings\
291                      = resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE)
292          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
293          self.assert_(len(projfile.GetProjections()) > 0)          self.assert_(len(projfile.GetProjections()) > 0)
294    
295            # see whether it got cached and we get the same projfile object
296            # when we read the file again
297            projfile2, warnings \
298                       = resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE)
299            self.assert_(projfile is projfile2)
300    
301    
302  class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):  class WriteProjFileTests(ProjFileTest):
303    
304      """Test cases for writing proj files"""      """Test cases for writing proj files"""
305    
# Line 296  class WriteProjFileTests(ProjFileTest, x Line 366  class WriteProjFileTests(ProjFileTest, x
366          self.doTestWrite(pf, file_contents)          self.doTestWrite(pf, file_contents)
367    
368    
369  class TestLoadingProjFile(support.FileLoadTestCase):  class ProjFileLoadTestCase(support.FileLoadTestCase):
370    
371        """Base class for the test cases that read specific test files"""
372    
373      file_extension = ".proj"      file_extension = ".proj"
374    
375        def tearDown(self):
376            """Clear the cache explicitly"""
377            resource.clear_proj_file_cache()
378    
379    
380    class TestLoadingProjFile(ProjFileLoadTestCase):
381    
382      file_contents = '''\      file_contents = '''\
383  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
384  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 350  class TestLoadingProjFile(support.FileLo Line 430  class TestLoadingProjFile(support.FileLo
430                                 "proj=tmerc", "units=m",                                 "proj=tmerc", "units=m",
431                                 "x_0=400000.000", "y_0=0.000"])                                 "x_0=400000.000", "y_0=0.000"])
432    
433        def test_caching(self):
434            # test whether the projfile cache works
435            projfile, warnings = resource.read_proj_file(self.filename())
436            projfile2, warnings = resource.read_proj_file(self.filename())
437    
438  class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):          # Both projfiles should be the same object
439            self.assert_(projfile2 is projfile)
440    
441            # If we clear the cache we should get a new one.
442            resource.clear_proj_file_cache()
443            projfile3, warnings = resource.read_proj_file(self.filename())
444            self.assert_(projfile3 is not projfile)
445    
446    
447    class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase):
448    
     file_extension = ".proj"  
449      file_contents = '''\      file_contents = '''\
450  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
451  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 370  class TestLoadingProjFileWithEmptyProjec Line 462  class TestLoadingProjFileWithEmptyProjec
462          self.assertEquals(len(projfile.GetProjections()), 0)          self.assertEquals(len(projfile.GetProjections()), 0)
463    
464    
465  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(ProjFileLoadTestCase):
                                         support.FileLoadTestCase):  
466    
     file_extension = ".proj"  
467      file_contents = '''\      file_contents = '''\
468  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
469  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 413  class TestProjFileWithInvalidParameters( Line 503  class TestProjFileWithInvalidParameters(
503    
504    
505  if __name__ == "__main__":  if __name__ == "__main__":
506      unittest.main()      support.run_tests()

Legend:
Removed from v.1818  
changed lines
  Added in v.2734

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26