/[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 1798 by bh, Thu Oct 9 10:17:49 2003 UTC revision 1818 by bh, Mon Oct 13 15:54:14 2003 UTC
# Line 89  class TestProjection(unittest.TestCase, Line 89  class TestProjection(unittest.TestCase,
89          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
90          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)          self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)
91    
92        def test_label(self):
93            """Test Projection.Label() without epsg"""
94            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
95                               name = "My Projection")
96            self.assertEquals(proj.Label(), "My Projection")
97    
98  sample_projfile = '''\      def test_label_epsg(self):
99  <?xml version="1.0" encoding="UTF-8"?>          """Test Projection.Label() with epsg"""
100  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
101  <projectionlist>                             name = "My Projection", epsg="42")
102      <projection name="Transverse Mercator">          self.assertEquals(proj.Label(), "EPSG    42 My Projection")
         <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>  
 '''  
103    
104  sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",      def test_epsgcode_for_non_epsg_projection(self):
105                                                    "ellps=clrk66",          """Test Projection.EPSGCode() without epsg"""
106                                                    "lat_0=90w",          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
107                                                    "lon_0=90w",                             name = "My Projection")
108                                                    "k=1"]),          self.assertEquals(proj.EPSGCode(), None)
                         ("Transverse Mercator", ["proj=tmerc",  
                                                   "ellps=clrk66",  
                                                   "lat_0=30w",  
                                                   "lon_0=30w",  
                                                   "k=1"]),  
                         ("Universal Transverse Mercator", ["proj=utm",  
                                                             "ellps=clrk66",  
                                                             "zone=1"])]  
109    
110  sample_projfile2 = '''\      def test_epsgcode_for_real_epsg_projection(self):
111  <?xml version="1.0" encoding="UTF-8"?>          """Test Projection.EPSGCode() with epsg"""
112  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
113  <projectionlist>                             name = "My Projection", epsg="42")
114  </projectionlist>          self.assertEquals(proj.EPSGCode(), "42")
 '''  
115    
 sample_projfile_data2 = []  
116    
117  class TestProjFile(unittest.TestCase, support.FileTestMixin,  class ProjFileTest(unittest.TestCase, support.FileTestMixin):
118                     xmlsupport.ValidationTest):  
119        """Base class for the proj file tests"""
120    
121        def filename(self):
122            """Return the filename for the test"""
123            return self.temp_file_name(self.id() + ".proj")
124    
125    
126    class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):
127    
128      """Test cases for reading and writing projection files.      """Test cases for reading and writing projection files.
129      """      """
130    
     def compare_xml(self, xml1, xml2):  
         self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))  
   
131      def test(self):      def test(self):
132          """Test ProjFile"""          """Test ProjFile"""
133    
# Line 211  class TestProjFile(unittest.TestCase, su Line 191  class TestProjFile(unittest.TestCase, su
191          self.assertRaises(ValueError, projFile.Replace, None, proj2)          self.assertRaises(ValueError, projFile.Replace, None, proj2)
192          self.assertRaises(ValueError, projFile.Replace, proj0, proj2)          self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
193    
194      def testRead(self):      def test_read_non_existing_file(self):
195          """Test read_proj_file"""          """Test read_proj_file with non-existing file"""
   
         self.doTestRead(sample_projfile_data, sample_projfile)  
         self.doTestRead(sample_projfile_data2, sample_projfile2)  
   
         #  
         # file doesn't exist  
         #  
196          self.assertRaises(IOError,          self.assertRaises(IOError,
197              resource.read_proj_file, self.temp_file_name("nonexistent.proj"))                            resource.read_proj_file,
198                              self.temp_file_name("nonexistent.proj"))
199    
200          #      def test_read_unreadable_file(self):
201          # file isn't readable          """Test read_proj_file with unreadable file
202          #  
203          filename = self.temp_file_name("projfile.proj")          As currently written this only works on unix-like systems and
204            not e.g. on MS Windows.
205            """
206            filename = self.filename()
207          file = open(filename, "w")          file = open(filename, "w")
208          file.close()          file.close()
209          os.chmod(filename, 0200) # write-only          os.chmod(filename, 0200) # write-only
210          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  
211    
212          #      def test_read_empty_file(self):
213          # file has invalid XML (or none at all)          """Test read_proj_file with empty file"""
214          #          filename = self.filename()
         filename = self.temp_file_name("projfile.proj")  
215          file = open(filename, "w")          file = open(filename, "w")
216          file.close()          file.close()
217    
218          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
219    
220      def testWrite(self):      def test_get_system_proj_file(self):
221          """Test write_proj_file"""          """Test resource.get_system_proj_file()
222    
223            This is primarily to test whether the system proj file contains
224            invalid projection paramers and whether the proj file is not
225            empty
226            """
227            projfile, warnings = resource.get_system_proj_file()
228            self.assertEquals(warnings, [])
229            self.assert_(len(projfile.GetProjections()) > 0)
230    
         self.doTestWrite(sample_projfile_data, sample_projfile)  
         self.doTestWrite(sample_projfile_data2, sample_projfile2)  
231    
232      def doTestWrite(self, data, expected):  class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):
233    
234          filename = self.temp_file_name("projfile.proj")      """Test cases for writing proj files"""
235    
236          pf = ProjFile(filename)      def compare_xml(self, xml1, xml2):
237          for proj in data:          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
             pf.Add(Projection(proj[1], proj[0]))  
238    
239          resource.write_proj_file(pf)      def doTestWrite(self, projfile, expected):
240            filename = self.filename()
241    
242            resource.write_proj_file(projfile)
243    
244          file = open(filename)          file = open(filename)
245          written_contents = file.read()          written_contents = file.read()
# Line 265  class TestProjFile(unittest.TestCase, su Line 248  class TestProjFile(unittest.TestCase, su
248          self.validate_data(written_contents)          self.validate_data(written_contents)
249          self.validate_data(expected)          self.validate_data(expected)
250    
251      def doTestRead(self, data, input):      def test_write(self):
252            """Test write_proj_file"""
253            pf = ProjFile(self.filename())
254            pf.Add(Projection(['proj=tmerc', 'ellps=clrk66',
255                               'lat_0=90w', 'lon_0=90w', 'k=1'],
256                              "Transverse Mercator",))
257            pf.Add(Projection(["proj=tmerc",
258                               "lat_0=0.000000000", "lon_0=-62.000000000",
259                               "k=0.999500", "x_0=400000.000", "y_0=0.000",
260                               "ellps=clrk80", "units=m"],
261                              "Anguilla 1957 / British West Indies Grid",
262                              epsg="200"))
263            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
264            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
265            <projectionlist>
266                <projection name="Transverse Mercator">
267                    <parameter value="proj=tmerc"/>
268                    <parameter value="ellps=clrk66"/>
269                    <parameter value="lat_0=90w"/>
270                    <parameter value="lon_0=90w"/>
271                    <parameter value="k=1"/>
272                </projection>
273                <projection epsg="200"
274                            name="Anguilla 1957 / British West Indies Grid">
275                    <parameter value="proj=tmerc"/>
276                    <parameter value="lat_0=0.000000000"/>
277                    <parameter value="lon_0=-62.000000000"/>
278                    <parameter value="k=0.999500"/>
279                    <parameter value="x_0=400000.000"/>
280                    <parameter value="y_0=0.000"/>
281                    <parameter value="ellps=clrk80"/>
282                    <parameter value="units=m"/>
283                </projection>
284            </projectionlist>
285            '''
286            self.doTestWrite(pf, file_contents)
287    
288        def test_write_empty_file(self):
289            """Test write empty ProjFile"""
290            pf = ProjFile(self.filename())
291            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
292            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
293            <projectionlist>
294            </projectionlist>
295            '''
296            self.doTestWrite(pf, file_contents)
297    
         filename = self.temp_file_name("projfile.proj")  
         file = open(filename, "w")  
         file.write(input)  
         file.close()  
298    
299          pf, warnings = resource.read_proj_file(filename)  class TestLoadingProjFile(support.FileLoadTestCase):
300    
301        file_extension = ".proj"
302        file_contents = '''\
303    <?xml version="1.0" encoding="UTF-8"?>
304    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
305    <projectionlist>
306        <projection name="Transverse Mercator">
307            <parameter value="proj=tmerc"/>
308            <parameter value="ellps=clrk66"/>
309            <parameter value="lat_0=90w"/>
310            <parameter value="lon_0=90w"/>
311            <parameter value="k=1"/>
312        </projection>
313        <projection epsg="200" name="Anguilla 1957 / British West Indies Grid">
314            <parameter value="proj=tmerc"/>
315            <parameter value="lat_0=0.000000000"/>
316            <parameter value="lon_0=-62.000000000"/>
317            <parameter value="k=0.999500"/>
318            <parameter value="x_0=400000.000"/>
319            <parameter value="y_0=0.000"/>
320            <parameter value="ellps=clrk80"/>
321            <parameter value="units=m"/>
322        </projection>
323    </projectionlist>
324    '''
325    
326        def check_projection(self, proj, label, parameters):
327            """Check the values of the proj's label and parameters"""
328            self.assertEquals(proj.Label(), label)
329            params = proj.GetAllParameters()[:]
330            params.sort()
331            self.assertEquals(params, parameters)
332    
333        def test(self):
334            projfile, warnings = resource.read_proj_file(self.filename())
335            # no warnings
336          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
337    
338          eq = self.assertEquals          # There are two projections
339            projs = projfile.GetProjections()
340            self.assertEquals(len(projs), 2)
341    
342          eq(pf.GetFilename(), filename)          self.check_projection(projs[0],
343                                  "Transverse Mercator",
344                                  ['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w',
345                                   'proj=tmerc'])
346            self.check_projection(projs[1],
347                             "EPSG   200 Anguilla 1957 / British West Indies Grid",
348                                  ["ellps=clrk80", "k=0.999500",
349                                   "lat_0=0.000000000", "lon_0=-62.000000000",
350                                   "proj=tmerc", "units=m",
351                                   "x_0=400000.000", "y_0=0.000"])
352    
         for proj, d in zip(pf.GetProjections(), data):  
             eq(proj.GetName(), d[0])  
             for param in proj.GetAllParameters():  
                 self.assert_(param in d[1])  
353    
354      def test_get_system_proj_file(self):  class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):
         """Test resource.get_system_proj_file()  
355    
356          This is primarily to test whether the system proj file contains      file_extension = ".proj"
357          invalid projection paramers and whether the proj file is not      file_contents = '''\
358          empty  <?xml version="1.0" encoding="UTF-8"?>
359          """  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
360          projfile, warnings = resource.get_system_proj_file()  <projectionlist>
361    </projectionlist>
362    '''
363    
364        def test(self):
365            projfile, warnings = resource.read_proj_file(self.filename())
366            # no warnings
367          self.assertEquals(warnings, [])          self.assertEquals(warnings, [])
368          self.assert_(len(projfile.GetProjections()) > 0)  
369            # There are no projections
370            self.assertEquals(len(projfile.GetProjections()), 0)
371    
372    
373  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(unittest.TestCase,
# Line 338  class TestProjFileWithInvalidParameters( Line 412  class TestProjFileWithInvalidParameters(
412                          ' invalid UTM zone number'])                          ' invalid UTM zone number'])
413    
414    
   
415  if __name__ == "__main__":  if __name__ == "__main__":
416      unittest.main()      unittest.main()

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26