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