24 |
import support |
import support |
25 |
support.initthuban() |
support.initthuban() |
26 |
|
|
27 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import XMLWriter, save_session |
28 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
29 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
30 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer, RasterLayer |
31 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
32 |
|
|
33 |
|
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
34 |
|
ClassGroupProperties |
35 |
|
|
36 |
|
from Thuban.Model.range import Range |
37 |
|
|
38 |
|
|
39 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
class SaxEventLister(xml.sax.handler.ContentHandler): |
40 |
|
|
75 |
|
|
76 |
return handler.eventlist |
return handler.eventlist |
77 |
|
|
78 |
|
class XMLWriterTest(unittest.TestCase): |
79 |
|
|
80 |
|
def testEncode(self): |
81 |
|
"""Test XMLWriter.encode""" |
82 |
|
writer = XMLWriter() |
83 |
|
|
84 |
|
writer.encode("hello world") |
85 |
|
writer.encode(unicode("hello world")) |
86 |
|
self.assertEquals(writer.encode('&"\'<>'), |
87 |
|
"&"'<>") |
88 |
|
self.assertEquals(writer.encode(unicode('&"\'<>')), |
89 |
|
"&"'<>") |
90 |
|
|
91 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
92 |
|
|
93 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
123 |
|
|
124 |
filename = self.temp_file_name("save_singlemap.thuban") |
filename = self.temp_file_name("save_singlemap.thuban") |
125 |
save_session(session, filename) |
save_session(session, filename) |
|
session.Destroy() |
|
126 |
|
|
127 |
file = open(filename) |
file = open(filename) |
128 |
written_contents = file.read() |
written_contents = file.read() |
129 |
file.close() |
file.close() |
130 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
131 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
132 |
<session title="single map&layer"> |
<session title="single map&layer"> |
133 |
<map title="Test Map"> |
<map title="Test Map"> |
137 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
138 |
</projection> |
</projection> |
139 |
<layer title="My Layer" filename="%s" |
<layer title="My Layer" filename="%s" |
140 |
fill="None" stroke="#000000" stroke_width="1"/> |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
141 |
</map> |
</map> |
142 |
</session>''' % os.path.join("..", "..", "Data", "iceland", |
</session>''' |
143 |
"political.shp") |
|
144 |
|
expected_contents = expected_template % \ |
145 |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
146 |
|
"true") |
147 |
|
|
148 |
|
#print written_contents |
149 |
|
#print "********************************************" |
150 |
|
#print expected_contents |
151 |
|
self.compare_xml(written_contents, expected_contents) |
152 |
|
|
153 |
|
layer.SetVisible(False) |
154 |
|
save_session(session, filename) |
155 |
|
|
156 |
|
file = open(filename) |
157 |
|
written_contents = file.read() |
158 |
|
file.close() |
159 |
|
expected_contents = expected_template % \ |
160 |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
161 |
|
"false") |
162 |
|
|
163 |
#print written_contents |
#print written_contents |
164 |
#print "********************************************" |
#print "********************************************" |
165 |
#print expected_contents |
#print expected_contents |
166 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
167 |
|
|
168 |
|
session.Destroy() |
169 |
|
|
170 |
def testLayerProjection(self): |
def testLayerProjection(self): |
171 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
172 |
session = Session("single map&layer") |
session = Session("single map&layer") |
198 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
199 |
</projection> |
</projection> |
200 |
<layer title="My Layer" filename="%s" |
<layer title="My Layer" filename="%s" |
201 |
fill="None" stroke="#000000" stroke_width="1"> |
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
202 |
<projection name="Layer Projection"> |
<projection name="Layer Projection"> |
203 |
<parameter value="proj=lcc"/> |
<parameter value="proj=lcc"/> |
204 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
213 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
214 |
|
|
215 |
|
|
216 |
|
def testRasterLayer(self): |
217 |
|
# deliberately put an apersand in the title :) |
218 |
|
session = Session("single map&layer") |
219 |
|
map = Map("Test Map") |
220 |
|
session.AddMap(map) |
221 |
|
# use shapefile from the example data |
222 |
|
imgfile = os.path.join(os.path.dirname(__file__), |
223 |
|
os.pardir, "Data", "iceland", "island.tif") |
224 |
|
layer = RasterLayer("My RasterLayer", imgfile) |
225 |
|
map.AddLayer(layer) |
226 |
|
|
227 |
|
filename = self.temp_file_name("save_singlemap.thuban") |
228 |
|
save_session(session, filename) |
229 |
|
session.Destroy() |
230 |
|
|
231 |
|
file = open(filename) |
232 |
|
written_contents = file.read() |
233 |
|
file.close() |
234 |
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
235 |
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
236 |
|
<session title="single map&layer"> |
237 |
|
<map title="Test Map"> |
238 |
|
<rasterlayer title="My RasterLayer" filename="%s" |
239 |
|
visible="true"> |
240 |
|
</rasterlayer> |
241 |
|
</map> |
242 |
|
</session>''' % os.path.join(os.path.dirname(__file__), |
243 |
|
os.pardir, "Data", "iceland", |
244 |
|
"island.tif") |
245 |
|
#print written_contents |
246 |
|
#print "********************************************" |
247 |
|
#print expected_contents |
248 |
|
self.compare_xml(written_contents, expected_contents) |
249 |
|
|
250 |
|
def testClassifiedLayer(self): |
251 |
|
"""Save a session with a single map with a single layer |
252 |
|
with a classificaton. |
253 |
|
""" |
254 |
|
# deliberately put an apersand in the title :) |
255 |
|
session = Session("single map&layer") |
256 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
257 |
|
map = Map("Test Map", projection = proj) |
258 |
|
session.AddMap(map) |
259 |
|
# use shapefile from the example data |
260 |
|
shpfile = os.path.join(os.path.dirname(__file__), |
261 |
|
os.pardir, "Data", "iceland", "political.shp") |
262 |
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
263 |
|
map.AddLayer(layer) |
264 |
|
|
265 |
|
clazz = layer.GetClassification() |
266 |
|
|
267 |
|
clazz.SetField("AREA") |
268 |
|
|
269 |
|
clazz.AppendGroup(ClassGroupSingleton(42, |
270 |
|
ClassGroupProperties(), |
271 |
|
"single")) |
272 |
|
clazz.AppendGroup(ClassGroupSingleton("text", |
273 |
|
ClassGroupProperties(), |
274 |
|
"single-text")) |
275 |
|
|
276 |
|
clazz.AppendGroup(ClassGroupRange(0, 42, |
277 |
|
ClassGroupProperties(), |
278 |
|
"range")) |
279 |
|
|
280 |
|
range = ClassGroupRange(Range("[0;42]")) |
281 |
|
range.SetProperties(ClassGroupProperties()) |
282 |
|
range.SetLabel("new-range") |
283 |
|
clazz.AppendGroup(range) |
284 |
|
|
285 |
|
filename = self.temp_file_name("save_singlemap.thuban") |
286 |
|
save_session(session, filename) |
287 |
|
|
288 |
|
file = open(filename) |
289 |
|
written_contents = file.read() |
290 |
|
file.close() |
291 |
|
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
292 |
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
293 |
|
<session title="single map&layer"> |
294 |
|
<map title="Test Map"> |
295 |
|
<projection name="Unknown"> |
296 |
|
<parameter value="zone=26"/> |
297 |
|
<parameter value="proj=utm"/> |
298 |
|
<parameter value="ellps=clrk66"/> |
299 |
|
</projection> |
300 |
|
<layer title="My Layer" filename="%s" |
301 |
|
fill="None" stroke="#000000" stroke_width="1" visible="%s"> |
302 |
|
<classification field="AREA" field_type="double"> |
303 |
|
<clnull label=""> |
304 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
305 |
|
</clnull> |
306 |
|
<clpoint value="42" label="single"> |
307 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
308 |
|
</clpoint> |
309 |
|
<clpoint value="text" label="single-text"> |
310 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
311 |
|
</clpoint> |
312 |
|
<clrange range="[0;42[" label="range"> |
313 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
314 |
|
</clrange> |
315 |
|
<clrange range="[0;42]" label="new-range"> |
316 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
317 |
|
</clrange> |
318 |
|
</classification> |
319 |
|
</layer> |
320 |
|
</map> |
321 |
|
</session>''' |
322 |
|
|
323 |
|
expected_contents = expected_template % \ |
324 |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
325 |
|
"true") |
326 |
|
|
327 |
|
#print written_contents |
328 |
|
#print "********************************************" |
329 |
|
#print expected_contents |
330 |
|
self.compare_xml(written_contents, expected_contents) |
331 |
|
|
332 |
|
session.Destroy() |
333 |
|
|
334 |
|
|
335 |
if __name__ == "__main__": |
if __name__ == "__main__": |