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_projection_units_geo(self): |
75 |
|
"""Test Projection.GetProjectedUnits() for geographic projection""" |
76 |
|
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
77 |
|
"ellps=clrk66"]) |
78 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
79 |
|
|
80 |
|
def test_get_projection_units_normal(self): |
81 |
|
"""Test Projection.GetProjectedUnits() for normal projection""" |
82 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
83 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
84 |
|
|
85 |
|
|
86 |
sample_projfile = '''\ |
sample_projfile = '''\ |
87 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
88 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
89 |
<projectionlist> |
<projectionlist> |
90 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
91 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
92 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
93 |
<parameter value="lat_0=90w"/> |
<parameter value="lat_0=90w"/> |
94 |
<parameter value="lon_0=90w"/> |
<parameter value="lon_0=90w"/> |
95 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
96 |
</projection> |
</projection> |
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=30w"/> |
<parameter value="lat_0=30w"/> |
101 |
<parameter value="lon_0=30w"/> |
<parameter value="lon_0=30w"/> |
102 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
103 |
</projection> |
</projection> |
104 |
<projection name="Universal Transverse Mercartor"> |
<projection name="Universal Transverse Mercator"> |
105 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
106 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
107 |
<parameter value="zone=1"/> |
<parameter value="zone=1"/> |
109 |
</projectionlist> |
</projectionlist> |
110 |
''' |
''' |
111 |
|
|
112 |
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
113 |
"ellps=clrk66", |
"ellps=clrk66", |
114 |
"lat_0=90w", |
"lat_0=90w", |
115 |
"lon_0=90w", |
"lon_0=90w", |
116 |
"k=1"]), |
"k=1"]), |
117 |
("Transverse Mercartor", ["proj=tmerc", |
("Transverse Mercator", ["proj=tmerc", |
118 |
"ellps=clrk66", |
"ellps=clrk66", |
119 |
"lat_0=30w", |
"lat_0=30w", |
120 |
"lon_0=30w", |
"lon_0=30w", |
121 |
"k=1"]), |
"k=1"]), |
122 |
("Universal Transverse Mercartor", ["proj=utm", |
("Universal Transverse Mercator", ["proj=utm", |
123 |
"ellps=clrk66", |
"ellps=clrk66", |
124 |
"zone=1"])] |
"zone=1"])] |
125 |
|
|
126 |
sample_projfile2 = '''\ |
sample_projfile2 = '''\ |
127 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
128 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
129 |
<projectionlist> |
<projectionlist> |
130 |
</projectionlist> |
</projectionlist> |
131 |
''' |
''' |
132 |
|
|
133 |
sample_projfile_data2 = [] |
sample_projfile_data2 = [] |
134 |
|
|
135 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
136 |
|
xmlsupport.ValidationTest): |
137 |
|
|
138 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
139 |
""" |
""" |
146 |
|
|
147 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
148 |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
149 |
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
150 |
|
"lat_1=0", "lat_2=20"]) |
151 |
|
|
152 |
eq = self.assertEquals |
eq = self.assertEquals |
153 |
|
|
255 |
written_contents = file.read() |
written_contents = file.read() |
256 |
file.close() |
file.close() |
257 |
self.compare_xml(written_contents, expected) |
self.compare_xml(written_contents, expected) |
258 |
|
self.validate_data(written_contents) |
259 |
|
self.validate_data(expected) |
260 |
|
|
261 |
def doTestRead(self, data, input): |
def doTestRead(self, data, input): |
262 |
|
|
265 |
file.write(input) |
file.write(input) |
266 |
file.close() |
file.close() |
267 |
|
|
268 |
pf = resource.read_proj_file(filename) |
pf, warnings = resource.read_proj_file(filename) |
269 |
|
self.assertEquals(warnings, []) |
270 |
|
|
271 |
eq = self.assertEquals |
eq = self.assertEquals |
272 |
|
|
277 |
for param in proj.GetAllParameters(): |
for param in proj.GetAllParameters(): |
278 |
self.assert_(param in d[1]) |
self.assert_(param in d[1]) |
279 |
|
|
280 |
|
def test_get_system_proj_file(self): |
281 |
|
"""Test resource.get_system_proj_file() |
282 |
|
|
283 |
|
This is primarily to test whether the system proj file contains |
284 |
|
invalid projection paramers and whether the proj file is not |
285 |
|
empty |
286 |
|
""" |
287 |
|
projfile, warnings = resource.get_system_proj_file() |
288 |
|
self.assertEquals(warnings, []) |
289 |
|
self.assert_(len(projfile.GetProjections()) > 0) |
290 |
|
|
291 |
|
|
292 |
|
class TestProjFileWithInvalidParameters(unittest.TestCase, |
293 |
|
support.FileLoadTestCase): |
294 |
|
|
295 |
|
file_extension = ".proj" |
296 |
|
file_contents = '''\ |
297 |
|
<?xml version="1.0" encoding="UTF-8"?> |
298 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
299 |
|
<projectionlist> |
300 |
|
<projection name="Universal Transverse Mercator"> |
301 |
|
<parameter value="proj=utm"/> |
302 |
|
<parameter value="ellps=clrk66"/> |
303 |
|
<!-- an invalid zone number to trigger the parameter checking |
304 |
|
in the proj library --> |
305 |
|
<parameter value="zone=1000"/> |
306 |
|
</projection> |
307 |
|
<projection name="Transverse Mercator"> |
308 |
|
<parameter value="proj=tmerc"/> |
309 |
|
<parameter value="ellps=clrk66"/> |
310 |
|
<parameter value="lat_0=90w"/> |
311 |
|
<parameter value="lon_0=90w"/> |
312 |
|
<parameter value="k=1"/> |
313 |
|
</projection> |
314 |
|
</projectionlist> |
315 |
|
''' |
316 |
|
|
317 |
|
def setUp(self): |
318 |
|
support.FileLoadTestCase.setUp(self) |
319 |
|
|
320 |
|
def test(self): |
321 |
|
"""Test reading a proj file with invalid parameters""" |
322 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
323 |
|
projs = projfile.GetProjections() |
324 |
|
self.assertEquals(len(projs), 1) |
325 |
|
params = projs[0].GetAllParameters()[:] |
326 |
|
params.sort() |
327 |
|
self.assertEquals(params, ['ellps=clrk66', 'k=1', 'lat_0=90w', |
328 |
|
'lon_0=90w', 'proj=tmerc']) |
329 |
|
self.assertEquals(warnings, |
330 |
|
['Error in projection "Universal Transverse Mercator":' |
331 |
|
' invalid UTM zone number']) |
332 |
|
|
333 |
|
|
334 |
|
|
335 |
if __name__ == "__main__": |
if __name__ == "__main__": |
336 |
unittest.main() |
unittest.main() |