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 |
80 |
self.assertEquals(proj.GetParameter("south"), "south") |
self.assertEquals(proj.GetParameter("south"), "south") |
81 |
|
|
82 |
def test_get_projection_units_geo(self): |
def test_get_projection_units_geo(self): |
83 |
"""Test Projection.GetProjectedUnits() for geographic projection""" |
"""Test Projection.GetProjectedUnits() for geographic projection. |
84 |
|
Test for the alias 'longlat' as well. |
85 |
|
""" |
86 |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
proj = Projection(["proj=latlong", "to_meter=0.017453292519943295", |
87 |
"ellps=clrk66"]) |
"ellps=clrk66"]) |
88 |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
89 |
|
proj = Projection(["proj=longlat", "to_meter=0.017453292519943295", |
90 |
|
"ellps=clrk66"]) |
91 |
|
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_DEGREES) |
92 |
|
|
93 |
def test_get_projection_units_normal(self): |
def test_get_projection_units_normal(self): |
94 |
"""Test Projection.GetProjectedUnits() for normal projection""" |
"""Test Projection.GetProjectedUnits() for normal projection""" |
120 |
self.assertEquals(proj.EPSGCode(), "42") |
self.assertEquals(proj.EPSGCode(), "42") |
121 |
|
|
122 |
|
|
|
class ProjFileTest(unittest.TestCase, support.FileTestMixin): |
|
123 |
|
|
124 |
"""Base class for the proj file tests""" |
class TestProjFileSimple: |
125 |
|
|
126 |
def filename(self): |
def test_init(self): |
127 |
"""Return the filename for the test""" |
"""Test ProjFile coinstructor""" |
128 |
return self.temp_file_name(self.id() + ".proj") |
proj_file = ProjFile("some_filename") |
129 |
|
self.assertEquals(proj_file.GetFilename(), "some_filename") |
130 |
|
self.assertEquals(len(proj_file.GetProjections()), 0) |
131 |
|
|
132 |
|
def test_set_filename(self): |
133 |
|
"""Test ProjFile.SetFilename()""" |
134 |
|
proj_file = ProjFile("some_filename") |
135 |
|
proj.SetFilename("other_name") |
136 |
|
self.assertEquals(proj_file.GetFilename(), "other_name") |
137 |
|
|
|
class TestProjFile(ProjFileTest, xmlsupport.ValidationTest): |
|
|
|
|
|
"""Test cases for reading and writing projection files. |
|
|
""" |
|
138 |
|
|
139 |
def test(self): |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
|
"""Test ProjFile""" |
|
140 |
|
|
141 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
"""Test cases for ProjFile objects""" |
|
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
|
|
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
|
|
"lat_1=0", "lat_2=20"]) |
|
|
|
|
|
eq = self.assertEquals |
|
|
|
|
|
|
|
|
# |
|
|
# __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) |
|
142 |
|
|
143 |
projFile.Remove(proj2) |
def setUp(self): |
144 |
projFile.Remove(proj1) |
self.clear_messages() |
145 |
|
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
146 |
|
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
147 |
|
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
148 |
|
"lat_1=0", "lat_2=20"]) |
149 |
|
self.proj_file = ProjFile("some_filename") |
150 |
|
for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]: |
151 |
|
self.proj_file.Subscribe(msg, self.subscribe_with_params, msg) |
152 |
|
|
153 |
|
def tearDown(self): |
154 |
|
self.clear_messages() |
155 |
|
self.proj_file.Destroy() |
156 |
|
|
157 |
|
def test_add_remove(self): |
158 |
|
"""Test ProjFile.Add() and ProjFile.Remove()""" |
159 |
|
self.proj_file.Add(self.proj0) |
160 |
|
self.proj_file.Add(self.proj1) |
161 |
|
self.assertEquals(self.proj_file.GetProjections(), |
162 |
|
[self.proj0, self.proj1]) |
163 |
|
self.check_messages([(self.proj0, PROJECTION_ADDED), |
164 |
|
(self.proj1, PROJECTION_ADDED)]) |
165 |
|
self.clear_messages() |
166 |
|
|
167 |
|
self.proj_file.Remove(self.proj0) |
168 |
|
self.assertEquals(self.proj_file.GetProjections(), [self.proj1]) |
169 |
|
self.check_messages([(self.proj0, PROJECTION_REMOVED)]) |
170 |
|
|
171 |
|
def test_remove_non_existing(self): |
172 |
|
"""Test ProjFile.Remove(<proj not in projfile>)""" |
173 |
|
self.assertRaises(ValueError, self.proj_file.Remove, self.proj0) |
174 |
|
# Nothing happened, so no messages should have been sent |
175 |
|
self.check_messages([]) |
176 |
|
|
177 |
|
def test_replace(self): |
178 |
|
"""Test ProjFile.Replace()""" |
179 |
|
self.proj_file.Add(self.proj0) |
180 |
|
self.proj_file.Add(self.proj1) |
181 |
|
self.clear_messages() |
182 |
|
|
183 |
# Replace() |
# Replace() |
184 |
projFile.Replace(proj0, proj1) |
self.proj_file.Replace(self.proj0, self.proj2) |
185 |
projs = projFile.GetProjections() |
self.assertEquals(self.proj_file.GetProjections(), |
186 |
eq(projs[0], proj1) |
[self.proj2, self.proj1]) |
187 |
|
self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)]) |
188 |
# replace a non-existent projection |
|
189 |
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
def test_replace_non_existing(self): |
190 |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
191 |
|
self.proj_file.Add(self.proj0) |
192 |
|
self.proj_file.Add(self.proj1) |
193 |
|
self.clear_messages() |
194 |
|
self.assertRaises(ValueError, |
195 |
|
self.proj_file.Replace, self.proj2, self.proj0) |
196 |
|
# All projections should still be there |
197 |
|
self.assertEquals(self.proj_file.GetProjections(), |
198 |
|
[self.proj0, self.proj1]) |
199 |
|
# Nothing happened, so no messages should have been sent |
200 |
|
self.check_messages([]) |
201 |
|
|
202 |
|
|
203 |
|
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
204 |
|
xmlsupport.ValidationTest): |
205 |
|
|
206 |
|
"""Base class for the proj file tests that read or write files""" |
207 |
|
|
208 |
|
def filename(self): |
209 |
|
"""Return the filename for the test""" |
210 |
|
return self.temp_file_name(self.id() + ".proj") |
211 |
|
|
212 |
|
|
213 |
|
class ProjFileReadTests(ProjFileTest): |
214 |
|
|
215 |
|
"""Test read ProjFile objects from files |
216 |
|
|
217 |
|
The files only cover error handling and the system projection file. |
218 |
|
""" |
219 |
|
|
220 |
def test_read_non_existing_file(self): |
def test_read_non_existing_file(self): |
221 |
"""Test read_proj_file with non-existing file""" |
"""Test read_proj_file with non-existing file""" |
254 |
self.assertEquals(warnings, []) |
self.assertEquals(warnings, []) |
255 |
self.assert_(len(projfile.GetProjections()) > 0) |
self.assert_(len(projfile.GetProjections()) > 0) |
256 |
|
|
257 |
|
# see whether it got cached and we get the same projfile object |
258 |
|
# when we read the file again |
259 |
|
projfile2, warnings = resource.get_system_proj_file() |
260 |
|
self.assert_(projfile is projfile2) |
261 |
|
|
262 |
class WriteProjFileTests(ProjFileTest, xmlsupport.ValidationTest): |
|
263 |
|
class WriteProjFileTests(ProjFileTest): |
264 |
|
|
265 |
"""Test cases for writing proj files""" |
"""Test cases for writing proj files""" |
266 |
|
|
327 |
self.doTestWrite(pf, file_contents) |
self.doTestWrite(pf, file_contents) |
328 |
|
|
329 |
|
|
330 |
class TestLoadingProjFile(support.FileLoadTestCase): |
class ProjFileLoadTestCase(support.FileLoadTestCase): |
331 |
|
|
332 |
|
"""Base class for the test cases that read specific test files""" |
333 |
|
|
334 |
file_extension = ".proj" |
file_extension = ".proj" |
335 |
|
|
336 |
|
def tearDown(self): |
337 |
|
"""Clear the cache explicitly""" |
338 |
|
resource.clear_proj_file_cache() |
339 |
|
|
340 |
|
|
341 |
|
class TestLoadingProjFile(ProjFileLoadTestCase): |
342 |
|
|
343 |
file_contents = '''\ |
file_contents = '''\ |
344 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
345 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
391 |
"proj=tmerc", "units=m", |
"proj=tmerc", "units=m", |
392 |
"x_0=400000.000", "y_0=0.000"]) |
"x_0=400000.000", "y_0=0.000"]) |
393 |
|
|
394 |
|
def test_caching(self): |
395 |
|
# test whether the projfile cache works |
396 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
397 |
|
projfile2, warnings = resource.read_proj_file(self.filename()) |
398 |
|
|
399 |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
# Both projfiles should be the same object |
400 |
|
self.assert_(projfile2 is projfile) |
401 |
|
|
402 |
|
# If we clear the cache we should get a new one. |
403 |
|
resource.clear_proj_file_cache() |
404 |
|
projfile3, warnings = resource.read_proj_file(self.filename()) |
405 |
|
self.assert_(projfile3 is not projfile) |
406 |
|
|
407 |
|
|
408 |
|
class TestLoadingProjFileWithEmptyProjectionlist(ProjFileLoadTestCase): |
409 |
|
|
|
file_extension = ".proj" |
|
410 |
file_contents = '''\ |
file_contents = '''\ |
411 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
412 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
423 |
self.assertEquals(len(projfile.GetProjections()), 0) |
self.assertEquals(len(projfile.GetProjections()), 0) |
424 |
|
|
425 |
|
|
426 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
class TestProjFileWithInvalidParameters(ProjFileLoadTestCase): |
|
support.FileLoadTestCase): |
|
427 |
|
|
|
file_extension = ".proj" |
|
428 |
file_contents = '''\ |
file_contents = '''\ |
429 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
430 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
464 |
|
|
465 |
|
|
466 |
if __name__ == "__main__": |
if __name__ == "__main__": |
467 |
unittest.main() |
support.run_tests() |