1 |
bh |
333 |
# 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 |
jonathan |
719 |
import os |
18 |
bh |
333 |
|
19 |
|
|
import support |
20 |
|
|
support.initthuban() |
21 |
|
|
|
22 |
jonathan |
698 |
from Thuban.Model.proj import Projection, ProjFile |
23 |
bh |
333 |
|
24 |
jonathan |
698 |
import Thuban.Model.resource as resource |
25 |
bh |
333 |
|
26 |
jonathan |
698 |
from test_save import sax_eventlist |
27 |
|
|
|
28 |
jonathan |
719 |
from xml.sax import SAXParseException |
29 |
jonathan |
698 |
|
30 |
jonathan |
719 |
|
31 |
bh |
333 |
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 |
jonathan |
714 |
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 |
bh |
333 |
|
59 |
|
|
|
60 |
jonathan |
698 |
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 testRead(self): |
118 |
|
|
"""Test ReadProjFile""" |
119 |
|
|
|
120 |
|
|
self.doTestRead(sample_projfile_data, sample_projfile) |
121 |
|
|
self.doTestRead(sample_projfile_data2, sample_projfile2) |
122 |
|
|
|
123 |
jonathan |
719 |
# |
124 |
|
|
# file doesn't exist |
125 |
|
|
# |
126 |
|
|
self.assertRaises(IOError, |
127 |
|
|
resource.ReadProjFile, self.temp_file_name("nonexistent.proj")) |
128 |
|
|
|
129 |
|
|
# |
130 |
|
|
# file isn't readable |
131 |
|
|
# |
132 |
|
|
filename = self.temp_file_name("projfile.proj") |
133 |
|
|
file = open(filename, "w") |
134 |
|
|
file.close() |
135 |
|
|
os.chmod(filename, 0200) # write-only |
136 |
|
|
self.assertRaises(IOError, resource.ReadProjFile, filename) |
137 |
|
|
os.chmod(filename, 0600) # read/write so we reuse the file |
138 |
|
|
|
139 |
|
|
# |
140 |
|
|
# file has invalid XML (or none at all) |
141 |
|
|
# |
142 |
|
|
filename = self.temp_file_name("projfile.proj") |
143 |
|
|
file = open(filename, "w") |
144 |
|
|
file.close() |
145 |
|
|
|
146 |
|
|
self.assertRaises(SAXParseException, resource.ReadProjFile, filename) |
147 |
|
|
|
148 |
jonathan |
698 |
def testWrite(self): |
149 |
|
|
"""Test WriteProjFile""" |
150 |
|
|
|
151 |
|
|
self.doTestWrite(sample_projfile_data, sample_projfile) |
152 |
|
|
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
153 |
|
|
|
154 |
|
|
def doTestWrite(self, data, expected): |
155 |
|
|
|
156 |
|
|
filename = self.temp_file_name("projfile.proj") |
157 |
|
|
|
158 |
|
|
pf = ProjFile(filename) |
159 |
|
|
for proj in data: |
160 |
|
|
pf.Add(Projection(proj[1], proj[0])) |
161 |
|
|
|
162 |
|
|
resource.WriteProjFile(pf) |
163 |
|
|
|
164 |
|
|
file = open(filename) |
165 |
|
|
written_contents = file.read() |
166 |
|
|
file.close() |
167 |
|
|
self.compare_xml(written_contents, expected) |
168 |
|
|
|
169 |
|
|
def doTestRead(self, data, input): |
170 |
|
|
|
171 |
|
|
filename = self.temp_file_name("projfile.proj") |
172 |
|
|
file = open(filename, "w") |
173 |
|
|
file.write(input) |
174 |
|
|
file.close() |
175 |
|
|
|
176 |
|
|
pf = resource.ReadProjFile(filename) |
177 |
|
|
|
178 |
|
|
eq = self.assertEquals |
179 |
|
|
|
180 |
|
|
eq(pf.GetFileName(), filename) |
181 |
|
|
|
182 |
|
|
for proj, d in zip(pf.GetProjections(), data): |
183 |
|
|
eq(proj.GetName(), d[0]) |
184 |
jonathan |
714 |
for param in proj.GetAllParameters(): |
185 |
jonathan |
698 |
self.assert_(param in d[1]) |
186 |
|
|
|
187 |
|
|
|
188 |
bh |
333 |
if __name__ == "__main__": |
189 |
|
|
unittest.main() |