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