/[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 1821 by bh, Tue Oct 14 13:54:03 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    
27  import Thuban.Model.resource as resource  import Thuban.Model.resource as resource
28    
# Line 70  class TestProjection(unittest.TestCase, Line 71  class TestProjection(unittest.TestCase,
71          proj = Projection(params, "MyName")          proj = Projection(params, "MyName")
72          self.assertEquals(proj.GetName(), "MyName")          self.assertEquals(proj.GetName(), "MyName")
73    
74        def test_get_parameter_without_equals_sign(self):
75            """Test Projection.GetParameter() for a parameter without '=' sign"""
76            proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"])
77            # The Projection class pretends that for parameters specified
78            # without a value the value is the same as the parameter name.
79            self.assertEquals(proj.GetParameter("south"), "south")
80    
81        def test_get_projection_units_geo(self):
82            """Test Projection.GetProjectedUnits() for geographic projection"""
83            proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
84                               "ellps=clrk66"])
85            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
86    
87        def test_get_projection_units_normal(self):
88            """Test Projection.GetProjectedUnits() for normal projection"""
89            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
90            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 TestProjFile(unittest.TestCase, support.FileTestMixin,  class TestProjFile(unittest.TestCase):
                    xmlsupport.ValidationTest):  
134    
135      """Test cases for reading and writing projection files.      """Test cases for reading and writing projection files.
136      """      """
137    
138      def compare_xml(self, xml1, xml2):      def setUp(self):
139          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
140            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      def test(self):          # Replace()
165          """Test ProjFile"""          proj_file.Replace(self.proj0, self.proj2)
166            self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1])
167    
168          proj0 = Projection(["proj=tmerc", "ellps=clrk66"])      def test_replace_non_existing(self):
169          proj1 = Projection(["proj=utm", "ellps=clrk66"])          """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
170          proj2 = Projection(["proj=lcc", "ellps=clrk66",          proj_file = ProjFile("some_filename")
171                              "lat_1=0", "lat_2=20"])          proj_file.Add(self.proj0)
172            proj_file.Add(self.proj1)
173          eq = self.assertEquals          self.assertRaises(ValueError,
174                              proj_file.Replace, self.proj2, self.proj0)
   
         #  
         # __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)  
175    
         projFile.Remove(proj2)  
         projFile.Remove(proj1)  
176    
177          # Replace()  class ProjFileTest(unittest.TestCase, support.FileTestMixin,
178          projFile.Replace(proj0, proj1)                     xmlsupport.ValidationTest):
179          projs = projFile.GetProjections()  
180          eq(projs[0], proj1)      """Base class for the proj file tests that read or write files"""
181    
182          # replace a non-existent projection      def filename(self):
183          self.assertRaises(ValueError, projFile.Replace, None, proj2)          """Return the filename for the test"""
184          self.assertRaises(ValueError, projFile.Replace, proj0, proj2)          return self.temp_file_name(self.id() + ".proj")
185    
186      def testRead(self):  
187          """Test read_proj_file"""  class ProjFileReadTests(ProjFileTest):
188    
189          self.doTestRead(sample_projfile_data, sample_projfile)      """Test read ProjFile objects from files"""
190          self.doTestRead(sample_projfile_data2, sample_projfile2)  
191        def test_read_non_existing_file(self):
192          #          """Test read_proj_file with non-existing file"""
         # file doesn't exist  
         #  
193          self.assertRaises(IOError,          self.assertRaises(IOError,
194              resource.read_proj_file, self.temp_file_name("nonexistent.proj"))                            resource.read_proj_file,
195                              self.temp_file_name("nonexistent.proj"))
196    
197        def test_read_unreadable_file(self):
198            """Test read_proj_file with unreadable file
199    
200          #          As currently written this only works on unix-like systems and
201          # file isn't readable          not e.g. on MS Windows.
202          #          """
203          filename = self.temp_file_name("projfile.proj")          filename = self.filename()
204          file = open(filename, "w")          file = open(filename, "w")
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):
210          # file has invalid XML (or none at all)          """Test read_proj_file with empty file"""
211          #          filename = self.filename()
         filename = self.temp_file_name("projfile.proj")  
212          file = open(filename, "w")          file = open(filename, "w")
213          file.close()          file.close()
214    
215          self.assertRaises(SAXParseException, resource.read_proj_file, filename)          self.assertRaises(SAXParseException, resource.read_proj_file, filename)
216    
217      def testWrite(self):      def test_get_system_proj_file(self):
218          """Test write_proj_file"""          """Test resource.get_system_proj_file()
219    
220            This is primarily to test whether the system proj file contains
221            invalid projection paramers and whether the proj file is not
222            empty
223            """
224            projfile, warnings = resource.get_system_proj_file()
225            self.assertEquals(warnings, [])
226            self.assert_(len(projfile.GetProjections()) > 0)
227    
         self.doTestWrite(sample_projfile_data, sample_projfile)  
         self.doTestWrite(sample_projfile_data2, sample_projfile2)  
228    
229      def doTestWrite(self, data, expected):  class WriteProjFileTests(ProjFileTest):
230    
231          filename = self.temp_file_name("projfile.proj")      """Test cases for writing proj files"""
232    
233          pf = ProjFile(filename)      def compare_xml(self, xml1, xml2):
234          for proj in data:          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
             pf.Add(Projection(proj[1], proj[0]))  
235    
236          resource.write_proj_file(pf)      def doTestWrite(self, projfile, expected):
237            filename = self.filename()
238    
239            resource.write_proj_file(projfile)
240    
241          file = open(filename)          file = open(filename)
242          written_contents = file.read()          written_contents = file.read()
# Line 246  class TestProjFile(unittest.TestCase, su Line 245  class TestProjFile(unittest.TestCase, su
245          self.validate_data(written_contents)          self.validate_data(written_contents)
246          self.validate_data(expected)          self.validate_data(expected)
247    
248      def doTestRead(self, data, input):      def test_write(self):
249            """Test write_proj_file"""
250            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):
286            """Test write empty ProjFile"""
287            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    
         filename = self.temp_file_name("projfile.proj")  
         file = open(filename, "w")  
         file.write(input)  
         file.close()  
295    
296          pf, warnings = resource.read_proj_file(filename)  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, [])          self.assertEquals(warnings, [])
334    
335          eq = self.assertEquals          # There are two projections
336            projs = projfile.GetProjections()
337            self.assertEquals(len(projs), 2)
338    
339          eq(pf.GetFilename(), filename)          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    
         for proj, d in zip(pf.GetProjections(), data):  
             eq(proj.GetName(), d[0])  
             for param in proj.GetAllParameters():  
                 self.assert_(param in d[1])  
350    
351      def test_get_system_proj_file(self):  class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):
         """Test resource.get_system_proj_file()  
352    
353          This is primarily to test whether the system proj file contains      file_extension = ".proj"
354          invalid projection paramers and whether the proj file is not      file_contents = '''\
355          empty  <?xml version="1.0" encoding="UTF-8"?>
356          """  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
357          projfile, warnings = resource.get_system_proj_file()  <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, [])          self.assertEquals(warnings, [])
365          self.assert_(len(projfile.GetProjections()) > 0)  
366            # There are no projections
367            self.assertEquals(len(projfile.GetProjections()), 0)
368    
369    
370  class TestProjFileWithInvalidParameters(unittest.TestCase,  class TestProjFileWithInvalidParameters(unittest.TestCase,
# Line 319  class TestProjFileWithInvalidParameters( Line 409  class TestProjFileWithInvalidParameters(
409                          ' invalid UTM zone number'])                          ' invalid UTM zone number'])
410    
411    
   
412  if __name__ == "__main__":  if __name__ == "__main__":
413      unittest.main()      unittest.main()

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26