1 |
# Copyright (c) 2002, 2003 by Intevation GmbH |
# Copyright (c) 2002, 2003, 2004, 2005, 2007 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
25 |
|
|
26 |
import dbflib |
import dbflib |
27 |
|
|
28 |
|
from Thuban import internal_from_unicode |
29 |
from Thuban.Lib.fileutil import relative_filename |
from Thuban.Lib.fileutil import relative_filename |
30 |
from Thuban.Model.save import XMLWriter, save_session, sort_data_stores |
from Thuban.Model.save import XMLWriter, save_session, sort_data_stores |
31 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
34 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
35 |
from Thuban.Model.table import DBFTable |
from Thuban.Model.table import DBFTable |
36 |
from Thuban.Model.transientdb import TransientJoinedTable |
from Thuban.Model.transientdb import TransientJoinedTable |
37 |
from Thuban.Model.data import DerivedShapeStore |
from Thuban.Model.data import DerivedShapeStore, SHAPETYPE_ARC |
38 |
|
|
39 |
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
40 |
ClassGroupProperties |
ClassGroupPattern, ClassGroupProperties |
41 |
|
|
42 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
43 |
|
|
54 |
eq(writer.encode("hello world"), "hello world") |
eq(writer.encode("hello world"), "hello world") |
55 |
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
56 |
|
|
57 |
eq(writer.encode("\x80\x90\xc2\x100"), |
eq(writer.encode(internal_from_unicode(u"\x80\x90\xc2\x100")), |
58 |
"\xc2\x80\xc2\x90\xc3\x82\x100") |
"\xc2\x80\xc2\x90\xc3\x82\x100") |
59 |
eq(writer.encode(u"\x80\x90\xc2\x100"), |
eq(writer.encode(u"\x80\x90\xc2\x100"), |
60 |
"\xc2\x80\xc2\x90\xc3\x82\x100") |
"\xc2\x80\xc2\x90\xc3\x82\x100") |
61 |
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
62 |
|
|
66 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
67 |
xmlsupport.ValidationTest): |
xmlsupport.ValidationTest): |
68 |
|
|
69 |
dtd = "http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd" |
dtd = "http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
70 |
thubanids = [((dtd, n), (None, "id")) for n in |
thubanids = [((dtd, n), (None, "id")) for n in |
71 |
["fileshapesource", "filetable", "jointable", |
["fileshapesource", "filetable", "jointable", |
72 |
"derivedshapesource", "dbshapesource", "dbconnection"]] |
"derivedshapesource", "dbshapesource", "dbconnection"]] |
112 |
file.close() |
file.close() |
113 |
self.compare_xml(written_contents, |
self.compare_xml(written_contents, |
114 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
115 |
'<!DOCTYPE session SYSTEM "thuban-1.0.dtd">\n' |
'<!DOCTYPE session SYSTEM "thuban-1.1.dtd">\n' |
116 |
'<session title="empty session" ' |
'<session title="empty session" ' |
117 |
'xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd">' |
'xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">' |
118 |
'\n</session>\n') |
'\n</session>\n') |
119 |
|
|
120 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
142 |
written_contents = file.read() |
written_contents = file.read() |
143 |
file.close() |
file.close() |
144 |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
145 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
146 |
<session title="single map&layer" |
<session title="single map&layer" |
147 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
148 |
<fileshapesource id="D1" |
<fileshapesource id="D1" |
149 |
filename="../../Data/iceland/political.shp" |
filename="../../Data/iceland/political.shp" |
150 |
filetype="shapefile"/> |
filetype="shapefile"/> |
156 |
<parameter value="datum=WGS84"/> |
<parameter value="datum=WGS84"/> |
157 |
<parameter value="units=m"/> |
<parameter value="units=m"/> |
158 |
</projection> |
</projection> |
159 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" visible="%s"> |
160 |
fill="None" stroke="#000000" stroke_width="1" visible="%s"/> |
<classification> |
161 |
|
<clnull label=""> |
162 |
|
<cldata fill="None" stroke="#000000" |
163 |
|
stroke_width="1"/> |
164 |
|
</clnull> |
165 |
|
</classification> |
166 |
|
</layer> |
167 |
</map> |
</map> |
168 |
</session>''' |
</session>''' |
169 |
|
|
210 |
written_contents = file.read() |
written_contents = file.read() |
211 |
file.close() |
file.close() |
212 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
213 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
214 |
<session title="single map&layer" |
<session title="single map&layer" |
215 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
216 |
<fileshapesource id="D1" |
<fileshapesource id="D1" |
217 |
filename="../../Data/iceland/political.shp" |
filename="../../Data/iceland/political.shp" |
218 |
filetype="shapefile"/> |
filetype="shapefile"/> |
222 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
223 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
224 |
</projection> |
</projection> |
225 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" visible="true"> |
|
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
|
226 |
<projection name="Layer Projection"> |
<projection name="Layer Projection"> |
227 |
<parameter value="proj=lcc"/> |
<parameter value="proj=lcc"/> |
228 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
229 |
<parameter value="lat_1=0"/> |
<parameter value="lat_1=0"/> |
230 |
<parameter value="lat_2=20"/> |
<parameter value="lat_2=20"/> |
231 |
</projection> |
</projection> |
232 |
|
<classification> |
233 |
|
<clnull label=""> |
234 |
|
<cldata fill="None" stroke="#000000" |
235 |
|
stroke_width="1"/> |
236 |
|
</clnull> |
237 |
|
</classification> |
238 |
</layer> |
</layer> |
239 |
</map> |
</map> |
240 |
</session>''' |
</session>''' |
246 |
self.validate_data(written_contents) |
self.validate_data(written_contents) |
247 |
|
|
248 |
def testRasterLayer(self): |
def testRasterLayer(self): |
|
# deliberately put an apersand in the title :) |
|
|
session = Session("single map&layer") |
|
|
map = Map("Test Map") |
|
|
session.AddMap(map) |
|
|
# use shapefile from the example data |
|
|
imgfile = os.path.join(os.path.dirname(__file__), |
|
|
os.pardir, "Data", "iceland", "island.tif") |
|
|
layer = RasterLayer("My RasterLayer", imgfile) |
|
|
map.AddLayer(layer) |
|
249 |
|
|
250 |
filename = self.temp_file_name("%s.thuban" % self.id()) |
MASK_NONE = RasterLayer.MASK_NONE |
251 |
save_session(session, filename) |
MASK_BIT = RasterLayer.MASK_BIT |
252 |
session.Destroy() |
MASK_ALPHA = RasterLayer.MASK_ALPHA |
253 |
|
|
254 |
|
for opacity, masktype, opname, maskname in \ |
255 |
|
[(1, MASK_BIT, '', ''), |
256 |
|
(.2, MASK_BIT, 'opacity="0.2"', ''), |
257 |
|
(1, MASK_ALPHA, '', 'masktype="alpha"'), |
258 |
|
(.5, MASK_ALPHA, 'opacity="0.5"', 'masktype="alpha"'), |
259 |
|
(1, MASK_NONE, '', 'masktype="none"'), |
260 |
|
(0, MASK_NONE, 'opacity="0"', 'masktype="none"') ]: |
261 |
|
|
|
file = open(filename) |
|
|
written_contents = file.read() |
|
|
file.close() |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
|
|
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
|
|
<session title="single map&layer" |
|
|
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
|
|
<map title="Test Map"> |
|
|
<rasterlayer title="My RasterLayer" |
|
|
filename="../../Data/iceland/island.tif" |
|
|
visible="true"> |
|
|
</rasterlayer> |
|
|
</map> |
|
|
</session>''' |
|
|
#print written_contents |
|
|
#print "********************************************" |
|
|
#print expected_contents |
|
|
self.compare_xml(written_contents, expected_contents) |
|
262 |
|
|
263 |
self.validate_data(written_contents) |
# deliberately put an apersand in the title :) |
264 |
|
session = Session("single map&layer") |
265 |
|
map = Map("Test Map") |
266 |
|
session.AddMap(map) |
267 |
|
# use shapefile from the example data |
268 |
|
imgfile = os.path.join(os.path.dirname(__file__), |
269 |
|
os.pardir, "Data", "iceland", "island.tif") |
270 |
|
layer = RasterLayer("My RasterLayer", imgfile) |
271 |
|
|
272 |
|
layer.SetOpacity(opacity) |
273 |
|
layer.SetMaskType(masktype) |
274 |
|
|
275 |
|
map.AddLayer(layer) |
276 |
|
|
277 |
|
filename = self.temp_file_name("%s.thuban" % self.id()) |
278 |
|
save_session(session, filename) |
279 |
|
session.Destroy() |
280 |
|
|
281 |
|
file = open(filename) |
282 |
|
written_contents = file.read() |
283 |
|
file.close() |
284 |
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
285 |
|
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
286 |
|
<session title="single map&layer" |
287 |
|
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
288 |
|
<map title="Test Map"> |
289 |
|
<rasterlayer title="My RasterLayer" |
290 |
|
filename="../../Data/iceland/island.tif" |
291 |
|
visible="true" %s> |
292 |
|
</rasterlayer> |
293 |
|
</map> |
294 |
|
</session>''' % (opname, ) |
295 |
|
#print written_contents |
296 |
|
#print "********************************************" |
297 |
|
#print expected_contents |
298 |
|
self.compare_xml(written_contents, expected_contents) |
299 |
|
|
300 |
|
self.validate_data(written_contents) |
301 |
|
|
302 |
def testClassifiedLayer(self): |
def testClassifiedLayer(self): |
303 |
"""Save a session with a single map with classifications""" |
"""Save a session with a single map with classifications""" |
337 |
layer2.SetClassificationColumn("POPYCOUN") |
layer2.SetClassificationColumn("POPYCOUN") |
338 |
|
|
339 |
# Classification with Latin 1 text |
# Classification with Latin 1 text |
340 |
clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue |
clazz.AppendGroup(ClassGroupSingleton( |
341 |
ClassGroupProperties(), |
internal_from_unicode(u'\xe4\xf6\xfc'), # ae, oe, ue |
342 |
'\xdcml\xe4uts')) # Uemlaeuts |
ClassGroupProperties(), |
343 |
|
internal_from_unicode(u'\xdcml\xe4uts'))) # Uemlaeuts |
344 |
|
|
345 |
|
# Pattern |
346 |
|
clazz.AppendGroup(ClassGroupPattern("BUI", ClassGroupProperties(), |
347 |
|
"pattern")) |
348 |
|
|
349 |
filename = self.temp_file_name("%s.thuban" % self.id()) |
filename = self.temp_file_name("%s.thuban" % self.id()) |
350 |
save_session(session, filename) |
save_session(session, filename) |
353 |
written_contents = file.read() |
written_contents = file.read() |
354 |
file.close() |
file.close() |
355 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
356 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
357 |
<session title="Map with Classifications" |
<session title="Map with Classifications" |
358 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
359 |
<fileshapesource id="D1" |
<fileshapesource id="D1" |
360 |
filename="../../Data/iceland/political.shp" |
filename="../../Data/iceland/political.shp" |
361 |
filetype="shapefile"/> |
filetype="shapefile"/> |
365 |
<parameter value="proj=utm"/> |
<parameter value="proj=utm"/> |
366 |
<parameter value="ellps=clrk66"/> |
<parameter value="ellps=clrk66"/> |
367 |
</projection> |
</projection> |
368 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" visible="true"> |
|
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
|
369 |
<classification field="AREA" field_type="double"> |
<classification field="AREA" field_type="double"> |
370 |
<clnull label=""> |
<clnull label=""> |
371 |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
384 |
</clrange> |
</clrange> |
385 |
</classification> |
</classification> |
386 |
</layer> |
</layer> |
387 |
<layer title="My Layer" shapestore="D1" |
<layer title="My Layer" shapestore="D1" visible="true"> |
|
fill="None" stroke="#000000" stroke_width="1" visible="true"> |
|
388 |
<classification field="POPYCOUN" field_type="string"> |
<classification field="POPYCOUN" field_type="string"> |
389 |
<clnull label=""> |
<clnull label=""> |
390 |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
393 |
label="\xc3\x9cml\xc3\xa4uts"> |
label="\xc3\x9cml\xc3\xa4uts"> |
394 |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
395 |
</clpoint> |
</clpoint> |
396 |
|
<clpattern pattern="BUI" label="pattern"> |
397 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
398 |
|
</clpattern> |
399 |
</classification> |
</classification> |
400 |
</layer> |
</layer> |
401 |
</map> |
</map> |
425 |
written_contents = file.read() |
written_contents = file.read() |
426 |
file.close() |
file.close() |
427 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
428 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
429 |
<session title="a DBF Table session" |
<session title="a DBF Table session" |
430 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
431 |
<filetable id="D1" filename="../../Data/iceland/political.dbf" |
<filetable id="D1" filename="../../Data/iceland/political.dbf" |
432 |
filetype="DBF" title="political"/> |
filetype="DBF" title="political"/> |
433 |
</session>''' |
</session>''' |
483 |
written_contents = file.read() |
written_contents = file.read() |
484 |
file.close() |
file.close() |
485 |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
486 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
487 |
<session title="A Joined Table session" |
<session title="A Joined Table session" |
488 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
489 |
<fileshapesource filename="../../Data/iceland/roads-line.shp" |
<fileshapesource filename="../../Data/iceland/roads-line.shp" |
490 |
filetype="shapefile" id="D142197204"/> |
filetype="shapefile" id="D142197204"/> |
491 |
<filetable filename="save_joinedtable.dbf" |
<filetable filename="save_joinedtable.dbf" |
501 |
shapesource="D142197204"/> |
shapesource="D142197204"/> |
502 |
<map title="Test Map"> |
<map title="Test Map"> |
503 |
<layer title="My Layer" |
<layer title="My Layer" |
504 |
shapestore="D141915644" visible="true" |
shapestore="D141915644" visible="true"> |
505 |
stroke="#000000" stroke_width="1" fill="None"/> |
<classification> |
506 |
|
<clnull label=""> |
507 |
|
<cldata fill="None" stroke="#000000" |
508 |
|
stroke_width="1"/> |
509 |
|
</clnull> |
510 |
|
</classification> |
511 |
|
</layer> |
512 |
</map> |
</map> |
513 |
</session>''' |
</session>''' |
514 |
|
|
530 |
class NonConnectionStore(PostGISShapeStore): |
class NonConnectionStore(PostGISShapeStore): |
531 |
"""Shapestore that doesn't try to access the server""" |
"""Shapestore that doesn't try to access the server""" |
532 |
def _fetch_table_information(self): |
def _fetch_table_information(self): |
533 |
pass |
# pretend that we've found a geometry column |
534 |
|
self.geometry_column = "the_geom" |
535 |
|
# pretend this is a ARC shape type. |
536 |
|
self.shape_type = SHAPETYPE_ARC |
537 |
|
def IDColumn(self): |
538 |
|
"""Return an object with a name attribute with value 'gid'""" |
539 |
|
class dummycol: |
540 |
|
name = "gid" |
541 |
|
return dummycol |
542 |
|
|
543 |
session = Session("A PostGIS Session") |
session = Session("A PostGIS Session") |
544 |
try: |
try: |
561 |
written = file.read() |
written = file.read() |
562 |
file.close() |
file.close() |
563 |
expected = '''<?xml version="1.0" encoding="UTF-8"?> |
expected = '''<?xml version="1.0" encoding="UTF-8"?> |
564 |
<!DOCTYPE session SYSTEM "thuban-1.0.dtd"> |
<!DOCTYPE session SYSTEM "thuban-1.1.dtd"> |
565 |
<session title="A PostGIS Session" |
<session title="A PostGIS Session" |
566 |
xmlns="http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd"> |
xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"> |
567 |
<dbconnection id="DB" |
<dbconnection id="DB" |
568 |
dbtype="postgis" dbname="plugh" |
dbtype="postgis" dbname="plugh" |
569 |
host="xyzzy" port="42" |
host="xyzzy" port="42" |
570 |
user="grue"/> |
user="grue"/> |
571 |
<dbshapesource id="roads" dbconn="DB" tablename="roads"/> |
<dbshapesource id="roads" dbconn="DB" tablename="roads" |
572 |
|
id_column="gid" geometry_column="the_geom"/> |
573 |
<map title="Test Map"> |
<map title="Test Map"> |
574 |
<layer title="Roads to Nowhere" |
<layer title="Roads to Nowhere" |
575 |
shapestore="roads" visible="true" |
shapestore="roads" visible="true"> |
576 |
stroke="#000000" stroke_width="1" fill="None"/> |
<classification> |
577 |
|
<clnull label=""> |
578 |
|
<cldata fill="None" stroke="#000000" |
579 |
|
stroke_width="1"/> |
580 |
|
</clnull> |
581 |
|
</classification> |
582 |
|
</layer> |
583 |
</map> |
</map> |
584 |
</session>''' |
</session>''' |
585 |
self.compare_xml(written, expected) |
self.compare_xml(written, expected) |