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 |
bh |
1818 |
def test_label(self): |
93 |
|
|
"""Test Projection.Label() without epsg""" |
94 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
95 |
|
|
name = "My Projection") |
96 |
|
|
self.assertEquals(proj.Label(), "My Projection") |
97 |
bh |
1793 |
|
98 |
bh |
1818 |
def test_label_epsg(self): |
99 |
|
|
"""Test Projection.Label() with epsg""" |
100 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
101 |
|
|
name = "My Projection", epsg="42") |
102 |
|
|
self.assertEquals(proj.Label(), "EPSG 42 My Projection") |
103 |
jonathan |
698 |
|
104 |
bh |
1818 |
def test_epsgcode_for_non_epsg_projection(self): |
105 |
|
|
"""Test Projection.EPSGCode() without epsg""" |
106 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
107 |
|
|
name = "My Projection") |
108 |
|
|
self.assertEquals(proj.EPSGCode(), None) |
109 |
jonathan |
698 |
|
110 |
bh |
1818 |
def test_epsgcode_for_real_epsg_projection(self): |
111 |
|
|
"""Test Projection.EPSGCode() with epsg""" |
112 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
113 |
|
|
name = "My Projection", epsg="42") |
114 |
|
|
self.assertEquals(proj.EPSGCode(), "42") |
115 |
jonathan |
698 |
|
116 |
|
|
|
117 |
|
|
|
118 |
bh |
1821 |
class TestProjFileSimple: |
119 |
bh |
1813 |
|
120 |
bh |
1821 |
def test_init(self): |
121 |
|
|
"""Test ProjFile coinstructor""" |
122 |
|
|
proj_file = ProjFile("some_filename") |
123 |
|
|
self.assertEquals(proj_file.GetFilename(), "some_filename") |
124 |
|
|
self.assertEquals(len(proj_file.GetProjections()), 0) |
125 |
bh |
1813 |
|
126 |
bh |
1821 |
def test_set_filename(self): |
127 |
|
|
"""Test ProjFile.SetFilename()""" |
128 |
|
|
proj_file = ProjFile("some_filename") |
129 |
|
|
proj.SetFilename("other_name") |
130 |
|
|
self.assertEquals(proj_file.GetFilename(), "other_name") |
131 |
bh |
1813 |
|
132 |
|
|
|
133 |
bh |
1821 |
class TestProjFile(unittest.TestCase): |
134 |
|
|
|
135 |
jonathan |
698 |
"""Test cases for reading and writing projection files. |
136 |
|
|
""" |
137 |
|
|
|
138 |
bh |
1821 |
def setUp(self): |
139 |
|
|
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
140 |
|
|
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
141 |
|
|
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
142 |
|
|
"lat_1=0", "lat_2=20"]) |
143 |
jonathan |
742 |
|
144 |
bh |
1821 |
def test_add_remove(self): |
145 |
|
|
"""Test ProjFile.Add() and ProjFile.Remove()""" |
146 |
|
|
proj_file = ProjFile("some_filename") |
147 |
|
|
proj_file.Add(self.proj0) |
148 |
|
|
proj_file.Add(self.proj1) |
149 |
|
|
self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1]) |
150 |
|
|
proj_file.Remove(self.proj0) |
151 |
|
|
self.assertEquals(proj_file.GetProjections(), [self.proj1]) |
152 |
jonathan |
742 |
|
153 |
bh |
1821 |
def test_remove_non_existing(self): |
154 |
|
|
"""Test ProjFile.Remove(<proj not in projfile>)""" |
155 |
|
|
proj_file = ProjFile("some_filename") |
156 |
|
|
self.assertRaises(ValueError, proj_file.Remove, self.proj0) |
157 |
jonathan |
742 |
|
158 |
bh |
1821 |
def test_replace(self): |
159 |
|
|
"""Test ProjFile.Replace()""" |
160 |
|
|
proj_file = ProjFile("some_filename") |
161 |
|
|
proj_file.Add(self.proj0) |
162 |
|
|
proj_file.Add(self.proj1) |
163 |
jonathan |
761 |
|
164 |
bh |
1821 |
# Replace() |
165 |
|
|
proj_file.Replace(self.proj0, self.proj2) |
166 |
|
|
self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1]) |
167 |
jonathan |
742 |
|
168 |
bh |
1821 |
def test_replace_non_existing(self): |
169 |
|
|
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
170 |
|
|
proj_file = ProjFile("some_filename") |
171 |
|
|
proj_file.Add(self.proj0) |
172 |
|
|
proj_file.Add(self.proj1) |
173 |
|
|
self.assertRaises(ValueError, |
174 |
|
|
proj_file.Replace, self.proj2, self.proj0) |
175 |
jonathan |
742 |
|
176 |
|
|
|
177 |
bh |
1821 |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
178 |
|
|
xmlsupport.ValidationTest): |
179 |
jonathan |
742 |
|
180 |
bh |
1821 |
"""Base class for the proj file tests that read or write files""" |
181 |
jonathan |
742 |
|
182 |
bh |
1821 |
def filename(self): |
183 |
|
|
"""Return the filename for the test""" |
184 |
|
|
return self.temp_file_name(self.id() + ".proj") |
185 |
jonathan |
742 |
|
186 |
|
|
|
187 |
bh |
1821 |
class ProjFileReadTests(ProjFileTest): |
188 |
jonathan |
752 |
|
189 |
bh |
1821 |
"""Test read ProjFile objects from files""" |
190 |
jonathan |
761 |
|
191 |
bh |
1811 |
def test_read_non_existing_file(self): |
192 |
|
|
"""Test read_proj_file with non-existing file""" |
193 |
jonathan |
719 |
self.assertRaises(IOError, |
194 |
bh |
1811 |
resource.read_proj_file, |
195 |
|
|
self.temp_file_name("nonexistent.proj")) |
196 |
jonathan |
719 |
|
197 |
bh |
1811 |
def test_read_unreadable_file(self): |
198 |
|
|
"""Test read_proj_file with unreadable file |
199 |
|
|
|
200 |
|
|
As currently written this only works on unix-like systems and |
201 |
|
|
not e.g. on MS Windows. |
202 |
|
|
""" |
203 |
bh |
1813 |
filename = self.filename() |
204 |
jonathan |
719 |
file = open(filename, "w") |
205 |
|
|
file.close() |
206 |
|
|
os.chmod(filename, 0200) # write-only |
207 |
jonathan |
1167 |
self.assertRaises(IOError, resource.read_proj_file, filename) |
208 |
jonathan |
719 |
|
209 |
bh |
1811 |
def test_read_empty_file(self): |
210 |
|
|
"""Test read_proj_file with empty file""" |
211 |
bh |
1813 |
filename = self.filename() |
212 |
jonathan |
719 |
file = open(filename, "w") |
213 |
|
|
file.close() |
214 |
|
|
|
215 |
jonathan |
1167 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
216 |
jonathan |
719 |
|
217 |
bh |
1787 |
def test_get_system_proj_file(self): |
218 |
|
|
"""Test resource.get_system_proj_file() |
219 |
jonathan |
698 |
|
220 |
bh |
1787 |
This is primarily to test whether the system proj file contains |
221 |
|
|
invalid projection paramers and whether the proj file is not |
222 |
|
|
empty |
223 |
|
|
""" |
224 |
|
|
projfile, warnings = resource.get_system_proj_file() |
225 |
|
|
self.assertEquals(warnings, []) |
226 |
|
|
self.assert_(len(projfile.GetProjections()) > 0) |
227 |
|
|
|
228 |
|
|
|
229 |
bh |
1821 |
class WriteProjFileTests(ProjFileTest): |
230 |
bh |
1813 |
|
231 |
|
|
"""Test cases for writing proj files""" |
232 |
|
|
|
233 |
|
|
def compare_xml(self, xml1, xml2): |
234 |
|
|
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
235 |
|
|
|
236 |
bh |
1818 |
def doTestWrite(self, projfile, expected): |
237 |
bh |
1813 |
filename = self.filename() |
238 |
|
|
|
239 |
bh |
1818 |
resource.write_proj_file(projfile) |
240 |
bh |
1813 |
|
241 |
|
|
file = open(filename) |
242 |
|
|
written_contents = file.read() |
243 |
|
|
file.close() |
244 |
|
|
self.compare_xml(written_contents, expected) |
245 |
|
|
self.validate_data(written_contents) |
246 |
|
|
self.validate_data(expected) |
247 |
|
|
|
248 |
|
|
def test_write(self): |
249 |
|
|
"""Test write_proj_file""" |
250 |
bh |
1818 |
pf = ProjFile(self.filename()) |
251 |
|
|
pf.Add(Projection(['proj=tmerc', 'ellps=clrk66', |
252 |
|
|
'lat_0=90w', 'lon_0=90w', 'k=1'], |
253 |
|
|
"Transverse Mercator",)) |
254 |
|
|
pf.Add(Projection(["proj=tmerc", |
255 |
|
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
256 |
|
|
"k=0.999500", "x_0=400000.000", "y_0=0.000", |
257 |
|
|
"ellps=clrk80", "units=m"], |
258 |
|
|
"Anguilla 1957 / British West Indies Grid", |
259 |
|
|
epsg="200")) |
260 |
|
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
261 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
262 |
|
|
<projectionlist> |
263 |
|
|
<projection name="Transverse Mercator"> |
264 |
|
|
<parameter value="proj=tmerc"/> |
265 |
|
|
<parameter value="ellps=clrk66"/> |
266 |
|
|
<parameter value="lat_0=90w"/> |
267 |
|
|
<parameter value="lon_0=90w"/> |
268 |
|
|
<parameter value="k=1"/> |
269 |
|
|
</projection> |
270 |
|
|
<projection epsg="200" |
271 |
|
|
name="Anguilla 1957 / British West Indies Grid"> |
272 |
|
|
<parameter value="proj=tmerc"/> |
273 |
|
|
<parameter value="lat_0=0.000000000"/> |
274 |
|
|
<parameter value="lon_0=-62.000000000"/> |
275 |
|
|
<parameter value="k=0.999500"/> |
276 |
|
|
<parameter value="x_0=400000.000"/> |
277 |
|
|
<parameter value="y_0=0.000"/> |
278 |
|
|
<parameter value="ellps=clrk80"/> |
279 |
|
|
<parameter value="units=m"/> |
280 |
|
|
</projection> |
281 |
|
|
</projectionlist> |
282 |
|
|
''' |
283 |
|
|
self.doTestWrite(pf, file_contents) |
284 |
bh |
1813 |
|
285 |
|
|
def test_write_empty_file(self): |
286 |
|
|
"""Test write empty ProjFile""" |
287 |
bh |
1818 |
pf = ProjFile(self.filename()) |
288 |
|
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
289 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
290 |
|
|
<projectionlist> |
291 |
|
|
</projectionlist> |
292 |
|
|
''' |
293 |
|
|
self.doTestWrite(pf, file_contents) |
294 |
bh |
1813 |
|
295 |
|
|
|
296 |
bh |
1818 |
class TestLoadingProjFile(support.FileLoadTestCase): |
297 |
|
|
|
298 |
|
|
file_extension = ".proj" |
299 |
|
|
file_contents = '''\ |
300 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
301 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
302 |
|
|
<projectionlist> |
303 |
|
|
<projection name="Transverse Mercator"> |
304 |
|
|
<parameter value="proj=tmerc"/> |
305 |
|
|
<parameter value="ellps=clrk66"/> |
306 |
|
|
<parameter value="lat_0=90w"/> |
307 |
|
|
<parameter value="lon_0=90w"/> |
308 |
|
|
<parameter value="k=1"/> |
309 |
|
|
</projection> |
310 |
|
|
<projection epsg="200" name="Anguilla 1957 / British West Indies Grid"> |
311 |
|
|
<parameter value="proj=tmerc"/> |
312 |
|
|
<parameter value="lat_0=0.000000000"/> |
313 |
|
|
<parameter value="lon_0=-62.000000000"/> |
314 |
|
|
<parameter value="k=0.999500"/> |
315 |
|
|
<parameter value="x_0=400000.000"/> |
316 |
|
|
<parameter value="y_0=0.000"/> |
317 |
|
|
<parameter value="ellps=clrk80"/> |
318 |
|
|
<parameter value="units=m"/> |
319 |
|
|
</projection> |
320 |
|
|
</projectionlist> |
321 |
|
|
''' |
322 |
|
|
|
323 |
|
|
def check_projection(self, proj, label, parameters): |
324 |
|
|
"""Check the values of the proj's label and parameters""" |
325 |
|
|
self.assertEquals(proj.Label(), label) |
326 |
|
|
params = proj.GetAllParameters()[:] |
327 |
|
|
params.sort() |
328 |
|
|
self.assertEquals(params, parameters) |
329 |
|
|
|
330 |
|
|
def test(self): |
331 |
|
|
projfile, warnings = resource.read_proj_file(self.filename()) |
332 |
|
|
# no warnings |
333 |
|
|
self.assertEquals(warnings, []) |
334 |
|
|
|
335 |
|
|
# There are two projections |
336 |
|
|
projs = projfile.GetProjections() |
337 |
|
|
self.assertEquals(len(projs), 2) |
338 |
|
|
|
339 |
|
|
self.check_projection(projs[0], |
340 |
|
|
"Transverse Mercator", |
341 |
|
|
['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w', |
342 |
|
|
'proj=tmerc']) |
343 |
|
|
self.check_projection(projs[1], |
344 |
|
|
"EPSG 200 Anguilla 1957 / British West Indies Grid", |
345 |
|
|
["ellps=clrk80", "k=0.999500", |
346 |
|
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
347 |
|
|
"proj=tmerc", "units=m", |
348 |
|
|
"x_0=400000.000", "y_0=0.000"]) |
349 |
|
|
|
350 |
|
|
|
351 |
|
|
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
352 |
|
|
|
353 |
|
|
file_extension = ".proj" |
354 |
|
|
file_contents = '''\ |
355 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
356 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
357 |
|
|
<projectionlist> |
358 |
|
|
</projectionlist> |
359 |
|
|
''' |
360 |
|
|
|
361 |
|
|
def test(self): |
362 |
|
|
projfile, warnings = resource.read_proj_file(self.filename()) |
363 |
|
|
# no warnings |
364 |
|
|
self.assertEquals(warnings, []) |
365 |
|
|
|
366 |
|
|
# There are no projections |
367 |
|
|
self.assertEquals(len(projfile.GetProjections()), 0) |
368 |
|
|
|
369 |
|
|
|
370 |
bh |
1787 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
371 |
|
|
support.FileLoadTestCase): |
372 |
|
|
|
373 |
|
|
file_extension = ".proj" |
374 |
|
|
file_contents = '''\ |
375 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
376 |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
377 |
|
|
<projectionlist> |
378 |
|
|
<projection name="Universal Transverse Mercator"> |
379 |
|
|
<parameter value="proj=utm"/> |
380 |
|
|
<parameter value="ellps=clrk66"/> |
381 |
|
|
<!-- an invalid zone number to trigger the parameter checking |
382 |
|
|
in the proj library --> |
383 |
|
|
<parameter value="zone=1000"/> |
384 |
|
|
</projection> |
385 |
|
|
<projection name="Transverse Mercator"> |
386 |
|
|
<parameter value="proj=tmerc"/> |
387 |
|
|
<parameter value="ellps=clrk66"/> |
388 |
|
|
<parameter value="lat_0=90w"/> |
389 |
|
|
<parameter value="lon_0=90w"/> |
390 |
|
|
<parameter value="k=1"/> |
391 |
|
|
</projection> |
392 |
|
|
</projectionlist> |
393 |
|
|
''' |
394 |
|
|
|
395 |
|
|
def setUp(self): |
396 |
|
|
support.FileLoadTestCase.setUp(self) |
397 |
|
|
|
398 |
|
|
def test(self): |
399 |
|
|
"""Test reading a proj file with invalid parameters""" |
400 |
|
|
projfile, warnings = resource.read_proj_file(self.filename()) |
401 |
|
|
projs = projfile.GetProjections() |
402 |
|
|
self.assertEquals(len(projs), 1) |
403 |
|
|
params = projs[0].GetAllParameters()[:] |
404 |
|
|
params.sort() |
405 |
|
|
self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w', |
406 |
|
|
'lon_0=90w', 'proj=tmerc']) |
407 |
|
|
self.assertEquals(warnings, |
408 |
|
|
['Error in projection "Universal Transverse Mercator":' |
409 |
|
|
' invalid UTM zone number']) |
410 |
|
|
|
411 |
|
|
|
412 |
bh |
333 |
if __name__ == "__main__": |
413 |
|
|
unittest.main() |