/[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 1813 by bh, Mon Oct 13 14:01:37 2003 UTC revision 1831 by bh, Thu Oct 16 16:36:11 2003 UTC
# Line 23  support.initthuban() Line 23  support.initthuban()
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       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 89  class TestProjection(unittest.TestCase, Line 90  class TestProjection(unittest.TestCase,
90          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
91          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)          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    
 sample_projfile_data2 = []  
133    
134  class ProjFileTest(unittest.TestCase, support.FileTestMixin):  class TestProjFile(unittest.TestCase, support.SubscriberMixin):
135    
136      """Base class for the proj file tests"""      """Test cases for ProjFile objects"""
137    
138      def filename(self):      def setUp(self):
139          """Return the filename for the test"""          self.clear_messages()
140          return self.temp_file_name(self.id() + ".proj")          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    
 class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):  
197    
198      """Test cases for reading and writing projection files.  class ProjFileTest(unittest.TestCase, support.FileTestMixin,
199      """                     xmlsupport.ValidationTest):
200    
201      def test(self):      """Base class for the proj file tests that read or write files"""
202          """Test ProjFile"""  
203        def filename(self):
204            """Return the filename for the test"""
205            return self.temp_file_name(self.id() + ".proj")
206    
         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)  
207    
208          projFile.Remove(proj2)  class ProjFileReadTests(ProjFileTest):
         projFile.Remove(proj1)  
209    
210          # Replace()      """Test read ProjFile objects from files
211          projFile.Replace(proj0, proj1)  
212          projs = projFile.GetProjections()      The files only cover error handling and the system projection file.
213          eq(projs[0], proj1)      """
   
         # 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)  
214    
215      def test_read_non_existing_file(self):      def test_read_non_existing_file(self):
216          """Test read_proj_file with non-existing file"""          """Test read_proj_file with non-existing file"""
# Line 238  class TestProjFile(ProjFileTest, xmlsupp Line 229  class TestProjFile(ProjFileTest, xmlsupp
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):      def test_read_empty_file(self):
234          """Test read_proj_file with empty file"""          """Test read_proj_file with empty file"""
# Line 248  class TestProjFile(ProjFileTest, xmlsupp Line 238  class TestProjFile(ProjFileTest, xmlsupp
238    
239          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
240    
     def doTestRead(self, data, input):  
   
         filename = self.filename()  
         file = open(filename, "w")  
         file.write(input)  
         file.close()  
   
         pf, warnings = resource.read_proj_file(filename)  
         self.assertEquals(warnings, [])  
   
         eq = self.assertEquals  
   
         eq(pf.GetFilename(), filename)  
   
         for proj, d in zip(pf.GetProjections(), data):  
             eq(proj.GetName(), d[0])  
             for param in proj.GetAllParameters():  
                 self.assert_(param in d[1])  
   
241      def test_get_system_proj_file(self):      def test_get_system_proj_file(self):
242          """Test resource.get_system_proj_file()          """Test resource.get_system_proj_file()
243    
# Line 278  class TestProjFile(ProjFileTest, xmlsupp Line 249  class TestProjFile(ProjFileTest, xmlsupp
249          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
250          self.assert_(len(projfile.GetProjections()) > 0)          self.assert_(len(projfile.GetProjections()) > 0)
251    
252            # see whether it got cached and we get the same projfile object
253            # when we read the file again
254            projfile2, warnings = resource.get_system_proj_file()
255            self.assert_(projfile is projfile2)
256    
257    
258  class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):  class WriteProjFileTests(ProjFileTest):
259    
260      """Test cases for writing proj files"""      """Test cases for writing proj files"""
261    
262      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
263          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
264    
265      def doTestWrite(self, data, expected):      def doTestWrite(self, projfile, expected):
266          filename = self.filename()          filename = self.filename()
267    
268          pf = ProjFile(filename)          resource.write_proj_file(projfile)
         for proj in data:  
             pf.Add(Projection(proj[1], proj[0]))  
   
         resource.write_proj_file(pf)  
269    
270          file = open(filename)          file = open(filename)
271          written_contents = file.read()          written_contents = file.read()
# Line 304  class WriteProjFileTests(ProjFileTest, x Line 276  class WriteProjFileTests(ProjFileTest, x
276    
277      def test_write(self):      def test_write(self):
278          """Test write_proj_file"""          """Test write_proj_file"""
279          self.doTestWrite(sample_projfile_data, sample_projfile)          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):      def test_write_empty_file(self):
315          """Test write empty ProjFile"""          """Test write empty ProjFile"""
316          self.doTestWrite(sample_projfile_data2, sample_projfile2)          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    
324    
325    class ProjFileLoadTestCase(support.FileLoadTestCase):
326    
327  class TestProjFileWithInvalidParameters(unittest.TestCase,      """Base class for the test cases that read specific test files"""
                                         support.FileLoadTestCase):  
328    
329      file_extension = ".proj"      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, [])
373    
374            # There are two projections
375            projs = projfile.GetProjections()
376            self.assertEquals(len(projs), 2)
377    
378            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        def test_caching(self):
390            # test whether the projfile cache works
391            projfile, warnings = resource.read_proj_file(self.filename())
392            projfile2, warnings = resource.read_proj_file(self.filename())
393    
394            # Both projfiles should be the same object
395            self.assert_(projfile2 is projfile)
396    
397            # If we clear the cache we should get a new one.
398            resource.clear_proj_file_cache()
399            projfile3, warnings = resource.read_proj_file(self.filename())
400            self.assert_(projfile3 is not projfile)
401    
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, [])
416    
417            # There are no projections
418            self.assertEquals(len(projfile.GetProjections()), 0)
419    
420    
421    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 354  class TestProjFileWithInvalidParameters( Line 459  class TestProjFileWithInvalidParameters(
459    
460    
461  if __name__ == "__main__":  if __name__ == "__main__":
462      unittest.main()      support.run_tests()

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26