/[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 1793 - (hide annotations)
Wed Oct 8 16:04:01 2003 UTC (21 years, 5 months ago) by bh
Original Path: trunk/thuban/test/test_proj.py
File MIME type: text/x-python
File size: 10895 byte(s)
(TestProjection.test_get_projection_units_geo)
(TestProjection.test_get_projection_units_normal): New. Tests for
the Projection.GetProjectedUnits method

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 333
27 jonathan 698 import Thuban.Model.resource as resource
28 bh 333
29 bh 1259 from xmlsupport import sax_eventlist
30 jonathan 698
31 jonathan 719 from xml.sax import SAXParseException
32 jonathan 698
33 jonathan 719
34 bh 333 class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
35    
36     """Test cases for the Thuban-specific Projection class
37     """
38    
39     def test(self):
40     """Test Projection"""
41 jonathan 761 params = ["zone=26", "proj=utm", "ellps=clrk66"]
42     proj = Projection(params)
43     self.assertEquals(proj.params, params)
44 bh 333
45     # It's not clear whether this value is really the correct one
46     # but a test failure here probably still means a bug somewhere
47     self.assertFloatSeqEqual(proj.Forward(0, 0),
48     [3623101.8103431347, 0.0],
49     epsilon = 1e-5)
50     self.assertFloatSeqEqual(proj.Inverse(3623101.8103431347, 0.0),
51     [-0.00065775699878736467, 0])
52    
53     self.assertFloatSeqEqual(proj.ForwardBBox((0, 0, 2, 2)),
54     (3620891.3077618643, 0.0,
55     3875381.8535437919, 252962.10480170773),
56     epsilon = 1e-5)
57    
58 jonathan 761 # GetName()
59     self.assertEquals(proj.GetName(), _("Unknown"))
60    
61     # GetParameter()
62 jonathan 714 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 bh 333
67 jonathan 761 # GetAllParameters()
68     self.assertEquals(proj.GetAllParameters(), params)
69 bh 333
70 jonathan 761 # GetName()
71     proj = Projection(params, "MyName")
72     self.assertEquals(proj.GetName(), "MyName")
73    
74 bh 1793 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 jonathan 761
80 bh 1793 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 jonathan 698 sample_projfile = '''\
87     <?xml version="1.0" encoding="UTF-8"?>
88 bh 1756 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
89 jonathan 698 <projectionlist>
90 bh 1756 <projection name="Transverse Mercator">
91 jonathan 698 <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 bh 1756 <projection name="Transverse Mercator">
98 jonathan 698 <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 bh 1756 <projection name="Universal Transverse Mercator">
105 jonathan 698 <parameter value="proj=utm"/>
106     <parameter value="ellps=clrk66"/>
107     <parameter value="zone=1"/>
108     </projection>
109     </projectionlist>
110     '''
111    
112 bh 1756 sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
113 jonathan 698 "ellps=clrk66",
114     "lat_0=90w",
115     "lon_0=90w",
116     "k=1"]),
117 bh 1756 ("Transverse Mercator", ["proj=tmerc",
118 jonathan 698 "ellps=clrk66",
119     "lat_0=30w",
120     "lon_0=30w",
121     "k=1"]),
122 bh 1756 ("Universal Transverse Mercator", ["proj=utm",
123 jonathan 698 "ellps=clrk66",
124     "zone=1"])]
125    
126     sample_projfile2 = '''\
127     <?xml version="1.0" encoding="UTF-8"?>
128 bh 1756 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
129 jonathan 698 <projectionlist>
130     </projectionlist>
131     '''
132    
133     sample_projfile_data2 = []
134    
135 bh 1756 class TestProjFile(unittest.TestCase, support.FileTestMixin,
136     xmlsupport.ValidationTest):
137 jonathan 698
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 jonathan 742 def test(self):
145     """Test ProjFile"""
146    
147     proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
148     proj1 = Projection(["proj=utm", "ellps=clrk66"])
149 bh 1687 proj2 = Projection(["proj=lcc", "ellps=clrk66",
150     "lat_1=0", "lat_2=20"])
151 jonathan 742
152     eq = self.assertEquals
153    
154 jonathan 761
155 jonathan 742 #
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 jonathan 761 self.assertRaises(ValueError, projFile.Remove, None)
182 jonathan 742 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 jonathan 761 projFile.Remove(proj2)
196     projFile.Remove(proj1)
197 jonathan 752
198 jonathan 761 # 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 jonathan 698 def testRead(self):
208 jonathan 1167 """Test read_proj_file"""
209 jonathan 698
210     self.doTestRead(sample_projfile_data, sample_projfile)
211     self.doTestRead(sample_projfile_data2, sample_projfile2)
212    
213 jonathan 719 #
214     # file doesn't exist
215     #
216     self.assertRaises(IOError,
217 jonathan 1167 resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
218 jonathan 719
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 jonathan 1167 self.assertRaises(IOError, resource.read_proj_file, filename)
227 jonathan 719 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 jonathan 1167 self.assertRaises(SAXParseException, resource.read_proj_file, filename)
237 jonathan 719
238 jonathan 698 def testWrite(self):
239 jonathan 1167 """Test write_proj_file"""
240 jonathan 698
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 jonathan 1167 resource.write_proj_file(pf)
253 jonathan 698
254     file = open(filename)
255     written_contents = file.read()
256     file.close()
257     self.compare_xml(written_contents, expected)
258 bh 1756 self.validate_data(written_contents)
259     self.validate_data(expected)
260 jonathan 698
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 bh 1787 pf, warnings = resource.read_proj_file(filename)
269     self.assertEquals(warnings, [])
270 jonathan 698
271     eq = self.assertEquals
272    
273 jonathan 742 eq(pf.GetFilename(), filename)
274 jonathan 698
275     for proj, d in zip(pf.GetProjections(), data):
276     eq(proj.GetName(), d[0])
277 jonathan 714 for param in proj.GetAllParameters():
278 jonathan 698 self.assert_(param in d[1])
279    
280 bh 1787 def test_get_system_proj_file(self):
281     """Test resource.get_system_proj_file()
282 jonathan 698
283 bh 1787 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 bh 333 if __name__ == "__main__":
336     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