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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1787 - (show annotations)
Wed Oct 8 10:39:18 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: 10276 byte(s)
(TestProjFile.doTestRead): Check the warnings.
(TestProjFileWithInvalidParameters.file_contents): New test cases
to test whether read_proj_file handles invalid projection
parameters correctly
(TestProjFile.test_get_system_proj_file): New. Simple test for
resource.get_system_proj_file

1 # Copyright (c) 2002, 2003 by Intevation GmbH
2 # 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 import os
18
19 import xmlsupport
20 import support
21 support.initthuban()
22
23 from Thuban import _
24 from Thuban.Model.proj import Projection, ProjFile
25
26 import Thuban.Model.resource as resource
27
28 from xmlsupport import sax_eventlist
29
30 from xml.sax import SAXParseException
31
32
33 class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
34
35 """Test cases for the Thuban-specific Projection class
36 """
37
38 def test(self):
39 """Test Projection"""
40 params = ["zone=26", "proj=utm", "ellps=clrk66"]
41 proj = Projection(params)
42 self.assertEquals(proj.params, params)
43
44 # It's not clear whether this value is really the correct one
45 # but a test failure here probably still means a bug somewhere
46 self.assertFloatSeqEqual(proj.Forward(0, 0),
47 [3623101.8103431347, 0.0],
48 epsilon = 1e-5)
49 self.assertFloatSeqEqual(proj.Inverse(3623101.8103431347, 0.0),
50 [-0.00065775699878736467, 0])
51
52 self.assertFloatSeqEqual(proj.ForwardBBox((0, 0, 2, 2)),
53 (3620891.3077618643, 0.0,
54 3875381.8535437919, 252962.10480170773),
55 epsilon = 1e-5)
56
57 # GetName()
58 self.assertEquals(proj.GetName(), _("Unknown"))
59
60 # GetParameter()
61 self.assertEquals(proj.GetParameter("zone"), "26")
62 self.assertEquals(proj.GetParameter("proj"), "utm")
63 self.assertEquals(proj.GetParameter("ellps"), "clrk66")
64 self.assertEquals(proj.GetParameter("hallo"), "")
65
66 # GetAllParameters()
67 self.assertEquals(proj.GetAllParameters(), params)
68
69 # GetName()
70 proj = Projection(params, "MyName")
71 self.assertEquals(proj.GetName(), "MyName")
72
73
74 sample_projfile = '''\
75 <?xml version="1.0" encoding="UTF-8"?>
76 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
77 <projectionlist>
78 <projection name="Transverse Mercator">
79 <parameter value="proj=tmerc"/>
80 <parameter value="ellps=clrk66"/>
81 <parameter value="lat_0=90w"/>
82 <parameter value="lon_0=90w"/>
83 <parameter value="k=1"/>
84 </projection>
85 <projection name="Transverse Mercator">
86 <parameter value="proj=tmerc"/>
87 <parameter value="ellps=clrk66"/>
88 <parameter value="lat_0=30w"/>
89 <parameter value="lon_0=30w"/>
90 <parameter value="k=1"/>
91 </projection>
92 <projection name="Universal Transverse Mercator">
93 <parameter value="proj=utm"/>
94 <parameter value="ellps=clrk66"/>
95 <parameter value="zone=1"/>
96 </projection>
97 </projectionlist>
98 '''
99
100 sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
101 "ellps=clrk66",
102 "lat_0=90w",
103 "lon_0=90w",
104 "k=1"]),
105 ("Transverse Mercator", ["proj=tmerc",
106 "ellps=clrk66",
107 "lat_0=30w",
108 "lon_0=30w",
109 "k=1"]),
110 ("Universal Transverse Mercator", ["proj=utm",
111 "ellps=clrk66",
112 "zone=1"])]
113
114 sample_projfile2 = '''\
115 <?xml version="1.0" encoding="UTF-8"?>
116 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
117 <projectionlist>
118 </projectionlist>
119 '''
120
121 sample_projfile_data2 = []
122
123 class TestProjFile(unittest.TestCase, support.FileTestMixin,
124 xmlsupport.ValidationTest):
125
126 """Test cases for reading and writing projection files.
127 """
128
129 def compare_xml(self, xml1, xml2):
130 self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
131
132 def test(self):
133 """Test ProjFile"""
134
135 proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
136 proj1 = Projection(["proj=utm", "ellps=clrk66"])
137 proj2 = Projection(["proj=lcc", "ellps=clrk66",
138 "lat_1=0", "lat_2=20"])
139
140 eq = self.assertEquals
141
142
143 #
144 # __init__()
145 # GetFilename()
146 # SetFilename()
147 #
148 for name in ["", "hello_world"]:
149 projFile = ProjFile(name)
150 eq(projFile.GetFilename(), name)
151
152 projFile.SetFilename("XXX")
153 projFile.SetFilename(name)
154 eq(projFile.GetFilename(), name)
155
156 # initial number of projections should be 0
157 eq(len(projFile.GetProjections()), 0)
158
159 #
160 # Add()
161 # Remove()
162 #
163 projFile.Add(proj0)
164 eq(len(projFile.GetProjections()), 1)
165 projFile.Remove(proj0)
166 eq(len(projFile.GetProjections()), 0)
167
168 # try to remove something that doesn't exist
169 self.assertRaises(ValueError, projFile.Remove, None)
170 self.assertRaises(ValueError, projFile.Remove, proj0)
171
172 projFile.Add(proj0)
173 projFile.Add(proj1)
174 projFile.Add(proj2)
175 eq(len(projFile.GetProjections()), 3)
176
177 # GetProjections() -- tests order
178 projs = projFile.GetProjections()
179 eq(projs[0], proj0)
180 eq(projs[1], proj1)
181 eq(projs[2], proj2)
182
183 projFile.Remove(proj2)
184 projFile.Remove(proj1)
185
186 # Replace()
187 projFile.Replace(proj0, proj1)
188 projs = projFile.GetProjections()
189 eq(projs[0], proj1)
190
191 # replace a non-existent projection
192 self.assertRaises(ValueError, projFile.Replace, None, proj2)
193 self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
194
195 def testRead(self):
196 """Test read_proj_file"""
197
198 self.doTestRead(sample_projfile_data, sample_projfile)
199 self.doTestRead(sample_projfile_data2, sample_projfile2)
200
201 #
202 # file doesn't exist
203 #
204 self.assertRaises(IOError,
205 resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
206
207 #
208 # file isn't readable
209 #
210 filename = self.temp_file_name("projfile.proj")
211 file = open(filename, "w")
212 file.close()
213 os.chmod(filename, 0200) # write-only
214 self.assertRaises(IOError, resource.read_proj_file, filename)
215 os.chmod(filename, 0600) # read/write so we reuse the file
216
217 #
218 # file has invalid XML (or none at all)
219 #
220 filename = self.temp_file_name("projfile.proj")
221 file = open(filename, "w")
222 file.close()
223
224 self.assertRaises(SAXParseException, resource.read_proj_file, filename)
225
226 def testWrite(self):
227 """Test write_proj_file"""
228
229 self.doTestWrite(sample_projfile_data, sample_projfile)
230 self.doTestWrite(sample_projfile_data2, sample_projfile2)
231
232 def doTestWrite(self, data, expected):
233
234 filename = self.temp_file_name("projfile.proj")
235
236 pf = ProjFile(filename)
237 for proj in data:
238 pf.Add(Projection(proj[1], proj[0]))
239
240 resource.write_proj_file(pf)
241
242 file = open(filename)
243 written_contents = file.read()
244 file.close()
245 self.compare_xml(written_contents, expected)
246 self.validate_data(written_contents)
247 self.validate_data(expected)
248
249 def doTestRead(self, data, input):
250
251 filename = self.temp_file_name("projfile.proj")
252 file = open(filename, "w")
253 file.write(input)
254 file.close()
255
256 pf, warnings = resource.read_proj_file(filename)
257 self.assertEquals(warnings, [])
258
259 eq = self.assertEquals
260
261 eq(pf.GetFilename(), filename)
262
263 for proj, d in zip(pf.GetProjections(), data):
264 eq(proj.GetName(), d[0])
265 for param in proj.GetAllParameters():
266 self.assert_(param in d[1])
267
268 def test_get_system_proj_file(self):
269 """Test resource.get_system_proj_file()
270
271 This is primarily to test whether the system proj file contains
272 invalid projection paramers and whether the proj file is not
273 empty
274 """
275 projfile, warnings = resource.get_system_proj_file()
276 self.assertEquals(warnings, [])
277 self.assert_(len(projfile.GetProjections()) > 0)
278
279
280 class TestProjFileWithInvalidParameters(unittest.TestCase,
281 support.FileLoadTestCase):
282
283 file_extension = ".proj"
284 file_contents = '''\
285 <?xml version="1.0" encoding="UTF-8"?>
286 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
287 <projectionlist>
288 <projection name="Universal Transverse Mercator">
289 <parameter value="proj=utm"/>
290 <parameter value="ellps=clrk66"/>
291 <!-- an invalid zone number to trigger the parameter checking
292 in the proj library -->
293 <parameter value="zone=1000"/>
294 </projection>
295 <projection name="Transverse Mercator">
296 <parameter value="proj=tmerc"/>
297 <parameter value="ellps=clrk66"/>
298 <parameter value="lat_0=90w"/>
299 <parameter value="lon_0=90w"/>
300 <parameter value="k=1"/>
301 </projection>
302 </projectionlist>
303 '''
304
305 def setUp(self):
306 support.FileLoadTestCase.setUp(self)
307
308 def test(self):
309 """Test reading a proj file with invalid parameters"""
310 projfile, warnings = resource.read_proj_file(self.filename())
311 projs = projfile.GetProjections()
312 self.assertEquals(len(projs), 1)
313 params = projs[0].GetAllParameters()[:]
314 params.sort()
315 self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w',
316 'lon_0=90w', 'proj=tmerc'])
317 self.assertEquals(warnings,
318 ['Error in projection "Universal Transverse Mercator":'
319 ' invalid UTM zone number'])
320
321
322
323 if __name__ == "__main__":
324 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