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 the Layer class |
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 |
import shapelib |
23 |
import dbflib |
24 |
|
25 |
from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
26 |
SHAPETYPE_POINT |
27 |
from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \ |
28 |
LAYER_VISIBILITY_CHANGED |
29 |
from Thuban.Model.color import Color |
30 |
|
31 |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
32 |
support.FloatComparisonMixin): |
33 |
|
34 |
"""Test cases for different layer (shape) types""" |
35 |
|
36 |
def assertFloatTuplesEqual(self, test, value): |
37 |
"""Assert equality of two lists of tuples of float""" |
38 |
for i in range(len(test)): |
39 |
self.assertFloatSeqEqual(test[i], value[i]) |
40 |
|
41 |
def test_arc_layer(self): |
42 |
"""Test Layer with arc shapes""" |
43 |
layer = Layer("Test Layer", |
44 |
os.path.join("..", "Data", "iceland", "roads-line.shp")) |
45 |
self.assertEquals(layer.Title(), "Test Layer") |
46 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
47 |
self.assertEquals(layer.NumShapes(), 839) |
48 |
shape = layer.Shape(32) |
49 |
self.assertFloatTuplesEqual(shape.Points(), |
50 |
[(-15.082174301147461, 66.27738189697265), |
51 |
(-15.026350021362305, 66.27339172363281)]) |
52 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
53 |
[-24.450359344482422, 63.426830291748047, |
54 |
-13.55668830871582, 66.520111083984375]) |
55 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)), |
56 |
[613, 726, 838]) |
57 |
|
58 |
def test_polygon_layer(self): |
59 |
"""Test Layer with polygon shapes""" |
60 |
layer = Layer("Test Layer", |
61 |
os.path.join("..", "Data", "iceland", "political.shp")) |
62 |
self.assertEquals(layer.Title(), "Test Layer") |
63 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
64 |
self.assertEquals(layer.NumShapes(), 156) |
65 |
shape = layer.Shape(4) |
66 |
self.assertFloatTuplesEqual(shape.Points(), |
67 |
[(-22.406391143798828, 64.714111328125), |
68 |
(-22.41621208190918, 64.71600341796875), |
69 |
(-22.406051635742188, 64.719200134277344), |
70 |
(-22.406391143798828, 64.714111328125)]) |
71 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
72 |
[-24.546524047851562, 63.286754608154297, |
73 |
-13.495815277099609, 66.563774108886719]) |
74 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)), |
75 |
[91, 92, 144, 146, 148, 150, 152, 153]) |
76 |
|
77 |
def test_point_layer(self): |
78 |
"""Test Layer with point shapes""" |
79 |
layer = Layer("Test Layer", |
80 |
os.path.join("..", "Data", "iceland", |
81 |
"cultural_landmark-point.shp")) |
82 |
self.assertEquals(layer.Title(), "Test Layer") |
83 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
84 |
self.assertEquals(layer.NumShapes(), 34) |
85 |
shape = layer.Shape(0) |
86 |
self.assertFloatTuplesEqual(shape.Points(), |
87 |
[(-22.711074829101562, 66.36572265625)]) |
88 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
89 |
[-23.806047439575195, 63.405960083007812, |
90 |
-15.12291431427002, 66.36572265625]) |
91 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)), |
92 |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
93 |
|
94 |
def test_empty_layer(self): |
95 |
"""Test Layer with empty shape file""" |
96 |
# create an empty shape file |
97 |
shapefilename = self.temp_file_name("layer_empty.shp") |
98 |
shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON) |
99 |
shp.close() |
100 |
# create an empty DBF file too because Thuban can't cope yet |
101 |
# with missing DBF file. |
102 |
dbffilename = self.temp_file_name("layer_empty.dbf") |
103 |
dbf = dbflib.create(dbffilename) |
104 |
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
105 |
|
106 |
# Now try to open it. |
107 |
layer = Layer("Empty Layer", shapefilename) |
108 |
self.assertEquals(layer.BoundingBox(), None) |
109 |
self.assertEquals(layer.LatLongBoundingBox(), None) |
110 |
self.assertEquals(layer.NumShapes(), 0) |
111 |
|
112 |
|
113 |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
114 |
|
115 |
"""Test cases for Layer method that modify the layer. |
116 |
""" |
117 |
|
118 |
def setUp(self): |
119 |
"""Clear the list of received messages and create a layer |
120 |
|
121 |
The layer is bound to self.layer. |
122 |
""" |
123 |
self.clear_messages() |
124 |
self.layer = Layer("Test Layer", |
125 |
os.path.join("..", "Data", "iceland", |
126 |
"political.shp")) |
127 |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
128 |
LAYER_LEGEND_CHANGED) |
129 |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
130 |
self.subscribe_with_params, |
131 |
LAYER_VISIBILITY_CHANGED) |
132 |
|
133 |
def tearDown(self): |
134 |
"""Clear the list of received messages and explictly destroy self.layer |
135 |
""" |
136 |
self.layer.Destroy() |
137 |
self.clear_messages() |
138 |
|
139 |
def test_initial_settings(self): |
140 |
"""Test Layer's initial legend attributes""" |
141 |
# test default settings |
142 |
self.failIf(self.layer.WasModified()) |
143 |
self.assertEquals(self.layer.fill, None) |
144 |
self.assertEquals(self.layer.stroke.hex(), "#000000") |
145 |
self.assertEquals(self.layer.stroke_width, 1) |
146 |
self.assertEquals(self.layer.Visible(), 1) |
147 |
# no messages should have been produced |
148 |
self.check_messages([]) |
149 |
|
150 |
def test_fill(self): |
151 |
"""Test Layer's fill attribute""" |
152 |
# modify the fill |
153 |
self.layer.SetFill(Color(0.5, 1.0, 0.75)) |
154 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
155 |
self.assertEquals(self.layer.fill.hex().lower(), "#7fffbf") |
156 |
self.assert_(self.layer.WasModified()) |
157 |
|
158 |
def test_stroke(self): |
159 |
"""Test Layer's stroke attribute""" |
160 |
self.layer.SetStroke(Color(0.5, 1.0, 0.75)) |
161 |
self.assertEquals(self.layer.stroke.hex().lower(), "#7fffbf") |
162 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
163 |
self.assert_(self.layer.WasModified()) |
164 |
|
165 |
def test_stroke_width(self): |
166 |
"""Test Layer's stroke_width attribute""" |
167 |
self.layer.SetStrokeWidth(3.0) |
168 |
self.assertEquals(self.layer.stroke_width, 3.0) |
169 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
170 |
self.assert_(self.layer.WasModified()) |
171 |
|
172 |
def test_visibility(self): |
173 |
"""Test Layer visibility""" |
174 |
self.layer.SetVisible(0) |
175 |
self.assertEquals(self.layer.Visible(), 0) |
176 |
self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)]) |
177 |
|
178 |
# currently, modifying the visibility doesn't count as changing |
179 |
# the layer. |
180 |
self.failIf(self.layer.WasModified()) |
181 |
|
182 |
def test_tree_info(self): |
183 |
"""Test Layer.TreeInfo""" |
184 |
self.assertEquals(self.layer.TreeInfo(), |
185 |
("Layer 'Test Layer'", |
186 |
['Shown', |
187 |
'Shapes: 156', |
188 |
('Extent (lat-lon):' |
189 |
' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
190 |
'Shapetype: Polygon', |
191 |
'Fill: None', |
192 |
'Outline: (0.000, 0.000, 0.000)'])) |
193 |
|
194 |
|
195 |
if __name__ == "__main__": |
196 |
unittest.main() |