1 |
# Copyright (c) 2002 by Intevation GmbH |
2 |
# 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 loading a thuban session from a file |
10 |
""" |
11 |
|
12 |
__version__ = "$Revision$" |
13 |
# $Source$ |
14 |
# $Id$ |
15 |
|
16 |
import os |
17 |
import unittest |
18 |
|
19 |
import support |
20 |
support.initthuban() |
21 |
|
22 |
from Thuban.Model.load import load_session, parse_color |
23 |
from Thuban.Model.session import Session |
24 |
from Thuban.Model.map import Map |
25 |
from Thuban.Model.layer import Layer |
26 |
from Thuban.Model.proj import Projection |
27 |
from Thuban.Model.color import Color |
28 |
|
29 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, FIELDTYPE_STRING |
30 |
|
31 |
from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\ |
32 |
ClassGroupSingleton, ClassGroupDefault |
33 |
|
34 |
def filenames_equal(name1, name2): |
35 |
"""Return true if the filenames name1 and name2 are equal. |
36 |
|
37 |
On systems where it is available, simply use os.path.samefile, |
38 |
otherwise return whether the normalized versions of the filenames |
39 |
according to os.path.normpath are equal. |
40 |
""" |
41 |
if hasattr(os.path, "samefile"): |
42 |
return os.path.samefile(name1, name2) |
43 |
return os.path.normpath(name1) == os.path.normpath(name2) |
44 |
|
45 |
|
46 |
contents_single_map = '''\ |
47 |
<?xml version="1.0" encoding="UTF-8"?> |
48 |
<!DOCTYPE session SYSTEM "thuban.dtd"> |
49 |
<session title="single map&layer"> |
50 |
<map title="Test Map"> |
51 |
<projection> |
52 |
<parameter value="zone=26"/> |
53 |
<parameter value="proj=utm"/> |
54 |
<parameter value="ellps=clrk66"/> |
55 |
</projection> |
56 |
<layer title="My Layer" stroke_width="1" fill="None" |
57 |
filename="../../Data/iceland/political.shp" |
58 |
stroke="#000000"/> |
59 |
</map> |
60 |
</session> |
61 |
''' |
62 |
|
63 |
|
64 |
class LoadSessionTest(unittest.TestCase, support.FileTestMixin): |
65 |
|
66 |
def setUp(self): |
67 |
"""Create the test files""" |
68 |
file = open(self.temp_file_name("load_singlelayer.thuban"), "w") |
69 |
file.write(contents_single_map) |
70 |
file.close() |
71 |
self.session = None |
72 |
|
73 |
def tearDown(self): |
74 |
if self.session is not None: |
75 |
self.session.Destroy() |
76 |
|
77 |
def testSingleLayer(self): |
78 |
"""Load a session with a single map with a single layer""" |
79 |
eq = self.assertEquals |
80 |
session = load_session(self.temp_file_name("load_singlelayer.thuban")) |
81 |
self.session = session |
82 |
|
83 |
# Check the title |
84 |
eq(session.Title(), "single map&layer") |
85 |
|
86 |
# the session has one map. |
87 |
maps = session.Maps() |
88 |
eq(len(maps), 1) |
89 |
|
90 |
# Check the map's attributes |
91 |
map = maps[0] |
92 |
eq(map.Title(), "Test Map") |
93 |
|
94 |
# the map has a single layer |
95 |
layers = map.Layers() |
96 |
eq(len(layers), 1) |
97 |
|
98 |
# Check the layer attributes |
99 |
layer = layers[0] |
100 |
eq(layer.Title(), "My Layer") |
101 |
self.failUnless(filenames_equal(layer.filename, |
102 |
os.path.join(self.temp_dir(), |
103 |
os.pardir, os.pardir, |
104 |
"Data", "iceland", |
105 |
"political.shp"))) |
106 |
eq(layer.GetClassification().GetDefaultFill(), Color.Transparent) |
107 |
eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000") |
108 |
|
109 |
self.session.Destroy() |
110 |
self.session = None |
111 |
|
112 |
def testClassification(self): |
113 |
"""Load a session with a map and classified layers.""" |
114 |
|
115 |
eq = self.assertEquals |
116 |
session = load_session("../Data/iceland_sample_test.thuban") |
117 |
self.session = session |
118 |
|
119 |
map = self.session.Maps()[0] # only one map in the sample |
120 |
|
121 |
layers = map.Layers() |
122 |
eq(len(layers), 3) |
123 |
|
124 |
expected = (("political", 0, |
125 |
[("default", (), "", |
126 |
("#000000", 1, "#c0c0c0"))]), |
127 |
("roads-line", 5, |
128 |
[("default", (), "", |
129 |
("#000000", 1, "None")), |
130 |
("range", (0.001, 0.3996), "short roads", |
131 |
("#ffffff", 1, "#ffffff")), |
132 |
("range", (0.3996, 0.7982), "less short", |
133 |
("#ccf4cc", 1, "#ccf4cc")), |
134 |
("range", (0.7982, 1.1968), "bit longer", |
135 |
("#99ea99", 1, "#99ea99")), |
136 |
("range", (1.1968, 1.5954), "where's the end?", |
137 |
("#66e066", 1, "#66e066")), |
138 |
("range", (1.5954, 1.994), "long roads", |
139 |
("#33d633", 1, "#33d633"))]), |
140 |
("something else", 6, |
141 |
[("default", (), "", |
142 |
("#000000", 1, "None")), |
143 |
("single", ("BUILDING"), "", |
144 |
("#0000ff", 1, "#0000ff")), |
145 |
("single", ("FARM"), "", |
146 |
("#00aaff", 1, "#00aaff")), |
147 |
("single", ("HUT"), "", |
148 |
("#00ffaa", 1, "#00ffaa")), |
149 |
("single", ("LIGHTHOUSE"), "", |
150 |
("#00ff00", 1, "#00ff00")), |
151 |
("single", ("OTHER/UNKNOWN"), "", |
152 |
("#aaff00", 1, "#aaff00")), |
153 |
("single", ("RUINS"), "", |
154 |
("#ffaa00", 1, "#ffaa00"))])) |
155 |
|
156 |
eq(len(layers), len(expected)) |
157 |
|
158 |
TITLE = 0 |
159 |
NUM_GROUPS = 1 |
160 |
CLASSES = 2 |
161 |
GROUP_TYPE = 0 |
162 |
GROUP_DATA = 1 |
163 |
GROUP_LABEL = 2 |
164 |
GROUP_PROPS = 3 |
165 |
|
166 |
for layer, data in zip(layers, expected): |
167 |
eq(layer.Title(), data[TITLE]) |
168 |
|
169 |
clazz = layer.GetClassification() |
170 |
eq(clazz.GetNumGroups(), data[NUM_GROUPS]) |
171 |
eq(clazz.GetNumGroups() + 1, len(data[CLASSES])) |
172 |
|
173 |
i = 0 |
174 |
for group in clazz: |
175 |
|
176 |
props = ClassGroupProperties() |
177 |
props.SetLineColor( |
178 |
parse_color(data[CLASSES][i][GROUP_PROPS][0])) |
179 |
props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1]) |
180 |
props.SetFill( |
181 |
parse_color(data[CLASSES][i][GROUP_PROPS][2])) |
182 |
|
183 |
if data[CLASSES][i][GROUP_TYPE] == "default": |
184 |
g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL]) |
185 |
elif data[CLASSES][i][GROUP_TYPE] == "range": |
186 |
g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0], |
187 |
data[CLASSES][i][GROUP_DATA][1], |
188 |
props, data[CLASSES][i][GROUP_LABEL]) |
189 |
elif data[CLASSES][i][GROUP_TYPE] == "single": |
190 |
g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA], |
191 |
props, data[CLASSES][i][GROUP_LABEL]) |
192 |
|
193 |
eq(group, g) |
194 |
|
195 |
i += 1 |
196 |
|
197 |
if __name__ == "__main__": |
198 |
unittest.main() |
199 |
|
200 |
|
201 |
|