1 |
bh |
331 |
# 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 |
bh |
336 |
import dbflib |
24 |
bh |
331 |
|
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 |
bh |
336 |
# 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 |
bh |
331 |
|
106 |
bh |
336 |
# Now try to open it. |
107 |
bh |
331 |
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 |
jonathan |
409 |
#self.assertEquals(self.layer.fill, None) |
144 |
|
|
#self.assertEquals(self.layer.stroke.hex(), "#000000") |
145 |
|
|
#self.assertEquals(self.layer.stroke_width, 1) |
146 |
bh |
331 |
self.assertEquals(self.layer.Visible(), 1) |
147 |
|
|
# no messages should have been produced |
148 |
|
|
self.check_messages([]) |
149 |
|
|
|
150 |
|
|
def test_visibility(self): |
151 |
|
|
"""Test Layer visibility""" |
152 |
|
|
self.layer.SetVisible(0) |
153 |
|
|
self.assertEquals(self.layer.Visible(), 0) |
154 |
|
|
self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)]) |
155 |
|
|
|
156 |
|
|
# currently, modifying the visibility doesn't count as changing |
157 |
|
|
# the layer. |
158 |
|
|
self.failIf(self.layer.WasModified()) |
159 |
|
|
|
160 |
jonathan |
395 |
|
161 |
|
|
# |
162 |
|
|
# the tree info now contains Color objects which are difficult to test |
163 |
|
|
# |
164 |
|
|
# def test_tree_info(self): |
165 |
|
|
# """Test Layer.TreeInfo""" |
166 |
|
|
# self.assertEquals(self.layer.TreeInfo(), |
167 |
|
|
# ("Layer 'Test Layer'", |
168 |
|
|
# ['Shown', |
169 |
|
|
# 'Shapes: 156', |
170 |
|
|
# ('Extent (lat-lon):' |
171 |
|
|
# ' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
172 |
|
|
# 'Shapetype: Polygon', |
173 |
|
|
# 'Fill: None', |
174 |
|
|
# 'Outline: (0.000, 0.000, 0.000)'])) |
175 |
bh |
331 |
|
176 |
|
|
|
177 |
|
|
if __name__ == "__main__": |
178 |
|
|
unittest.main() |