/[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 333 by bh, Fri Sep 20 15:47:07 2002 UTC revision 1818 by bh, Mon Oct 13 15:54:14 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  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
28    
29    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 28  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 44  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")
63            self.assertEquals(proj.GetParameter("proj"), "utm")
64            self.assertEquals(proj.GetParameter("ellps"), "clrk66")
65            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_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    
116    
117    class ProjFileTest(unittest.TestCase, support.FileTestMixin):
118    
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.
129        """
130    
131        def test(self):
132            """Test ProjFile"""
133    
134            proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
135            proj1 = Projection(["proj=utm", "ellps=clrk66"])
136            proj2 = Projection(["proj=lcc", "ellps=clrk66",
137                                "lat_1=0", "lat_2=20"])
138    
139            eq = self.assertEquals
140    
141    
142            #
143            # __init__()
144            # GetFilename()
145            # SetFilename()
146            #
147            for name in ["", "hello_world"]:
148                projFile = ProjFile(name)
149                eq(projFile.GetFilename(), name)
150    
151                projFile.SetFilename("XXX")
152                projFile.SetFilename(name)
153                eq(projFile.GetFilename(), name)
154    
155            # initial number of projections should be 0
156            eq(len(projFile.GetProjections()), 0)
157    
158            #
159            # Add()
160            # Remove()
161            #
162            projFile.Add(proj0)
163            eq(len(projFile.GetProjections()), 1)
164            projFile.Remove(proj0)
165            eq(len(projFile.GetProjections()), 0)
166    
167            # try to remove something that doesn't exist
168            self.assertRaises(ValueError, projFile.Remove, None)
169            self.assertRaises(ValueError, projFile.Remove, proj0)
170    
171            projFile.Add(proj0)
172            projFile.Add(proj1)
173            projFile.Add(proj2)
174            eq(len(projFile.GetProjections()), 3)
175    
176            # GetProjections() -- tests order
177            projs = projFile.GetProjections()
178            eq(projs[0], proj0)
179            eq(projs[1], proj1)
180            eq(projs[2], proj2)
181    
182            projFile.Remove(proj2)
183            projFile.Remove(proj1)
184    
185            # Replace()
186            projFile.Replace(proj0, proj1)
187            projs = projFile.GetProjections()
188            eq(projs[0], proj1)
189    
190            # replace a non-existent projection
191            self.assertRaises(ValueError, projFile.Replace, None, proj2)
192            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
193    
194        def test_read_non_existing_file(self):
195            """Test read_proj_file with non-existing file"""
196            self.assertRaises(IOError,
197                              resource.read_proj_file,
198                              self.temp_file_name("nonexistent.proj"))
199    
200        def test_read_unreadable_file(self):
201            """Test read_proj_file with unreadable file
202    
203            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")
208            file.close()
209            os.chmod(filename, 0200) # write-only
210            self.assertRaises(IOError, resource.read_proj_file, filename)
211    
212        def test_read_empty_file(self):
213            """Test read_proj_file with empty file"""
214            filename = self.filename()
215            file = open(filename, "w")
216            file.close()
217    
218            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
219    
220        def test_get_system_proj_file(self):
221            """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    
231    
232    class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest):
233    
234        """Test cases for writing proj files"""
235    
236        def compare_xml(self, xml1, xml2):
237            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
238    
239        def doTestWrite(self, projfile, expected):
240            filename = self.filename()
241    
242            resource.write_proj_file(projfile)
243    
244            file = open(filename)
245            written_contents = file.read()
246            file.close()
247            self.compare_xml(written_contents, expected)
248            self.validate_data(written_contents)
249            self.validate_data(expected)
250    
251        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    
298    
299    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, [])
337    
338            # There are two projections
339            projs = projfile.GetProjections()
340            self.assertEquals(len(projs), 2)
341    
342            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    
353    
354    class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):
355    
356        file_extension = ".proj"
357        file_contents = '''\
358    <?xml version="1.0" encoding="UTF-8"?>
359    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
360    <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, [])
368    
369            # There are no projections
370            self.assertEquals(len(projfile.GetProjections()), 0)
371    
372    
373    class TestProjFileWithInvalidParameters(unittest.TestCase,
374                                            support.FileLoadTestCase):
375    
376        file_extension = ".proj"
377        file_contents = '''\
378    <?xml version="1.0" encoding="UTF-8"?>
379    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
380    <projectionlist>
381        <projection name="Universal Transverse Mercator">
382            <parameter value="proj=utm"/>
383            <parameter value="ellps=clrk66"/>
384            <!-- an invalid zone number to trigger the parameter checking
385                 in the proj library -->
386            <parameter value="zone=1000"/>
387        </projection>
388        <projection name="Transverse Mercator">
389            <parameter value="proj=tmerc"/>
390            <parameter value="ellps=clrk66"/>
391            <parameter value="lat_0=90w"/>
392            <parameter value="lon_0=90w"/>
393            <parameter value="k=1"/>
394        </projection>
395    </projectionlist>
396    '''
397    
398        def setUp(self):
399            support.FileLoadTestCase.setUp(self)
400    
401        def test(self):
402            """Test reading a proj file with invalid parameters"""
403            projfile, warnings = resource.read_proj_file(self.filename())
404            projs = projfile.GetProjections()
405            self.assertEquals(len(projs), 1)
406            params = projs[0].GetAllParameters()[:]
407            params.sort()
408            self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
409                                       'lon_0=90w', 'proj=tmerc'])
410            self.assertEquals(warnings,
411                           ['Error in projection "Universal Transverse Mercator":'
412                            ' invalid UTM zone number'])
413    
414    
415  if __name__ == "__main__":  if __name__ == "__main__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26