/[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 1684 - (show annotations)
Thu Aug 28 18:55:01 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_proj.py
File MIME type: text/x-python
File size: 8064 byte(s)
Import things from Thuban after calling
initthuban

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