/[thuban]/branches/WIP-pyshapelib-bramz/test/test_proj.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/test_proj.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1825 - (hide annotations)
Tue Oct 14 15:21:17 2003 UTC (21 years, 4 months ago) by bh
Original Path: trunk/thuban/test/test_proj.py
File MIME type: text/x-python
File size: 15910 byte(s)
(TestProjFile.setUp): Subscribe to some of the
proj file messages
(TestProjFile.test_add_remove)
(TestProjFile.test_remove_non_existing)
(TestProjFile.test_replace)
(TestProjFile.test_replace_non_existing): Test whether the right
messages are sent

1 bh 1259 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 333 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Test the Thuban-specific Projection class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import unittest
17 jonathan 719 import os
18 bh 333
19 bh 1756 import xmlsupport
20 bh 333 import support
21     support.initthuban()
22    
23 bh 1684 from Thuban import _
24 bh 1793 from Thuban.Model.proj import Projection, ProjFile, \
25     PROJ_UNITS_METERS, PROJ_UNITS_DEGREES
26 bh 1825 from Thuban.Model.messages import PROJECTION_ADDED, PROJECTION_REMOVED, \
27     PROJECTION_REPLACED
28 jonathan 698 import Thuban.Model.resource as resource
29 bh 333
30 bh 1259 from xmlsupport import sax_eventlist
31 jonathan 698
32 jonathan 719 from xml.sax import SAXParseException
33 jonathan 698
34 jonathan 719
35 bh 333 class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
36    
37     """Test cases for the Thuban-specific Projection class
38     """
39    
40     def test(self):
41     """Test Projection"""
42 jonathan 761 params = ["zone=26", "proj=utm", "ellps=clrk66"]
43     proj = Projection(params)
44     self.assertEquals(proj.params, params)
45 bh 333
46     # It's not clear whether this value is really the correct one
47     # but a test failure here probably still means a bug somewhere
48     self.assertFloatSeqEqual(proj.Forward(0, 0),
49     [3623101.8103431347, 0.0],
50     epsilon = 1e-5)
51     self.assertFloatSeqEqual(proj.Inverse(3623101.8103431347, 0.0),
52     [-0.00065775699878736467, 0])
53    
54     self.assertFloatSeqEqual(proj.ForwardBBox((0, 0, 2, 2)),
55     (3620891.3077618643, 0.0,
56     3875381.8535437919, 252962.10480170773),
57     epsilon = 1e-5)
58    
59 jonathan 761 # GetName()
60     self.assertEquals(proj.GetName(), _("Unknown"))
61    
62     # GetParameter()
63 jonathan 714 self.assertEquals(proj.GetParameter("zone"), "26")
64     self.assertEquals(proj.GetParameter("proj"), "utm")
65     self.assertEquals(proj.GetParameter("ellps"), "clrk66")
66     self.assertEquals(proj.GetParameter("hallo"), "")
67 bh 333
68 jonathan 761 # GetAllParameters()
69     self.assertEquals(proj.GetAllParameters(), params)
70 bh 333
71 jonathan 761 # GetName()
72     proj = Projection(params, "MyName")
73     self.assertEquals(proj.GetName(), "MyName")
74    
75 bh 1798 def test_get_parameter_without_equals_sign(self):
76     """Test Projection.GetParameter() for a parameter without '=' sign"""
77     proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"])
78     # The Projection class pretends that for parameters specified
79     # without a value the value is the same as the parameter name.
80     self.assertEquals(proj.GetParameter("south"), "south")
81    
82 bh 1793 def test_get_projection_units_geo(self):
83     """Test Projection.GetProjectedUnits() for geographic projection"""
84     proj = Projection(["proj=latlong", "to_meter=0.017453292519943295",
85     "ellps=clrk66"])
86     self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES)
87 jonathan 761
88 bh 1793 def test_get_projection_units_normal(self):
89     """Test Projection.GetProjectedUnits() for normal projection"""
90     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
91     self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS)
92    
93 bh 1818 def test_label(self):
94     """Test Projection.Label() without epsg"""
95     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
96     name = "My Projection")
97     self.assertEquals(proj.Label(), "My Projection")
98 bh 1793
99 bh 1818 def test_label_epsg(self):
100     """Test Projection.Label() with epsg"""
101     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
102     name = "My Projection", epsg="42")
103     self.assertEquals(proj.Label(), "EPSG 42 My Projection")
104 jonathan 698
105 bh 1818 def test_epsgcode_for_non_epsg_projection(self):
106     """Test Projection.EPSGCode() without epsg"""
107     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
108     name = "My Projection")
109     self.assertEquals(proj.EPSGCode(), None)
110 jonathan 698
111 bh 1818 def test_epsgcode_for_real_epsg_projection(self):
112     """Test Projection.EPSGCode() with epsg"""
113     proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"],
114     name = "My Projection", epsg="42")
115     self.assertEquals(proj.EPSGCode(), "42")
116 jonathan 698
117    
118    
119 bh 1821 class TestProjFileSimple:
120 bh 1813
121 bh 1821 def test_init(self):
122     """Test ProjFile coinstructor"""
123     proj_file = ProjFile("some_filename")
124     self.assertEquals(proj_file.GetFilename(), "some_filename")
125     self.assertEquals(len(proj_file.GetProjections()), 0)
126 bh 1813
127 bh 1821 def test_set_filename(self):
128     """Test ProjFile.SetFilename()"""
129     proj_file = ProjFile("some_filename")
130     proj.SetFilename("other_name")
131     self.assertEquals(proj_file.GetFilename(), "other_name")
132 bh 1813
133    
134 bh 1825 class TestProjFile(unittest.TestCase, support.SubscriberMixin):
135 bh 1821
136 jonathan 698 """Test cases for reading and writing projection files.
137     """
138    
139 bh 1821 def setUp(self):
140 bh 1825 self.clear_messages()
141 bh 1821 self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
142     self.proj1 = Projection(["proj=utm", "ellps=clrk66"])
143     self.proj2 = Projection(["proj=lcc", "ellps=clrk66",
144     "lat_1=0", "lat_2=20"])
145 bh 1825 self.proj_file = ProjFile("some_filename")
146     for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]:
147     self.proj_file.Subscribe(msg, self.subscribe_with_params, msg)
148 jonathan 742
149 bh 1821 def test_add_remove(self):
150     """Test ProjFile.Add() and ProjFile.Remove()"""
151 bh 1825 self.proj_file.Add(self.proj0)
152     self.proj_file.Add(self.proj1)
153     self.assertEquals(self.proj_file.GetProjections(),
154     [self.proj0, self.proj1])
155     self.check_messages([(self.proj0, PROJECTION_ADDED),
156     (self.proj1, PROJECTION_ADDED)])
157     self.clear_messages()
158 jonathan 742
159 bh 1825 self.proj_file.Remove(self.proj0)
160     self.assertEquals(self.proj_file.GetProjections(), [self.proj1])
161     self.check_messages([(self.proj0, PROJECTION_REMOVED)])
162    
163 bh 1821 def test_remove_non_existing(self):
164     """Test ProjFile.Remove(<proj not in projfile>)"""
165 bh 1825 self.assertRaises(ValueError, self.proj_file.Remove, self.proj0)
166     # Nothing happened, so no messages should have been sent
167     self.check_messages([])
168 jonathan 742
169 bh 1821 def test_replace(self):
170     """Test ProjFile.Replace()"""
171 bh 1825 self.proj_file.Add(self.proj0)
172     self.proj_file.Add(self.proj1)
173     self.clear_messages()
174 jonathan 761
175 bh 1821 # Replace()
176 bh 1825 self.proj_file.Replace(self.proj0, self.proj2)
177     self.assertEquals(self.proj_file.GetProjections(),
178     [self.proj2, self.proj1])
179     self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)])
180 jonathan 742
181 bh 1821 def test_replace_non_existing(self):
182     """Test ProjFile.Replace(<proj not in projfile>, <some proj>)"""
183 bh 1825 self.proj_file.Add(self.proj0)
184     self.proj_file.Add(self.proj1)
185     self.clear_messages()
186 bh 1821 self.assertRaises(ValueError,
187 bh 1825 self.proj_file.Replace, self.proj2, self.proj0)
188     # All projections should still be there
189     self.assertEquals(self.proj_file.GetProjections(),
190     [self.proj0, self.proj1])
191     # Nothing happened, so no messages should have been sent
192     self.check_messages([])
193 jonathan 742
194    
195 bh 1821 class ProjFileTest(unittest.TestCase, support.FileTestMixin,
196     xmlsupport.ValidationTest):
197 jonathan 742
198 bh 1821 """Base class for the proj file tests that read or write files"""
199 jonathan 742
200 bh 1821 def filename(self):
201     """Return the filename for the test"""
202     return self.temp_file_name(self.id() + ".proj")
203 jonathan 742
204    
205 bh 1821 class ProjFileReadTests(ProjFileTest):
206 jonathan 752
207 bh 1821 """Test read ProjFile objects from files"""
208 jonathan 761
209 bh 1811 def test_read_non_existing_file(self):
210     """Test read_proj_file with non-existing file"""
211 jonathan 719 self.assertRaises(IOError,
212 bh 1811 resource.read_proj_file,
213     self.temp_file_name("nonexistent.proj"))
214 jonathan 719
215 bh 1811 def test_read_unreadable_file(self):
216     """Test read_proj_file with unreadable file
217    
218     As currently written this only works on unix-like systems and
219     not e.g. on MS Windows.
220     """
221 bh 1813 filename = self.filename()
222 jonathan 719 file = open(filename, "w")
223     file.close()
224     os.chmod(filename, 0200) # write-only
225 jonathan 1167 self.assertRaises(IOError, resource.read_proj_file, filename)
226 jonathan 719
227 bh 1811 def test_read_empty_file(self):
228     """Test read_proj_file with empty file"""
229 bh 1813 filename = self.filename()
230 jonathan 719 file = open(filename, "w")
231     file.close()
232    
233 jonathan 1167 self.assertRaises(SAXParseException, resource.read_proj_file, filename)
234 jonathan 719
235 bh 1787 def test_get_system_proj_file(self):
236     """Test resource.get_system_proj_file()
237 jonathan 698
238 bh 1787 This is primarily to test whether the system proj file contains
239     invalid projection paramers and whether the proj file is not
240     empty
241     """
242     projfile, warnings = resource.get_system_proj_file()
243     self.assertEquals(warnings, [])
244     self.assert_(len(projfile.GetProjections()) > 0)
245    
246    
247 bh 1821 class WriteProjFileTests(ProjFileTest):
248 bh 1813
249     """Test cases for writing proj files"""
250    
251     def compare_xml(self, xml1, xml2):
252     self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
253    
254 bh 1818 def doTestWrite(self, projfile, expected):
255 bh 1813 filename = self.filename()
256    
257 bh 1818 resource.write_proj_file(projfile)
258 bh 1813
259     file = open(filename)
260     written_contents = file.read()
261     file.close()
262     self.compare_xml(written_contents, expected)
263     self.validate_data(written_contents)
264     self.validate_data(expected)
265    
266     def test_write(self):
267     """Test write_proj_file"""
268 bh 1818 pf = ProjFile(self.filename())
269     pf.Add(Projection(['proj=tmerc', 'ellps=clrk66',
270     'lat_0=90w', 'lon_0=90w', 'k=1'],
271     "Transverse Mercator",))
272     pf.Add(Projection(["proj=tmerc",
273     "lat_0=0.000000000", "lon_0=-62.000000000",
274     "k=0.999500", "x_0=400000.000", "y_0=0.000",
275     "ellps=clrk80", "units=m"],
276     "Anguilla 1957 / British West Indies Grid",
277     epsg="200"))
278     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
279     <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
280     <projectionlist>
281     <projection name="Transverse Mercator">
282     <parameter value="proj=tmerc"/>
283     <parameter value="ellps=clrk66"/>
284     <parameter value="lat_0=90w"/>
285     <parameter value="lon_0=90w"/>
286     <parameter value="k=1"/>
287     </projection>
288     <projection epsg="200"
289     name="Anguilla 1957 / British West Indies Grid">
290     <parameter value="proj=tmerc"/>
291     <parameter value="lat_0=0.000000000"/>
292     <parameter value="lon_0=-62.000000000"/>
293     <parameter value="k=0.999500"/>
294     <parameter value="x_0=400000.000"/>
295     <parameter value="y_0=0.000"/>
296     <parameter value="ellps=clrk80"/>
297     <parameter value="units=m"/>
298     </projection>
299     </projectionlist>
300     '''
301     self.doTestWrite(pf, file_contents)
302 bh 1813
303     def test_write_empty_file(self):
304     """Test write empty ProjFile"""
305 bh 1818 pf = ProjFile(self.filename())
306     file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
307     <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
308     <projectionlist>
309     </projectionlist>
310     '''
311     self.doTestWrite(pf, file_contents)
312 bh 1813
313    
314 bh 1818 class TestLoadingProjFile(support.FileLoadTestCase):
315    
316     file_extension = ".proj"
317     file_contents = '''\
318     <?xml version="1.0" encoding="UTF-8"?>
319     <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
320     <projectionlist>
321     <projection name="Transverse Mercator">
322     <parameter value="proj=tmerc"/>
323     <parameter value="ellps=clrk66"/>
324     <parameter value="lat_0=90w"/>
325     <parameter value="lon_0=90w"/>
326     <parameter value="k=1"/>
327     </projection>
328     <projection epsg="200" name="Anguilla 1957 / British West Indies Grid">
329     <parameter value="proj=tmerc"/>
330     <parameter value="lat_0=0.000000000"/>
331     <parameter value="lon_0=-62.000000000"/>
332     <parameter value="k=0.999500"/>
333     <parameter value="x_0=400000.000"/>
334     <parameter value="y_0=0.000"/>
335     <parameter value="ellps=clrk80"/>
336     <parameter value="units=m"/>
337     </projection>
338     </projectionlist>
339     '''
340    
341     def check_projection(self, proj, label, parameters):
342     """Check the values of the proj's label and parameters"""
343     self.assertEquals(proj.Label(), label)
344     params = proj.GetAllParameters()[:]
345     params.sort()
346     self.assertEquals(params, parameters)
347    
348     def test(self):
349     projfile, warnings = resource.read_proj_file(self.filename())
350     # no warnings
351     self.assertEquals(warnings, [])
352    
353     # There are two projections
354     projs = projfile.GetProjections()
355     self.assertEquals(len(projs), 2)
356    
357     self.check_projection(projs[0],
358     "Transverse Mercator",
359     ['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w',
360     'proj=tmerc'])
361     self.check_projection(projs[1],
362     "EPSG 200 Anguilla 1957 / British West Indies Grid",
363     ["ellps=clrk80", "k=0.999500",
364     "lat_0=0.000000000", "lon_0=-62.000000000",
365     "proj=tmerc", "units=m",
366     "x_0=400000.000", "y_0=0.000"])
367    
368    
369     class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase):
370    
371     file_extension = ".proj"
372     file_contents = '''\
373     <?xml version="1.0" encoding="UTF-8"?>
374     <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
375     <projectionlist>
376     </projectionlist>
377     '''
378    
379     def test(self):
380     projfile, warnings = resource.read_proj_file(self.filename())
381     # no warnings
382     self.assertEquals(warnings, [])
383    
384     # There are no projections
385     self.assertEquals(len(projfile.GetProjections()), 0)
386    
387    
388 bh 1787 class TestProjFileWithInvalidParameters(unittest.TestCase,
389     support.FileLoadTestCase):
390    
391     file_extension = ".proj"
392     file_contents = '''\
393     <?xml version="1.0" encoding="UTF-8"?>
394     <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
395     <projectionlist>
396     <projection name="Universal Transverse Mercator">
397     <parameter value="proj=utm"/>
398     <parameter value="ellps=clrk66"/>
399     <!-- an invalid zone number to trigger the parameter checking
400     in the proj library -->
401     <parameter value="zone=1000"/>
402     </projection>
403     <projection name="Transverse Mercator">
404     <parameter value="proj=tmerc"/>
405     <parameter value="ellps=clrk66"/>
406     <parameter value="lat_0=90w"/>
407     <parameter value="lon_0=90w"/>
408     <parameter value="k=1"/>
409     </projection>
410     </projectionlist>
411     '''
412    
413     def setUp(self):
414     support.FileLoadTestCase.setUp(self)
415    
416     def test(self):
417     """Test reading a proj file with invalid parameters"""
418     projfile, warnings = resource.read_proj_file(self.filename())
419     projs = projfile.GetProjections()
420     self.assertEquals(len(projs), 1)
421     params = projs[0].GetAllParameters()[:]
422     params.sort()
423     self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
424     'lon_0=90w', 'proj=tmerc'])
425     self.assertEquals(warnings,
426     ['Error in projection "Universal Transverse Mercator":'
427     ' invalid UTM zone number'])
428    
429    
430 bh 333 if __name__ == "__main__":
431     unittest.main()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26