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 |
|
|
93 |
sample_projfile = '''\ |
sample_projfile = '''\ |
94 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
95 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
96 |
<projectionlist> |
<projectionlist> |
97 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
98 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
99 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
100 |
<parameter value="lat_0=90w"/> |
<parameter value="lat_0=90w"/> |
101 |
<parameter value="lon_0=90w"/> |
<parameter value="lon_0=90w"/> |
102 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
103 |
</projection> |
</projection> |
104 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
105 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
106 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
107 |
<parameter value="lat_0=30w"/> |
<parameter value="lat_0=30w"/> |
108 |
<parameter value="lon_0=30w"/> |
<parameter value="lon_0=30w"/> |
109 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
110 |
</projection> |
</projection> |
111 |
<projection name="Universal Transverse Mercartor"> |
<projection name="Universal Transverse Mercator"> |
112 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
113 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
114 |
<parameter value="zone=1"/> |
<parameter value="zone=1"/> |
116 |
</projectionlist> |
</projectionlist> |
117 |
''' |
''' |
118 |
|
|
119 |
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
120 |
"ellps=clrk66", |
"ellps=clrk66", |
121 |
"lat_0=90w", |
"lat_0=90w", |
122 |
"lon_0=90w", |
"lon_0=90w", |
123 |
"k=1"]), |
"k=1"]), |
124 |
("Transverse Mercartor", ["proj=tmerc", |
("Transverse Mercator", ["proj=tmerc", |
125 |
"ellps=clrk66", |
"ellps=clrk66", |
126 |
"lat_0=30w", |
"lat_0=30w", |
127 |
"lon_0=30w", |
"lon_0=30w", |
128 |
"k=1"]), |
"k=1"]), |
129 |
("Universal Transverse Mercartor", ["proj=utm", |
("Universal Transverse Mercator", ["proj=utm", |
130 |
"ellps=clrk66", |
"ellps=clrk66", |
131 |
"zone=1"])] |
"zone=1"])] |
132 |
|
|
133 |
sample_projfile2 = '''\ |
sample_projfile2 = '''\ |
134 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
135 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
136 |
<projectionlist> |
<projectionlist> |
137 |
</projectionlist> |
</projectionlist> |
138 |
''' |
''' |
139 |
|
|
140 |
sample_projfile_data2 = [] |
sample_projfile_data2 = [] |
141 |
|
|
142 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
143 |
|
xmlsupport.ValidationTest): |
144 |
|
|
145 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
146 |
""" |
""" |
153 |
|
|
154 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
155 |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
156 |
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
157 |
|
"lat_1=0", "lat_2=20"]) |
158 |
|
|
159 |
eq = self.assertEquals |
eq = self.assertEquals |
160 |
|
|
262 |
written_contents = file.read() |
written_contents = file.read() |
263 |
file.close() |
file.close() |
264 |
self.compare_xml(written_contents, expected) |
self.compare_xml(written_contents, expected) |
265 |
|
self.validate_data(written_contents) |
266 |
|
self.validate_data(expected) |
267 |
|
|
268 |
def doTestRead(self, data, input): |
def doTestRead(self, data, input): |
269 |
|
|
272 |
file.write(input) |
file.write(input) |
273 |
file.close() |
file.close() |
274 |
|
|
275 |
pf = resource.read_proj_file(filename) |
pf, warnings = resource.read_proj_file(filename) |
276 |
|
self.assertEquals(warnings, []) |
277 |
|
|
278 |
eq = self.assertEquals |
eq = self.assertEquals |
279 |
|
|
284 |
for param in proj.GetAllParameters(): |
for param in proj.GetAllParameters(): |
285 |
self.assert_(param in d[1]) |
self.assert_(param in d[1]) |
286 |
|
|
287 |
|
def test_get_system_proj_file(self): |
288 |
|
"""Test resource.get_system_proj_file() |
289 |
|
|
290 |
|
This is primarily to test whether the system proj file contains |
291 |
|
invalid projection paramers and whether the proj file is not |
292 |
|
empty |
293 |
|
""" |
294 |
|
projfile, warnings = resource.get_system_proj_file() |
295 |
|
self.assertEquals(warnings, []) |
296 |
|
self.assert_(len(projfile.GetProjections()) > 0) |
297 |
|
|
298 |
|
|
299 |
|
class TestProjFileWithInvalidParameters(unittest.TestCase, |
300 |
|
support.FileLoadTestCase): |
301 |
|
|
302 |
|
file_extension = ".proj" |
303 |
|
file_contents = '''\ |
304 |
|
<?xml version="1.0" encoding="UTF-8"?> |
305 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
306 |
|
<projectionlist> |
307 |
|
<projection name="Universal Transverse Mercator"> |
308 |
|
<parameter value="proj=utm"/> |
309 |
|
<parameter value="ellps=clrk66"/> |
310 |
|
<!-- an invalid zone number to trigger the parameter checking |
311 |
|
in the proj library --> |
312 |
|
<parameter value="zone=1000"/> |
313 |
|
</projection> |
314 |
|
<projection name="Transverse Mercator"> |
315 |
|
<parameter value="proj=tmerc"/> |
316 |
|
<parameter value="ellps=clrk66"/> |
317 |
|
<parameter value="lat_0=90w"/> |
318 |
|
<parameter value="lon_0=90w"/> |
319 |
|
<parameter value="k=1"/> |
320 |
|
</projection> |
321 |
|
</projectionlist> |
322 |
|
''' |
323 |
|
|
324 |
|
def setUp(self): |
325 |
|
support.FileLoadTestCase.setUp(self) |
326 |
|
|
327 |
|
def test(self): |
328 |
|
"""Test reading a proj file with invalid parameters""" |
329 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
330 |
|
projs = projfile.GetProjections() |
331 |
|
self.assertEquals(len(projs), 1) |
332 |
|
params = projs[0].GetAllParameters()[:] |
333 |
|
params.sort() |
334 |
|
self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w', |
335 |
|
'lon_0=90w', 'proj=tmerc']) |
336 |
|
self.assertEquals(warnings, |
337 |
|
['Error in projection "Universal Transverse Mercator":' |
338 |
|
' invalid UTM zone number']) |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
if __name__ == "__main__": |
if __name__ == "__main__": |
343 |
unittest.main() |
unittest.main() |