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 |
90 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
91 |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
92 |
|
|
93 |
|
def test_label(self): |
94 |
|
"""Test Projection.Label() without epsg""" |
95 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
96 |
|
name = "My Projection") |
97 |
|
self.assertEquals(proj.Label(), "My Projection") |
98 |
|
|
99 |
|
def test_label_epsg(self): |
100 |
|
"""Test Projection.Label() with epsg""" |
101 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
102 |
|
name = "My Projection", epsg="42") |
103 |
|
self.assertEquals(proj.Label(), "EPSG 42 My Projection") |
104 |
|
|
105 |
|
def test_epsgcode_for_non_epsg_projection(self): |
106 |
|
"""Test Projection.EPSGCode() without epsg""" |
107 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
108 |
|
name = "My Projection") |
109 |
|
self.assertEquals(proj.EPSGCode(), None) |
110 |
|
|
111 |
|
def test_epsgcode_for_real_epsg_projection(self): |
112 |
|
"""Test Projection.EPSGCode() with epsg""" |
113 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
114 |
|
name = "My Projection", epsg="42") |
115 |
|
self.assertEquals(proj.EPSGCode(), "42") |
116 |
|
|
|
sample_projfile = '''\ |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
|
|
<projectionlist> |
|
|
<projection name="Transverse Mercator"> |
|
|
<parameter value="proj=tmerc"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="lat_0=90w"/> |
|
|
<parameter value="lon_0=90w"/> |
|
|
<parameter value="k=1"/> |
|
|
</projection> |
|
|
<projection name="Transverse Mercator"> |
|
|
<parameter value="proj=tmerc"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="lat_0=30w"/> |
|
|
<parameter value="lon_0=30w"/> |
|
|
<parameter value="k=1"/> |
|
|
</projection> |
|
|
<projection name="Universal Transverse Mercator"> |
|
|
<parameter value="proj=utm"/> |
|
|
<parameter value="ellps=clrk66"/> |
|
|
<parameter value="zone=1"/> |
|
|
</projection> |
|
|
</projectionlist> |
|
|
''' |
|
117 |
|
|
|
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
|
|
"ellps=clrk66", |
|
|
"lat_0=90w", |
|
|
"lon_0=90w", |
|
|
"k=1"]), |
|
|
("Transverse Mercator", ["proj=tmerc", |
|
|
"ellps=clrk66", |
|
|
"lat_0=30w", |
|
|
"lon_0=30w", |
|
|
"k=1"]), |
|
|
("Universal Transverse Mercator", ["proj=utm", |
|
|
"ellps=clrk66", |
|
|
"zone=1"])] |
|
118 |
|
|
119 |
sample_projfile2 = '''\ |
class TestProjFileSimple: |
120 |
<?xml version="1.0" encoding="UTF-8"?> |
|
121 |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
def test_init(self): |
122 |
<projectionlist> |
"""Test ProjFile coinstructor""" |
123 |
</projectionlist> |
proj_file = ProjFile("some_filename") |
124 |
''' |
self.assertEquals(proj_file.GetFilename(), "some_filename") |
125 |
|
self.assertEquals(len(proj_file.GetProjections()), 0) |
126 |
|
|
127 |
|
def test_set_filename(self): |
128 |
|
"""Test ProjFile.SetFilename()""" |
129 |
|
proj_file = ProjFile("some_filename") |
130 |
|
proj.SetFilename("other_name") |
131 |
|
self.assertEquals(proj_file.GetFilename(), "other_name") |
132 |
|
|
|
sample_projfile_data2 = [] |
|
133 |
|
|
134 |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
class TestProjFile(unittest.TestCase, support.SubscriberMixin): |
|
xmlsupport.ValidationTest): |
|
135 |
|
|
136 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
137 |
""" |
""" |
138 |
|
|
139 |
def compare_xml(self, xml1, xml2): |
def setUp(self): |
140 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.clear_messages() |
141 |
|
self.proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
142 |
|
self.proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
143 |
|
self.proj2 = Projection(["proj=lcc", "ellps=clrk66", |
144 |
|
"lat_1=0", "lat_2=20"]) |
145 |
|
self.proj_file = ProjFile("some_filename") |
146 |
|
for msg in [PROJECTION_ADDED, PROJECTION_REMOVED, PROJECTION_REPLACED]: |
147 |
|
self.proj_file.Subscribe(msg, self.subscribe_with_params, msg) |
148 |
|
|
149 |
|
def test_add_remove(self): |
150 |
|
"""Test ProjFile.Add() and ProjFile.Remove()""" |
151 |
|
self.proj_file.Add(self.proj0) |
152 |
|
self.proj_file.Add(self.proj1) |
153 |
|
self.assertEquals(self.proj_file.GetProjections(), |
154 |
|
[self.proj0, self.proj1]) |
155 |
|
self.check_messages([(self.proj0, PROJECTION_ADDED), |
156 |
|
(self.proj1, PROJECTION_ADDED)]) |
157 |
|
self.clear_messages() |
158 |
|
|
159 |
|
self.proj_file.Remove(self.proj0) |
160 |
|
self.assertEquals(self.proj_file.GetProjections(), [self.proj1]) |
161 |
|
self.check_messages([(self.proj0, PROJECTION_REMOVED)]) |
162 |
|
|
163 |
|
def test_remove_non_existing(self): |
164 |
|
"""Test ProjFile.Remove(<proj not in projfile>)""" |
165 |
|
self.assertRaises(ValueError, self.proj_file.Remove, self.proj0) |
166 |
|
# Nothing happened, so no messages should have been sent |
167 |
|
self.check_messages([]) |
168 |
|
|
169 |
|
def test_replace(self): |
170 |
|
"""Test ProjFile.Replace()""" |
171 |
|
self.proj_file.Add(self.proj0) |
172 |
|
self.proj_file.Add(self.proj1) |
173 |
|
self.clear_messages() |
174 |
|
|
175 |
def test(self): |
# Replace() |
176 |
"""Test ProjFile""" |
self.proj_file.Replace(self.proj0, self.proj2) |
177 |
|
self.assertEquals(self.proj_file.GetProjections(), |
178 |
|
[self.proj2, self.proj1]) |
179 |
|
self.check_messages([(self.proj0, self.proj2, PROJECTION_REPLACED)]) |
180 |
|
|
181 |
|
def test_replace_non_existing(self): |
182 |
|
"""Test ProjFile.Replace(<proj not in projfile>, <some proj>)""" |
183 |
|
self.proj_file.Add(self.proj0) |
184 |
|
self.proj_file.Add(self.proj1) |
185 |
|
self.clear_messages() |
186 |
|
self.assertRaises(ValueError, |
187 |
|
self.proj_file.Replace, self.proj2, self.proj0) |
188 |
|
# All projections should still be there |
189 |
|
self.assertEquals(self.proj_file.GetProjections(), |
190 |
|
[self.proj0, self.proj1]) |
191 |
|
# Nothing happened, so no messages should have been sent |
192 |
|
self.check_messages([]) |
193 |
|
|
|
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
|
|
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) |
|
194 |
|
|
195 |
projFile.Remove(proj2) |
class ProjFileTest(unittest.TestCase, support.FileTestMixin, |
196 |
projFile.Remove(proj1) |
xmlsupport.ValidationTest): |
197 |
|
|
198 |
# Replace() |
"""Base class for the proj file tests that read or write files""" |
199 |
projFile.Replace(proj0, proj1) |
|
200 |
projs = projFile.GetProjections() |
def filename(self): |
201 |
eq(projs[0], proj1) |
"""Return the filename for the test""" |
202 |
|
return self.temp_file_name(self.id() + ".proj") |
203 |
# replace a non-existent projection |
|
204 |
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
|
205 |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
class ProjFileReadTests(ProjFileTest): |
206 |
|
|
207 |
def testRead(self): |
"""Test read ProjFile objects from files""" |
208 |
"""Test read_proj_file""" |
|
209 |
|
def test_read_non_existing_file(self): |
210 |
self.doTestRead(sample_projfile_data, sample_projfile) |
"""Test read_proj_file with non-existing file""" |
|
self.doTestRead(sample_projfile_data2, sample_projfile2) |
|
|
|
|
|
# |
|
|
# file doesn't exist |
|
|
# |
|
211 |
self.assertRaises(IOError, |
self.assertRaises(IOError, |
212 |
resource.read_proj_file, self.temp_file_name("nonexistent.proj")) |
resource.read_proj_file, |
213 |
|
self.temp_file_name("nonexistent.proj")) |
214 |
|
|
215 |
|
def test_read_unreadable_file(self): |
216 |
|
"""Test read_proj_file with unreadable file |
217 |
|
|
218 |
# |
As currently written this only works on unix-like systems and |
219 |
# file isn't readable |
not e.g. on MS Windows. |
220 |
# |
""" |
221 |
filename = self.temp_file_name("projfile.proj") |
filename = self.filename() |
222 |
file = open(filename, "w") |
file = open(filename, "w") |
223 |
file.close() |
file.close() |
224 |
os.chmod(filename, 0200) # write-only |
os.chmod(filename, 0200) # write-only |
225 |
self.assertRaises(IOError, resource.read_proj_file, filename) |
self.assertRaises(IOError, resource.read_proj_file, filename) |
|
os.chmod(filename, 0600) # read/write so we reuse the file |
|
226 |
|
|
227 |
# |
def test_read_empty_file(self): |
228 |
# file has invalid XML (or none at all) |
"""Test read_proj_file with empty file""" |
229 |
# |
filename = self.filename() |
|
filename = self.temp_file_name("projfile.proj") |
|
230 |
file = open(filename, "w") |
file = open(filename, "w") |
231 |
file.close() |
file.close() |
232 |
|
|
233 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
234 |
|
|
235 |
def testWrite(self): |
def test_get_system_proj_file(self): |
236 |
"""Test write_proj_file""" |
"""Test resource.get_system_proj_file() |
237 |
|
|
238 |
|
This is primarily to test whether the system proj file contains |
239 |
|
invalid projection paramers and whether the proj file is not |
240 |
|
empty |
241 |
|
""" |
242 |
|
projfile, warnings = resource.get_system_proj_file() |
243 |
|
self.assertEquals(warnings, []) |
244 |
|
self.assert_(len(projfile.GetProjections()) > 0) |
245 |
|
|
|
self.doTestWrite(sample_projfile_data, sample_projfile) |
|
|
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
|
246 |
|
|
247 |
def doTestWrite(self, data, expected): |
class WriteProjFileTests(ProjFileTest): |
248 |
|
|
249 |
filename = self.temp_file_name("projfile.proj") |
"""Test cases for writing proj files""" |
250 |
|
|
251 |
pf = ProjFile(filename) |
def compare_xml(self, xml1, xml2): |
252 |
for proj in data: |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
|
pf.Add(Projection(proj[1], proj[0])) |
|
253 |
|
|
254 |
resource.write_proj_file(pf) |
def doTestWrite(self, projfile, expected): |
255 |
|
filename = self.filename() |
256 |
|
|
257 |
|
resource.write_proj_file(projfile) |
258 |
|
|
259 |
file = open(filename) |
file = open(filename) |
260 |
written_contents = file.read() |
written_contents = file.read() |
263 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
264 |
self.validate_data(expected) |
self.validate_data(expected) |
265 |
|
|
266 |
def doTestRead(self, data, input): |
def test_write(self): |
267 |
|
"""Test write_proj_file""" |
268 |
|
pf = ProjFile(self.filename()) |
269 |
|
pf.Add(Projection(['proj=tmerc', 'ellps=clrk66', |
270 |
|
'lat_0=90w', 'lon_0=90w', 'k=1'], |
271 |
|
"Transverse Mercator",)) |
272 |
|
pf.Add(Projection(["proj=tmerc", |
273 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
274 |
|
"k=0.999500", "x_0=400000.000", "y_0=0.000", |
275 |
|
"ellps=clrk80", "units=m"], |
276 |
|
"Anguilla 1957 / British West Indies Grid", |
277 |
|
epsg="200")) |
278 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
279 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
280 |
|
<projectionlist> |
281 |
|
<projection name="Transverse Mercator"> |
282 |
|
<parameter value="proj=tmerc"/> |
283 |
|
<parameter value="ellps=clrk66"/> |
284 |
|
<parameter value="lat_0=90w"/> |
285 |
|
<parameter value="lon_0=90w"/> |
286 |
|
<parameter value="k=1"/> |
287 |
|
</projection> |
288 |
|
<projection epsg="200" |
289 |
|
name="Anguilla 1957 / British West Indies Grid"> |
290 |
|
<parameter value="proj=tmerc"/> |
291 |
|
<parameter value="lat_0=0.000000000"/> |
292 |
|
<parameter value="lon_0=-62.000000000"/> |
293 |
|
<parameter value="k=0.999500"/> |
294 |
|
<parameter value="x_0=400000.000"/> |
295 |
|
<parameter value="y_0=0.000"/> |
296 |
|
<parameter value="ellps=clrk80"/> |
297 |
|
<parameter value="units=m"/> |
298 |
|
</projection> |
299 |
|
</projectionlist> |
300 |
|
''' |
301 |
|
self.doTestWrite(pf, file_contents) |
302 |
|
|
303 |
|
def test_write_empty_file(self): |
304 |
|
"""Test write empty ProjFile""" |
305 |
|
pf = ProjFile(self.filename()) |
306 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
307 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
308 |
|
<projectionlist> |
309 |
|
</projectionlist> |
310 |
|
''' |
311 |
|
self.doTestWrite(pf, file_contents) |
312 |
|
|
|
filename = self.temp_file_name("projfile.proj") |
|
|
file = open(filename, "w") |
|
|
file.write(input) |
|
|
file.close() |
|
313 |
|
|
314 |
pf, warnings = resource.read_proj_file(filename) |
class TestLoadingProjFile(support.FileLoadTestCase): |
315 |
|
|
316 |
|
file_extension = ".proj" |
317 |
|
file_contents = '''\ |
318 |
|
<?xml version="1.0" encoding="UTF-8"?> |
319 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
320 |
|
<projectionlist> |
321 |
|
<projection name="Transverse Mercator"> |
322 |
|
<parameter value="proj=tmerc"/> |
323 |
|
<parameter value="ellps=clrk66"/> |
324 |
|
<parameter value="lat_0=90w"/> |
325 |
|
<parameter value="lon_0=90w"/> |
326 |
|
<parameter value="k=1"/> |
327 |
|
</projection> |
328 |
|
<projection epsg="200" name="Anguilla 1957 / British West Indies Grid"> |
329 |
|
<parameter value="proj=tmerc"/> |
330 |
|
<parameter value="lat_0=0.000000000"/> |
331 |
|
<parameter value="lon_0=-62.000000000"/> |
332 |
|
<parameter value="k=0.999500"/> |
333 |
|
<parameter value="x_0=400000.000"/> |
334 |
|
<parameter value="y_0=0.000"/> |
335 |
|
<parameter value="ellps=clrk80"/> |
336 |
|
<parameter value="units=m"/> |
337 |
|
</projection> |
338 |
|
</projectionlist> |
339 |
|
''' |
340 |
|
|
341 |
|
def check_projection(self, proj, label, parameters): |
342 |
|
"""Check the values of the proj's label and parameters""" |
343 |
|
self.assertEquals(proj.Label(), label) |
344 |
|
params = proj.GetAllParameters()[:] |
345 |
|
params.sort() |
346 |
|
self.assertEquals(params, parameters) |
347 |
|
|
348 |
|
def test(self): |
349 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
350 |
|
# no warnings |
351 |
self.assertEquals(warnings, []) |
self.assertEquals(warnings, []) |
352 |
|
|
353 |
eq = self.assertEquals |
# There are two projections |
354 |
|
projs = projfile.GetProjections() |
355 |
|
self.assertEquals(len(projs), 2) |
356 |
|
|
357 |
eq(pf.GetFilename(), filename) |
self.check_projection(projs[0], |
358 |
|
"Transverse Mercator", |
359 |
|
['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w', |
360 |
|
'proj=tmerc']) |
361 |
|
self.check_projection(projs[1], |
362 |
|
"EPSG 200 Anguilla 1957 / British West Indies Grid", |
363 |
|
["ellps=clrk80", "k=0.999500", |
364 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
365 |
|
"proj=tmerc", "units=m", |
366 |
|
"x_0=400000.000", "y_0=0.000"]) |
367 |
|
|
|
for proj, d in zip(pf.GetProjections(), data): |
|
|
eq(proj.GetName(), d[0]) |
|
|
for param in proj.GetAllParameters(): |
|
|
self.assert_(param in d[1]) |
|
368 |
|
|
369 |
def test_get_system_proj_file(self): |
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
|
"""Test resource.get_system_proj_file() |
|
370 |
|
|
371 |
This is primarily to test whether the system proj file contains |
file_extension = ".proj" |
372 |
invalid projection paramers and whether the proj file is not |
file_contents = '''\ |
373 |
empty |
<?xml version="1.0" encoding="UTF-8"?> |
374 |
""" |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
375 |
projfile, warnings = resource.get_system_proj_file() |
<projectionlist> |
376 |
|
</projectionlist> |
377 |
|
''' |
378 |
|
|
379 |
|
def test(self): |
380 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
381 |
|
# no warnings |
382 |
self.assertEquals(warnings, []) |
self.assertEquals(warnings, []) |
383 |
self.assert_(len(projfile.GetProjections()) > 0) |
|
384 |
|
# There are no projections |
385 |
|
self.assertEquals(len(projfile.GetProjections()), 0) |
386 |
|
|
387 |
|
|
388 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
427 |
' invalid UTM zone number']) |
' invalid UTM zone number']) |
428 |
|
|
429 |
|
|
|
|
|
430 |
if __name__ == "__main__": |
if __name__ == "__main__": |
431 |
unittest.main() |
unittest.main() |