21 |
import xml.sax.handler |
import xml.sax.handler |
22 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
23 |
|
|
24 |
|
import xmlsupport |
25 |
|
|
26 |
import support |
import support |
27 |
support.initthuban() |
support.initthuban() |
28 |
|
|
29 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import XMLWriter, save_session |
30 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
31 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
32 |
from Thuban.Model.layer import Layer, RasterLayer |
from Thuban.Model.layer import Layer, RasterLayer |
33 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
34 |
|
|
35 |
|
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
36 |
|
ClassGroupProperties |
37 |
|
|
38 |
|
from Thuban.Model.range import Range |
39 |
|
|
40 |
|
|
41 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
class SaxEventLister(xml.sax.handler.ContentHandler): |
42 |
|
|
77 |
|
|
78 |
return handler.eventlist |
return handler.eventlist |
79 |
|
|
80 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
class XMLWriterTest(unittest.TestCase): |
81 |
|
|
82 |
|
def testEncode(self): |
83 |
|
"""Test XMLWriter.encode""" |
84 |
|
writer = XMLWriter() |
85 |
|
eq = self.assertEquals |
86 |
|
|
87 |
|
eq(writer.encode("hello world"), "hello world") |
88 |
|
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
89 |
|
|
90 |
|
eq(writer.encode("\x80\x90\xc2\x100"), |
91 |
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
92 |
|
eq(writer.encode(u"\x80\x90\xc2\x100"), |
93 |
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
94 |
|
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
95 |
|
|
96 |
|
eq(writer.encode('&"\'<>'), "&"'<>") |
97 |
|
eq(writer.encode(unicode('&"\'<>')), "&"'<>") |
98 |
|
|
99 |
|
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
100 |
|
xmlsupport.ValidationTest): |
101 |
|
|
102 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
103 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
117 |
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
118 |
'<session title="empty session">\n</session>\n') |
'<session title="empty session">\n</session>\n') |
119 |
|
|
120 |
|
self.validate_data(written_contents) |
121 |
|
|
122 |
def testSingleLayer(self): |
def testSingleLayer(self): |
123 |
"""Save a session with a single map with a single layer""" |
"""Save a session with a single map with a single layer""" |
124 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
161 |
#print expected_contents |
#print expected_contents |
162 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
163 |
|
|
164 |
|
self.validate_data(written_contents) |
165 |
|
|
166 |
layer.SetVisible(False) |
layer.SetVisible(False) |
167 |
save_session(session, filename) |
save_session(session, filename) |
168 |
|
|
177 |
#print "********************************************" |
#print "********************************************" |
178 |
#print expected_contents |
#print expected_contents |
179 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
180 |
|
self.validate_data(written_contents) |
181 |
|
|
182 |
session.Destroy() |
session.Destroy() |
183 |
|
|
226 |
#print expected_contents |
#print expected_contents |
227 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
228 |
|
|
229 |
|
self.validate_data(written_contents) |
230 |
|
|
231 |
def testRasterLayer(self): |
def testRasterLayer(self): |
232 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
233 |
session = Session("single map&layer") |
session = Session("single map&layer") |
238 |
os.pardir, "Data", "iceland", "island.tif") |
os.pardir, "Data", "iceland", "island.tif") |
239 |
layer = RasterLayer("My RasterLayer", imgfile) |
layer = RasterLayer("My RasterLayer", imgfile) |
240 |
map.AddLayer(layer) |
map.AddLayer(layer) |
241 |
|
|
242 |
filename = self.temp_file_name("save_singlemap.thuban") |
filename = self.temp_file_name("save_singlemap.thuban") |
243 |
save_session(session, filename) |
save_session(session, filename) |
244 |
session.Destroy() |
session.Destroy() |
245 |
|
|
246 |
file = open(filename) |
file = open(filename) |
247 |
written_contents = file.read() |
written_contents = file.read() |
248 |
file.close() |
file.close() |
262 |
#print expected_contents |
#print expected_contents |
263 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
264 |
|
|
265 |
|
self.validate_data(written_contents) |
266 |
|
|
267 |
|
def testClassifiedLayer(self): |
268 |
|
"""Save a session with a single map with a single layer |
269 |
|
with a classificaton. |
270 |
|
""" |
271 |
|
# deliberately put an apersand in the title :) |
272 |
|
session = Session("single map&layer") |
273 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
274 |
|
map = Map("Test Map", projection = proj) |
275 |
|
session.AddMap(map) |
276 |
|
# use shapefile from the example data |
277 |
|
shpfile = os.path.join(os.path.dirname(__file__), |
278 |
|
os.pardir, "Data", "iceland", "political.shp") |
279 |
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
280 |
|
map.AddLayer(layer) |
281 |
|
|
282 |
|
clazz = layer.GetClassification() |
283 |
|
|
284 |
|
clazz.SetField("AREA") |
285 |
|
|
286 |
|
clazz.AppendGroup(ClassGroupSingleton(42, |
287 |
|
ClassGroupProperties(), |
288 |
|
"single")) |
289 |
|
clazz.AppendGroup(ClassGroupSingleton("text", |
290 |
|
ClassGroupProperties(), |
291 |
|
"single-text")) |
292 |
|
|
293 |
|
clazz.AppendGroup(ClassGroupRange(0, 42, |
294 |
|
ClassGroupProperties(), |
295 |
|
"range")) |
296 |
|
|
297 |
|
range = ClassGroupRange(Range("[0;42]")) |
298 |
|
range.SetProperties(ClassGroupProperties()) |
299 |
|
range.SetLabel("new-range") |
300 |
|
clazz.AppendGroup(range) |
301 |
|
|
302 |
|
filename = self.temp_file_name("save_singlemap.thuban") |
303 |
|
save_session(session, filename) |
304 |
|
|
305 |
|
file = open(filename) |
306 |
|
written_contents = file.read() |
307 |
|
file.close() |
308 |
|
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
309 |
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
310 |
|
<session title="single map&layer"> |
311 |
|
<map title="Test Map"> |
312 |
|
<projection name="Unknown"> |
313 |
|
<parameter value="zone=26"/> |
314 |
|
<parameter value="proj=utm"/> |
315 |
|
<parameter value="ellps=clrk66"/> |
316 |
|
</projection> |
317 |
|
<layer title="My Layer" filename="%s" |
318 |
|
fill="None" stroke="#000000" stroke_width="1" visible="%s"> |
319 |
|
<classification field="AREA" field_type="double"> |
320 |
|
<clnull label=""> |
321 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
322 |
|
</clnull> |
323 |
|
<clpoint value="42" label="single"> |
324 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
325 |
|
</clpoint> |
326 |
|
<clpoint value="text" label="single-text"> |
327 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
328 |
|
</clpoint> |
329 |
|
<clrange range="[0;42[" label="range"> |
330 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
331 |
|
</clrange> |
332 |
|
<clrange range="[0;42]" label="new-range"> |
333 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
334 |
|
</clrange> |
335 |
|
</classification> |
336 |
|
</layer> |
337 |
|
</map> |
338 |
|
</session>''' |
339 |
|
|
340 |
|
expected_contents = expected_template % \ |
341 |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
342 |
|
"true") |
343 |
|
|
344 |
|
#print written_contents |
345 |
|
#print "********************************************" |
346 |
|
#print expected_contents |
347 |
|
self.compare_xml(written_contents, expected_contents) |
348 |
|
|
349 |
|
self.validate_data(written_contents) |
350 |
|
|
351 |
|
session.Destroy() |
352 |
|
|
353 |
|
|
354 |
if __name__ == "__main__": |
if __name__ == "__main__": |
355 |
# Fake the __file__ global because it's needed by a test |
# Fake the __file__ global because it's needed by a test |