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