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 |
bh |
1245 |
import xmlsupport |
21 |
bh |
1638 |
import postgissupport |
22 |
bh |
1245 |
|
23 |
bh |
292 |
import support |
24 |
|
|
support.initthuban() |
25 |
|
|
|
26 |
bh |
1268 |
import dbflib |
27 |
|
|
|
28 |
|
|
from Thuban.Lib.fileutil import relative_filename |
29 |
|
|
from Thuban.Model.save import XMLWriter, save_session, sort_data_stores |
30 |
bh |
292 |
from Thuban.Model.session import Session |
31 |
|
|
from Thuban.Model.map import Map |
32 |
jonathan |
947 |
from Thuban.Model.layer import Layer, RasterLayer |
33 |
bh |
292 |
from Thuban.Model.proj import Projection |
34 |
bh |
1268 |
from Thuban.Model.table import DBFTable |
35 |
|
|
from Thuban.Model.transientdb import TransientJoinedTable |
36 |
|
|
from Thuban.Model.data import DerivedShapeStore |
37 |
bh |
292 |
|
38 |
jonathan |
1168 |
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
39 |
|
|
ClassGroupProperties |
40 |
bh |
292 |
|
41 |
jonathan |
1168 |
from Thuban.Model.range import Range |
42 |
|
|
|
43 |
bh |
1638 |
from Thuban.Model.postgisdb import PostGISConnection, PostGISShapeStore |
44 |
jonathan |
1168 |
|
45 |
bh |
1638 |
|
46 |
jonathan |
1168 |
class XMLWriterTest(unittest.TestCase): |
47 |
|
|
|
48 |
|
|
def testEncode(self): |
49 |
|
|
"""Test XMLWriter.encode""" |
50 |
|
|
writer = XMLWriter() |
51 |
jonathan |
1200 |
eq = self.assertEquals |
52 |
jonathan |
1168 |
|
53 |
jonathan |
1200 |
eq(writer.encode("hello world"), "hello world") |
54 |
|
|
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
55 |
jonathan |
1173 |
|
56 |
jonathan |
1200 |
eq(writer.encode("\x80\x90\xc2\x100"), |
57 |
|
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
58 |
|
|
eq(writer.encode(u"\x80\x90\xc2\x100"), |
59 |
|
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
60 |
|
|
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
61 |
jonathan |
1173 |
|
62 |
jonathan |
1200 |
eq(writer.encode('&"\'<>'), "&"'<>") |
63 |
|
|
eq(writer.encode(unicode('&"\'<>')), "&"'<>") |
64 |
jonathan |
1168 |
|
65 |
bh |
1245 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
66 |
|
|
xmlsupport.ValidationTest): |
67 |
bh |
292 |
|
68 |
bh |
2036 |
dtd = "http://thuban.intevation.org/dtds/thuban-1.0.0.dtd" |
69 |
bh |
1268 |
thubanids = [((dtd, n), (None, "id")) for n in |
70 |
|
|
["fileshapesource", "filetable", "jointable", |
71 |
bh |
1638 |
"derivedshapesource", "dbshapesource", "dbconnection"]] |
72 |
bh |
1268 |
thubanidrefs = [((dtd, n), (None, m)) for n, m in |
73 |
|
|
[("layer", "shapestore"), |
74 |
|
|
("jointable", "left"), |
75 |
|
|
("jointable", "right"), |
76 |
|
|
("derivedshapesource", "table"), |
77 |
bh |
1638 |
("derivedshapesource", "shapesource"), |
78 |
|
|
("dbshapesource", "dbconn")]] |
79 |
bh |
1268 |
del n, m, dtd |
80 |
|
|
|
81 |
bh |
1677 |
def tearDown(self): |
82 |
|
|
"""Call self.session.Destroy |
83 |
|
|
|
84 |
|
|
Test cases that create session should bind it to self.session so |
85 |
|
|
that it gets destroyed properly |
86 |
|
|
""" |
87 |
|
|
if hasattr(self, "session"): |
88 |
|
|
self.session.Destroy() |
89 |
|
|
self.session = None |
90 |
|
|
|
91 |
bh |
292 |
def compare_xml(self, xml1, xml2): |
92 |
bh |
1375 |
list1 = xmlsupport.sax_eventlist(xml1, ids = self.thubanids, |
93 |
|
|
idrefs = self.thubanidrefs) |
94 |
|
|
list2 = xmlsupport.sax_eventlist(xml2, ids = self.thubanids, |
95 |
|
|
idrefs = self.thubanidrefs) |
96 |
|
|
if list1 != list2: |
97 |
|
|
for a, b in zip(list1, list2): |
98 |
|
|
if a != b: |
99 |
|
|
self.fail("%r != %r" % (a, b)) |
100 |
bh |
292 |
|
101 |
bh |
1375 |
|
102 |
bh |
292 |
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 |
bh |
1845 |
'<!DOCTYPE session SYSTEM "thuban-1.0.dtd">\n' |
115 |
bh |
1268 |
'<session title="empty session" ' |
116 |
bh |
2036 |
'xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd">' |
117 |
bh |
1268 |
'\n</session>\n') |
118 |
bh |
292 |
|
119 |
bh |
1245 |
self.validate_data(written_contents) |
120 |
|
|
|
121 |
bh |
292 |
def testSingleLayer(self): |
122 |
|
|
"""Save a session with a single map with a single layer""" |
123 |
|
|
# deliberately put an apersand in the title :) |
124 |
|
|
session = Session("single map&layer") |
125 |
bh |
1845 |
proj = Projection(["proj=utm", "zone=27", "ellps=WGS84", |
126 |
|
|
"datum=WGS84", "units=m"], |
127 |
|
|
name = "WGS 84 / UTM zone 27N", |
128 |
|
|
epsg = "32627") |
129 |
bh |
292 |
map = Map("Test Map", projection = proj) |
130 |
|
|
session.AddMap(map) |
131 |
|
|
# use shapefile from the example data |
132 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
133 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
134 |
bh |
723 |
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
135 |
bh |
292 |
map.AddLayer(layer) |
136 |
|
|
|
137 |
|
|
filename = self.temp_file_name("save_singlemap.thuban") |
138 |
|
|
save_session(session, filename) |
139 |
|
|
|
140 |
|
|
file = open(filename) |
141 |
|
|
written_contents = file.read() |
142 |
|
|
file.close() |
143 |
jonathan |
775 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
144 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
145 |
bh |
1268 |
<session title="single map&layer" |
146 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
147 |
bh |
1989 |
<fileshapesource id="D1" |
148 |
|
|
filename="../../Data/iceland/political.shp" |
149 |
|
|
filetype="shapefile"/> |
150 |
bh |
292 |
<map title="Test Map"> |
151 |
bh |
1845 |
<projection epsg="32627" name="WGS 84 / UTM zone 27N"> |
152 |
bh |
292 |
<parameter value="proj=utm"/> |
153 |
bh |
1845 |
<parameter value="zone=27"/> |
154 |
|
|
<parameter value="ellps=WGS84"/> |
155 |
|
|
<parameter value="datum=WGS84"/> |
156 |
|
|
<parameter value="units=m"/> |
157 |
bh |
292 |
</projection> |
158 |
bh |
1268 |
<layer title="My Layer" shapestore="D1" |
159 |
jonathan |
775 |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
160 |
bh |
292 |
</map> |
161 |
bh |
1268 |
</session>''' |
162 |
|
|
|
163 |
bh |
1989 |
expected_contents = expected_template % "true" |
164 |
jonathan |
775 |
|
165 |
bh |
292 |
self.compare_xml(written_contents, expected_contents) |
166 |
|
|
|
167 |
bh |
1245 |
self.validate_data(written_contents) |
168 |
|
|
|
169 |
bh |
1989 |
# Repeat with an invisible layer |
170 |
jonathan |
775 |
layer.SetVisible(False) |
171 |
|
|
save_session(session, filename) |
172 |
|
|
|
173 |
|
|
file = open(filename) |
174 |
|
|
written_contents = file.read() |
175 |
|
|
file.close() |
176 |
bh |
1989 |
expected_contents = expected_template % "false" |
177 |
jonathan |
775 |
self.compare_xml(written_contents, expected_contents) |
178 |
bh |
1245 |
self.validate_data(written_contents) |
179 |
jonathan |
775 |
|
180 |
|
|
session.Destroy() |
181 |
|
|
|
182 |
jonathan |
755 |
def testLayerProjection(self): |
183 |
bh |
1268 |
"""Test saving layers with projections""" |
184 |
jonathan |
755 |
# deliberately put an apersand in the title :) |
185 |
bh |
1678 |
session = self.session = Session("single map&layer") |
186 |
jonathan |
755 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
187 |
|
|
map = Map("Test Map", projection = proj) |
188 |
|
|
session.AddMap(map) |
189 |
|
|
# use shapefile from the example data |
190 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
191 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
192 |
|
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
193 |
bh |
1687 |
proj = Projection(["proj=lcc", "ellps=clrk66", |
194 |
|
|
"lat_1=0", "lat_2=20"], |
195 |
|
|
"Layer Projection") |
196 |
jonathan |
755 |
layer.SetProjection(proj) |
197 |
|
|
map.AddLayer(layer) |
198 |
bh |
292 |
|
199 |
bh |
1268 |
filename = self.temp_file_name("save_layerproj.thuban") |
200 |
jonathan |
755 |
save_session(session, filename) |
201 |
bh |
292 |
|
202 |
jonathan |
755 |
file = open(filename) |
203 |
|
|
written_contents = file.read() |
204 |
|
|
file.close() |
205 |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
206 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
207 |
bh |
1268 |
<session title="single map&layer" |
208 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
209 |
bh |
1989 |
<fileshapesource id="D1" |
210 |
|
|
filename="../../Data/iceland/political.shp" |
211 |
|
|
filetype="shapefile"/> |
212 |
jonathan |
755 |
<map title="Test Map"> |
213 |
|
|
<projection name="Unknown"> |
214 |
|
|
<parameter value="zone=26"/> |
215 |
|
|
<parameter value="proj=utm"/> |
216 |
|
|
<parameter value="ellps=clrk66"/> |
217 |
|
|
</projection> |
218 |
bh |
1268 |
<layer title="My Layer" shapestore="D1" |
219 |
jonathan |
775 |
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
220 |
jonathan |
755 |
<projection name="Layer Projection"> |
221 |
|
|
<parameter value="proj=lcc"/> |
222 |
|
|
<parameter value="ellps=clrk66"/> |
223 |
bh |
1687 |
<parameter value="lat_1=0"/> |
224 |
|
|
<parameter value="lat_2=20"/> |
225 |
jonathan |
755 |
</projection> |
226 |
|
|
</layer> |
227 |
|
|
</map> |
228 |
bh |
1989 |
</session>''' |
229 |
jonathan |
755 |
#print written_contents |
230 |
|
|
#print "********************************************" |
231 |
|
|
#print expected_contents |
232 |
|
|
self.compare_xml(written_contents, expected_contents) |
233 |
|
|
|
234 |
bh |
1245 |
self.validate_data(written_contents) |
235 |
|
|
|
236 |
jonathan |
947 |
def testRasterLayer(self): |
237 |
|
|
# deliberately put an apersand in the title :) |
238 |
|
|
session = Session("single map&layer") |
239 |
|
|
map = Map("Test Map") |
240 |
|
|
session.AddMap(map) |
241 |
|
|
# use shapefile from the example data |
242 |
|
|
imgfile = os.path.join(os.path.dirname(__file__), |
243 |
|
|
os.pardir, "Data", "iceland", "island.tif") |
244 |
|
|
layer = RasterLayer("My RasterLayer", imgfile) |
245 |
|
|
map.AddLayer(layer) |
246 |
bh |
1245 |
|
247 |
bh |
1599 |
filename = self.temp_file_name("%s.thuban" % self.id()) |
248 |
jonathan |
947 |
save_session(session, filename) |
249 |
|
|
session.Destroy() |
250 |
bh |
1245 |
|
251 |
jonathan |
947 |
file = open(filename) |
252 |
|
|
written_contents = file.read() |
253 |
|
|
file.close() |
254 |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
255 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
256 |
bh |
1268 |
<session title="single map&layer" |
257 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
258 |
jonathan |
947 |
<map title="Test Map"> |
259 |
bh |
1989 |
<rasterlayer title="My RasterLayer" |
260 |
|
|
filename="../../Data/iceland/island.tif" |
261 |
|
|
visible="true"> |
262 |
jonathan |
947 |
</rasterlayer> |
263 |
|
|
</map> |
264 |
bh |
1989 |
</session>''' |
265 |
jonathan |
947 |
#print written_contents |
266 |
|
|
#print "********************************************" |
267 |
|
|
#print expected_contents |
268 |
|
|
self.compare_xml(written_contents, expected_contents) |
269 |
jonathan |
755 |
|
270 |
bh |
1245 |
self.validate_data(written_contents) |
271 |
|
|
|
272 |
jonathan |
1168 |
def testClassifiedLayer(self): |
273 |
bh |
1417 |
"""Save a session with a single map with classifications""" |
274 |
jonathan |
1168 |
# deliberately put an apersand in the title :) |
275 |
bh |
1417 |
session = Session("Map with Classifications") |
276 |
jonathan |
1168 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
277 |
|
|
map = Map("Test Map", projection = proj) |
278 |
|
|
session.AddMap(map) |
279 |
|
|
# use shapefile from the example data |
280 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
281 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
282 |
|
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
283 |
|
|
map.AddLayer(layer) |
284 |
bh |
1417 |
layer2 = Layer("My Layer", layer.ShapeStore()) |
285 |
|
|
map.AddLayer(layer2) |
286 |
jonathan |
755 |
|
287 |
jonathan |
1168 |
clazz = layer.GetClassification() |
288 |
|
|
|
289 |
bh |
1452 |
layer.SetClassificationColumn("AREA") |
290 |
jonathan |
1168 |
|
291 |
bh |
1417 |
clazz.AppendGroup(ClassGroupSingleton(42, ClassGroupProperties(), |
292 |
|
|
"single")) |
293 |
|
|
clazz.AppendGroup(ClassGroupSingleton("text", ClassGroupProperties(), |
294 |
|
|
"single-text")) |
295 |
jonathan |
1168 |
|
296 |
jonathan |
1357 |
clazz.AppendGroup(ClassGroupRange((0, 42), |
297 |
jonathan |
1168 |
ClassGroupProperties(), |
298 |
|
|
"range")) |
299 |
|
|
|
300 |
|
|
range = ClassGroupRange(Range("[0;42]")) |
301 |
|
|
range.SetProperties(ClassGroupProperties()) |
302 |
|
|
range.SetLabel("new-range") |
303 |
|
|
clazz.AppendGroup(range) |
304 |
|
|
|
305 |
bh |
1417 |
|
306 |
|
|
clazz = layer2.GetClassification() |
307 |
bh |
1452 |
layer2.SetClassificationColumn("POPYCOUN") |
308 |
bh |
1417 |
|
309 |
|
|
# Classification with Latin 1 text |
310 |
|
|
clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue |
311 |
|
|
ClassGroupProperties(), |
312 |
|
|
'\xdcml\xe4uts')) # Uemlaeuts |
313 |
|
|
|
314 |
|
|
|
315 |
|
|
filename = self.temp_file_name("%s.thuban" % self.id()) |
316 |
jonathan |
1168 |
save_session(session, filename) |
317 |
|
|
|
318 |
|
|
file = open(filename) |
319 |
|
|
written_contents = file.read() |
320 |
|
|
file.close() |
321 |
bh |
1989 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
322 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
323 |
bh |
1417 |
<session title="Map with Classifications" |
324 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
325 |
bh |
1989 |
<fileshapesource id="D1" |
326 |
|
|
filename="../../Data/iceland/political.shp" |
327 |
|
|
filetype="shapefile"/> |
328 |
jonathan |
1168 |
<map title="Test Map"> |
329 |
|
|
<projection name="Unknown"> |
330 |
|
|
<parameter value="zone=26"/> |
331 |
|
|
<parameter value="proj=utm"/> |
332 |
|
|
<parameter value="ellps=clrk66"/> |
333 |
|
|
</projection> |
334 |
bh |
1268 |
<layer title="My Layer" shapestore="D1" |
335 |
bh |
1989 |
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
336 |
jonathan |
1168 |
<classification field="AREA" field_type="double"> |
337 |
|
|
<clnull label=""> |
338 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
339 |
|
|
</clnull> |
340 |
|
|
<clpoint value="42" label="single"> |
341 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
342 |
|
|
</clpoint> |
343 |
|
|
<clpoint value="text" label="single-text"> |
344 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
345 |
|
|
</clpoint> |
346 |
|
|
<clrange range="[0;42[" label="range"> |
347 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
348 |
|
|
</clrange> |
349 |
|
|
<clrange range="[0;42]" label="new-range"> |
350 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
351 |
|
|
</clrange> |
352 |
|
|
</classification> |
353 |
|
|
</layer> |
354 |
bh |
1417 |
<layer title="My Layer" shapestore="D1" |
355 |
|
|
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
356 |
|
|
<classification field="POPYCOUN" field_type="string"> |
357 |
|
|
<clnull label=""> |
358 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
359 |
|
|
</clnull> |
360 |
|
|
<clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc" |
361 |
|
|
label="\xc3\x9cml\xc3\xa4uts"> |
362 |
|
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
363 |
|
|
</clpoint> |
364 |
|
|
</classification> |
365 |
|
|
</layer> |
366 |
jonathan |
1168 |
</map> |
367 |
bh |
1245 |
</session>''' |
368 |
|
|
|
369 |
jonathan |
1168 |
#print written_contents |
370 |
|
|
#print "********************************************" |
371 |
|
|
#print expected_contents |
372 |
|
|
self.compare_xml(written_contents, expected_contents) |
373 |
|
|
|
374 |
bh |
1245 |
self.validate_data(written_contents) |
375 |
|
|
|
376 |
jonathan |
1168 |
session.Destroy() |
377 |
|
|
|
378 |
bh |
1268 |
def test_dbf_table(self): |
379 |
|
|
"""Test saving a session with a dbf table link""" |
380 |
bh |
1677 |
session = self.session = Session("a DBF Table session") |
381 |
bh |
1268 |
# use shapefile from the example data |
382 |
|
|
dbffile = os.path.join(os.path.dirname(__file__), |
383 |
|
|
os.pardir, "Data", "iceland", "political.dbf") |
384 |
|
|
table = session.AddTable(DBFTable(dbffile)) |
385 |
jonathan |
1168 |
|
386 |
bh |
1268 |
filename = self.temp_file_name("save_singletable.thuban") |
387 |
|
|
save_session(session, filename) |
388 |
|
|
|
389 |
|
|
file = open(filename) |
390 |
|
|
written_contents = file.read() |
391 |
|
|
file.close() |
392 |
bh |
1989 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
393 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
394 |
bh |
1268 |
<session title="a DBF Table session" |
395 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
396 |
bh |
1989 |
<filetable id="D1" filename="../../Data/iceland/political.dbf" |
397 |
|
|
filetype="DBF" title="political"/> |
398 |
bh |
1268 |
</session>''' |
399 |
|
|
|
400 |
|
|
self.compare_xml(written_contents, expected_contents) |
401 |
bh |
1640 |
self.validate_data(written_contents) |
402 |
bh |
1268 |
|
403 |
|
|
def test_joined_table(self): |
404 |
|
|
"""Test saving a session with joined table""" |
405 |
|
|
# Create a simple table to use in the join |
406 |
|
|
dbffile = self.temp_file_name("save_joinedtable.dbf") |
407 |
|
|
dbf = dbflib.create(dbffile) |
408 |
|
|
dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0) |
409 |
|
|
dbf.add_field("TEXT", dbflib.FTString, 10, 0) |
410 |
|
|
dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"}) |
411 |
|
|
dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"}) |
412 |
|
|
dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"}) |
413 |
|
|
dbf.close() |
414 |
|
|
|
415 |
|
|
# Create the session and a map |
416 |
|
|
session = Session("A Joined Table session") |
417 |
|
|
try: |
418 |
|
|
map = Map("Test Map") |
419 |
|
|
session.AddMap(map) |
420 |
|
|
|
421 |
|
|
# Add the dbf file to the session |
422 |
|
|
dbftable = session.AddTable(DBFTable(dbffile)) |
423 |
|
|
|
424 |
|
|
# Create a layer with the shapefile to use in the join |
425 |
|
|
shpfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), |
426 |
|
|
os.pardir, "Data", "iceland", |
427 |
|
|
"roads-line.shp") |
428 |
|
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
429 |
|
|
map.AddLayer(layer) |
430 |
|
|
|
431 |
|
|
# Do the join |
432 |
|
|
store = layer.ShapeStore() |
433 |
|
|
#for col in store.Table().Columns(): |
434 |
|
|
# print col.name |
435 |
|
|
joined = TransientJoinedTable(session.TransientDB(), |
436 |
|
|
store.Table(), "RDLNTYPE", |
437 |
bh |
1375 |
dbftable, "RDTYPE", |
438 |
|
|
outer_join = True) |
439 |
bh |
1268 |
store = session.AddShapeStore(DerivedShapeStore(store, joined)) |
440 |
|
|
layer.SetShapeStore(store) |
441 |
|
|
|
442 |
|
|
# Save the session |
443 |
|
|
filename = self.temp_file_name("save_joinedtable.thuban") |
444 |
|
|
save_session(session, filename) |
445 |
|
|
|
446 |
|
|
# Read it back and compare |
447 |
|
|
file = open(filename) |
448 |
|
|
written_contents = file.read() |
449 |
|
|
file.close() |
450 |
bh |
1989 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
451 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
452 |
bh |
1268 |
<session title="A Joined Table session" |
453 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
454 |
bh |
1989 |
<fileshapesource filename="../../Data/iceland/roads-line.shp" |
455 |
bh |
1268 |
filetype="shapefile" id="D142197204"/> |
456 |
bh |
1989 |
<filetable filename="save_joinedtable.dbf" |
457 |
bh |
1268 |
title="save_joinedtable" |
458 |
|
|
filetype="DBF" id="D141881756"/> |
459 |
|
|
<jointable id="D142180284" |
460 |
|
|
title="Join of roads-line and save_joinedtable" |
461 |
|
|
leftcolumn="RDLNTYPE" left="D142197204" |
462 |
bh |
1375 |
rightcolumn="RDTYPE" right="D141881756" |
463 |
|
|
jointype="LEFT OUTER" /> |
464 |
bh |
1268 |
<derivedshapesource id="D141915644" |
465 |
|
|
table="D142180284" |
466 |
|
|
shapesource="D142197204"/> |
467 |
|
|
<map title="Test Map"> |
468 |
|
|
<layer title="My Layer" |
469 |
|
|
shapestore="D141915644" visible="true" |
470 |
|
|
stroke="#000000" stroke_width="1" fill="None"/> |
471 |
|
|
</map> |
472 |
|
|
</session>''' |
473 |
|
|
|
474 |
|
|
self.compare_xml(written_contents, expected_contents) |
475 |
bh |
1640 |
self.validate_data(written_contents) |
476 |
bh |
1268 |
finally: |
477 |
|
|
session.Destroy() |
478 |
|
|
session = None |
479 |
|
|
|
480 |
|
|
|
481 |
bh |
1638 |
def test_save_postgis(self): |
482 |
|
|
"""Test saving a session with a postgis connection""" |
483 |
|
|
|
484 |
|
|
class NonConnection(PostGISConnection): |
485 |
|
|
"""connection class that doesn't actually connect """ |
486 |
|
|
def connect(self): |
487 |
|
|
pass |
488 |
|
|
|
489 |
|
|
class NonConnectionStore(PostGISShapeStore): |
490 |
|
|
"""Shapestore that doesn't try to access the server""" |
491 |
|
|
def _fetch_table_information(self): |
492 |
|
|
pass |
493 |
|
|
|
494 |
|
|
session = Session("A PostGIS Session") |
495 |
|
|
try: |
496 |
|
|
dbconn = NonConnection(dbname="plugh", host="xyzzy", port="42", |
497 |
|
|
user="grue") |
498 |
|
|
session.AddDBConnection(dbconn) |
499 |
|
|
map = Map("Test Map") |
500 |
|
|
session.AddMap(map) |
501 |
|
|
store = NonConnectionStore(dbconn, "roads") |
502 |
|
|
session.AddShapeStore(store) |
503 |
|
|
layer = Layer("Roads to Nowhere", store) |
504 |
|
|
map.AddLayer(layer) |
505 |
|
|
|
506 |
|
|
# Save the session |
507 |
|
|
filename = self.temp_file_name(self.id() + ".thuban") |
508 |
|
|
save_session(session, filename) |
509 |
|
|
|
510 |
|
|
# Read it back and compare |
511 |
|
|
file = open(filename) |
512 |
|
|
written = file.read() |
513 |
|
|
file.close() |
514 |
|
|
expected = '''<?xml version="1.0" encoding="UTF-8"?> |
515 |
bh |
1845 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
516 |
bh |
1638 |
<session title="A PostGIS Session" |
517 |
bh |
2036 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"> |
518 |
bh |
1638 |
<dbconnection id="DB" |
519 |
|
|
dbtype="postgis" dbname="plugh" |
520 |
|
|
host="xyzzy" port="42" |
521 |
|
|
user="grue"/> |
522 |
|
|
<dbshapesource id="roads" dbconn="DB" tablename="roads"/> |
523 |
|
|
<map title="Test Map"> |
524 |
|
|
<layer title="Roads to Nowhere" |
525 |
|
|
shapestore="roads" visible="true" |
526 |
|
|
stroke="#000000" stroke_width="1" fill="None"/> |
527 |
|
|
</map> |
528 |
|
|
</session>''' |
529 |
|
|
self.compare_xml(written, expected) |
530 |
|
|
self.validate_data(written) |
531 |
|
|
finally: |
532 |
|
|
session.Destroy() |
533 |
|
|
|
534 |
|
|
|
535 |
bh |
1268 |
class MockDataStore: |
536 |
|
|
|
537 |
|
|
"""A very simple data store that only has dependencies""" |
538 |
|
|
|
539 |
|
|
def __init__(self, name, *dependencies): |
540 |
|
|
self.name = name |
541 |
|
|
self.dependencies = dependencies |
542 |
|
|
|
543 |
|
|
def __repr__(self): |
544 |
|
|
return self.name |
545 |
|
|
|
546 |
|
|
def Dependencies(self): |
547 |
|
|
return self.dependencies |
548 |
|
|
|
549 |
|
|
|
550 |
|
|
class TestStoreSort(unittest.TestCase): |
551 |
|
|
|
552 |
|
|
def check_sort(self, containers, sorted): |
553 |
|
|
"""Check whether the list of data containers is sorted""" |
554 |
|
|
# check whether sorted is in the right order |
555 |
|
|
seen = {} |
556 |
|
|
for container in sorted: |
557 |
|
|
self.failIf(id(container) in seen, |
558 |
|
|
"Container %r at least twice in %r" % (container, |
559 |
|
|
sorted)) |
560 |
|
|
for dep in container.Dependencies(): |
561 |
|
|
self.assert_(id(dep) in seen, |
562 |
|
|
"Dependency %r of %r not yet seen" % (dep, |
563 |
|
|
container)) |
564 |
|
|
seen[id(container)] = 1 |
565 |
|
|
# check whether all of containers is in sorted |
566 |
|
|
for container in containers: |
567 |
|
|
self.assert_(id(container) in seen, |
568 |
|
|
"Container %r in containers but not in sorted") |
569 |
|
|
self.assertEquals(len(containers), len(sorted)) |
570 |
|
|
|
571 |
|
|
def test_sort_data_stores(self): |
572 |
|
|
"""Test Thuban.Model.save.sort_data_stores""" |
573 |
|
|
d1 = MockDataStore("d1") |
574 |
|
|
d2 = MockDataStore("d2") |
575 |
|
|
d3 = MockDataStore("d3", d1) |
576 |
|
|
d4 = MockDataStore("d4", d1, d3) |
577 |
|
|
|
578 |
|
|
containers = [d4, d1, d2, d3] |
579 |
|
|
self.check_sort(containers, sort_data_stores(containers)) |
580 |
|
|
containers = [d1, d3, d2, d4] |
581 |
|
|
self.check_sort(containers, sort_data_stores(containers)) |
582 |
|
|
|
583 |
|
|
|
584 |
|
|
|
585 |
bh |
292 |
if __name__ == "__main__": |
586 |
|
|
# Fake the __file__ global because it's needed by a test |
587 |
|
|
import sys |
588 |
|
|
__file__ = sys.argv[0] |
589 |
bh |
723 |
support.run_tests() |