/[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 1787 by bh, Wed Oct 8 10:39:18 2003 UTC revision 1831 by bh, Thu Oct 16 16:36:11 2003 UTC
# Line 21  import support Line 21  import support
21  support.initthuban()  support.initthuban()
22    
23  from Thuban import _  from Thuban import _
24  from Thuban.Model.proj import Projection, ProjFile  from Thuban.Model.proj import Projection, ProjFile, \
25         PROJ_UNITS_METERS, PROJ_UNITS_DEGREES
26    from Thuban.Model.messages import PROJECTION_ADDED, PROJECTION_REMOVED, \
27         PROJECTION_REPLACED
28  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
29    
30  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
# Line 70  class TestProjection(unittest.TestCase, Line 72  class TestProjection(unittest.TestCase,
72          proj = Projection(params, "MyName")          proj = Projection(params, "MyName")
73          self.assertEquals(proj.GetName(), "MyName")          self.assertEquals(proj.GetName(), "MyName")
74    
75        def test_get_parameter_without_equals_sign(self):
76            """Test Projection.GetParameter() for a parameter without '=' sign"""
77            proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"])
78            # The Projection class pretends that for parameters specified
79            # without a value the value is the same as the parameter name.
80            self.assertEquals(proj.GetParameter("south"), "south")
81    
82        def test_get_projection_units_geo(self):
83            """Test Projection.GetProjectedUnits() for geographic projection"""
84            proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
85                               "ellps=clrk66"])
86            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
87    
88        def test_get_projection_units_normal(self):
89            """Test Projection.GetProjectedUnits() for normal projection"""
90            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
91            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)
92    
93        def test_label(self):
94            """Test Projection.Label() without epsg"""
95            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
96                               name = "My Projection")
97            self.assertEquals(proj.Label(), "My Projection")
98    
99        def test_label_epsg(self):
100            """Test Projection.Label() with epsg"""
101            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
102                               name = "My Projection", epsg="42")
103            self.assertEquals(proj.Label(), "EPSG    42 My Projection")
104    
105        def test_epsgcode_for_non_epsg_projection(self):
106            """Test Projection.EPSGCode() without epsg"""
107            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
108                               name = "My Projection")
109            self.assertEquals(proj.EPSGCode(), None)
110    
111        def test_epsgcode_for_real_epsg_projection(self):
112            """Test Projection.EPSGCode() with epsg"""
113            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
114                               name = "My Projection", epsg="42")
115            self.assertEquals(proj.EPSGCode(), "42")
116    
 sample_projfile = '''\  
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  
 <projectionlist>  
     <projection name="Transverse Mercator">  
         <parameter value="proj=tmerc"/>  
         <parameter value="ellps=clrk66"/>  
         <parameter value="lat_0=90w"/>  
         <parameter value="lon_0=90w"/>  
         <parameter value="k=1"/>  
     </projection>  
     <projection name="Transverse Mercator">  
         <parameter value="proj=tmerc"/>  
         <parameter value="ellps=clrk66"/>  
         <parameter value="lat_0=30w"/>  
         <parameter value="lon_0=30w"/>  
         <parameter value="k=1"/>  
     </projection>  
     <projection name="Universal Transverse Mercator">  
         <parameter value="proj=utm"/>  
         <parameter value="ellps=clrk66"/>  
         <parameter value="zone=1"/>  
     </projection>  
 </projectionlist>  
 '''  
117    
 sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",  
                                                   "ellps=clrk66",  
                                                   "lat_0=90w",  
                                                   "lon_0=90w",  
                                                   "k=1"]),  
                         ("Transverse Mercator", ["proj=tmerc",  
                                                   "ellps=clrk66",  
                                                   "lat_0=30w",  
                                                   "lon_0=30w",  
                                                   "k=1"]),  
                         ("Universal Transverse Mercator", ["proj=utm",  
                                                             "ellps=clrk66",  
                                                             "zone=1"])]  
118    
119  sample_projfile2 = '''\  class TestProjFileSimple:
120  <?xml version="1.0" encoding="UTF-8"?>  
121  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">      def test_init(self):
122  <projectionlist>          """Test ProjFile coinstructor"""
123  </projectionlist>          proj_file = ProjFile("some_filename")
124  '''          self.assertEquals(proj_file.GetFilename(), "some_filename")
125            self.assertEquals(len(proj_file.GetProjections()), 0)
126    
127        def test_set_filename(self):
128            """Test ProjFile.SetFilename()"""
129            proj_file = ProjFile("some_filename")
130            proj.SetFilename("other_name")
131            self.assertEquals(proj_file.GetFilename(), "other_name")
132    
133    
134    class TestProjFile(unittest.TestCase, support.SubscriberMixin):
135    
136  sample_projfile_data2 = []      """Test cases for ProjFile objects"""
137    
138  class TestProjFile(unittest.TestCase, support.FileTestMixin,      def setUp(self):
139            self.clear_messages()
140            self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
141            self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
142            self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
143                                     "lat_1=0", "lat_2=20"])
144            self.proj_file = ProjFile("some_filename")
145            for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]:
146                self.proj_file.Subscribe(msg, self.subscribe_with_params, msg)
147    
148        def tearDown(self):
149            self.clear_messages()
150            self.proj_file.Destroy()
151    
152        def test_add_remove(self):
153            """Test ProjFile.Add() and ProjFile.Remove()"""
154            self.proj_file.Add(self.proj0)
155            self.proj_file.Add(self.proj1)
156            self.assertEquals(self.proj_file.GetProjections(),
157                              [self.proj0, self.proj1])
158            self.check_messages([(self.proj0, PROJECTION_ADDED),
159                                 (self.proj1, PROJECTION_ADDED)])
160            self.clear_messages()
161    
162            self.proj_file.Remove(self.proj0)
163            self.assertEquals(self.proj_file.GetProjections(), [self.proj1])
164            self.check_messages([(self.proj0, PROJECTION_REMOVED)])
165    
166        def test_remove_non_existing(self):
167            """Test ProjFile.Remove(<proj not in projfile>)"""
168            self.assertRaises(ValueError, self.proj_file.Remove, self.proj0)
169            # Nothing happened, so no messages should have been sent
170            self.check_messages([])
171    
172        def test_replace(self):
173            """Test ProjFile.Replace()"""
174            self.proj_file.Add(self.proj0)
175            self.proj_file.Add(self.proj1)
176            self.clear_messages()
177    
178            # Replace()
179            self.proj_file.Replace(self.proj0, self.proj2)
180            self.assertEquals(self.proj_file.GetProjections(),
181                              [self.proj2, self.proj1])
182            self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)])
183    
184        def test_replace_non_existing(self):
185            """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
186            self.proj_file.Add(self.proj0)
187            self.proj_file.Add(self.proj1)
188            self.clear_messages()
189            self.assertRaises(ValueError,
190                              self.proj_file.Replace, self.proj2, self.proj0)
191            # All projections should still be there
192            self.assertEquals(self.proj_file.GetProjections(),
193                              [self.proj0, self.proj1])
194            # Nothing happened, so no messages should have been sent
195            self.check_messages([])
196    
197    
198    class ProjFileTest(unittest.TestCase, support.FileTestMixin,
199                     xmlsupport.ValidationTest):                     xmlsupport.ValidationTest):
200    
201      """Test cases for reading and writing projection files.      """Base class for the proj file tests that read or write files"""
     """  
202    
203      def compare_xml(self, xml1, xml2):      def filename(self):
204          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          """Return the filename for the test"""
205            return self.temp_file_name(self.id() + ".proj")
206    
     def test(self):  
         """Test ProjFile"""  
207    
208          proj0 = Projection(["proj=tmerc", "ellps=clrk66"])  class ProjFileReadTests(ProjFileTest):
         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)  
209    
210          projFile.Remove(proj2)      """Test read ProjFile objects from files
         projFile.Remove(proj1)  
211    
212          # Replace()      The files only cover error handling and the system projection file.
213          projFile.Replace(proj0, proj1)      """
214          projs = projFile.GetProjections()  
215          eq(projs[0], proj1)      def test_read_non_existing_file(self):
216            """Test read_proj_file with non-existing file"""
         # replace a non-existent projection  
         self.assertRaises(ValueError, projFile.Replace, None, proj2)  
         self.assertRaises(ValueError, projFile.Replace, proj0, proj2)  
   
     def testRead(self):  
         """Test read_proj_file"""  
   
         self.doTestRead(sample_projfile_data, sample_projfile)  
         self.doTestRead(sample_projfile_data2, sample_projfile2)  
   
         #  
         # file doesn't exist  
         #  
217          self.assertRaises(IOError,          self.assertRaises(IOError,
218              resource.read_proj_file, self.temp_file_name("nonexistent.proj"))                            resource.read_proj_file,
219                              self.temp_file_name("nonexistent.proj"))
220    
221        def test_read_unreadable_file(self):
222            """Test read_proj_file with unreadable file
223    
224          #          As currently written this only works on unix-like systems and
225          # file isn't readable          not e.g. on MS Windows.
226          #          """
227          filename = self.temp_file_name("projfile.proj")          filename = self.filename()
228          file = open(filename, "w")          file = open(filename, "w")
229          file.close()          file.close()
230          os.chmod(filename, 0200) # write-only          os.chmod(filename, 0200) # write-only
231          self.assertRaises(IOError, resource.read_proj_file, filename)          self.assertRaises(IOError, resource.read_proj_file, filename)
         os.chmod(filename, 0600) # read/write so we reuse the file  
232    
233          #      def test_read_empty_file(self):
234          # file has invalid XML (or none at all)          """Test read_proj_file with empty file"""
235          #          filename = self.filename()
         filename = self.temp_file_name("projfile.proj")  
236          file = open(filename, "w")          file = open(filename, "w")
237          file.close()          file.close()
238    
239          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
240    
241      def testWrite(self):      def test_get_system_proj_file(self):
242          """Test write_proj_file"""          """Test resource.get_system_proj_file()
243    
244            This is primarily to test whether the system proj file contains
245            invalid projection paramers and whether the proj file is not
246            empty
247            """
248            projfile, warnings = resource.get_system_proj_file()
249            self.assertEquals(warnings, [])
250            self.assert_(len(projfile.GetProjections()) > 0)
251    
252          self.doTestWrite(sample_projfile_data, sample_projfile)          # see whether it got cached and we get the same projfile object
253          self.doTestWrite(sample_projfile_data2, sample_projfile2)          # when we read the file again
254            projfile2, warnings = resource.get_system_proj_file()
255            self.assert_(projfile is projfile2)
256    
     def doTestWrite(self, data, expected):  
257    
258          filename = self.temp_file_name("projfile.proj")  class WriteProjFileTests(ProjFileTest):
259    
260          pf = ProjFile(filename)      """Test cases for writing proj files"""
261          for proj in data:  
262              pf.Add(Projection(proj[1], proj[0]))      def compare_xml(self, xml1, xml2):
263            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
264    
265          resource.write_proj_file(pf)      def doTestWrite(self, projfile, expected):
266            filename = self.filename()
267    
268            resource.write_proj_file(projfile)
269    
270          file = open(filename)          file = open(filename)
271          written_contents = file.read()          written_contents = file.read()
# Line 246  class TestProjFile(unittest.TestCase, su Line 274  class TestProjFile(unittest.TestCase, su
274          self.validate_data(written_contents)          self.validate_data(written_contents)
275          self.validate_data(expected)          self.validate_data(expected)
276    
277      def doTestRead(self, data, input):      def test_write(self):
278            """Test write_proj_file"""
279            pf = ProjFile(self.filename())
280            pf.Add(Projection(['proj=tmerc', 'ellps=clrk66',
281                               'lat_0=90w', 'lon_0=90w', 'k=1'],
282                              "Transverse Mercator",))
283            pf.Add(Projection(["proj=tmerc",
284                               "lat_0=0.000000000", "lon_0=-62.000000000",
285                               "k=0.999500", "x_0=400000.000", "y_0=0.000",
286                               "ellps=clrk80", "units=m"],
287                              "Anguilla 1957 / British West Indies Grid",
288                              epsg="200"))
289            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
290            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
291            <projectionlist>
292                <projection name="Transverse Mercator">
293                    <parameter value="proj=tmerc"/>
294                    <parameter value="ellps=clrk66"/>
295                    <parameter value="lat_0=90w"/>
296                    <parameter value="lon_0=90w"/>
297                    <parameter value="k=1"/>
298                </projection>
299                <projection epsg="200"
300                            name="Anguilla 1957 / British West Indies Grid">
301                    <parameter value="proj=tmerc"/>
302                    <parameter value="lat_0=0.000000000"/>
303                    <parameter value="lon_0=-62.000000000"/>
304                    <parameter value="k=0.999500"/>
305                    <parameter value="x_0=400000.000"/>
306                    <parameter value="y_0=0.000"/>
307                    <parameter value="ellps=clrk80"/>
308                    <parameter value="units=m"/>
309                </projection>
310            </projectionlist>
311            '''
312            self.doTestWrite(pf, file_contents)
313    
314        def test_write_empty_file(self):
315            """Test write empty ProjFile"""
316            pf = ProjFile(self.filename())
317            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
318            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
319            <projectionlist>
320            </projectionlist>
321            '''
322            self.doTestWrite(pf, file_contents)
323    
         filename = self.temp_file_name("projfile.proj")  
         file = open(filename, "w")  
         file.write(input)  
         file.close()  
324    
325          pf, warnings = resource.read_proj_file(filename)  class ProjFileLoadTestCase(support.FileLoadTestCase):
326    
327        """Base class for the test cases that read specific test files"""
328    
329        file_extension = ".proj"
330    
331        def tearDown(self):
332            """Clear the cache explicitly"""
333            resource.clear_proj_file_cache()
334    
335    
336    class TestLoadingProjFile(ProjFileLoadTestCase):
337    
338        file_contents = '''\
339    <?xml version="1.0" encoding="UTF-8"?>
340    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
341    <projectionlist>
342        <projection name="Transverse Mercator">
343            <parameter value="proj=tmerc"/>
344            <parameter value="ellps=clrk66"/>
345            <parameter value="lat_0=90w"/>
346            <parameter value="lon_0=90w"/>
347            <parameter value="k=1"/>
348        </projection>
349        <projection epsg="200" name="Anguilla 1957 / British West Indies Grid">
350            <parameter value="proj=tmerc"/>
351            <parameter value="lat_0=0.000000000"/>
352            <parameter value="lon_0=-62.000000000"/>
353            <parameter value="k=0.999500"/>
354            <parameter value="x_0=400000.000"/>
355            <parameter value="y_0=0.000"/>
356            <parameter value="ellps=clrk80"/>
357            <parameter value="units=m"/>
358        </projection>
359    </projectionlist>
360    '''
361    
362        def check_projection(self, proj, label, parameters):
363            """Check the values of the proj's label and parameters"""
364            self.assertEquals(proj.Label(), label)
365            params = proj.GetAllParameters()[:]
366            params.sort()
367            self.assertEquals(params, parameters)
368    
369        def test(self):
370            projfile, warnings = resource.read_proj_file(self.filename())
371            # no warnings
372          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
373    
374          eq = self.assertEquals          # There are two projections
375            projs = projfile.GetProjections()
376            self.assertEquals(len(projs), 2)
377    
378          eq(pf.GetFilename(), filename)          self.check_projection(projs[0],
379                                  "Transverse Mercator",
380                                  ['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w',
381                                   'proj=tmerc'])
382            self.check_projection(projs[1],
383                             "EPSG   200 Anguilla 1957 / British West Indies Grid",
384                                  ["ellps=clrk80", "k=0.999500",
385                                   "lat_0=0.000000000", "lon_0=-62.000000000",
386                                   "proj=tmerc", "units=m",
387                                   "x_0=400000.000", "y_0=0.000"])
388    
389          for proj, d in zip(pf.GetProjections(), data):      def test_caching(self):
390              eq(proj.GetName(), d[0])          # test whether the projfile cache works
391              for param in proj.GetAllParameters():          projfile, warnings = resource.read_proj_file(self.filename())
392                  self.assert_(param in d[1])          projfile2, warnings = resource.read_proj_file(self.filename())
393    
394      def test_get_system_proj_file(self):          # Both projfiles should be the same object
395          """Test resource.get_system_proj_file()          self.assert_(projfile2 is projfile)
396    
397          This is primarily to test whether the system proj file contains          # If we clear the cache we should get a new one.
398          invalid projection paramers and whether the proj file is not          resource.clear_proj_file_cache()
399          empty          projfile3, warnings = resource.read_proj_file(self.filename())
400          """          self.assert_(projfile3 is not projfile)
401          projfile, warnings = resource.get_system_proj_file()  
402    
403    class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase):
404    
405        file_contents = '''\
406    <?xml version="1.0" encoding="UTF-8"?>
407    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
408    <projectionlist>
409    </projectionlist>
410    '''
411    
412        def test(self):
413            projfile, warnings = resource.read_proj_file(self.filename())
414            # no warnings
415          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
         self.assert_(len(projfile.GetProjections()) > 0)  
416    
417            # There are no projections
418            self.assertEquals(len(projfile.GetProjections()), 0)
419    
 class TestProjFileWithInvalidParameters(unittest.TestCase,  
                                         support.FileLoadTestCase):  
420    
421      file_extension = ".proj"  class TestProjFileWithInvalidParameters(ProjFileLoadTestCase):
422    
423      file_contents = '''\      file_contents = '''\
424  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
425  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
# Line 319  class TestProjFileWithInvalidParameters( Line 458  class TestProjFileWithInvalidParameters(
458                          ' invalid UTM zone number'])                          ' invalid UTM zone number'])
459    
460    
   
461  if __name__ == "__main__":  if __name__ == "__main__":
462      unittest.main()      support.run_tests()

Legend:
Removed from v.1787  
changed lines
  Added in v.1831

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26