1 |
# Copyright (c) 2002 by Intevation GmbH |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
13 |
# $Source$ |
# $Source$ |
14 |
# $Id$ |
# $Id$ |
15 |
|
|
|
from Thuban import _ |
|
|
|
|
16 |
import unittest |
import unittest |
17 |
import os |
import os |
18 |
|
|
19 |
|
import xmlsupport |
20 |
import support |
import support |
21 |
support.initthuban() |
support.initthuban() |
22 |
|
|
23 |
|
from Thuban import _ |
24 |
from Thuban.Model.proj import Projection, ProjFile |
from Thuban.Model.proj import Projection, ProjFile |
25 |
|
|
26 |
import Thuban.Model.resource as resource |
import Thuban.Model.resource as resource |
27 |
|
|
28 |
from test_save import sax_eventlist |
from xmlsupport import sax_eventlist |
29 |
|
|
30 |
from xml.sax import SAXParseException |
from xml.sax import SAXParseException |
31 |
|
|
73 |
|
|
74 |
sample_projfile = '''\ |
sample_projfile = '''\ |
75 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
76 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
77 |
<projectionlist> |
<projectionlist> |
78 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
79 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
80 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
81 |
<parameter value="lat_0=90w"/> |
<parameter value="lat_0=90w"/> |
82 |
<parameter value="lon_0=90w"/> |
<parameter value="lon_0=90w"/> |
83 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
84 |
</projection> |
</projection> |
85 |
<projection name="Transverse Mercartor"> |
<projection name="Transverse Mercator"> |
86 |
<parameter value="proj=tmerc"/> |
<parameter value="proj=tmerc"/> |
87 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
88 |
<parameter value="lat_0=30w"/> |
<parameter value="lat_0=30w"/> |
89 |
<parameter value="lon_0=30w"/> |
<parameter value="lon_0=30w"/> |
90 |
<parameter value="k=1"/> |
<parameter value="k=1"/> |
91 |
</projection> |
</projection> |
92 |
<projection name="Universal Transverse Mercartor"> |
<projection name="Universal Transverse Mercator"> |
93 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
94 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
95 |
<parameter value="zone=1"/> |
<parameter value="zone=1"/> |
97 |
</projectionlist> |
</projectionlist> |
98 |
''' |
''' |
99 |
|
|
100 |
sample_projfile_data = [("Transverse Mercartor", ["proj=tmerc", |
sample_projfile_data = [("Transverse Mercator", ["proj=tmerc", |
101 |
"ellps=clrk66", |
"ellps=clrk66", |
102 |
"lat_0=90w", |
"lat_0=90w", |
103 |
"lon_0=90w", |
"lon_0=90w", |
104 |
"k=1"]), |
"k=1"]), |
105 |
("Transverse Mercartor", ["proj=tmerc", |
("Transverse Mercator", ["proj=tmerc", |
106 |
"ellps=clrk66", |
"ellps=clrk66", |
107 |
"lat_0=30w", |
"lat_0=30w", |
108 |
"lon_0=30w", |
"lon_0=30w", |
109 |
"k=1"]), |
"k=1"]), |
110 |
("Universal Transverse Mercartor", ["proj=utm", |
("Universal Transverse Mercator", ["proj=utm", |
111 |
"ellps=clrk66", |
"ellps=clrk66", |
112 |
"zone=1"])] |
"zone=1"])] |
113 |
|
|
114 |
sample_projfile2 = '''\ |
sample_projfile2 = '''\ |
115 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
116 |
<!DOCTYPE projfile SYSTEM "thuban.dtd"> |
<!DOCTYPE projectionlist SYSTEM "projfile.dtd"> |
117 |
<projectionlist> |
<projectionlist> |
118 |
</projectionlist> |
</projectionlist> |
119 |
''' |
''' |
120 |
|
|
121 |
sample_projfile_data2 = [] |
sample_projfile_data2 = [] |
122 |
|
|
123 |
class TestProjFile(unittest.TestCase, support.FileTestMixin): |
class TestProjFile(unittest.TestCase, support.FileTestMixin, |
124 |
|
xmlsupport.ValidationTest): |
125 |
|
|
126 |
"""Test cases for reading and writing projection files. |
"""Test cases for reading and writing projection files. |
127 |
""" |
""" |
134 |
|
|
135 |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
136 |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
137 |
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
proj2 = Projection(["proj=lcc", "ellps=clrk66", |
138 |
|
"lat_1=0", "lat_2=20"]) |
139 |
|
|
140 |
eq = self.assertEquals |
eq = self.assertEquals |
141 |
|
|
193 |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
self.assertRaises(ValueError, projFile.Replace, proj0, proj2) |
194 |
|
|
195 |
def testRead(self): |
def testRead(self): |
196 |
"""Test ReadProjFile""" |
"""Test read_proj_file""" |
197 |
|
|
198 |
self.doTestRead(sample_projfile_data, sample_projfile) |
self.doTestRead(sample_projfile_data, sample_projfile) |
199 |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
202 |
# file doesn't exist |
# file doesn't exist |
203 |
# |
# |
204 |
self.assertRaises(IOError, |
self.assertRaises(IOError, |
205 |
resource.ReadProjFile, self.temp_file_name("nonexistent.proj")) |
resource.read_proj_file, self.temp_file_name("nonexistent.proj")) |
206 |
|
|
207 |
# |
# |
208 |
# file isn't readable |
# file isn't readable |
211 |
file = open(filename, "w") |
file = open(filename, "w") |
212 |
file.close() |
file.close() |
213 |
os.chmod(filename, 0200) # write-only |
os.chmod(filename, 0200) # write-only |
214 |
self.assertRaises(IOError, resource.ReadProjFile, filename) |
self.assertRaises(IOError, resource.read_proj_file, filename) |
215 |
os.chmod(filename, 0600) # read/write so we reuse the file |
os.chmod(filename, 0600) # read/write so we reuse the file |
216 |
|
|
217 |
# |
# |
221 |
file = open(filename, "w") |
file = open(filename, "w") |
222 |
file.close() |
file.close() |
223 |
|
|
224 |
self.assertRaises(SAXParseException, resource.ReadProjFile, filename) |
self.assertRaises(SAXParseException, resource.read_proj_file, filename) |
225 |
|
|
226 |
def testWrite(self): |
def testWrite(self): |
227 |
"""Test WriteProjFile""" |
"""Test write_proj_file""" |
228 |
|
|
229 |
self.doTestWrite(sample_projfile_data, sample_projfile) |
self.doTestWrite(sample_projfile_data, sample_projfile) |
230 |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
self.doTestWrite(sample_projfile_data2, sample_projfile2) |
237 |
for proj in data: |
for proj in data: |
238 |
pf.Add(Projection(proj[1], proj[0])) |
pf.Add(Projection(proj[1], proj[0])) |
239 |
|
|
240 |
resource.WriteProjFile(pf) |
resource.write_proj_file(pf) |
241 |
|
|
242 |
file = open(filename) |
file = open(filename) |
243 |
written_contents = file.read() |
written_contents = file.read() |
244 |
file.close() |
file.close() |
245 |
self.compare_xml(written_contents, expected) |
self.compare_xml(written_contents, expected) |
246 |
|
self.validate_data(written_contents) |
247 |
|
self.validate_data(expected) |
248 |
|
|
249 |
def doTestRead(self, data, input): |
def doTestRead(self, data, input): |
250 |
|
|
253 |
file.write(input) |
file.write(input) |
254 |
file.close() |
file.close() |
255 |
|
|
256 |
pf = resource.ReadProjFile(filename) |
pf = resource.read_proj_file(filename) |
257 |
|
|
258 |
eq = self.assertEquals |
eq = self.assertEquals |
259 |
|
|