/[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 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  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_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 = '''\
87    <?xml version="1.0" encoding="UTF-8"?>
88    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
89    <projectionlist>
90        <projection name="Transverse Mercator">
91            <parameter value="proj=tmerc"/>
92            <parameter value="ellps=clrk66"/>
93            <parameter value="lat_0=90w"/>
94            <parameter value="lon_0=90w"/>
95            <parameter value="k=1"/>
96        </projection>
97        <projection name="Transverse Mercator">
98            <parameter value="proj=tmerc"/>
99            <parameter value="ellps=clrk66"/>
100            <parameter value="lat_0=30w"/>
101            <parameter value="lon_0=30w"/>
102            <parameter value="k=1"/>
103        </projection>
104        <projection name="Universal Transverse Mercator">
105            <parameter value="proj=utm"/>
106            <parameter value="ellps=clrk66"/>
107            <parameter value="zone=1"/>
108        </projection>
109    </projectionlist>
110    '''
111    
112    sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
113                                                      "ellps=clrk66",
114                                                      "lat_0=90w",
115                                                      "lon_0=90w",
116                                                      "k=1"]),
117                            ("Transverse Mercator", ["proj=tmerc",
118                                                      "ellps=clrk66",
119                                                      "lat_0=30w",
120                                                      "lon_0=30w",
121                                                      "k=1"]),
122                            ("Universal Transverse Mercator", ["proj=utm",
123                                                                "ellps=clrk66",
124                                                                "zone=1"])]
125    
126    sample_projfile2 = '''\
127    <?xml version="1.0" encoding="UTF-8"?>
128    <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
129    <projectionlist>
130    </projectionlist>
131    '''
132    
133    sample_projfile_data2 = []
134    
135    class TestProjFile(unittest.TestCase, support.FileTestMixin,
136                       xmlsupport.ValidationTest):
137    
138        """Test cases for reading and writing projection files.
139        """
140    
141        def compare_xml(self, xml1, xml2):
142            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):
208            """Test read_proj_file"""
209    
210            self.doTestRead(sample_projfile_data, sample_projfile)
211            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):
239            """Test write_proj_file"""
240    
241            self.doTestWrite(sample_projfile_data, sample_projfile)
242            self.doTestWrite(sample_projfile_data2, sample_projfile2)
243    
244        def doTestWrite(self, data, expected):
245    
246            filename = self.temp_file_name("projfile.proj")
247    
248            pf = ProjFile(filename)
249            for proj in data:
250                pf.Add(Projection(proj[1], proj[0]))
251    
252            resource.write_proj_file(pf)
253    
254            file = open(filename)
255            written_contents = file.read()
256            file.close()
257            self.compare_xml(written_contents, expected)
258            self.validate_data(written_contents)
259            self.validate_data(expected)
260    
261        def doTestRead(self, data, input):
262    
263            filename = self.temp_file_name("projfile.proj")
264            file = open(filename, "w")
265            file.write(input)
266            file.close()
267    
268            pf, warnings = resource.read_proj_file(filename)
269            self.assertEquals(warnings, [])
270    
271            eq = self.assertEquals
272    
273            eq(pf.GetFilename(), filename)
274    
275            for proj, d in zip(pf.GetProjections(), data):
276                eq(proj.GetName(), d[0])
277                for param in proj.GetAllParameters():
278                    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__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26