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