/[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 1811 by bh, Mon Oct 13 13:36:34 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    
93    sample_projfile = '''\
94    <?xml version="1.0" encoding="UTF-8"?>
95    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
96    <projectionlist>
97        <projection name="Transverse Mercator">
98            <parameter value="proj=tmerc"/>
99            <parameter value="ellps=clrk66"/>
100            <parameter value="lat_0=90w"/>
101            <parameter value="lon_0=90w"/>
102            <parameter value="k=1"/>
103        </projection>
104        <projection name="Transverse Mercator">
105            <parameter value="proj=tmerc"/>
106            <parameter value="ellps=clrk66"/>
107            <parameter value="lat_0=30w"/>
108            <parameter value="lon_0=30w"/>
109            <parameter value="k=1"/>
110        </projection>
111        <projection name="Universal Transverse Mercator">
112            <parameter value="proj=utm"/>
113            <parameter value="ellps=clrk66"/>
114            <parameter value="zone=1"/>
115        </projection>
116    </projectionlist>
117    '''
118    
119    sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
120                                                      "ellps=clrk66",
121                                                      "lat_0=90w",
122                                                      "lon_0=90w",
123                                                      "k=1"]),
124                            ("Transverse Mercator", ["proj=tmerc",
125                                                      "ellps=clrk66",
126                                                      "lat_0=30w",
127                                                      "lon_0=30w",
128                                                      "k=1"]),
129                            ("Universal Transverse Mercator", ["proj=utm",
130                                                                "ellps=clrk66",
131                                                                "zone=1"])]
132    
133    sample_projfile2 = '''\
134    <?xml version="1.0" encoding="UTF-8"?>
135    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
136    <projectionlist>
137    </projectionlist>
138    '''
139    
140    sample_projfile_data2 = []
141    
142    class TestProjFile(unittest.TestCase, support.FileTestMixin,
143                       xmlsupport.ValidationTest):
144    
145        """Test cases for reading and writing projection files.
146        """
147    
148        def compare_xml(self, xml1, xml2):
149            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
150    
151        def test(self):
152            """Test ProjFile"""
153    
154            proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
155            proj1 = Projection(["proj=utm", "ellps=clrk66"])
156            proj2 = Projection(["proj=lcc", "ellps=clrk66",
157                                "lat_1=0", "lat_2=20"])
158    
159            eq = self.assertEquals
160    
161    
162            #
163            # __init__()
164            # GetFilename()
165            # SetFilename()
166            #
167            for name in ["", "hello_world"]:
168                projFile = ProjFile(name)
169                eq(projFile.GetFilename(), name)
170    
171                projFile.SetFilename("XXX")
172                projFile.SetFilename(name)
173                eq(projFile.GetFilename(), name)
174    
175            # initial number of projections should be 0
176            eq(len(projFile.GetProjections()), 0)
177    
178            #
179            # Add()
180            # Remove()
181            #
182            projFile.Add(proj0)
183            eq(len(projFile.GetProjections()), 1)
184            projFile.Remove(proj0)
185            eq(len(projFile.GetProjections()), 0)
186    
187            # try to remove something that doesn't exist
188            self.assertRaises(ValueError, projFile.Remove, None)
189            self.assertRaises(ValueError, projFile.Remove, proj0)
190    
191            projFile.Add(proj0)
192            projFile.Add(proj1)
193            projFile.Add(proj2)
194            eq(len(projFile.GetProjections()), 3)
195    
196            # GetProjections() -- tests order
197            projs = projFile.GetProjections()
198            eq(projs[0], proj0)
199            eq(projs[1], proj1)
200            eq(projs[2], proj2)
201    
202            projFile.Remove(proj2)
203            projFile.Remove(proj1)
204    
205            # Replace()
206            projFile.Replace(proj0, proj1)
207            projs = projFile.GetProjections()
208            eq(projs[0], proj1)
209    
210            # replace a non-existent projection
211            self.assertRaises(ValueError, projFile.Replace, None, proj2)
212            self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
213    
214        def testRead(self):
215            """Test read_proj_file"""
216            self.doTestRead(sample_projfile_data, sample_projfile)
217            self.doTestRead(sample_projfile_data2, sample_projfile2)
218    
219        def test_read_non_existing_file(self):
220            """Test read_proj_file with non-existing file"""
221            self.assertRaises(IOError,
222                              resource.read_proj_file,
223                              self.temp_file_name("nonexistent.proj"))
224    
225        def test_read_unreadable_file(self):
226            """Test read_proj_file with unreadable file
227    
228            As currently written this only works on unix-like systems and
229            not e.g. on MS Windows.
230            """
231            filename = self.temp_file_name("projfile.proj")
232            file = open(filename, "w")
233            file.close()
234            os.chmod(filename, 0200) # write-only
235            self.assertRaises(IOError, resource.read_proj_file, filename)
236            os.chmod(filename, 0600) # read/write so we reuse the file
237    
238        def test_read_empty_file(self):
239            """Test read_proj_file with empty file"""
240            filename = self.temp_file_name("projfile.proj")
241            file = open(filename, "w")
242            file.close()
243    
244            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
245    
246        def testWrite(self):
247            """Test write_proj_file"""
248    
249            self.doTestWrite(sample_projfile_data, sample_projfile)
250            self.doTestWrite(sample_projfile_data2, sample_projfile2)
251    
252        def doTestWrite(self, data, expected):
253    
254            filename = self.temp_file_name("projfile.proj")
255    
256            pf = ProjFile(filename)
257            for proj in data:
258                pf.Add(Projection(proj[1], proj[0]))
259    
260            resource.write_proj_file(pf)
261    
262            file = open(filename)
263            written_contents = file.read()
264            file.close()
265            self.compare_xml(written_contents, expected)
266            self.validate_data(written_contents)
267            self.validate_data(expected)
268    
269        def doTestRead(self, data, input):
270    
271            filename = self.temp_file_name("projfile.proj")
272            file = open(filename, "w")
273            file.write(input)
274            file.close()
275    
276            pf, warnings = resource.read_proj_file(filename)
277            self.assertEquals(warnings, [])
278    
279            eq = self.assertEquals
280    
281            eq(pf.GetFilename(), filename)
282    
283            for proj, d in zip(pf.GetProjections(), data):
284                eq(proj.GetName(), d[0])
285                for param in proj.GetAllParameters():
286                    self.assert_(param in d[1])
287    
288        def test_get_system_proj_file(self):
289            """Test resource.get_system_proj_file()
290    
291            This is primarily to test whether the system proj file contains
292            invalid projection paramers and whether the proj file is not
293            empty
294            """
295            projfile, warnings = resource.get_system_proj_file()
296            self.assertEquals(warnings, [])
297            self.assert_(len(projfile.GetProjections()) > 0)
298    
299    
300    class TestProjFileWithInvalidParameters(unittest.TestCase,
301                                            support.FileLoadTestCase):
302    
303        file_extension = ".proj"
304        file_contents = '''\
305    <?xml version="1.0" encoding="UTF-8"?>
306    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
307    <projectionlist>
308        <projection name="Universal Transverse Mercator">
309            <parameter value="proj=utm"/>
310            <parameter value="ellps=clrk66"/>
311            <!-- an invalid zone number to trigger the parameter checking
312                 in the proj library -->
313            <parameter value="zone=1000"/>
314        </projection>
315        <projection name="Transverse Mercator">
316            <parameter value="proj=tmerc"/>
317            <parameter value="ellps=clrk66"/>
318            <parameter value="lat_0=90w"/>
319            <parameter value="lon_0=90w"/>
320            <parameter value="k=1"/>
321        </projection>
322    </projectionlist>
323    '''
324    
325        def setUp(self):
326            support.FileLoadTestCase.setUp(self)
327    
328        def test(self):
329            """Test reading a proj file with invalid parameters"""
330            projfile, warnings = resource.read_proj_file(self.filename())
331            projs = projfile.GetProjections()
332            self.assertEquals(len(projs), 1)
333            params = projs[0].GetAllParameters()[:]
334            params.sort()
335            self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
336                                       'lon_0=90w', 'proj=tmerc'])
337            self.assertEquals(warnings,
338                           ['Error in projection "Universal Transverse Mercator":'
339                            ' invalid UTM zone number'])
340    
341    
342    
343  if __name__ == "__main__":  if __name__ == "__main__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26