1 |
# Copyright (c) 2002, 2003 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 |
layer.Destroy() |
58 |
|
59 |
def test_polygon_layer(self): |
60 |
"""Test Layer with polygon shapes""" |
61 |
layer = Layer("Test Layer", |
62 |
os.path.join("..", "Data", "iceland", "political.shp")) |
63 |
self.assertEquals(layer.Title(), "Test Layer") |
64 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
65 |
self.assertEquals(layer.NumShapes(), 156) |
66 |
shape = layer.Shape(4) |
67 |
self.assertFloatTuplesEqual(shape.Points(), |
68 |
[(-22.406391143798828, 64.714111328125), |
69 |
(-22.41621208190918, 64.71600341796875), |
70 |
(-22.406051635742188, 64.719200134277344), |
71 |
(-22.406391143798828, 64.714111328125)]) |
72 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
73 |
[-24.546524047851562, 63.286754608154297, |
74 |
-13.495815277099609, 66.563774108886719]) |
75 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)), |
76 |
[91, 92, 144, 146, 148, 150, 152, 153]) |
77 |
layer.Destroy() |
78 |
|
79 |
def test_point_layer(self): |
80 |
"""Test Layer with point shapes""" |
81 |
layer = Layer("Test Layer", |
82 |
os.path.join("..", "Data", "iceland", |
83 |
"cultural_landmark-point.shp")) |
84 |
self.assertEquals(layer.Title(), "Test Layer") |
85 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
86 |
self.assertEquals(layer.NumShapes(), 34) |
87 |
shape = layer.Shape(0) |
88 |
self.assertFloatTuplesEqual(shape.Points(), |
89 |
[(-22.711074829101562, 66.36572265625)]) |
90 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
91 |
[-23.806047439575195, 63.405960083007812, |
92 |
-15.12291431427002, 66.36572265625]) |
93 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)), |
94 |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
95 |
layer.Destroy() |
96 |
|
97 |
def test_empty_layer(self): |
98 |
"""Test Layer with empty shape file""" |
99 |
# create an empty shape file |
100 |
shapefilename = self.temp_file_name("layer_empty.shp") |
101 |
shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON) |
102 |
shp.close() |
103 |
# create an empty DBF file too because Thuban can't cope yet |
104 |
# with missing DBF file. |
105 |
dbffilename = self.temp_file_name("layer_empty.dbf") |
106 |
dbf = dbflib.create(dbffilename) |
107 |
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
108 |
|
109 |
# Now try to open it. |
110 |
layer = Layer("Empty Layer", shapefilename) |
111 |
self.assertEquals(layer.BoundingBox(), None) |
112 |
self.assertEquals(layer.LatLongBoundingBox(), None) |
113 |
self.assertEquals(layer.NumShapes(), 0) |
114 |
layer.Destroy() |
115 |
|
116 |
|
117 |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
118 |
|
119 |
"""Test cases for Layer method that modify the layer. |
120 |
""" |
121 |
|
122 |
def setUp(self): |
123 |
"""Clear the list of received messages and create a layer |
124 |
|
125 |
The layer is bound to self.layer. |
126 |
""" |
127 |
self.clear_messages() |
128 |
self.layer = Layer("Test Layer", |
129 |
os.path.join("..", "Data", "iceland", |
130 |
"political.shp")) |
131 |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
132 |
LAYER_LEGEND_CHANGED) |
133 |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
134 |
self.subscribe_with_params, |
135 |
LAYER_VISIBILITY_CHANGED) |
136 |
|
137 |
def tearDown(self): |
138 |
"""Clear the list of received messages and explictly destroy self.layer |
139 |
""" |
140 |
self.layer.Destroy() |
141 |
self.clear_messages() |
142 |
|
143 |
def test_initial_settings(self): |
144 |
"""Test Layer's initial legend attributes""" |
145 |
# test default settings |
146 |
self.failIf(self.layer.WasModified()) |
147 |
#self.assertEquals(self.layer.fill, None) |
148 |
#self.assertEquals(self.layer.stroke.hex(), "#000000") |
149 |
#self.assertEquals(self.layer.stroke_width, 1) |
150 |
self.assertEquals(self.layer.Visible(), 1) |
151 |
# no messages should have been produced |
152 |
self.check_messages([]) |
153 |
|
154 |
def test_visibility(self): |
155 |
"""Test Layer visibility""" |
156 |
self.layer.SetVisible(0) |
157 |
self.assertEquals(self.layer.Visible(), 0) |
158 |
self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)]) |
159 |
|
160 |
# currently, modifying the visibility doesn't count as changing |
161 |
# the layer. |
162 |
self.failIf(self.layer.WasModified()) |
163 |
|
164 |
|
165 |
# |
166 |
# the tree info now contains Color objects which are difficult to test |
167 |
# |
168 |
# def test_tree_info(self): |
169 |
# """Test Layer.TreeInfo""" |
170 |
# self.assertEquals(self.layer.TreeInfo(), |
171 |
# ("Layer 'Test Layer'", |
172 |
# ['Shown', |
173 |
# 'Shapes: 156', |
174 |
# ('Extent (lat-lon):' |
175 |
# ' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
176 |
# 'Shapetype: Polygon', |
177 |
# 'Fill: None', |
178 |
# 'Outline: (0.000, 0.000, 0.000)'])) |
179 |
|
180 |
|
181 |
if __name__ == "__main__": |
182 |
support.run_tests() |