/[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 714 by jonathan, Wed Apr 23 08:47:05 2003 UTC revision 1793 by bh, Wed Oct 8 16:04:01 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002 by Intevation GmbH  # Copyright (c) 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 14  __version__ = "$Revision$" Line 14  __version__ = "$Revision$"
14  # $Id$  # $Id$
15    
16  import unittest  import unittest
17    import os
18    
19    import xmlsupport
20  import support  import support
21  support.initthuban()  support.initthuban()
22    
23  from Thuban.Model.proj import Projection, ProjFile  from Thuban import _
24    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    
29  from test_save import sax_eventlist  from xmlsupport import sax_eventlist
30    
31    from xml.sax import SAXParseException
32    
33    
34  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):  class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
# Line 32  class TestProjection(unittest.TestCase, Line 38  class TestProjection(unittest.TestCase,
38    
39      def test(self):      def test(self):
40          """Test Projection"""          """Test Projection"""
41          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          params = ["zone=26", "proj=utm", "ellps=clrk66"]
42          self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(params)
43            self.assertEquals(proj.params, params)
44    
45          # It's not clear whether this value is really the correct one          # It's not clear whether this value is really the correct one
46          # but a test failure here probably still means a bug somewhere          # but a test failure here probably still means a bug somewhere
# Line 48  class TestProjection(unittest.TestCase, Line 55  class TestProjection(unittest.TestCase,
55                                    3875381.8535437919, 252962.10480170773),                                    3875381.8535437919, 252962.10480170773),
56                                   epsilon = 1e-5)                                   epsilon = 1e-5)
57    
58            # GetName()
59            self.assertEquals(proj.GetName(), _("Unknown"))
60    
61            # GetParameter()
62          self.assertEquals(proj.GetParameter("zone"), "26")          self.assertEquals(proj.GetParameter("zone"), "26")
63          self.assertEquals(proj.GetParameter("proj"), "utm")          self.assertEquals(proj.GetParameter("proj"), "utm")
64          self.assertEquals(proj.GetParameter("ellps"), "clrk66")          self.assertEquals(proj.GetParameter("ellps"), "clrk66")
65          self.assertEquals(proj.GetParameter("hallo"), "")          self.assertEquals(proj.GetParameter("hallo"), "")
66    
67            # GetAllParameters()
68            self.assertEquals(proj.GetAllParameters(), params)
69    
70            # GetName()
71            proj = Projection(params, "MyName")
72            self.assertEquals(proj.GetName(), "MyName")
73    
74        def test_get_projection_units_geo(self):
75            """Test Projection.GetProjectedUnits() for geographic projection"""
76            proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
77                               "ellps=clrk66"])
78            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
79    
80        def test_get_projection_units_normal(self):
81            """Test Projection.GetProjectedUnits() for normal projection"""
82            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
83            self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)
84    
85    
86  sample_projfile = '''\  sample_projfile = '''\
87  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
88  <!DOCTYPE projfile SYSTEM "thuban.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
89  <projectionlist>  <projectionlist>
90      <projection name="Transverse Mercartor">      <projection name="Transverse Mercator">
91          <parameter value="proj=tmerc"/>          <parameter value="proj=tmerc"/>
92          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
93          <parameter value="lat_0=90w"/>          <parameter value="lat_0=90w"/>
94          <parameter value="lon_0=90w"/>          <parameter value="lon_0=90w"/>
95          <parameter value="k=1"/>          <parameter value="k=1"/>
96      </projection>      </projection>
97      <projection name="Transverse Mercartor">      <projection name="Transverse Mercator">
98          <parameter value="proj=tmerc"/>          <parameter value="proj=tmerc"/>
99          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
100          <parameter value="lat_0=30w"/>          <parameter value="lat_0=30w"/>
101          <parameter value="lon_0=30w"/>          <parameter value="lon_0=30w"/>
102          <parameter value="k=1"/>          <parameter value="k=1"/>
103      </projection>      </projection>
104      <projection name="Universal Transverse Mercartor">      <projection name="Universal Transverse Mercator">
105          <parameter value="proj=utm"/>          <parameter value="proj=utm"/>
106          <parameter value="ellps=clrk66"/>          <parameter value="ellps=clrk66"/>
107          <parameter value="zone=1"/>          <parameter value="zone=1"/>
# Line 80  sample_projfile = '''\ Line 109  sample_projfile = '''\
109  </projectionlist>  </projectionlist>
110  '''  '''
111    
112  sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc",  sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
113                                                    "ellps=clrk66",                                                    "ellps=clrk66",
114                                                    "lat_0=90w",                                                    "lat_0=90w",
115                                                    "lon_0=90w",                                                    "lon_0=90w",
116                                                    "k=1"]),                                                    "k=1"]),
117                          ("Transverse Mercartor", ["proj=tmerc",                          ("Transverse Mercator", ["proj=tmerc",
118                                                    "ellps=clrk66",                                                    "ellps=clrk66",
119                                                    "lat_0=30w",                                                    "lat_0=30w",
120                                                    "lon_0=30w",                                                    "lon_0=30w",
121                                                    "k=1"]),                                                    "k=1"]),
122                          ("Universal Transverse Mercartor", ["proj=utm",                          ("Universal Transverse Mercator", ["proj=utm",
123                                                              "ellps=clrk66",                                                              "ellps=clrk66",
124                                                              "zone=1"])]                                                              "zone=1"])]
125    
126  sample_projfile2 = '''\  sample_projfile2 = '''\
127  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
128  <!DOCTYPE projfile SYSTEM "thuban.dtd">  <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
129  <projectionlist>  <projectionlist>
130  </projectionlist>  </projectionlist>
131  '''  '''
132    
133  sample_projfile_data2 = []  sample_projfile_data2 = []
134    
135  class TestProjFile(unittest.TestCase, support.FileTestMixin):  class TestProjFile(unittest.TestCase, support.FileTestMixin,
136                       xmlsupport.ValidationTest):
137    
138      """Test cases for reading and writing projection files.      """Test cases for reading and writing projection files.
139      """      """
# Line 111  class TestProjFile(unittest.TestCase, su Line 141  class TestProjFile(unittest.TestCase, su
141      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
142          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))          self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
143    
144        def test(self):
145            """Test ProjFile"""
146    
147            proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
148            proj1 = Projection(["proj=utm", "ellps=clrk66"])
149            proj2 = Projection(["proj=lcc", "ellps=clrk66",
150                                "lat_1=0", "lat_2=20"])
151    
152            eq = self.assertEquals
153    
154    
155            #
156            # __init__()
157            # GetFilename()
158            # SetFilename()
159            #
160            for name in ["", "hello_world"]:
161                projFile = ProjFile(name)
162                eq(projFile.GetFilename(), name)
163    
164                projFile.SetFilename("XXX")
165                projFile.SetFilename(name)
166                eq(projFile.GetFilename(), name)
167    
168            # initial number of projections should be 0
169            eq(len(projFile.GetProjections()), 0)
170    
171            #
172            # Add()
173            # Remove()
174            #
175            projFile.Add(proj0)
176            eq(len(projFile.GetProjections()), 1)
177            projFile.Remove(proj0)
178            eq(len(projFile.GetProjections()), 0)
179    
180            # try to remove something that doesn't exist
181            self.assertRaises(ValueError, projFile.Remove, None)
182            self.assertRaises(ValueError, projFile.Remove, proj0)
183    
184            projFile.Add(proj0)
185            projFile.Add(proj1)
186            projFile.Add(proj2)
187            eq(len(projFile.GetProjections()), 3)
188    
189            # GetProjections() -- tests order
190            projs = projFile.GetProjections()
191            eq(projs[0], proj0)
192            eq(projs[1], proj1)
193            eq(projs[2], proj2)
194    
195            projFile.Remove(proj2)
196            projFile.Remove(proj1)
197    
198            # Replace()
199            projFile.Replace(proj0, proj1)
200            projs = projFile.GetProjections()
201            eq(projs[0], proj1)
202    
203            # replace a non-existent projection
204            self.assertRaises(ValueError, projFile.Replace, None, proj2)
205            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
206    
207      def testRead(self):      def testRead(self):
208          """Test ReadProjFile"""          """Test read_proj_file"""
209    
210          self.doTestRead(sample_projfile_data, sample_projfile)          self.doTestRead(sample_projfile_data, sample_projfile)
211          self.doTestRead(sample_projfile_data2, sample_projfile2)          self.doTestRead(sample_projfile_data2, sample_projfile2)
212    
213            #
214            # file doesn't exist
215            #
216            self.assertRaises(IOError,
217                resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
218    
219            #
220            # file isn't readable
221            #
222            filename = self.temp_file_name("projfile.proj")
223            file = open(filename, "w")
224            file.close()
225            os.chmod(filename, 0200) # write-only
226            self.assertRaises(IOError, resource.read_proj_file, filename)
227            os.chmod(filename, 0600) # read/write so we reuse the file
228    
229            #
230            # file has invalid XML (or none at all)
231            #
232            filename = self.temp_file_name("projfile.proj")
233            file = open(filename, "w")
234            file.close()
235    
236            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
237    
238      def testWrite(self):      def testWrite(self):
239          """Test WriteProjFile"""          """Test write_proj_file"""
240    
241          self.doTestWrite(sample_projfile_data, sample_projfile)          self.doTestWrite(sample_projfile_data, sample_projfile)
242          self.doTestWrite(sample_projfile_data2, sample_projfile2)          self.doTestWrite(sample_projfile_data2, sample_projfile2)
# Line 131  class TestProjFile(unittest.TestCase, su Line 249  class TestProjFile(unittest.TestCase, su
249          for proj in data:          for proj in data:
250              pf.Add(Projection(proj[1], proj[0]))              pf.Add(Projection(proj[1], proj[0]))
251    
252          resource.WriteProjFile(pf)          resource.write_proj_file(pf)
253    
254          file = open(filename)          file = open(filename)
255          written_contents = file.read()          written_contents = file.read()
256          file.close()          file.close()
257          self.compare_xml(written_contents, expected)          self.compare_xml(written_contents, expected)
258            self.validate_data(written_contents)
259            self.validate_data(expected)
260    
261      def doTestRead(self, data, input):      def doTestRead(self, data, input):
262    
# Line 145  class TestProjFile(unittest.TestCase, su Line 265  class TestProjFile(unittest.TestCase, su
265          file.write(input)          file.write(input)
266          file.close()          file.close()
267    
268          pf = resource.ReadProjFile(filename)          pf, warnings = resource.read_proj_file(filename)
269            self.assertEquals(warnings, [])
270    
271          eq = self.assertEquals          eq = self.assertEquals
272    
273          eq(pf.GetFileName(), filename)          eq(pf.GetFilename(), filename)
274    
275          for proj, d in zip(pf.GetProjections(), data):          for proj, d in zip(pf.GetProjections(), data):
276              eq(proj.GetName(), d[0])              eq(proj.GetName(), d[0])
277              for param in proj.GetAllParameters():              for param in proj.GetAllParameters():
278                  self.assert_(param in d[1])                  self.assert_(param in d[1])
279    
280        def test_get_system_proj_file(self):
281            """Test resource.get_system_proj_file()
282    
283            This is primarily to test whether the system proj file contains
284            invalid projection paramers and whether the proj file is not
285            empty
286            """
287            projfile, warnings = resource.get_system_proj_file()
288            self.assertEquals(warnings, [])
289            self.assert_(len(projfile.GetProjections()) > 0)
290    
291    
292    class TestProjFileWithInvalidParameters(unittest.TestCase,
293                                            support.FileLoadTestCase):
294    
295        file_extension = ".proj"
296        file_contents = '''\
297    <?xml version="1.0" encoding="UTF-8"?>
298    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
299    <projectionlist>
300        <projection name="Universal Transverse Mercator">
301            <parameter value="proj=utm"/>
302            <parameter value="ellps=clrk66"/>
303            <!-- an invalid zone number to trigger the parameter checking
304                 in the proj library -->
305            <parameter value="zone=1000"/>
306        </projection>
307        <projection name="Transverse Mercator">
308            <parameter value="proj=tmerc"/>
309            <parameter value="ellps=clrk66"/>
310            <parameter value="lat_0=90w"/>
311            <parameter value="lon_0=90w"/>
312            <parameter value="k=1"/>
313        </projection>
314    </projectionlist>
315    '''
316    
317        def setUp(self):
318            support.FileLoadTestCase.setUp(self)
319    
320        def test(self):
321            """Test reading a proj file with invalid parameters"""
322            projfile, warnings = resource.read_proj_file(self.filename())
323            projs = projfile.GetProjections()
324            self.assertEquals(len(projs), 1)
325            params = projs[0].GetAllParameters()[:]
326            params.sort()
327            self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
328                                       'lon_0=90w', 'proj=tmerc'])
329            self.assertEquals(warnings,
330                           ['Error in projection "Universal Transverse Mercator":'
331                            ' invalid UTM zone number'])
332    
333    
334    
335  if __name__ == "__main__":  if __name__ == "__main__":
336      unittest.main()      unittest.main()

Legend:
Removed from v.714  
changed lines
  Added in v.1793

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26