23 |
from Thuban import _ |
from Thuban import _ |
24 |
from Thuban.Model.proj import Projection, ProjFile, \ |
from Thuban.Model.proj import Projection, ProjFile, \ |
25 |
PROJ_UNITS_METERS, PROJ_UNITS_DEGREES |
PROJ_UNITS_METERS, PROJ_UNITS_DEGREES |
26 |
|
from Thuban.Model.messages import PROJECTION_ADDED, PROJECTION_REMOVED, \ |
27 |
|
PROJECTION_REPLACED |
28 |
import Thuban.Model.resource as resource |
import Thuban.Model.resource as resource |
29 |
|
|
30 |
from xmlsupport import sax_eventlist |
from xmlsupport import sax_eventlist |
55 |
(3620891.3077618643, 0.0, |
(3620891.3077618643, 0.0, |
56 |
3875381.8535437919, 252962.10480170773), |
3875381.8535437919, 252962.10480170773), |
57 |
epsilon = 1e-5) |
epsilon = 1e-5) |
58 |
|
self.assertFloatSeqEqual(proj.InverseBBox((3620891.3077618643, 0.0, |
59 |
|
3875381.8535437919, |
60 |
|
252962.10480170773)), |
61 |
|
(-0.018341599754143501, 0.0, |
62 |
|
2.017992992681688, 2.0377390677846736), |
63 |
|
epsilon = 1e-5) |
64 |
|
|
65 |
# GetName() |
# GetName() |
66 |
self.assertEquals(proj.GetName(), _("Unknown")) |
self.assertEquals(proj.GetName(), _("Unknown")) |
86 |
self.assertEquals(proj.GetParameter("south"), "south") |
self.assertEquals(proj.GetParameter("south"), "south") |
87 |
|
|
88 |
def test_get_projection_units_geo(self): |
def test_get_projection_units_geo(self): |
89 |
"""Test Projection.GetProjectedUnits() for geographic projection""" |
"""Test Projection.GetProjectedUnits() for geographic projection. |
90 |
|
Test for the alias 'longlat' as well. |
91 |
|
""" |
92 |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
93 |
"ellps=clrk66"]) |
"ellps=clrk66"]) |
94 |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
95 |
|
proj = Projection(["proj=longlat", "to_meter=0.017453292519943295", |
96 |
|
"ellps=clrk66"]) |
97 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
98 |
|
|
99 |
def test_get_projection_units_normal(self): |
def test_get_projection_units_normal(self): |
100 |
"""Test Projection.GetProjectedUnits() for normal projection""" |
"""Test Projection.GetProjectedUnits() for normal projection""" |
142 |
self.assertEquals(proj_file.GetFilename(), "other_name") |
self.assertEquals(proj_file.GetFilename(), "other_name") |
143 |
|
|
144 |
|
|
145 |
class TestProjFile(unittest.TestCase): |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
146 |
|
|
147 |
"""Test cases for reading and writing projection files. |
"""Test cases for ProjFile objects""" |
|
""" |
|
148 |
|
|
149 |
def setUp(self): |
def setUp(self): |
150 |
|
self.clear_messages() |
151 |
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
152 |
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
153 |
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
154 |
"lat_1=0", "lat_2=20"]) |
"lat_1=0", "lat_2=20"]) |
155 |
|
self.proj_file = ProjFile("some_filename") |
156 |
|
for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]: |
157 |
|
self.proj_file.Subscribe(msg, self.subscribe_with_params, msg) |
158 |
|
|
159 |
|
def tearDown(self): |
160 |
|
self.clear_messages() |
161 |
|
self.proj_file.Destroy() |
162 |
|
|
163 |
def test_add_remove(self): |
def test_add_remove(self): |
164 |
"""Test ProjFile.Add() and ProjFile.Remove()""" |
"""Test ProjFile.Add() and ProjFile.Remove()""" |
165 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
166 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
167 |
proj_file.Add(self.proj1) |
self.assertEquals(self.proj_file.GetProjections(), |
168 |
self.assertEquals(proj_file.GetProjections(), [self.proj0, self.proj1]) |
[self.proj0, self.proj1]) |
169 |
proj_file.Remove(self.proj0) |
self.check_messages([(self.proj0, PROJECTION_ADDED), |
170 |
self.assertEquals(proj_file.GetProjections(), [self.proj1]) |
(self.proj1, PROJECTION_ADDED)]) |
171 |
|
self.clear_messages() |
172 |
|
|
173 |
|
self.proj_file.Remove(self.proj0) |
174 |
|
self.assertEquals(self.proj_file.GetProjections(), [self.proj1]) |
175 |
|
self.check_messages([(self.proj0, PROJECTION_REMOVED)]) |
176 |
|
|
177 |
def test_remove_non_existing(self): |
def test_remove_non_existing(self): |
178 |
"""Test ProjFile.Remove(<proj not in projfile>)""" |
"""Test ProjFile.Remove(<proj not in projfile>)""" |
179 |
proj_file = ProjFile("some_filename") |
self.assertRaises(ValueError, self.proj_file.Remove, self.proj0) |
180 |
self.assertRaises(ValueError, proj_file.Remove, self.proj0) |
# Nothing happened, so no messages should have been sent |
181 |
|
self.check_messages([]) |
182 |
|
|
183 |
def test_replace(self): |
def test_replace(self): |
184 |
"""Test ProjFile.Replace()""" |
"""Test ProjFile.Replace()""" |
185 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
186 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
187 |
proj_file.Add(self.proj1) |
self.clear_messages() |
188 |
|
|
189 |
# Replace() |
# Replace() |
190 |
proj_file.Replace(self.proj0, self.proj2) |
self.proj_file.Replace(self.proj0, self.proj2) |
191 |
self.assertEquals(proj_file.GetProjections(), [self.proj2, self.proj1]) |
self.assertEquals(self.proj_file.GetProjections(), |
192 |
|
[self.proj2, self.proj1]) |
193 |
|
self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)]) |
194 |
|
|
195 |
def test_replace_non_existing(self): |
def test_replace_non_existing(self): |
196 |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
197 |
proj_file = ProjFile("some_filename") |
self.proj_file.Add(self.proj0) |
198 |
proj_file.Add(self.proj0) |
self.proj_file.Add(self.proj1) |
199 |
proj_file.Add(self.proj1) |
self.clear_messages() |
200 |
self.assertRaises(ValueError, |
self.assertRaises(ValueError, |
201 |
proj_file.Replace, self.proj2, self.proj0) |
self.proj_file.Replace, self.proj2, self.proj0) |
202 |
|
# All projections should still be there |
203 |
|
self.assertEquals(self.proj_file.GetProjections(), |
204 |
|
[self.proj0, self.proj1]) |
205 |
|
# Nothing happened, so no messages should have been sent |
206 |
|
self.check_messages([]) |
207 |
|
|
208 |
|
|
209 |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
218 |
|
|
219 |
class ProjFileReadTests(ProjFileTest): |
class ProjFileReadTests(ProjFileTest): |
220 |
|
|
221 |
"""Test read ProjFile objects from files""" |
"""Test read ProjFile objects from files |
222 |
|
|
223 |
|
The files only cover error handling and the system projection file. |
224 |
|
""" |
225 |
|
|
226 |
def test_read_non_existing_file(self): |
def test_read_non_existing_file(self): |
227 |
"""Test read_proj_file with non-existing file""" |
"""Test read_proj_file with non-existing file""" |
235 |
As currently written this only works on unix-like systems and |
As currently written this only works on unix-like systems and |
236 |
not e.g. on MS Windows. |
not e.g. on MS Windows. |
237 |
""" |
""" |
238 |
|
if os.name != "posix": |
239 |
|
raise support.SkipTest("Test only works on posix systems") |
240 |
filename = self.filename() |
filename = self.filename() |
241 |
file = open(filename, "w") |
file = open(filename, "w") |
242 |
file.close() |
file.close() |
252 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
253 |
|
|
254 |
def test_get_system_proj_file(self): |
def test_get_system_proj_file(self): |
255 |
"""Test resource.get_system_proj_file() |
"""Test resource.get_system_proj_file(DEFAULT_PROJ_FILE) |
256 |
|
|
257 |
This is primarily to test whether the system proj file contains |
This is primarily to test whether the system proj file contains |
258 |
invalid projection paramers and whether the proj file is not |
invalid projection paramers and whether the proj file is not |
259 |
empty |
empty |
260 |
""" |
""" |
261 |
projfile, warnings = resource.get_system_proj_file() |
projfile, warnings\ |
262 |
|
= resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE) |
263 |
self.assertEquals(warnings, []) |
self.assertEquals(warnings, []) |
264 |
self.assert_(len(projfile.GetProjections()) > 0) |
self.assert_(len(projfile.GetProjections()) > 0) |
265 |
|
|
266 |
|
# see whether it got cached and we get the same projfile object |
267 |
|
# when we read the file again |
268 |
|
projfile2, warnings \ |
269 |
|
= resource.get_system_proj_file(resource.DEFAULT_PROJ_FILE) |
270 |
|
self.assert_(projfile is projfile2) |
271 |
|
|
272 |
|
|
273 |
class WriteProjFileTests(ProjFileTest): |
class WriteProjFileTests(ProjFileTest): |
274 |
|
|
337 |
self.doTestWrite(pf, file_contents) |
self.doTestWrite(pf, file_contents) |
338 |
|
|
339 |
|
|
340 |
class TestLoadingProjFile(support.FileLoadTestCase): |
class ProjFileLoadTestCase(support.FileLoadTestCase): |
341 |
|
|
342 |
|
"""Base class for the test cases that read specific test files""" |
343 |
|
|
344 |
file_extension = ".proj" |
file_extension = ".proj" |
345 |
|
|
346 |
|
def tearDown(self): |
347 |
|
"""Clear the cache explicitly""" |
348 |
|
resource.clear_proj_file_cache() |
349 |
|
|
350 |
|
|
351 |
|
class TestLoadingProjFile(ProjFileLoadTestCase): |
352 |
|
|
353 |
file_contents = '''\ |
file_contents = '''\ |
354 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
355 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
401 |
"proj=tmerc", "units=m", |
"proj=tmerc", "units=m", |
402 |
"x_0=400000.000", "y_0=0.000"]) |
"x_0=400000.000", "y_0=0.000"]) |
403 |
|
|
404 |
|
def test_caching(self): |
405 |
|
# test whether the projfile cache works |
406 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
407 |
|
projfile2, warnings = resource.read_proj_file(self.filename()) |
408 |
|
|
409 |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
# Both projfiles should be the same object |
410 |
|
self.assert_(projfile2 is projfile) |
411 |
|
|
412 |
|
# If we clear the cache we should get a new one. |
413 |
|
resource.clear_proj_file_cache() |
414 |
|
projfile3, warnings = resource.read_proj_file(self.filename()) |
415 |
|
self.assert_(projfile3 is not projfile) |
416 |
|
|
417 |
|
|
418 |
|
class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase): |
419 |
|
|
|
file_extension = ".proj" |
|
420 |
file_contents = '''\ |
file_contents = '''\ |
421 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
422 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
433 |
self.assertEquals(len(projfile.GetProjections()), 0) |
self.assertEquals(len(projfile.GetProjections()), 0) |
434 |
|
|
435 |
|
|
436 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
class TestProjFileWithInvalidParameters(ProjFileLoadTestCase): |
|
support.FileLoadTestCase): |
|
437 |
|
|
|
file_extension = ".proj" |
|
438 |
file_contents = '''\ |
file_contents = '''\ |
439 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
440 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
474 |
|
|
475 |
|
|
476 |
if __name__ == "__main__": |
if __name__ == "__main__": |
477 |
unittest.main() |
support.run_tests() |