89 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
90 |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
self.assertEquals(proj.GetProjectedUnits(), PROJ_UNITS_METERS) |
91 |
|
|
92 |
|
def test_label(self): |
93 |
|
"""Test Projection.Label() without epsg""" |
94 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
95 |
|
name = "My Projection") |
96 |
|
self.assertEquals(proj.Label(), "My Projection") |
97 |
|
|
98 |
|
def test_label_epsg(self): |
99 |
|
"""Test Projection.Label() with epsg""" |
100 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
101 |
|
name = "My Projection", epsg="42") |
102 |
|
self.assertEquals(proj.Label(), "EPSG 42 My Projection") |
103 |
|
|
104 |
|
def test_epsgcode_for_non_epsg_projection(self): |
105 |
|
"""Test Projection.EPSGCode() without epsg""" |
106 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
107 |
|
name = "My Projection") |
108 |
|
self.assertEquals(proj.EPSGCode(), None) |
109 |
|
|
110 |
|
def test_epsgcode_for_real_epsg_projection(self): |
111 |
|
"""Test Projection.EPSGCode() with epsg""" |
112 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"], |
113 |
|
name = "My Projection", epsg="42") |
114 |
|
self.assertEquals(proj.EPSGCode(), "42") |
115 |
|
|
|
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> |
|
|
''' |
|
|
|
|
|
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"])] |
|
|
|
|
|
sample_projfile2 = '''\ |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
|
|
<projectionlist> |
|
|
</projectionlist> |
|
|
''' |
|
|
|
|
|
sample_projfile_data2 = [] |
|
116 |
|
|
117 |
class ProjFileTest(unittest.TestCase, support.FileTestMixin): |
class ProjFileTest(unittest.TestCase, support.FileTestMixin): |
118 |
|
|
191 |
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
self.assertRaises(ValueError, projFile.Replace, None, proj2) |
192 |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
193 |
|
|
|
def testRead(self): |
|
|
"""Test read_proj_file""" |
|
|
self.doTestRead(sample_projfile_data, sample_projfile) |
|
|
self.doTestRead(sample_projfile_data2, sample_projfile2) |
|
|
|
|
194 |
def test_read_non_existing_file(self): |
def test_read_non_existing_file(self): |
195 |
"""Test read_proj_file with non-existing file""" |
"""Test read_proj_file with non-existing file""" |
196 |
self.assertRaises(IOError, |
self.assertRaises(IOError, |
208 |
file.close() |
file.close() |
209 |
os.chmod(filename, 0200) # write-only |
os.chmod(filename, 0200) # write-only |
210 |
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 |
|
211 |
|
|
212 |
def test_read_empty_file(self): |
def test_read_empty_file(self): |
213 |
"""Test read_proj_file with empty file""" |
"""Test read_proj_file with empty file""" |
217 |
|
|
218 |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
219 |
|
|
|
def doTestRead(self, data, input): |
|
|
|
|
|
filename = self.filename() |
|
|
file = open(filename, "w") |
|
|
file.write(input) |
|
|
file.close() |
|
|
|
|
|
pf, warnings = resource.read_proj_file(filename) |
|
|
self.assertEquals(warnings, []) |
|
|
|
|
|
eq = self.assertEquals |
|
|
|
|
|
eq(pf.GetFilename(), filename) |
|
|
|
|
|
for proj, d in zip(pf.GetProjections(), data): |
|
|
eq(proj.GetName(), d[0]) |
|
|
for param in proj.GetAllParameters(): |
|
|
self.assert_(param in d[1]) |
|
|
|
|
220 |
def test_get_system_proj_file(self): |
def test_get_system_proj_file(self): |
221 |
"""Test resource.get_system_proj_file() |
"""Test resource.get_system_proj_file() |
222 |
|
|
236 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
237 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
238 |
|
|
239 |
def doTestWrite(self, data, expected): |
def doTestWrite(self, projfile, expected): |
240 |
filename = self.filename() |
filename = self.filename() |
241 |
|
|
242 |
pf = ProjFile(filename) |
resource.write_proj_file(projfile) |
|
for proj in data: |
|
|
pf.Add(Projection(proj[1], proj[0])) |
|
|
|
|
|
resource.write_proj_file(pf) |
|
243 |
|
|
244 |
file = open(filename) |
file = open(filename) |
245 |
written_contents = file.read() |
written_contents = file.read() |
250 |
|
|
251 |
def test_write(self): |
def test_write(self): |
252 |
"""Test write_proj_file""" |
"""Test write_proj_file""" |
253 |
self.doTestWrite(sample_projfile_data, sample_projfile) |
pf = ProjFile(self.filename()) |
254 |
|
pf.Add(Projection(['proj=tmerc', 'ellps=clrk66', |
255 |
|
'lat_0=90w', 'lon_0=90w', 'k=1'], |
256 |
|
"Transverse Mercator",)) |
257 |
|
pf.Add(Projection(["proj=tmerc", |
258 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
259 |
|
"k=0.999500", "x_0=400000.000", "y_0=0.000", |
260 |
|
"ellps=clrk80", "units=m"], |
261 |
|
"Anguilla 1957 / British West Indies Grid", |
262 |
|
epsg="200")) |
263 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
264 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
265 |
|
<projectionlist> |
266 |
|
<projection name="Transverse Mercator"> |
267 |
|
<parameter value="proj=tmerc"/> |
268 |
|
<parameter value="ellps=clrk66"/> |
269 |
|
<parameter value="lat_0=90w"/> |
270 |
|
<parameter value="lon_0=90w"/> |
271 |
|
<parameter value="k=1"/> |
272 |
|
</projection> |
273 |
|
<projection epsg="200" |
274 |
|
name="Anguilla 1957 / British West Indies Grid"> |
275 |
|
<parameter value="proj=tmerc"/> |
276 |
|
<parameter value="lat_0=0.000000000"/> |
277 |
|
<parameter value="lon_0=-62.000000000"/> |
278 |
|
<parameter value="k=0.999500"/> |
279 |
|
<parameter value="x_0=400000.000"/> |
280 |
|
<parameter value="y_0=0.000"/> |
281 |
|
<parameter value="ellps=clrk80"/> |
282 |
|
<parameter value="units=m"/> |
283 |
|
</projection> |
284 |
|
</projectionlist> |
285 |
|
''' |
286 |
|
self.doTestWrite(pf, file_contents) |
287 |
|
|
288 |
def test_write_empty_file(self): |
def test_write_empty_file(self): |
289 |
"""Test write empty ProjFile""" |
"""Test write empty ProjFile""" |
290 |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
pf = ProjFile(self.filename()) |
291 |
|
file_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
292 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
293 |
|
<projectionlist> |
294 |
|
</projectionlist> |
295 |
|
''' |
296 |
|
self.doTestWrite(pf, file_contents) |
297 |
|
|
298 |
|
|
299 |
|
class TestLoadingProjFile(support.FileLoadTestCase): |
300 |
|
|
301 |
|
file_extension = ".proj" |
302 |
|
file_contents = '''\ |
303 |
|
<?xml version="1.0" encoding="UTF-8"?> |
304 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
305 |
|
<projectionlist> |
306 |
|
<projection name="Transverse Mercator"> |
307 |
|
<parameter value="proj=tmerc"/> |
308 |
|
<parameter value="ellps=clrk66"/> |
309 |
|
<parameter value="lat_0=90w"/> |
310 |
|
<parameter value="lon_0=90w"/> |
311 |
|
<parameter value="k=1"/> |
312 |
|
</projection> |
313 |
|
<projection epsg="200" name="Anguilla 1957 / British West Indies Grid"> |
314 |
|
<parameter value="proj=tmerc"/> |
315 |
|
<parameter value="lat_0=0.000000000"/> |
316 |
|
<parameter value="lon_0=-62.000000000"/> |
317 |
|
<parameter value="k=0.999500"/> |
318 |
|
<parameter value="x_0=400000.000"/> |
319 |
|
<parameter value="y_0=0.000"/> |
320 |
|
<parameter value="ellps=clrk80"/> |
321 |
|
<parameter value="units=m"/> |
322 |
|
</projection> |
323 |
|
</projectionlist> |
324 |
|
''' |
325 |
|
|
326 |
|
def check_projection(self, proj, label, parameters): |
327 |
|
"""Check the values of the proj's label and parameters""" |
328 |
|
self.assertEquals(proj.Label(), label) |
329 |
|
params = proj.GetAllParameters()[:] |
330 |
|
params.sort() |
331 |
|
self.assertEquals(params, parameters) |
332 |
|
|
333 |
|
def test(self): |
334 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
335 |
|
# no warnings |
336 |
|
self.assertEquals(warnings, []) |
337 |
|
|
338 |
|
# There are two projections |
339 |
|
projs = projfile.GetProjections() |
340 |
|
self.assertEquals(len(projs), 2) |
341 |
|
|
342 |
|
self.check_projection(projs[0], |
343 |
|
"Transverse Mercator", |
344 |
|
['ellps=clrk66', 'k=1', 'lat_0=90w', 'lon_0=90w', |
345 |
|
'proj=tmerc']) |
346 |
|
self.check_projection(projs[1], |
347 |
|
"EPSG 200 Anguilla 1957 / British West Indies Grid", |
348 |
|
["ellps=clrk80", "k=0.999500", |
349 |
|
"lat_0=0.000000000", "lon_0=-62.000000000", |
350 |
|
"proj=tmerc", "units=m", |
351 |
|
"x_0=400000.000", "y_0=0.000"]) |
352 |
|
|
353 |
|
|
354 |
|
class TestLoadingProjFileWithEmptyProjectionlist(support.FileLoadTestCase): |
355 |
|
|
356 |
|
file_extension = ".proj" |
357 |
|
file_contents = '''\ |
358 |
|
<?xml version="1.0" encoding="UTF-8"?> |
359 |
|
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
360 |
|
<projectionlist> |
361 |
|
</projectionlist> |
362 |
|
''' |
363 |
|
|
364 |
|
def test(self): |
365 |
|
projfile, warnings = resource.read_proj_file(self.filename()) |
366 |
|
# no warnings |
367 |
|
self.assertEquals(warnings, []) |
368 |
|
|
369 |
|
# There are no projections |
370 |
|
self.assertEquals(len(projfile.GetProjections()), 0) |
371 |
|
|
372 |
|
|
373 |
class TestProjFileWithInvalidParameters(unittest.TestCase, |
class TestProjFileWithInvalidParameters(unittest.TestCase, |