1 |
bh |
723 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
bh |
292 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Test saving a thuban session as XML |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
# $Source$ |
14 |
|
|
# $Id$ |
15 |
|
|
|
16 |
|
|
import os |
17 |
|
|
import unittest |
18 |
|
|
from StringIO import StringIO |
19 |
|
|
|
20 |
|
|
import xml.sax |
21 |
|
|
import xml.sax.handler |
22 |
jonathan |
530 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
23 |
bh |
292 |
|
24 |
|
|
import support |
25 |
|
|
support.initthuban() |
26 |
|
|
|
27 |
jonathan |
1168 |
from Thuban.Model.save import XMLWriter, save_session |
28 |
bh |
292 |
from Thuban.Model.session import Session |
29 |
|
|
from Thuban.Model.map import Map |
30 |
jonathan |
947 |
from Thuban.Model.layer import Layer, RasterLayer |
31 |
bh |
292 |
from Thuban.Model.proj import Projection |
32 |
|
|
|
33 |
jonathan |
1168 |
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
34 |
|
|
ClassGroupProperties |
35 |
bh |
292 |
|
36 |
jonathan |
1168 |
from Thuban.Model.range import Range |
37 |
|
|
|
38 |
|
|
|
39 |
bh |
292 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
40 |
|
|
|
41 |
|
|
def __init__(self): |
42 |
|
|
self.eventlist = [] |
43 |
|
|
|
44 |
|
|
def startElementNS(self, name, qname, attrs): |
45 |
|
|
items = attrs.items() |
46 |
|
|
items.sort() |
47 |
|
|
self.eventlist.append(("start", name, qname, items)) |
48 |
|
|
|
49 |
|
|
def endElementNS(self, name, qname): |
50 |
|
|
self.eventlist.append(("end", name, qname)) |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
def sax_eventlist(data): |
54 |
|
|
"""Return a list of SAX event generated for the XML data. |
55 |
|
|
""" |
56 |
|
|
handler = SaxEventLister() |
57 |
|
|
parser = make_parser() |
58 |
|
|
parser.setContentHandler(handler) |
59 |
|
|
parser.setErrorHandler(ErrorHandler()) |
60 |
|
|
parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
61 |
|
|
|
62 |
jonathan |
530 |
# |
63 |
|
|
# see comment at the end of Thuban/Model/load.py |
64 |
|
|
# |
65 |
|
|
try: |
66 |
|
|
parser.setFeature(xml.sax.handler.feature_validation, 0) |
67 |
|
|
parser.setFeature(xml.sax.handler.feature_external_ges, 0) |
68 |
|
|
parser.setFeature(xml.sax.handler.feature_external_pes, 0) |
69 |
|
|
except SAXNotRecognizedException: |
70 |
|
|
pass |
71 |
|
|
|
72 |
bh |
292 |
inpsrc = xml.sax.InputSource() |
73 |
|
|
inpsrc.setByteStream(StringIO(data)) |
74 |
|
|
parser.parse(inpsrc) |
75 |
|
|
|
76 |
|
|
return handler.eventlist |
77 |
|
|
|
78 |
jonathan |
1168 |
class XMLWriterTest(unittest.TestCase): |
79 |
|
|
|
80 |
|
|
def testEncode(self): |
81 |
|
|
"""Test XMLWriter.encode""" |
82 |
|
|
writer = XMLWriter() |
83 |
jonathan |
1200 |
eq = self.assertEquals |
84 |
jonathan |
1168 |
|
85 |
jonathan |
1200 |
eq(writer.encode("hello world"), "hello world") |
86 |
|
|
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
87 |
jonathan |
1173 |
|
88 |
jonathan |
1200 |
eq(writer.encode("\x80\x90\xc2\x100"), |
89 |
|
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
90 |
|
|
eq(writer.encode(u"\x80\x90\xc2\x100"), |
91 |
|
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
92 |
|
|
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
93 |
jonathan |
1173 |
|
94 |
jonathan |
1200 |
eq(writer.encode('&"\'<>'), "&"'<>") |
95 |
|
|
eq(writer.encode(unicode('&"\'<>')), "&"'<>") |
96 |
jonathan |
1168 |
|
97 |
bh |
292 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
98 |
|
|
|
99 |
|
|
def compare_xml(self, xml1, xml2): |
100 |
|
|
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
101 |
|
|
|
102 |
|
|
def testEmptySession(self): |
103 |
|
|
"""Save an empty session""" |
104 |
|
|
session = Session("empty session") |
105 |
|
|
filename = self.temp_file_name("save_emptysession.thuban") |
106 |
|
|
save_session(session, filename) |
107 |
|
|
session.Destroy() |
108 |
|
|
|
109 |
|
|
file = open(filename) |
110 |
|
|
written_contents = file.read() |
111 |
|
|
file.close() |
112 |
|
|
self.compare_xml(written_contents, |
113 |
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n' |
114 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
115 |
|
|
'<session title="empty session">\n</session>\n') |
116 |
|
|
|
117 |
|
|
def testSingleLayer(self): |
118 |
|
|
"""Save a session with a single map with a single layer""" |
119 |
|
|
# deliberately put an apersand in the title :) |
120 |
|
|
session = Session("single map&layer") |
121 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
122 |
|
|
map = Map("Test Map", projection = proj) |
123 |
|
|
session.AddMap(map) |
124 |
|
|
# use shapefile from the example data |
125 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
126 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
127 |
bh |
723 |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
128 |
bh |
292 |
map.AddLayer(layer) |
129 |
|
|
|
130 |
|
|
filename = self.temp_file_name("save_singlemap.thuban") |
131 |
|
|
save_session(session, filename) |
132 |
|
|
|
133 |
|
|
file = open(filename) |
134 |
|
|
written_contents = file.read() |
135 |
|
|
file.close() |
136 |
jonathan |
775 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
137 |
bh |
292 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
138 |
|
|
<session title="single map&layer"> |
139 |
|
|
<map title="Test Map"> |
140 |
jonathan |
755 |
<projection name="Unknown"> |
141 |
bh |
292 |
<parameter value="zone=26"/> |
142 |
|
|
<parameter value="proj=utm"/> |
143 |
|
|
<parameter value="ellps=clrk66"/> |
144 |
|
|
</projection> |
145 |
|
|
<layer title="My Layer" filename="%s" |
146 |
jonathan |
775 |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
147 |
bh |
292 |
</map> |
148 |
jonathan |
775 |
</session>''' |
149 |
|
|
|
150 |
|
|
expected_contents = expected_template % \ |
151 |
|
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
152 |
|
|
"true") |
153 |
|
|
|
154 |
jonathan |
494 |
#print written_contents |
155 |
|
|
#print "********************************************" |
156 |
|
|
#print expected_contents |
157 |
bh |
292 |
self.compare_xml(written_contents, expected_contents) |
158 |
|
|
|
159 |
jonathan |
775 |
layer.SetVisible(False) |
160 |
|
|
save_session(session, filename) |
161 |
|
|
|
162 |
|
|
file = open(filename) |
163 |
|
|
written_contents = file.read() |
164 |
|
|
file.close() |
165 |
|
|
expected_contents = expected_template % \ |
166 |
|
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
167 |
|
|
"false") |
168 |
|
|
|
169 |
|
|
#print written_contents |
170 |
|
|
#print "********************************************" |
171 |
|
|
#print expected_contents |
172 |
|
|
self.compare_xml(written_contents, expected_contents) |
173 |
|
|
|
174 |
|
|
session.Destroy() |
175 |
|
|
|
176 |
jonathan |
755 |
def testLayerProjection(self): |
177 |
|
|
# deliberately put an apersand in the title :) |
178 |
|
|
session = Session("single map&layer") |
179 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
180 |
|
|
map = Map("Test Map", projection = proj) |
181 |
|
|
session.AddMap(map) |
182 |
|
|
# use shapefile from the example data |
183 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
184 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
185 |
|
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
186 |
|
|
proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection") |
187 |
|
|
layer.SetProjection(proj) |
188 |
|
|
map.AddLayer(layer) |
189 |
bh |
292 |
|
190 |
jonathan |
755 |
filename = self.temp_file_name("save_singlemap.thuban") |
191 |
|
|
save_session(session, filename) |
192 |
|
|
session.Destroy() |
193 |
bh |
292 |
|
194 |
jonathan |
755 |
file = open(filename) |
195 |
|
|
written_contents = file.read() |
196 |
|
|
file.close() |
197 |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
198 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
199 |
|
|
<session title="single map&layer"> |
200 |
|
|
<map title="Test Map"> |
201 |
|
|
<projection name="Unknown"> |
202 |
|
|
<parameter value="zone=26"/> |
203 |
|
|
<parameter value="proj=utm"/> |
204 |
|
|
<parameter value="ellps=clrk66"/> |
205 |
|
|
</projection> |
206 |
|
|
<layer title="My Layer" filename="%s" |
207 |
jonathan |
775 |
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
208 |
jonathan |
755 |
<projection name="Layer Projection"> |
209 |
|
|
<parameter value="proj=lcc"/> |
210 |
|
|
<parameter value="ellps=clrk66"/> |
211 |
|
|
</projection> |
212 |
|
|
</layer> |
213 |
|
|
</map> |
214 |
|
|
</session>''' % os.path.join("..", "..", "Data", "iceland", |
215 |
|
|
"political.shp") |
216 |
|
|
#print written_contents |
217 |
|
|
#print "********************************************" |
218 |
|
|
#print expected_contents |
219 |
|
|
self.compare_xml(written_contents, expected_contents) |
220 |
|
|
|
221 |
|
|
|
222 |
jonathan |
947 |
def testRasterLayer(self): |
223 |
|
|
# deliberately put an apersand in the title :) |
224 |
|
|
session = Session("single map&layer") |
225 |
|
|
map = Map("Test Map") |
226 |
|
|
session.AddMap(map) |
227 |
|
|
# use shapefile from the example data |
228 |
|
|
imgfile = os.path.join(os.path.dirname(__file__), |
229 |
|
|
os.pardir, "Data", "iceland", "island.tif") |
230 |
|
|
layer = RasterLayer("My RasterLayer", imgfile) |
231 |
|
|
map.AddLayer(layer) |
232 |
|
|
|
233 |
|
|
filename = self.temp_file_name("save_singlemap.thuban") |
234 |
|
|
save_session(session, filename) |
235 |
|
|
session.Destroy() |
236 |
|
|
|
237 |
|
|
file = open(filename) |
238 |
|
|
written_contents = file.read() |
239 |
|
|
file.close() |
240 |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
241 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
242 |
|
|
<session title="single map&layer"> |
243 |
|
|
<map title="Test Map"> |
244 |
|
|
<rasterlayer title="My RasterLayer" filename="%s" |
245 |
|
|
visible="true"> |
246 |
|
|
</rasterlayer> |
247 |
|
|
</map> |
248 |
|
|
</session>''' % os.path.join(os.path.dirname(__file__), |
249 |
|
|
os.pardir, "Data", "iceland", |
250 |
|
|
"island.tif") |
251 |
|
|
#print written_contents |
252 |
|
|
#print "********************************************" |
253 |
|
|
#print expected_contents |
254 |
|
|
self.compare_xml(written_contents, expected_contents) |
255 |
jonathan |
755 |
|
256 |
jonathan |
1168 |
def testClassifiedLayer(self): |
257 |
|
|
"""Save a session with a single map with a single layer |
258 |
|
|
with a classificaton. |
259 |
|
|
""" |
260 |
|
|
# deliberately put an apersand in the title :) |
261 |
|
|
session = Session("single map&layer") |
262 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
263 |
|
|
map = Map("Test Map", projection = proj) |
264 |
|
|
session.AddMap(map) |
265 |
|
|
# use shapefile from the example data |
266 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
267 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
268 |
|
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
269 |
|
|
map.AddLayer(layer) |
270 |
jonathan |
755 |
|
271 |
jonathan |
1168 |
clazz = layer.GetClassification() |
272 |
|
|
|
273 |
|
|
clazz.SetField("AREA") |
274 |
|
|
|
275 |
|
|
clazz.AppendGroup(ClassGroupSingleton(42, |
276 |
|
|
ClassGroupProperties(), |
277 |
|
|
"single")) |
278 |
|
|
clazz.AppendGroup(ClassGroupSingleton("text", |
279 |
|
|
ClassGroupProperties(), |
280 |
|
|
"single-text")) |
281 |
|
|
|
282 |
|
|
clazz.AppendGroup(ClassGroupRange(0, 42, |
283 |
|
|
ClassGroupProperties(), |
284 |
|
|
"range")) |
285 |
|
|
|
286 |
|
|
range = ClassGroupRange(Range("[0;42]")) |
287 |
|
|
range.SetProperties(ClassGroupProperties()) |
288 |
|
|
range.SetLabel("new-range") |
289 |
|
|
clazz.AppendGroup(range) |
290 |
|
|
|
291 |
|
|
filename = self.temp_file_name("save_singlemap.thuban") |
292 |
|
|
save_session(session, filename) |
293 |
|
|
|
294 |
|
|
file = open(filename) |
295 |
|
|
written_contents = file.read() |
296 |
|
|
file.close() |
297 |
|
|
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
298 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
299 |
|
|
<session title="single map&layer"> |
300 |
|
|
<map title="Test Map"> |
301 |
|
|
<projection name="Unknown"> |
302 |
|
|
<parameter value="zone=26"/> |
303 |
|
|
<parameter value="proj=utm"/> |
304 |
|
|
<parameter value="ellps=clrk66"/> |
305 |
|
|
</projection> |
306 |
|
|
<layer title="My Layer" filename="%s" |
307 |
|
|
fill="None" stroke="#000000" stroke_width="1" visible="%s"> |
308 |
|
|
<classification field="AREA" field_type="double"> |
309 |
|
|
<clnull label=""> |
310 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
311 |
|
|
</clnull> |
312 |
|
|
<clpoint value="42" label="single"> |
313 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
314 |
|
|
</clpoint> |
315 |
|
|
<clpoint value="text" label="single-text"> |
316 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
317 |
|
|
</clpoint> |
318 |
|
|
<clrange range="[0;42[" label="range"> |
319 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
320 |
|
|
</clrange> |
321 |
|
|
<clrange range="[0;42]" label="new-range"> |
322 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
323 |
|
|
</clrange> |
324 |
|
|
</classification> |
325 |
|
|
</layer> |
326 |
|
|
</map> |
327 |
|
|
</session>''' |
328 |
|
|
|
329 |
|
|
expected_contents = expected_template % \ |
330 |
|
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
331 |
|
|
"true") |
332 |
|
|
|
333 |
|
|
#print written_contents |
334 |
|
|
#print "********************************************" |
335 |
|
|
#print expected_contents |
336 |
|
|
self.compare_xml(written_contents, expected_contents) |
337 |
|
|
|
338 |
|
|
session.Destroy() |
339 |
|
|
|
340 |
|
|
|
341 |
bh |
292 |
if __name__ == "__main__": |
342 |
|
|
# Fake the __file__ global because it's needed by a test |
343 |
|
|
import sys |
344 |
|
|
__file__ = sys.argv[0] |
345 |
bh |
723 |
support.run_tests() |