1 |
# Copyright (c) 2002 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 |
|
18 |
import support |
19 |
support.initthuban() |
20 |
|
21 |
from Thuban.Model.proj import Projection, ProjFile |
22 |
|
23 |
import Thuban.Model.resource as resource |
24 |
|
25 |
from test_save import sax_eventlist |
26 |
|
27 |
|
28 |
class TestProjection(unittest.TestCase, support.FloatComparisonMixin): |
29 |
|
30 |
"""Test cases for the Thuban-specific Projection class |
31 |
""" |
32 |
|
33 |
def test(self): |
34 |
"""Test Projection""" |
35 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
36 |
self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"]) |
37 |
|
38 |
# It's not clear whether this value is really the correct one |
39 |
# but a test failure here probably still means a bug somewhere |
40 |
self.assertFloatSeqEqual(proj.Forward(0, 0), |
41 |
[3623101.8103431347, 0.0], |
42 |
epsilon = 1e-5) |
43 |
self.assertFloatSeqEqual(proj.Inverse(3623101.8103431347, 0.0), |
44 |
[-0.00065775699878736467, 0]) |
45 |
|
46 |
self.assertFloatSeqEqual(proj.ForwardBBox((0, 0, 2, 2)), |
47 |
(3620891.3077618643, 0.0, |
48 |
3875381.8535437919, 252962.10480170773), |
49 |
epsilon = 1e-5) |
50 |
|
51 |
|
52 |
|
53 |
sample_projfile = '''\ |
54 |
<?xml version="1.0" encoding="UTF-8"?> |
55 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
56 |
<projectionlist> |
57 |
<projection name="Transverse Mercartor"> |
58 |
<parameter value="proj=tmerc"/> |
59 |
<parameter value="ellps=clrk66"/> |
60 |
<parameter value="lat_0=90w"/> |
61 |
<parameter value="lon_0=90w"/> |
62 |
<parameter value="k=1"/> |
63 |
</projection> |
64 |
<projection name="Transverse Mercartor"> |
65 |
<parameter value="proj=tmerc"/> |
66 |
<parameter value="ellps=clrk66"/> |
67 |
<parameter value="lat_0=30w"/> |
68 |
<parameter value="lon_0=30w"/> |
69 |
<parameter value="k=1"/> |
70 |
</projection> |
71 |
<projection name="Universal Transverse Mercartor"> |
72 |
<parameter value="proj=utm"/> |
73 |
<parameter value="ellps=clrk66"/> |
74 |
<parameter value="zone=1"/> |
75 |
</projection> |
76 |
</projectionlist> |
77 |
''' |
78 |
|
79 |
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
80 |
"ellps=clrk66", |
81 |
"lat_0=90w", |
82 |
"lon_0=90w", |
83 |
"k=1"]), |
84 |
("Transverse Mercartor", ["proj=tmerc", |
85 |
"ellps=clrk66", |
86 |
"lat_0=30w", |
87 |
"lon_0=30w", |
88 |
"k=1"]), |
89 |
("Universal Transverse Mercartor", ["proj=utm", |
90 |
"ellps=clrk66", |
91 |
"zone=1"])] |
92 |
|
93 |
sample_projfile2 = '''\ |
94 |
<?xml version="1.0" encoding="UTF-8"?> |
95 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
96 |
<projectionlist> |
97 |
</projectionlist> |
98 |
''' |
99 |
|
100 |
sample_projfile_data2 = [] |
101 |
|
102 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
103 |
|
104 |
"""Test cases for reading and writing projection files. |
105 |
""" |
106 |
|
107 |
def compare_xml(self, xml1, xml2): |
108 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
109 |
|
110 |
def testRead(self): |
111 |
"""Test ReadProjFile""" |
112 |
|
113 |
self.doTestRead(sample_projfile_data, sample_projfile) |
114 |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
115 |
|
116 |
def testWrite(self): |
117 |
"""Test WriteProjFile""" |
118 |
|
119 |
self.doTestWrite(sample_projfile_data, sample_projfile) |
120 |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
121 |
|
122 |
def doTestWrite(self, data, expected): |
123 |
|
124 |
filename = self.temp_file_name("projfile.proj") |
125 |
|
126 |
pf = ProjFile(filename) |
127 |
for proj in data: |
128 |
pf.Add(Projection(proj[1], proj[0])) |
129 |
|
130 |
resource.WriteProjFile(pf) |
131 |
|
132 |
file = open(filename) |
133 |
written_contents = file.read() |
134 |
file.close() |
135 |
self.compare_xml(written_contents, expected) |
136 |
|
137 |
def doTestRead(self, data, input): |
138 |
|
139 |
filename = self.temp_file_name("projfile.proj") |
140 |
file = open(filename, "w") |
141 |
file.write(input) |
142 |
file.close() |
143 |
|
144 |
pf = resource.ReadProjFile(filename) |
145 |
|
146 |
eq = self.assertEquals |
147 |
|
148 |
eq(pf.GetFileName(), filename) |
149 |
|
150 |
for proj, d in zip(pf.GetProjections(), data): |
151 |
eq(proj.GetName(), d[0]) |
152 |
for param in proj.GetParameters(): |
153 |
self.assert_(param in d[1]) |
154 |
|
155 |
|
156 |
if __name__ == "__main__": |
157 |
unittest.main() |