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