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 |
1798 |
def test_get_parameter_without_equals_sign(self): |
75 |
|
|
"""Test Projection.GetParameter() for a parameter without '=' sign""" |
76 |
|
|
proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"]) |
77 |
|
|
# The Projection class pretends that for parameters specified |
78 |
|
|
# without a value the value is the same as the parameter name. |
79 |
|
|
self.assertEquals(proj.GetParameter("south"), "south") |
80 |
|
|
|
81 |
bh |
1793 |
def test_get_projection_units_geo(self): |
82 |
|
|
"""Test Projection.GetProjectedUnits() for geographic projection""" |
83 |
|
|
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
84 |
|
|
"ellps=clrk66"]) |
85 |
|
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
86 |
jonathan |
761 |
|
87 |
bh |
1793 |
def test_get_projection_units_normal(self): |
88 |
|
|
"""Test Projection.GetProjectedUnits() for normal projection""" |
89 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
90 |
|
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
91 |
|
|
|
92 |
|
|
|
93 |
jonathan |
698 |
sample_projfile = '''\ |
94 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
95 |
bh |
1756 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
96 |
jonathan |
698 |
<projectionlist> |
97 |
bh |
1756 |
<projection name="Transverse Mercator"> |
98 |
jonathan |
698 |
<parameter value="proj=tmerc"/> |
99 |
|
|
<parameter value="ellps=clrk66"/> |
100 |
|
|
<parameter value="lat_0=90w"/> |
101 |
|
|
<parameter value="lon_0=90w"/> |
102 |
|
|
<parameter value="k=1"/> |
103 |
|
|
</projection> |
104 |
bh |
1756 |
<projection name="Transverse Mercator"> |
105 |
jonathan |
698 |
<parameter value="proj=tmerc"/> |
106 |
|
|
<parameter value="ellps=clrk66"/> |
107 |
|
|
<parameter value="lat_0=30w"/> |
108 |
|
|
<parameter value="lon_0=30w"/> |
109 |
|
|
<parameter value="k=1"/> |
110 |
|
|
</projection> |
111 |
bh |
1756 |
<projection name="Universal Transverse Mercator"> |
112 |
jonathan |
698 |
<parameter value="proj=utm"/> |
113 |
|
|
<parameter value="ellps=clrk66"/> |
114 |
|
|
<parameter value="zone=1"/> |
115 |
|
|
</projection> |
116 |
|
|
</projectionlist> |
117 |
|
|
''' |
118 |
|
|
|
119 |
bh |
1756 |
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
120 |
jonathan |
698 |
"ellps=clrk66", |
121 |
|
|
"lat_0=90w", |
122 |
|
|
"lon_0=90w", |
123 |
|
|
"k=1"]), |
124 |
bh |
1756 |
("Transverse Mercator", ["proj=tmerc", |
125 |
jonathan |
698 |
"ellps=clrk66", |
126 |
|
|
"lat_0=30w", |
127 |
|
|
"lon_0=30w", |
128 |
|
|
"k=1"]), |
129 |
bh |
1756 |
("Universal Transverse Mercator", ["proj=utm", |
130 |
jonathan |
698 |
"ellps=clrk66", |
131 |
|
|
"zone=1"])] |
132 |
|
|
|
133 |
|
|
sample_projfile2 = '''\ |
134 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
135 |
bh |
1756 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
136 |
jonathan |
698 |
<projectionlist> |
137 |
|
|
</projectionlist> |
138 |
|
|
''' |
139 |
|
|
|
140 |
|
|
sample_projfile_data2 = [] |
141 |
|
|
|
142 |
bh |
1756 |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
143 |
|
|
xmlsupport.ValidationTest): |
144 |
jonathan |
698 |
|
145 |
|
|
"""Test cases for reading and writing projection files. |
146 |
|
|
""" |
147 |
|
|
|
148 |
|
|
def compare_xml(self, xml1, xml2): |
149 |
|
|
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
150 |
|
|
|
151 |
jonathan |
742 |
def test(self): |
152 |
|
|
"""Test ProjFile""" |
153 |
|
|
|
154 |
|
|
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
155 |
|
|
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
156 |
bh |
1687 |
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
157 |
|
|
"lat_1=0", "lat_2=20"]) |
158 |
jonathan |
742 |
|
159 |
|
|
eq = self.assertEquals |
160 |
|
|
|
161 |
jonathan |
761 |
|
162 |
jonathan |
742 |
# |
163 |
|
|
# __init__() |
164 |
|
|
# GetFilename() |
165 |
|
|
# SetFilename() |
166 |
|
|
# |
167 |
|
|
for name in ["", "hello_world"]: |
168 |
|
|
projFile = ProjFile(name) |
169 |
|
|
eq(projFile.GetFilename(), name) |
170 |
|
|
|
171 |
|
|
projFile.SetFilename("XXX") |
172 |
|
|
projFile.SetFilename(name) |
173 |
|
|
eq(projFile.GetFilename(), name) |
174 |
|
|
|
175 |
|
|
# initial number of projections should be 0 |
176 |
|
|
eq(len(projFile.GetProjections()), 0) |
177 |
|
|
|
178 |
|
|
# |
179 |
|
|
# Add() |
180 |
|
|
# Remove() |
181 |
|
|
# |
182 |
|
|
projFile.Add(proj0) |
183 |
|
|
eq(len(projFile.GetProjections()), 1) |
184 |
|
|
projFile.Remove(proj0) |
185 |
|
|
eq(len(projFile.GetProjections()), 0) |
186 |
|
|
|
187 |
|
|
# try to remove something that doesn't exist |
188 |
jonathan |
761 |
self.assertRaises(ValueError, projFile.Remove, None) |
189 |
jonathan |
742 |
self.assertRaises(ValueError, projFile.Remove, proj0) |
190 |
|
|
|
191 |
|
|
projFile.Add(proj0) |
192 |
|
|
projFile.Add(proj1) |
193 |
|
|
projFile.Add(proj2) |
194 |
|
|
eq(len(projFile.GetProjections()), 3) |
195 |
|
|
|
196 |
|
|
# GetProjections() -- tests order |
197 |
|
|
projs = projFile.GetProjections() |
198 |
|
|
eq(projs[0], proj0) |
199 |
|
|
eq(projs[1], proj1) |
200 |
|
|
eq(projs[2], proj2) |
201 |
|
|
|
202 |
jonathan |
761 |
projFile.Remove(proj2) |
203 |
|
|
projFile.Remove(proj1) |
204 |
jonathan |
752 |
|
205 |
jonathan |
761 |
# Replace() |
206 |
|
|
projFile.Replace(proj0, proj1) |
207 |
|
|
projs = projFile.GetProjections() |
208 |
|
|
eq(projs[0], proj1) |
209 |
|
|
|
210 |
|
|
# replace a non-existent projection |
211 |
|
|
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
212 |
|
|
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
213 |
|
|
|
214 |
jonathan |
698 |
def testRead(self): |
215 |
jonathan |
1167 |
"""Test read_proj_file""" |
216 |
jonathan |
698 |
|
217 |
|
|
self.doTestRead(sample_projfile_data, sample_projfile) |
218 |
|
|
self.doTestRead(sample_projfile_data2, sample_projfile2) |
219 |
|
|
|
220 |
jonathan |
719 |
# |
221 |
|
|
# file doesn't exist |
222 |
|
|
# |
223 |
|
|
self.assertRaises(IOError, |
224 |
jonathan |
1167 |
resource.read_proj_file, self.temp_file_name("nonexistent.proj")) |
225 |
jonathan |
719 |
|
226 |
|
|
# |
227 |
|
|
# file isn't readable |
228 |
|
|
# |
229 |
|
|
filename = self.temp_file_name("projfile.proj") |
230 |
|
|
file = open(filename, "w") |
231 |
|
|
file.close() |
232 |
|
|
os.chmod(filename, 0200) # write-only |
233 |
jonathan |
1167 |
self.assertRaises(IOError, resource.read_proj_file, filename) |
234 |
jonathan |
719 |
os.chmod(filename, 0600) # read/write so we reuse the file |
235 |
|
|
|
236 |
|
|
# |
237 |
|
|
# file has invalid XML (or none at all) |
238 |
|
|
# |
239 |
|
|
filename = self.temp_file_name("projfile.proj") |
240 |
|
|
file = open(filename, "w") |
241 |
|
|
file.close() |
242 |
|
|
|
243 |
jonathan |
1167 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
244 |
jonathan |
719 |
|
245 |
jonathan |
698 |
def testWrite(self): |
246 |
jonathan |
1167 |
"""Test write_proj_file""" |
247 |
jonathan |
698 |
|
248 |
|
|
self.doTestWrite(sample_projfile_data, sample_projfile) |
249 |
|
|
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
250 |
|
|
|
251 |
|
|
def doTestWrite(self, data, expected): |
252 |
|
|
|
253 |
|
|
filename = self.temp_file_name("projfile.proj") |
254 |
|
|
|
255 |
|
|
pf = ProjFile(filename) |
256 |
|
|
for proj in data: |
257 |
|
|
pf.Add(Projection(proj[1], proj[0])) |
258 |
|
|
|
259 |
jonathan |
1167 |
resource.write_proj_file(pf) |
260 |
jonathan |
698 |
|
261 |
|
|
file = open(filename) |
262 |
|
|
written_contents = file.read() |
263 |
|
|
file.close() |
264 |
|
|
self.compare_xml(written_contents, expected) |
265 |
bh |
1756 |
self.validate_data(written_contents) |
266 |
|
|
self.validate_data(expected) |
267 |
jonathan |
698 |
|
268 |
|
|
def doTestRead(self, data, input): |
269 |
|
|
|
270 |
|
|
filename = self.temp_file_name("projfile.proj") |
271 |
|
|
file = open(filename, "w") |
272 |
|
|
file.write(input) |
273 |
|
|
file.close() |
274 |
|
|
|
275 |
bh |
1787 |
pf, warnings = resource.read_proj_file(filename) |
276 |
|
|
self.assertEquals(warnings, []) |
277 |
jonathan |
698 |
|
278 |
|
|
eq = self.assertEquals |
279 |
|
|
|
280 |
jonathan |
742 |
eq(pf.GetFilename(), filename) |
281 |
jonathan |
698 |
|
282 |
|
|
for proj, d in zip(pf.GetProjections(), data): |
283 |
|
|
eq(proj.GetName(), d[0]) |
284 |
jonathan |
714 |
for param in proj.GetAllParameters(): |
285 |
jonathan |
698 |
self.assert_(param in d[1]) |
286 |
|
|
|
287 |
bh |
1787 |
def test_get_system_proj_file(self): |
288 |
|
|
"""Test resource.get_system_proj_file() |
289 |
jonathan |
698 |
|
290 |
bh |
1787 |
This is primarily to test whether the system proj file contains |
291 |
|
|
invalid projection paramers and whether the proj file is not |
292 |
|
|
empty |
293 |
|
|
""" |
294 |
|
|
projfile, warnings = resource.get_system_proj_file() |
295 |
|
|
self.assertEquals(warnings, []) |
296 |
|
|
self.assert_(len(projfile.GetProjections()) > 0) |
297 |
|
|
|
298 |
|
|
|
299 |
|
|
class TestProjFileWithInvalidParameters(unittest.TestCase, |
300 |
|
|
support.FileLoadTestCase): |
301 |
|
|
|
302 |
|
|
file_extension = ".proj" |
303 |
|
|
file_contents = '''\ |
304 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
305 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
306 |
|
|
<projectionlist> |
307 |
|
|
<projection name="Universal Transverse Mercator"> |
308 |
|
|
<parameter value="proj=utm"/> |
309 |
|
|
<parameter value="ellps=clrk66"/> |
310 |
|
|
<!-- an invalid zone number to trigger the parameter checking |
311 |
|
|
in the proj library --> |
312 |
|
|
<parameter value="zone=1000"/> |
313 |
|
|
</projection> |
314 |
|
|
<projection name="Transverse Mercator"> |
315 |
|
|
<parameter value="proj=tmerc"/> |
316 |
|
|
<parameter value="ellps=clrk66"/> |
317 |
|
|
<parameter value="lat_0=90w"/> |
318 |
|
|
<parameter value="lon_0=90w"/> |
319 |
|
|
<parameter value="k=1"/> |
320 |
|
|
</projection> |
321 |
|
|
</projectionlist> |
322 |
|
|
''' |
323 |
|
|
|
324 |
|
|
def setUp(self): |
325 |
|
|
support.FileLoadTestCase.setUp(self) |
326 |
|
|
|
327 |
|
|
def test(self): |
328 |
|
|
"""Test reading a proj file with invalid parameters""" |
329 |
|
|
projfile, warnings = resource.read_proj_file(self.filename()) |
330 |
|
|
projs = projfile.GetProjections() |
331 |
|
|
self.assertEquals(len(projs), 1) |
332 |
|
|
params = projs[0].GetAllParameters()[:] |
333 |
|
|
params.sort() |
334 |
|
|
self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w', |
335 |
|
|
'lon_0=90w', 'proj=tmerc']) |
336 |
|
|
self.assertEquals(warnings, |
337 |
|
|
['Error in projection "Universal Transverse Mercator":' |
338 |
|
|
' invalid UTM zone number']) |
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
bh |
333 |
if __name__ == "__main__": |
343 |
|
|
unittest.main() |