/[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 1821 by bh, Tue Oct 14 13:54:03 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    
118    class TestProjFileSimple:
119    
120        def test_init(self):
121            """Test ProjFile coinstructor"""
122            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    
132    
133    class TestProjFile(unittest.TestCase):
134    
135        """Test cases for reading and writing projection files.
136        """
137    
138        def setUp(self):
139            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            # Replace()
165            proj_file.Replace(self.proj0, self.proj2)
166            self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1])
167    
168        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    
176    
177    class ProjFileTest(unittest.TestCase, support.FileTestMixin,
178                       xmlsupport.ValidationTest):
179    
180        """Base class for the proj file tests that read or write files"""
181    
182        def filename(self):
183            """Return the filename for the test"""
184            return self.temp_file_name(self.id() + ".proj")
185    
186    
187    class ProjFileReadTests(ProjFileTest):
188    
189        """Test read ProjFile objects from files"""
190    
191        def test_read_non_existing_file(self):
192            """Test read_proj_file with non-existing file"""
193            self.assertRaises(IOError,
194                              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            not e.g. on MS Windows.
202            """
203            filename = self.filename()
204            file = open(filename, "w")
205            file.close()
206            os.chmod(filename, 0200) # write-only
207            self.assertRaises(IOError, resource.read_proj_file, filename)
208    
209        def test_read_empty_file(self):
210            """Test read_proj_file with empty file"""
211            filename = self.filename()
212            file = open(filename, "w")
213            file.close()
214    
215            self.assertRaises(SAXParseException, resource.read_proj_file, filename)
216    
217        def test_get_system_proj_file(self):
218            """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    
228    
229    class WriteProjFileTests(ProjFileTest):
230    
231        """Test cases for writing proj files"""
232    
233        def compare_xml(self, xml1, xml2):
234            self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
235    
236        def doTestWrite(self, projfile, expected):
237            filename = self.filename()
238    
239            resource.write_proj_file(projfile)
240    
241            file = open(filename)
242            written_contents = file.read()
243            file.close()
244            self.compare_xml(written_contents, expected)
245            self.validate_data(written_contents)
246            self.validate_data(expected)
247    
248        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    
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,
371                                            support.FileLoadTestCase):
372    
373        file_extension = ".proj"
374        file_contents = '''\
375    <?xml version="1.0" encoding="UTF-8"?>
376    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
377    <projectionlist>
378        <projection name="Universal Transverse Mercator">
379            <parameter value="proj=utm"/>
380            <parameter value="ellps=clrk66"/>
381            <!-- an invalid zone number to trigger the parameter checking
382                 in the proj library -->
383            <parameter value="zone=1000"/>
384        </projection>
385        <projection name="Transverse Mercator">
386            <parameter value="proj=tmerc"/>
387            <parameter value="ellps=clrk66"/>
388            <parameter value="lat_0=90w"/>
389            <parameter value="lon_0=90w"/>
390            <parameter value="k=1"/>
391        </projection>
392    </projectionlist>
393    '''
394    
395        def setUp(self):
396            support.FileLoadTestCase.setUp(self)
397    
398        def test(self):
399            """Test reading a proj file with invalid parameters"""
400            projfile, warnings = resource.read_proj_file(self.filename())
401            projs = projfile.GetProjections()
402            self.assertEquals(len(projs), 1)
403            params = projs[0].GetAllParameters()[:]
404            params.sort()
405            self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
406                                       'lon_0=90w', 'proj=tmerc'])
407            self.assertEquals(warnings,
408                           ['Error in projection "Universal Transverse Mercator":'
409                            ' invalid UTM zone number'])
410    
411    
412  if __name__ == "__main__":  if __name__ == "__main__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26