/[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 1821 by bh, Tue Oct 14 13:54:03 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        def test_label_epsg(self):
99            """Test Projection.Label() with epsg"""
100            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
101                               name = "My Projection", epsg="42")
102            self.assertEquals(proj.Label(), "EPSG    42 My Projection")
103    
104        def test_epsgcode_for_non_epsg_projection(self):
105            """Test Projection.EPSGCode() without epsg"""
106            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
107                               name = "My Projection")
108            self.assertEquals(proj.EPSGCode(), None)
109    
110        def test_epsgcode_for_real_epsg_projection(self):
111            """Test Projection.EPSGCode() with epsg"""
112            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
113                               name = "My Projection", epsg="42")
114            self.assertEquals(proj.EPSGCode(), "42")
115    
 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>  
 '''  
116    
 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"])]  
117    
118  sample_projfile2 = '''\  class TestProjFileSimple:
119  <?xml version="1.0" encoding="UTF-8"?>  
120  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">      def test_init(self):
121  <projectionlist>          """Test ProjFile coinstructor"""
122  </projectionlist>          proj_file = ProjFile("some_filename")
123  '''          self.assertEquals(proj_file.GetFilename(), "some_filename")
124            self.assertEquals(len(proj_file.GetProjections()), 0)
125    
126        def test_set_filename(self):
127            """Test ProjFile.SetFilename()"""
128            proj_file = ProjFile("some_filename")
129            proj.SetFilename("other_name")
130            self.assertEquals(proj_file.GetFilename(), "other_name")
131    
 sample_projfile_data2 = []  
132    
133  class ProjFileTest(unittest.TestCase, support.FileTestMixin):  class TestProjFile(unittest.TestCase):
134    
135      """Base class for the proj file tests"""      """Test cases for reading and writing projection files.
136        """
137    
138      def filename(self):      def setUp(self):
139          """Return the filename for the test"""          self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
140          return self.temp_file_name(self.id() + ".proj")          self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
141            self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
142                                     "lat_1=0", "lat_2=20"])
143    
144        def test_add_remove(self):
145            """Test ProjFile.Add() and ProjFile.Remove()"""
146            proj_file = ProjFile("some_filename")
147            proj_file.Add(self.proj0)
148            proj_file.Add(self.proj1)
149            self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1])
150            proj_file.Remove(self.proj0)
151            self.assertEquals(proj_file.GetProjections(), [self.proj1])
152    
153        def test_remove_non_existing(self):
154            """Test ProjFile.Remove(<proj not in projfile>)"""
155            proj_file = ProjFile("some_filename")
156            self.assertRaises(ValueError, proj_file.Remove, self.proj0)
157    
158        def test_replace(self):
159            """Test ProjFile.Replace()"""
160            proj_file = ProjFile("some_filename")
161            proj_file.Add(self.proj0)
162            proj_file.Add(self.proj1)
163    
164            # Replace()
165            proj_file.Replace(self.proj0, self.proj2)
166            self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1])
167    
168  class TestProjFile(ProjFileTest, xmlsupport.ValidationTest):      def test_replace_non_existing(self):
169            """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
170            proj_file = ProjFile("some_filename")
171            proj_file.Add(self.proj0)
172            proj_file.Add(self.proj1)
173            self.assertRaises(ValueError,
174                              proj_file.Replace, self.proj2, self.proj0)
175    
     """Test cases for reading and writing projection files.  
     """  
176    
177      def test(self):  class ProjFileTest(unittest.TestCase, support.FileTestMixin,
178          """Test ProjFile"""                     xmlsupport.ValidationTest):
179    
180          proj0 = Projection(["proj=tmerc", "ellps=clrk66"])      """Base class for the proj file tests that read or write files"""
         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)  
181    
182          projFile.Remove(proj2)      def filename(self):
183          projFile.Remove(proj1)          """Return the filename for the test"""
184            return self.temp_file_name(self.id() + ".proj")
185    
186          # Replace()  
187          projFile.Replace(proj0, proj1)  class ProjFileReadTests(ProjFileTest):
188          projs = projFile.GetProjections()  
189          eq(projs[0], proj1)      """Test read ProjFile objects from files"""
   
         # 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)  
190    
191      def test_read_non_existing_file(self):      def test_read_non_existing_file(self):
192          """Test read_proj_file with non-existing file"""          """Test read_proj_file with non-existing file"""
# Line 238  class TestProjFile(ProjFileTest, xmlsupp Line 205  class TestProjFile(ProjFileTest, xmlsupp
205          file.close()          file.close()
206          os.chmod(filename, 0200) # write-only          os.chmod(filename, 0200) # write-only
207          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  
208    
209      def test_read_empty_file(self):      def test_read_empty_file(self):
210          """Test read_proj_file with empty file"""          """Test read_proj_file with empty file"""
# Line 248  class TestProjFile(ProjFileTest, xmlsupp Line 214  class TestProjFile(ProjFileTest, xmlsupp
214    
215          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
216    
     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])  
   
217      def test_get_system_proj_file(self):      def test_get_system_proj_file(self):
218          """Test resource.get_system_proj_file()          """Test resource.get_system_proj_file()
219    
# Line 279  class TestProjFile(ProjFileTest, xmlsupp Line 226  class TestProjFile(ProjFileTest, xmlsupp
226          self.assert_(len(projfile.GetProjections()) > 0)          self.assert_(len(projfile.GetProjections()) > 0)
227    
228    
229  class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):  class WriteProjFileTests(ProjFileTest):
230    
231      """Test cases for writing proj files"""      """Test cases for writing proj files"""
232    
233      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
234          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
235    
236      def doTestWrite(self, data, expected):      def doTestWrite(self, projfile, expected):
237          filename = self.filename()          filename = self.filename()
238    
239          pf = ProjFile(filename)          resource.write_proj_file(projfile)
         for proj in data:  
             pf.Add(Projection(proj[1], proj[0]))  
   
         resource.write_proj_file(pf)  
240    
241          file = open(filename)          file = open(filename)
242          written_contents = file.read()          written_contents = file.read()
# Line 304  class WriteProjFileTests(ProjFileTest, x Line 247  class WriteProjFileTests(ProjFileTest, x
247    
248      def test_write(self):      def test_write(self):
249          """Test write_proj_file"""          """Test write_proj_file"""
250          self.doTestWrite(sample_projfile_data, sample_projfile)          pf = ProjFile(self.filename())
251            pf.Add(Projection(['proj=tmerc', 'ellps=clrk66',
252                               'lat_0=90w', 'lon_0=90w', 'k=1'],
253                              "Transverse Mercator",))
254            pf.Add(Projection(["proj=tmerc",
255                               "lat_0=0.000000000", "lon_0=-62.000000000",
256                               "k=0.999500", "x_0=400000.000", "y_0=0.000",
257                               "ellps=clrk80", "units=m"],
258                              "Anguilla 1957 / British West Indies Grid",
259                              epsg="200"))
260            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
261            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
262            <projectionlist>
263                <projection name="Transverse Mercator">
264                    <parameter value="proj=tmerc"/>
265                    <parameter value="ellps=clrk66"/>
266                    <parameter value="lat_0=90w"/>
267                    <parameter value="lon_0=90w"/>
268                    <parameter value="k=1"/>
269                </projection>
270                <projection epsg="200"
271                            name="Anguilla 1957 / British West Indies Grid">
272                    <parameter value="proj=tmerc"/>
273                    <parameter value="lat_0=0.000000000"/>
274                    <parameter value="lon_0=-62.000000000"/>
275                    <parameter value="k=0.999500"/>
276                    <parameter value="x_0=400000.000"/>
277                    <parameter value="y_0=0.000"/>
278                    <parameter value="ellps=clrk80"/>
279                    <parameter value="units=m"/>
280                </projection>
281            </projectionlist>
282            '''
283            self.doTestWrite(pf, file_contents)
284    
285      def test_write_empty_file(self):      def test_write_empty_file(self):
286          """Test write empty ProjFile"""          """Test write empty ProjFile"""
287          self.doTestWrite(sample_projfile_data2, sample_projfile2)          pf = ProjFile(self.filename())
288            file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
289            <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
290            <projectionlist>
291            </projectionlist>
292            '''
293            self.doTestWrite(pf, file_contents)
294    
295    
296    class TestLoadingProjFile(support.FileLoadTestCase):
297    
298        file_extension = ".proj"
299        file_contents = '''\
300    <?xml version="1.0" encoding="UTF-8"?>
301    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
302    <projectionlist>
303        <projection name="Transverse Mercator">
304            <parameter value="proj=tmerc"/>
305            <parameter value="ellps=clrk66"/>
306            <parameter value="lat_0=90w"/>
307            <parameter value="lon_0=90w"/>
308            <parameter value="k=1"/>
309        </projection>
310        <projection epsg="200" name="Anguilla 1957 / British West Indies Grid">
311            <parameter value="proj=tmerc"/>
312            <parameter value="lat_0=0.000000000"/>
313            <parameter value="lon_0=-62.000000000"/>
314            <parameter value="k=0.999500"/>
315            <parameter value="x_0=400000.000"/>
316            <parameter value="y_0=0.000"/>
317            <parameter value="ellps=clrk80"/>
318            <parameter value="units=m"/>
319        </projection>
320    </projectionlist>
321    '''
322    
323        def check_projection(self, proj, label, parameters):
324            """Check the values of the proj's label and parameters"""
325            self.assertEquals(proj.Label(), label)
326            params = proj.GetAllParameters()[:]
327            params.sort()
328            self.assertEquals(params, parameters)
329    
330        def test(self):
331            projfile, warnings = resource.read_proj_file(self.filename())
332            # no warnings
333            self.assertEquals(warnings, [])
334    
335            # There are two projections
336            projs = projfile.GetProjections()
337            self.assertEquals(len(projs), 2)
338    
339            self.check_projection(projs[0],
340                                  "Transverse Mercator",
341                                  ['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w',
342                                   'proj=tmerc'])
343            self.check_projection(projs[1],
344                             "EPSG   200 Anguilla 1957 / British West Indies Grid",
345                                  ["ellps=clrk80", "k=0.999500",
346                                   "lat_0=0.000000000", "lon_0=-62.000000000",
347                                   "proj=tmerc", "units=m",
348                                   "x_0=400000.000", "y_0=0.000"])
349    
350    
351    class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):
352    
353        file_extension = ".proj"
354        file_contents = '''\
355    <?xml version="1.0" encoding="UTF-8"?>
356    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
357    <projectionlist>
358    </projectionlist>
359    '''
360    
361        def test(self):
362            projfile, warnings = resource.read_proj_file(self.filename())
363            # no warnings
364            self.assertEquals(warnings, [])
365    
366            # There are no projections
367            self.assertEquals(len(projfile.GetProjections()), 0)
368    
369    
370  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(unittest.TestCase,

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26