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 |
|
|
|
18 |
|
|
import support |
19 |
|
|
support.initthuban() |
20 |
|
|
|
21 |
jonathan |
698 |
from Thuban.Model.proj import Projection, ProjFile |
22 |
bh |
333 |
|
23 |
jonathan |
698 |
import Thuban.Model.resource as resource |
24 |
bh |
333 |
|
25 |
jonathan |
698 |
from test_save import sax_eventlist |
26 |
|
|
|
27 |
|
|
|
28 |
bh |
333 |
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 |
jonathan |
714 |
self.assertEquals(proj.GetParameter("zone"), "26") |
52 |
|
|
self.assertEquals(proj.GetParameter("proj"), "utm") |
53 |
|
|
self.assertEquals(proj.GetParameter("ellps"), "clrk66") |
54 |
|
|
self.assertEquals(proj.GetParameter("hallo"), "") |
55 |
bh |
333 |
|
56 |
|
|
|
57 |
jonathan |
698 |
sample_projfile = '''\ |
58 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
59 |
|
|
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
60 |
|
|
<projectionlist> |
61 |
|
|
<projection name="Transverse Mercartor"> |
62 |
|
|
<parameter value="proj=tmerc"/> |
63 |
|
|
<parameter value="ellps=clrk66"/> |
64 |
|
|
<parameter value="lat_0=90w"/> |
65 |
|
|
<parameter value="lon_0=90w"/> |
66 |
|
|
<parameter value="k=1"/> |
67 |
|
|
</projection> |
68 |
|
|
<projection name="Transverse Mercartor"> |
69 |
|
|
<parameter value="proj=tmerc"/> |
70 |
|
|
<parameter value="ellps=clrk66"/> |
71 |
|
|
<parameter value="lat_0=30w"/> |
72 |
|
|
<parameter value="lon_0=30w"/> |
73 |
|
|
<parameter value="k=1"/> |
74 |
|
|
</projection> |
75 |
|
|
<projection name="Universal Transverse Mercartor"> |
76 |
|
|
<parameter value="proj=utm"/> |
77 |
|
|
<parameter value="ellps=clrk66"/> |
78 |
|
|
<parameter value="zone=1"/> |
79 |
|
|
</projection> |
80 |
|
|
</projectionlist> |
81 |
|
|
''' |
82 |
|
|
|
83 |
|
|
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
84 |
|
|
"ellps=clrk66", |
85 |
|
|
"lat_0=90w", |
86 |
|
|
"lon_0=90w", |
87 |
|
|
"k=1"]), |
88 |
|
|
("Transverse Mercartor", ["proj=tmerc", |
89 |
|
|
"ellps=clrk66", |
90 |
|
|
"lat_0=30w", |
91 |
|
|
"lon_0=30w", |
92 |
|
|
"k=1"]), |
93 |
|
|
("Universal Transverse Mercartor", ["proj=utm", |
94 |
|
|
"ellps=clrk66", |
95 |
|
|
"zone=1"])] |
96 |
|
|
|
97 |
|
|
sample_projfile2 = '''\ |
98 |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
99 |
|
|
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
100 |
|
|
<projectionlist> |
101 |
|
|
</projectionlist> |
102 |
|
|
''' |
103 |
|
|
|
104 |
|
|
sample_projfile_data2 = [] |
105 |
|
|
|
106 |
|
|
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
107 |
|
|
|
108 |
|
|
"""Test cases for reading and writing projection files. |
109 |
|
|
""" |
110 |
|
|
|
111 |
|
|
def compare_xml(self, xml1, xml2): |
112 |
|
|
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
113 |
|
|
|
114 |
|
|
def testRead(self): |
115 |
|
|
"""Test ReadProjFile""" |
116 |
|
|
|
117 |
|
|
self.doTestRead(sample_projfile_data, sample_projfile) |
118 |
|
|
self.doTestRead(sample_projfile_data2, sample_projfile2) |
119 |
|
|
|
120 |
|
|
def testWrite(self): |
121 |
|
|
"""Test WriteProjFile""" |
122 |
|
|
|
123 |
|
|
self.doTestWrite(sample_projfile_data, sample_projfile) |
124 |
|
|
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
125 |
|
|
|
126 |
|
|
def doTestWrite(self, data, expected): |
127 |
|
|
|
128 |
|
|
filename = self.temp_file_name("projfile.proj") |
129 |
|
|
|
130 |
|
|
pf = ProjFile(filename) |
131 |
|
|
for proj in data: |
132 |
|
|
pf.Add(Projection(proj[1], proj[0])) |
133 |
|
|
|
134 |
|
|
resource.WriteProjFile(pf) |
135 |
|
|
|
136 |
|
|
file = open(filename) |
137 |
|
|
written_contents = file.read() |
138 |
|
|
file.close() |
139 |
|
|
self.compare_xml(written_contents, expected) |
140 |
|
|
|
141 |
|
|
def doTestRead(self, data, input): |
142 |
|
|
|
143 |
|
|
filename = self.temp_file_name("projfile.proj") |
144 |
|
|
file = open(filename, "w") |
145 |
|
|
file.write(input) |
146 |
|
|
file.close() |
147 |
|
|
|
148 |
|
|
pf = resource.ReadProjFile(filename) |
149 |
|
|
|
150 |
|
|
eq = self.assertEquals |
151 |
|
|
|
152 |
|
|
eq(pf.GetFileName(), filename) |
153 |
|
|
|
154 |
|
|
for proj, d in zip(pf.GetProjections(), data): |
155 |
|
|
eq(proj.GetName(), d[0]) |
156 |
jonathan |
714 |
for param in proj.GetAllParameters(): |
157 |
jonathan |
698 |
self.assert_(param in d[1]) |
158 |
|
|
|
159 |
|
|
|
160 |
bh |
333 |
if __name__ == "__main__": |
161 |
|
|
unittest.main() |