1 |
# Copyright (c) 2002 by Intevation GmbH |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
16 |
import unittest |
import unittest |
17 |
import os |
import os |
18 |
|
|
19 |
|
import xmlsupport |
20 |
import support |
import support |
21 |
support.initthuban() |
support.initthuban() |
22 |
|
|
23 |
|
from Thuban import _ |
24 |
from Thuban.Model.proj import Projection, ProjFile |
from Thuban.Model.proj import Projection, ProjFile |
25 |
|
|
26 |
import Thuban.Model.resource as resource |
import Thuban.Model.resource as resource |
27 |
|
|
28 |
from test_save import sax_eventlist |
from xmlsupport import sax_eventlist |
29 |
|
|
30 |
from xml.sax import SAXParseException |
from xml.sax import SAXParseException |
31 |
|
|
37 |
|
|
38 |
def test(self): |
def test(self): |
39 |
"""Test Projection""" |
"""Test Projection""" |
40 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
params = ["zone=26", "proj=utm", "ellps=clrk66"] |
41 |
self.assertEquals(proj.params, ["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(params) |
42 |
|
self.assertEquals(proj.params, params) |
43 |
|
|
44 |
# It's not clear whether this value is really the correct one |
# It's not clear whether this value is really the correct one |
45 |
# but a test failure here probably still means a bug somewhere |
# but a test failure here probably still means a bug somewhere |
54 |
3875381.8535437919, 252962.10480170773), |
3875381.8535437919, 252962.10480170773), |
55 |
epsilon = 1e-5) |
epsilon = 1e-5) |
56 |
|
|
57 |
|
# GetName() |
58 |
|
self.assertEquals(proj.GetName(), _("Unknown")) |
59 |
|
|
60 |
|
# GetParameter() |
61 |
self.assertEquals(proj.GetParameter("zone"), "26") |
self.assertEquals(proj.GetParameter("zone"), "26") |
62 |
self.assertEquals(proj.GetParameter("proj"), "utm") |
self.assertEquals(proj.GetParameter("proj"), "utm") |
63 |
self.assertEquals(proj.GetParameter("ellps"), "clrk66") |
self.assertEquals(proj.GetParameter("ellps"), "clrk66") |
64 |
self.assertEquals(proj.GetParameter("hallo"), "") |
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 = '''\ |
sample_projfile = '''\ |
75 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
76 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
77 |
<projectionlist> |
<projectionlist> |
78 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
79 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
80 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
81 |
<parameter value="lat_0=90w"/> |
<parameter value="lat_0=90w"/> |
82 |
<parameter value="lon_0=90w"/> |
<parameter value="lon_0=90w"/> |
83 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
84 |
</projection> |
</projection> |
85 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
86 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
87 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
88 |
<parameter value="lat_0=30w"/> |
<parameter value="lat_0=30w"/> |
89 |
<parameter value="lon_0=30w"/> |
<parameter value="lon_0=30w"/> |
90 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
91 |
</projection> |
</projection> |
92 |
<projection name="Universal Transverse Mercartor"> |
<projection name="Universal Transverse Mercator"> |
93 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
94 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
95 |
<parameter value="zone=1"/> |
<parameter value="zone=1"/> |
97 |
</projectionlist> |
</projectionlist> |
98 |
''' |
''' |
99 |
|
|
100 |
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
101 |
"ellps=clrk66", |
"ellps=clrk66", |
102 |
"lat_0=90w", |
"lat_0=90w", |
103 |
"lon_0=90w", |
"lon_0=90w", |
104 |
"k=1"]), |
"k=1"]), |
105 |
("Transverse Mercartor", ["proj=tmerc", |
("Transverse Mercator", ["proj=tmerc", |
106 |
"ellps=clrk66", |
"ellps=clrk66", |
107 |
"lat_0=30w", |
"lat_0=30w", |
108 |
"lon_0=30w", |
"lon_0=30w", |
109 |
"k=1"]), |
"k=1"]), |
110 |
("Universal Transverse Mercartor", ["proj=utm", |
("Universal Transverse Mercator", ["proj=utm", |
111 |
"ellps=clrk66", |
"ellps=clrk66", |
112 |
"zone=1"])] |
"zone=1"])] |
113 |
|
|
114 |
sample_projfile2 = '''\ |
sample_projfile2 = '''\ |
115 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
116 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
117 |
<projectionlist> |
<projectionlist> |
118 |
</projectionlist> |
</projectionlist> |
119 |
''' |
''' |
120 |
|
|
121 |
sample_projfile_data2 = [] |
sample_projfile_data2 = [] |
122 |
|
|
123 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
124 |
|
xmlsupport.ValidationTest): |
125 |
|
|
126 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
127 |
""" |
""" |
134 |
|
|
135 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
136 |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
137 |
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
138 |
|
"lat_1=0", "lat_2=20"]) |
139 |
|
|
140 |
eq = self.assertEquals |
eq = self.assertEquals |
141 |
|
|
142 |
|
|
143 |
# |
# |
144 |
# __init__() |
# __init__() |
145 |
# GetFilename() |
# GetFilename() |
166 |
eq(len(projFile.GetProjections()), 0) |
eq(len(projFile.GetProjections()), 0) |
167 |
|
|
168 |
# try to remove something that doesn't exist |
# try to remove something that doesn't exist |
169 |
|
self.assertRaises(ValueError, projFile.Remove, None) |
170 |
self.assertRaises(ValueError, projFile.Remove, proj0) |
self.assertRaises(ValueError, projFile.Remove, proj0) |
171 |
|
|
172 |
projFile.Add(proj0) |
projFile.Add(proj0) |
180 |
eq(projs[1], proj1) |
eq(projs[1], proj1) |
181 |
eq(projs[2], proj2) |
eq(projs[2], proj2) |
182 |
|
|
183 |
|
projFile.Remove(proj2) |
184 |
|
projFile.Remove(proj1) |
185 |
|
|
186 |
|
# Replace() |
187 |
|
projFile.Replace(proj0, proj1) |
188 |
|
projs = projFile.GetProjections() |
189 |
|
eq(projs[0], proj1) |
190 |
|
|
191 |
|
# replace a non-existent projection |
192 |
|
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
193 |
|
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
194 |
|
|
195 |
def testRead(self): |
def testRead(self): |
196 |
"""Test ReadProjFile""" |
"""Test read_proj_file""" |
197 |
|
|
198 |
self.doTestRead(sample_projfile_data, sample_projfile) |
self.doTestRead(sample_projfile_data, sample_projfile) |
199 |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
202 |
# file doesn't exist |
# file doesn't exist |
203 |
# |
# |
204 |
self.assertRaises(IOError, |
self.assertRaises(IOError, |
205 |
resource.ReadProjFile, self.temp_file_name("nonexistent.proj")) |
resource.read_proj_file, self.temp_file_name("nonexistent.proj")) |
206 |
|
|
207 |
# |
# |
208 |
# file isn't readable |
# file isn't readable |
211 |
file = open(filename, "w") |
file = open(filename, "w") |
212 |
file.close() |
file.close() |
213 |
os.chmod(filename, 0200) # write-only |
os.chmod(filename, 0200) # write-only |
214 |
self.assertRaises(IOError, resource.ReadProjFile, filename) |
self.assertRaises(IOError, resource.read_proj_file, filename) |
215 |
os.chmod(filename, 0600) # read/write so we reuse the file |
os.chmod(filename, 0600) # read/write so we reuse the file |
216 |
|
|
217 |
# |
# |
221 |
file = open(filename, "w") |
file = open(filename, "w") |
222 |
file.close() |
file.close() |
223 |
|
|
224 |
self.assertRaises(SAXParseException, resource.ReadProjFile, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
225 |
|
|
226 |
def testWrite(self): |
def testWrite(self): |
227 |
"""Test WriteProjFile""" |
"""Test write_proj_file""" |
228 |
|
|
229 |
self.doTestWrite(sample_projfile_data, sample_projfile) |
self.doTestWrite(sample_projfile_data, sample_projfile) |
230 |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
237 |
for proj in data: |
for proj in data: |
238 |
pf.Add(Projection(proj[1], proj[0])) |
pf.Add(Projection(proj[1], proj[0])) |
239 |
|
|
240 |
resource.WriteProjFile(pf) |
resource.write_proj_file(pf) |
241 |
|
|
242 |
file = open(filename) |
file = open(filename) |
243 |
written_contents = file.read() |
written_contents = file.read() |
244 |
file.close() |
file.close() |
245 |
self.compare_xml(written_contents, expected) |
self.compare_xml(written_contents, expected) |
246 |
|
self.validate_data(written_contents) |
247 |
|
self.validate_data(expected) |
248 |
|
|
249 |
def doTestRead(self, data, input): |
def doTestRead(self, data, input): |
250 |
|
|
253 |
file.write(input) |
file.write(input) |
254 |
file.close() |
file.close() |
255 |
|
|
256 |
pf = resource.ReadProjFile(filename) |
pf = resource.read_proj_file(filename) |
257 |
|
|
258 |
eq = self.assertEquals |
eq = self.assertEquals |
259 |
|
|