14 |
# $Id$ |
# $Id$ |
15 |
|
|
16 |
import unittest |
import unittest |
17 |
|
import locale |
18 |
import os |
import os |
19 |
|
|
20 |
|
import localessupport |
21 |
import xmlsupport |
import xmlsupport |
22 |
import support |
import support |
23 |
support.initthuban() |
support.initthuban() |
57 |
(3620891.3077618643, 0.0, |
(3620891.3077618643, 0.0, |
58 |
3875381.8535437919, 252962.10480170773), |
3875381.8535437919, 252962.10480170773), |
59 |
epsilon = 1e-5) |
epsilon = 1e-5) |
60 |
|
self.assertFloatSeqEqual(proj.InverseBBox((3620891.3077618643, 0.0, |
61 |
|
3875381.8535437919, |
62 |
|
252962.10480170773)), |
63 |
|
(-0.018341599754143501, 0.0, |
64 |
|
2.017992992681688, 2.0377390677846736), |
65 |
|
epsilon = 1e-5) |
66 |
|
|
67 |
# GetName() |
# GetName() |
68 |
self.assertEquals(proj.GetName(), _("Unknown")) |
self.assertEquals(proj.GetName(), _("Unknown")) |
88 |
self.assertEquals(proj.GetParameter("south"), "south") |
self.assertEquals(proj.GetParameter("south"), "south") |
89 |
|
|
90 |
def test_get_projection_units_geo(self): |
def test_get_projection_units_geo(self): |
91 |
"""Test Projection.GetProjectedUnits() for geographic projection""" |
"""Test Projection.GetProjectedUnits() for geographic projection. |
92 |
|
Test for the alias 'longlat' as well. |
93 |
|
""" |
94 |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
95 |
"ellps=clrk66"]) |
"ellps=clrk66"]) |
96 |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
97 |
|
proj = Projection(["proj=longlat", "to_meter=0.017453292519943295", |
98 |
|
"ellps=clrk66"]) |
99 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
100 |
|
|
101 |
|
def test_lc_numeric_robustness(self): |
102 |
|
"""Test if an LC_NUMERIC local with comma as decimal_point will work. |
103 |
|
|
104 |
|
Some versions of proj are not robust against this. |
105 |
|
Starting with Python 2.4 there is a different behaviour when |
106 |
|
calling C extensions and now they will see changes locales settings |
107 |
|
which might tickle the bug in proj. |
108 |
|
""" |
109 |
|
params = ["proj=latlong", "to_meter=0.01745", "ellps=clrk66"] |
110 |
|
|
111 |
|
oldlocale = localessupport.setdecimalcommalocale() |
112 |
|
if oldlocale == None: |
113 |
|
raise support.SkipTest( |
114 |
|
"No locale with comma as decimal_point found.") |
115 |
|
|
116 |
|
proj = Projection(params) |
117 |
|
result1 = proj.Forward(1,1) |
118 |
|
|
119 |
|
locale.setlocale(locale.LC_NUMERIC, "C") |
120 |
|
proj = Projection(params) |
121 |
|
result2= proj.Forward(1,1) |
122 |
|
|
123 |
|
locale.setlocale(locale.LC_NUMERIC, oldlocale) |
124 |
|
self.assertFloatSeqEqual(result1, result2, epsilon = 1e-5 ) |
125 |
|
|
126 |
def test_get_projection_units_normal(self): |
def test_get_projection_units_normal(self): |
127 |
"""Test Projection.GetProjectedUnits() for normal projection""" |
"""Test Projection.GetProjectedUnits() for normal projection""" |
171 |
|
|
172 |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
173 |
|
|
174 |
"""Test cases for reading and writing projection files. |
"""Test cases for ProjFile objects""" |
|
""" |
|
175 |
|
|
176 |
def setUp(self): |
def setUp(self): |
177 |
self.clear_messages() |
self.clear_messages() |
245 |
|
|
246 |
class ProjFileReadTests(ProjFileTest): |
class ProjFileReadTests(ProjFileTest): |
247 |
|
|
248 |
"""Test read ProjFile objects from files""" |
"""Test read ProjFile objects from files |
249 |
|
|
250 |
|
The files only cover error handling and the system projection file. |
251 |
|
""" |
252 |
|
|
253 |
def test_read_non_existing_file(self): |
def test_read_non_existing_file(self): |
254 |
"""Test read_proj_file with non-existing file""" |
"""Test read_proj_file with non-existing file""" |
262 |
As currently written this only works on unix-like systems and |
As currently written this only works on unix-like systems and |
263 |
not e.g. on MS Windows. |
not e.g. on MS Windows. |
264 |
""" |
""" |
265 |
|
if os.name != "posix": |
266 |
|
raise support.SkipTest("Test only works on posix systems") |
267 |
filename = self.filename() |
filename = self.filename() |
268 |
file = open(filename, "w") |
file = open(filename, "w") |
269 |
file.close() |
file.close() |
279 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
280 |
|
|
281 |
def test_get_system_proj_file(self): |
def test_get_system_proj_file(self): |
282 |
"""Test resource.get_system_proj_file() |
"""Test resource.get_system_proj_file(DEFAULT_PROJ_FILE) |
283 |
|
|
284 |
This is primarily to test whether the system proj file contains |
This is primarily to test whether the system proj file contains |
285 |
invalid projection paramers and whether the proj file is not |
invalid projection paramers and whether the proj file is not |
286 |
empty |
empty |
287 |
""" |
""" |
288 |
projfile, warnings = resource.get_system_proj_file() |
projfile, warnings\ |
289 |
|
= resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE) |
290 |
self.assertEquals(warnings, []) |
self.assertEquals(warnings, []) |
291 |
self.assert_(len(projfile.GetProjections()) > 0) |
self.assert_(len(projfile.GetProjections()) > 0) |
292 |
|
|
293 |
|
# see whether it got cached and we get the same projfile object |
294 |
|
# when we read the file again |
295 |
|
projfile2, warnings \ |
296 |
|
= resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE) |
297 |
|
self.assert_(projfile is projfile2) |
298 |
|
|
299 |
|
|
300 |
class WriteProjFileTests(ProjFileTest): |
class WriteProjFileTests(ProjFileTest): |
301 |
|
|
364 |
self.doTestWrite(pf, file_contents) |
self.doTestWrite(pf, file_contents) |
365 |
|
|
366 |
|
|
367 |
class TestLoadingProjFile(support.FileLoadTestCase): |
class ProjFileLoadTestCase(support.FileLoadTestCase): |
368 |
|
|
369 |
|
"""Base class for the test cases that read specific test files""" |
370 |
|
|
371 |
file_extension = ".proj" |
file_extension = ".proj" |
372 |
|
|
373 |
|
def tearDown(self): |
374 |
|
"""Clear the cache explicitly""" |
375 |
|
resource.clear_proj_file_cache() |
376 |
|
|
377 |
|
|
378 |
|
class TestLoadingProjFile(ProjFileLoadTestCase): |
379 |
|
|
380 |
file_contents = '''\ |
file_contents = '''\ |
381 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
382 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
428 |
"proj=tmerc", "units=m", |
"proj=tmerc", "units=m", |
429 |
"x_0=400000.000", "y_0=0.000"]) |
"x_0=400000.000", "y_0=0.000"]) |
430 |
|
|
431 |
|
def test_caching(self): |
432 |
|
# test whether the projfile cache works |
433 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
434 |
|
projfile2, warnings = resource.read_proj_file(self.filename()) |
435 |
|
|
436 |
|
# Both projfiles should be the same object |
437 |
|
self.assert_(projfile2 is projfile) |
438 |
|
|
439 |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
# If we clear the cache we should get a new one. |
440 |
|
resource.clear_proj_file_cache() |
441 |
|
projfile3, warnings = resource.read_proj_file(self.filename()) |
442 |
|
self.assert_(projfile3 is not projfile) |
443 |
|
|
444 |
|
|
445 |
|
class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase): |
446 |
|
|
|
file_extension = ".proj" |
|
447 |
file_contents = '''\ |
file_contents = '''\ |
448 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
449 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
460 |
self.assertEquals(len(projfile.GetProjections()), 0) |
self.assertEquals(len(projfile.GetProjections()), 0) |
461 |
|
|
462 |
|
|
463 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
class TestProjFileWithInvalidParameters(ProjFileLoadTestCase): |
|
support.FileLoadTestCase): |
|
464 |
|
|
|
file_extension = ".proj" |
|
465 |
file_contents = '''\ |
file_contents = '''\ |
466 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
467 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |