13 |
# $Source$ |
# $Source$ |
14 |
# $Id$ |
# $Id$ |
15 |
|
|
|
from Thuban import _ |
|
|
|
|
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.Model.proj import Projection, ProjFile |
from Thuban import _ |
24 |
|
from Thuban.Model.proj import Projection, ProjFile, \ |
25 |
|
PROJ_UNITS_METERS, PROJ_UNITS_DEGREES |
26 |
|
|
27 |
import Thuban.Model.resource as resource |
import Thuban.Model.resource as resource |
28 |
|
|
71 |
proj = Projection(params, "MyName") |
proj = Projection(params, "MyName") |
72 |
self.assertEquals(proj.GetName(), "MyName") |
self.assertEquals(proj.GetName(), "MyName") |
73 |
|
|
74 |
|
def test_get_parameter_without_equals_sign(self): |
75 |
|
"""Test Projection.GetParameter() for a parameter without '=' sign""" |
76 |
|
proj = Projection(["proj=utm", "zone=34", "south", "ellps=clrk66"]) |
77 |
|
# The Projection class pretends that for parameters specified |
78 |
|
# without a value the value is the same as the parameter name. |
79 |
|
self.assertEquals(proj.GetParameter("south"), "south") |
80 |
|
|
81 |
|
def test_get_projection_units_geo(self): |
82 |
|
"""Test Projection.GetProjectedUnits() for geographic projection""" |
83 |
|
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
84 |
|
"ellps=clrk66"]) |
85 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
86 |
|
|
87 |
|
def test_get_projection_units_normal(self): |
88 |
|
"""Test Projection.GetProjectedUnits() for normal projection""" |
89 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
90 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
91 |
|
|
92 |
|
def test_label(self): |
93 |
|
"""Test Projection.Label() without epsg""" |
94 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
95 |
|
name = "My Projection") |
96 |
|
self.assertEquals(proj.Label(), "My Projection") |
97 |
|
|
98 |
|
def test_label_epsg(self): |
99 |
|
"""Test Projection.Label() with epsg""" |
100 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
101 |
|
name = "My Projection", epsg="42") |
102 |
|
self.assertEquals(proj.Label(), "EPSG 42 My Projection") |
103 |
|
|
104 |
|
def test_epsgcode_for_non_epsg_projection(self): |
105 |
|
"""Test Projection.EPSGCode() without epsg""" |
106 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
107 |
|
name = "My Projection") |
108 |
|
self.assertEquals(proj.EPSGCode(), None) |
109 |
|
|
110 |
|
def test_epsgcode_for_real_epsg_projection(self): |
111 |
|
"""Test Projection.EPSGCode() with epsg""" |
112 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
113 |
|
name = "My Projection", epsg="42") |
114 |
|
self.assertEquals(proj.EPSGCode(), "42") |
115 |
|
|
|
sample_projfile = '''\ |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
|
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
|
|
<projectionlist> |
|
|
<projection name="Transverse Mercartor"> |
|
|
<parameter value="proj=tmerc"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="lat_0=90w"/> |
|
|
<parameter value="lon_0=90w"/> |
|
|
<parameter value="k=1"/> |
|
|
</projection> |
|
|
<projection name="Transverse Mercartor"> |
|
|
<parameter value="proj=tmerc"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="lat_0=30w"/> |
|
|
<parameter value="lon_0=30w"/> |
|
|
<parameter value="k=1"/> |
|
|
</projection> |
|
|
<projection name="Universal Transverse Mercartor"> |
|
|
<parameter value="proj=utm"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="zone=1"/> |
|
|
</projection> |
|
|
</projectionlist> |
|
|
''' |
|
116 |
|
|
|
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
|
|
"ellps=clrk66", |
|
|
"lat_0=90w", |
|
|
"lon_0=90w", |
|
|
"k=1"]), |
|
|
("Transverse Mercartor", ["proj=tmerc", |
|
|
"ellps=clrk66", |
|
|
"lat_0=30w", |
|
|
"lon_0=30w", |
|
|
"k=1"]), |
|
|
("Universal Transverse Mercartor", ["proj=utm", |
|
|
"ellps=clrk66", |
|
|
"zone=1"])] |
|
117 |
|
|
118 |
sample_projfile2 = '''\ |
class TestProjFileSimple: |
119 |
<?xml version="1.0" encoding="UTF-8"?> |
|
120 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
def test_init(self): |
121 |
<projectionlist> |
"""Test ProjFile coinstructor""" |
122 |
</projectionlist> |
proj_file = ProjFile("some_filename") |
123 |
''' |
self.assertEquals(proj_file.GetFilename(), "some_filename") |
124 |
|
self.assertEquals(len(proj_file.GetProjections()), 0) |
125 |
|
|
126 |
|
def test_set_filename(self): |
127 |
|
"""Test ProjFile.SetFilename()""" |
128 |
|
proj_file = ProjFile("some_filename") |
129 |
|
proj.SetFilename("other_name") |
130 |
|
self.assertEquals(proj_file.GetFilename(), "other_name") |
131 |
|
|
|
sample_projfile_data2 = [] |
|
132 |
|
|
133 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
class TestProjFile(unittest.TestCase): |
134 |
|
|
135 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
136 |
""" |
""" |
137 |
|
|
138 |
def compare_xml(self, xml1, xml2): |
def setUp(self): |
139 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
140 |
|
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
141 |
|
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
142 |
|
"lat_1=0", "lat_2=20"]) |
143 |
|
|
144 |
|
def test_add_remove(self): |
145 |
|
"""Test ProjFile.Add() and ProjFile.Remove()""" |
146 |
|
proj_file = ProjFile("some_filename") |
147 |
|
proj_file.Add(self.proj0) |
148 |
|
proj_file.Add(self.proj1) |
149 |
|
self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1]) |
150 |
|
proj_file.Remove(self.proj0) |
151 |
|
self.assertEquals(proj_file.GetProjections(), [self.proj1]) |
152 |
|
|
153 |
|
def test_remove_non_existing(self): |
154 |
|
"""Test ProjFile.Remove(<proj not in projfile>)""" |
155 |
|
proj_file = ProjFile("some_filename") |
156 |
|
self.assertRaises(ValueError, proj_file.Remove, self.proj0) |
157 |
|
|
158 |
|
def test_replace(self): |
159 |
|
"""Test ProjFile.Replace()""" |
160 |
|
proj_file = ProjFile("some_filename") |
161 |
|
proj_file.Add(self.proj0) |
162 |
|
proj_file.Add(self.proj1) |
163 |
|
|
164 |
def test(self): |
# Replace() |
165 |
"""Test ProjFile""" |
proj_file.Replace(self.proj0, self.proj2) |
166 |
|
self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1]) |
167 |
|
|
168 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
def test_replace_non_existing(self): |
169 |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
170 |
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
proj_file = ProjFile("some_filename") |
171 |
|
proj_file.Add(self.proj0) |
172 |
eq = self.assertEquals |
proj_file.Add(self.proj1) |
173 |
|
self.assertRaises(ValueError, |
174 |
|
proj_file.Replace, self.proj2, self.proj0) |
|
# |
|
|
# __init__() |
|
|
# GetFilename() |
|
|
# SetFilename() |
|
|
# |
|
|
for name in ["", "hello_world"]: |
|
|
projFile = ProjFile(name) |
|
|
eq(projFile.GetFilename(), name) |
|
|
|
|
|
projFile.SetFilename("XXX") |
|
|
projFile.SetFilename(name) |
|
|
eq(projFile.GetFilename(), name) |
|
|
|
|
|
# initial number of projections should be 0 |
|
|
eq(len(projFile.GetProjections()), 0) |
|
|
|
|
|
# |
|
|
# Add() |
|
|
# Remove() |
|
|
# |
|
|
projFile.Add(proj0) |
|
|
eq(len(projFile.GetProjections()), 1) |
|
|
projFile.Remove(proj0) |
|
|
eq(len(projFile.GetProjections()), 0) |
|
|
|
|
|
# try to remove something that doesn't exist |
|
|
self.assertRaises(ValueError, projFile.Remove, None) |
|
|
self.assertRaises(ValueError, projFile.Remove, proj0) |
|
|
|
|
|
projFile.Add(proj0) |
|
|
projFile.Add(proj1) |
|
|
projFile.Add(proj2) |
|
|
eq(len(projFile.GetProjections()), 3) |
|
|
|
|
|
# GetProjections() -- tests order |
|
|
projs = projFile.GetProjections() |
|
|
eq(projs[0], proj0) |
|
|
eq(projs[1], proj1) |
|
|
eq(projs[2], proj2) |
|
175 |
|
|
|
projFile.Remove(proj2) |
|
|
projFile.Remove(proj1) |
|
176 |
|
|
177 |
# Replace() |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
178 |
projFile.Replace(proj0, proj1) |
xmlsupport.ValidationTest): |
179 |
projs = projFile.GetProjections() |
|
180 |
eq(projs[0], proj1) |
"""Base class for the proj file tests that read or write files""" |
181 |
|
|
182 |
# replace a non-existent projection |
def filename(self): |
183 |
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
"""Return the filename for the test""" |
184 |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
return self.temp_file_name(self.id() + ".proj") |
185 |
|
|
186 |
def testRead(self): |
|
187 |
"""Test read_proj_file""" |
class ProjFileReadTests(ProjFileTest): |
188 |
|
|
189 |
self.doTestRead(sample_projfile_data, sample_projfile) |
"""Test read ProjFile objects from files""" |
190 |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
|
191 |
|
def test_read_non_existing_file(self): |
192 |
# |
"""Test read_proj_file with non-existing file""" |
|
# file doesn't exist |
|
|
# |
|
193 |
self.assertRaises(IOError, |
self.assertRaises(IOError, |
194 |
resource.read_proj_file, self.temp_file_name("nonexistent.proj")) |
resource.read_proj_file, |
195 |
|
self.temp_file_name("nonexistent.proj")) |
196 |
|
|
197 |
|
def test_read_unreadable_file(self): |
198 |
|
"""Test read_proj_file with unreadable file |
199 |
|
|
200 |
# |
As currently written this only works on unix-like systems and |
201 |
# file isn't readable |
not e.g. on MS Windows. |
202 |
# |
""" |
203 |
filename = self.temp_file_name("projfile.proj") |
filename = self.filename() |
204 |
file = open(filename, "w") |
file = open(filename, "w") |
205 |
file.close() |
file.close() |
206 |
os.chmod(filename, 0200) # write-only |
os.chmod(filename, 0200) # write-only |
207 |
self.assertRaises(IOError, resource.read_proj_file, filename) |
self.assertRaises(IOError, resource.read_proj_file, filename) |
|
os.chmod(filename, 0600) # read/write so we reuse the file |
|
208 |
|
|
209 |
# |
def test_read_empty_file(self): |
210 |
# file has invalid XML (or none at all) |
"""Test read_proj_file with empty file""" |
211 |
# |
filename = self.filename() |
|
filename = self.temp_file_name("projfile.proj") |
|
212 |
file = open(filename, "w") |
file = open(filename, "w") |
213 |
file.close() |
file.close() |
214 |
|
|
215 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
216 |
|
|
217 |
def testWrite(self): |
def test_get_system_proj_file(self): |
218 |
"""Test write_proj_file""" |
"""Test resource.get_system_proj_file() |
219 |
|
|
220 |
|
This is primarily to test whether the system proj file contains |
221 |
|
invalid projection paramers and whether the proj file is not |
222 |
|
empty |
223 |
|
""" |
224 |
|
projfile, warnings = resource.get_system_proj_file() |
225 |
|
self.assertEquals(warnings, []) |
226 |
|
self.assert_(len(projfile.GetProjections()) > 0) |
227 |
|
|
|
self.doTestWrite(sample_projfile_data, sample_projfile) |
|
|
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
|
228 |
|
|
229 |
def doTestWrite(self, data, expected): |
class WriteProjFileTests(ProjFileTest): |
230 |
|
|
231 |
filename = self.temp_file_name("projfile.proj") |
"""Test cases for writing proj files""" |
232 |
|
|
233 |
pf = ProjFile(filename) |
def compare_xml(self, xml1, xml2): |
234 |
for proj in data: |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
235 |
pf.Add(Projection(proj[1], proj[0])) |
|
236 |
|
def doTestWrite(self, projfile, expected): |
237 |
|
filename = self.filename() |
238 |
|
|
239 |
resource.write_proj_file(pf) |
resource.write_proj_file(projfile) |
240 |
|
|
241 |
file = open(filename) |
file = open(filename) |
242 |
written_contents = file.read() |
written_contents = file.read() |
243 |
file.close() |
file.close() |
244 |
self.compare_xml(written_contents, expected) |
self.compare_xml(written_contents, expected) |
245 |
|
self.validate_data(written_contents) |
246 |
|
self.validate_data(expected) |
247 |
|
|
248 |
def doTestRead(self, data, input): |
def test_write(self): |
249 |
|
"""Test write_proj_file""" |
250 |
|
pf = ProjFile(self.filename()) |
251 |
|
pf.Add(Projection(['proj=tmerc', 'ellps=clrk66', |
252 |
|
'lat_0=90w', 'lon_0=90w', 'k=1'], |
253 |
|
"Transverse Mercator",)) |
254 |
|
pf.Add(Projection(["proj=tmerc", |
255 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
256 |
|
"k=0.999500", "x_0=400000.000", "y_0=0.000", |
257 |
|
"ellps=clrk80", "units=m"], |
258 |
|
"Anguilla 1957 / British West Indies Grid", |
259 |
|
epsg="200")) |
260 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
261 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
262 |
|
<projectionlist> |
263 |
|
<projection name="Transverse Mercator"> |
264 |
|
<parameter value="proj=tmerc"/> |
265 |
|
<parameter value="ellps=clrk66"/> |
266 |
|
<parameter value="lat_0=90w"/> |
267 |
|
<parameter value="lon_0=90w"/> |
268 |
|
<parameter value="k=1"/> |
269 |
|
</projection> |
270 |
|
<projection epsg="200" |
271 |
|
name="Anguilla 1957 / British West Indies Grid"> |
272 |
|
<parameter value="proj=tmerc"/> |
273 |
|
<parameter value="lat_0=0.000000000"/> |
274 |
|
<parameter value="lon_0=-62.000000000"/> |
275 |
|
<parameter value="k=0.999500"/> |
276 |
|
<parameter value="x_0=400000.000"/> |
277 |
|
<parameter value="y_0=0.000"/> |
278 |
|
<parameter value="ellps=clrk80"/> |
279 |
|
<parameter value="units=m"/> |
280 |
|
</projection> |
281 |
|
</projectionlist> |
282 |
|
''' |
283 |
|
self.doTestWrite(pf, file_contents) |
284 |
|
|
285 |
|
def test_write_empty_file(self): |
286 |
|
"""Test write empty ProjFile""" |
287 |
|
pf = ProjFile(self.filename()) |
288 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
289 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
290 |
|
<projectionlist> |
291 |
|
</projectionlist> |
292 |
|
''' |
293 |
|
self.doTestWrite(pf, file_contents) |
294 |
|
|
|
filename = self.temp_file_name("projfile.proj") |
|
|
file = open(filename, "w") |
|
|
file.write(input) |
|
|
file.close() |
|
295 |
|
|
296 |
pf = resource.read_proj_file(filename) |
class TestLoadingProjFile(support.FileLoadTestCase): |
297 |
|
|
298 |
|
file_extension = ".proj" |
299 |
|
file_contents = '''\ |
300 |
|
<?xml version="1.0" encoding="UTF-8"?> |
301 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
302 |
|
<projectionlist> |
303 |
|
<projection name="Transverse Mercator"> |
304 |
|
<parameter value="proj=tmerc"/> |
305 |
|
<parameter value="ellps=clrk66"/> |
306 |
|
<parameter value="lat_0=90w"/> |
307 |
|
<parameter value="lon_0=90w"/> |
308 |
|
<parameter value="k=1"/> |
309 |
|
</projection> |
310 |
|
<projection epsg="200" name="Anguilla 1957 / British West Indies Grid"> |
311 |
|
<parameter value="proj=tmerc"/> |
312 |
|
<parameter value="lat_0=0.000000000"/> |
313 |
|
<parameter value="lon_0=-62.000000000"/> |
314 |
|
<parameter value="k=0.999500"/> |
315 |
|
<parameter value="x_0=400000.000"/> |
316 |
|
<parameter value="y_0=0.000"/> |
317 |
|
<parameter value="ellps=clrk80"/> |
318 |
|
<parameter value="units=m"/> |
319 |
|
</projection> |
320 |
|
</projectionlist> |
321 |
|
''' |
322 |
|
|
323 |
|
def check_projection(self, proj, label, parameters): |
324 |
|
"""Check the values of the proj's label and parameters""" |
325 |
|
self.assertEquals(proj.Label(), label) |
326 |
|
params = proj.GetAllParameters()[:] |
327 |
|
params.sort() |
328 |
|
self.assertEquals(params, parameters) |
329 |
|
|
330 |
|
def test(self): |
331 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
332 |
|
# no warnings |
333 |
|
self.assertEquals(warnings, []) |
334 |
|
|
335 |
|
# There are two projections |
336 |
|
projs = projfile.GetProjections() |
337 |
|
self.assertEquals(len(projs), 2) |
338 |
|
|
339 |
|
self.check_projection(projs[0], |
340 |
|
"Transverse Mercator", |
341 |
|
['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w', |
342 |
|
'proj=tmerc']) |
343 |
|
self.check_projection(projs[1], |
344 |
|
"EPSG 200 Anguilla 1957 / British West Indies Grid", |
345 |
|
["ellps=clrk80", "k=0.999500", |
346 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
347 |
|
"proj=tmerc", "units=m", |
348 |
|
"x_0=400000.000", "y_0=0.000"]) |
349 |
|
|
|
eq = self.assertEquals |
|
350 |
|
|
351 |
eq(pf.GetFilename(), filename) |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
352 |
|
|
353 |
|
file_extension = ".proj" |
354 |
|
file_contents = '''\ |
355 |
|
<?xml version="1.0" encoding="UTF-8"?> |
356 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
357 |
|
<projectionlist> |
358 |
|
</projectionlist> |
359 |
|
''' |
360 |
|
|
361 |
for proj, d in zip(pf.GetProjections(), data): |
def test(self): |
362 |
eq(proj.GetName(), d[0]) |
projfile, warnings = resource.read_proj_file(self.filename()) |
363 |
for param in proj.GetAllParameters(): |
# no warnings |
364 |
self.assert_(param in d[1]) |
self.assertEquals(warnings, []) |
365 |
|
|
366 |
|
# There are no projections |
367 |
|
self.assertEquals(len(projfile.GetProjections()), 0) |
368 |
|
|
369 |
|
|
370 |
|
class TestProjFileWithInvalidParameters(unittest.TestCase, |
371 |
|
support.FileLoadTestCase): |
372 |
|
|
373 |
|
file_extension = ".proj" |
374 |
|
file_contents = '''\ |
375 |
|
<?xml version="1.0" encoding="UTF-8"?> |
376 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
377 |
|
<projectionlist> |
378 |
|
<projection name="Universal Transverse Mercator"> |
379 |
|
<parameter value="proj=utm"/> |
380 |
|
<parameter value="ellps=clrk66"/> |
381 |
|
<!-- an invalid zone number to trigger the parameter checking |
382 |
|
in the proj library --> |
383 |
|
<parameter value="zone=1000"/> |
384 |
|
</projection> |
385 |
|
<projection name="Transverse Mercator"> |
386 |
|
<parameter value="proj=tmerc"/> |
387 |
|
<parameter value="ellps=clrk66"/> |
388 |
|
<parameter value="lat_0=90w"/> |
389 |
|
<parameter value="lon_0=90w"/> |
390 |
|
<parameter value="k=1"/> |
391 |
|
</projection> |
392 |
|
</projectionlist> |
393 |
|
''' |
394 |
|
|
395 |
|
def setUp(self): |
396 |
|
support.FileLoadTestCase.setUp(self) |
397 |
|
|
398 |
|
def test(self): |
399 |
|
"""Test reading a proj file with invalid parameters""" |
400 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
401 |
|
projs = projfile.GetProjections() |
402 |
|
self.assertEquals(len(projs), 1) |
403 |
|
params = projs[0].GetAllParameters()[:] |
404 |
|
params.sort() |
405 |
|
self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w', |
406 |
|
'lon_0=90w', 'proj=tmerc']) |
407 |
|
self.assertEquals(warnings, |
408 |
|
['Error in projection "Universal Transverse Mercator":' |
409 |
|
' invalid UTM zone number']) |
410 |
|
|
411 |
|
|
412 |
if __name__ == "__main__": |
if __name__ == "__main__": |