1 |
# Copyright (c) 2002 by Intevation GmbH |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
22 |
import shapelib |
import shapelib |
23 |
import dbflib |
import dbflib |
24 |
|
|
25 |
from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.session import Session |
26 |
SHAPETYPE_POINT |
from Thuban.Model.layer import BaseLayer, Layer, RasterLayer, \ |
27 |
|
SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
28 |
from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \ |
from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \ |
29 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED |
30 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
31 |
|
from Thuban.Model.table import FIELDTYPE_DOUBLE |
32 |
|
from Thuban.Model.proj import Projection |
33 |
|
|
34 |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
35 |
support.FloatComparisonMixin): |
support.FloatComparisonMixin): |
41 |
for i in range(len(test)): |
for i in range(len(test)): |
42 |
self.assertFloatSeqEqual(test[i], value[i]) |
self.assertFloatSeqEqual(test[i], value[i]) |
43 |
|
|
44 |
|
def setUp(self): |
45 |
|
"""Create a session""" |
46 |
|
self.session = Session("Test session for %s" % self.__class__) |
47 |
|
|
48 |
|
def tearDown(self): |
49 |
|
self.session = None |
50 |
|
|
51 |
|
def build_path(self, filename): |
52 |
|
return os.path.join("..", "Data", "iceland", filename) |
53 |
|
|
54 |
|
def open_shapefile(self, filename): |
55 |
|
"""Open and return a shapestore for filename in the iceland data set""" |
56 |
|
return self.session.OpenShapefile(self.build_path(filename)) |
57 |
|
|
58 |
|
def test_base_layer(self): |
59 |
|
layer = BaseLayer("Test BaseLayer") |
60 |
|
self.assertEquals(layer.Title(), "Test BaseLayer") |
61 |
|
self.failUnless(layer.Visible()) |
62 |
|
|
63 |
|
# toggle visibility |
64 |
|
layer.SetVisible(False) |
65 |
|
self.failIf(layer.Visible()) |
66 |
|
|
67 |
|
layer.SetVisible(True) |
68 |
|
self.failUnless(layer.Visible()) |
69 |
|
|
70 |
|
self.failIf(layer.HasClassification()) |
71 |
|
self.assertEquals(layer.GetProjection(), None) |
72 |
|
|
73 |
|
# set/get projection |
74 |
|
proj = Projection(["proj=utm", "zone=26"]) |
75 |
|
|
76 |
|
layer.SetProjection(proj) |
77 |
|
self.failUnless(layer.GetProjection() is proj) |
78 |
|
|
79 |
|
# __init__ with other arguments |
80 |
|
layer = BaseLayer("Test BaseLayer", False, proj) |
81 |
|
self.failIf(layer.Visible()) |
82 |
|
self.failUnless(layer.GetProjection() is proj) |
83 |
|
|
84 |
def test_arc_layer(self): |
def test_arc_layer(self): |
85 |
"""Test Layer with arc shapes""" |
"""Test Layer with arc shapes""" |
86 |
layer = Layer("Test Layer", |
layer = Layer("Test Layer", self.open_shapefile("roads-line.shp")) |
|
os.path.join("..", "Data", "iceland", "roads-line.shp")) |
|
|
self.assertEquals(layer.Title(), "Test Layer") |
|
87 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
88 |
self.assertEquals(layer.NumShapes(), 839) |
self.assertEquals(layer.NumShapes(), 839) |
89 |
shape = layer.Shape(32) |
shape = layer.Shape(32) |
96 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)), |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)), |
97 |
[613, 726, 838]) |
[613, 726, 838]) |
98 |
|
|
99 |
|
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]), |
100 |
|
[-15.082174301147461, 66.27339172363281, |
101 |
|
-15.026350021362305, 66.27738189697265]) |
102 |
|
|
103 |
|
shape = layer.Shape(33) |
104 |
|
self.assertFloatTuplesEqual(shape.Points(), |
105 |
|
[(-22.248506546020508, 66.30645751953125), |
106 |
|
(-22.232730865478516, 66.294075012207031), |
107 |
|
(-22.23158073425293, 66.287689208984375), |
108 |
|
(-22.246318817138672, 66.270065307617188)]) |
109 |
|
|
110 |
|
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]), |
111 |
|
[-22.248506546020508, 66.270065307617188, |
112 |
|
-15.026350021362305, 66.30645751953125]) |
113 |
|
|
114 |
|
self.assertEquals(layer.ShapesBoundingBox([]), None) |
115 |
|
self.assertEquals(layer.ShapesBoundingBox(None), None) |
116 |
|
|
117 |
|
layer.Destroy() |
118 |
|
|
119 |
def test_polygon_layer(self): |
def test_polygon_layer(self): |
120 |
"""Test Layer with polygon shapes""" |
"""Test Layer with polygon shapes""" |
121 |
layer = Layer("Test Layer", |
layer = Layer("Test Layer", self.open_shapefile("political.shp")) |
|
os.path.join("..", "Data", "iceland", "political.shp")) |
|
|
self.assertEquals(layer.Title(), "Test Layer") |
|
122 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
123 |
self.assertEquals(layer.NumShapes(), 156) |
self.assertEquals(layer.NumShapes(), 156) |
124 |
shape = layer.Shape(4) |
shape = layer.Shape(4) |
132 |
-13.495815277099609, 66.563774108886719]) |
-13.495815277099609, 66.563774108886719]) |
133 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)), |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)), |
134 |
[91, 92, 144, 146, 148, 150, 152, 153]) |
[91, 92, 144, 146, 148, 150, 152, 153]) |
135 |
|
layer.Destroy() |
136 |
|
|
137 |
def test_point_layer(self): |
def test_point_layer(self): |
138 |
"""Test Layer with point shapes""" |
"""Test Layer with point shapes""" |
139 |
layer = Layer("Test Layer", |
layer = Layer("Test Layer", |
140 |
os.path.join("..", "Data", "iceland", |
self.open_shapefile("cultural_landmark-point.shp")) |
|
"cultural_landmark-point.shp")) |
|
|
self.assertEquals(layer.Title(), "Test Layer") |
|
141 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
142 |
self.assertEquals(layer.NumShapes(), 34) |
self.assertEquals(layer.NumShapes(), 34) |
143 |
shape = layer.Shape(0) |
shape = layer.Shape(0) |
148 |
-15.12291431427002, 66.36572265625]) |
-15.12291431427002, 66.36572265625]) |
149 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)), |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)), |
150 |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
151 |
|
layer.Destroy() |
152 |
|
|
153 |
def test_empty_layer(self): |
def test_empty_layer(self): |
154 |
"""Test Layer with empty shape file""" |
"""Test Layer with empty shape file""" |
163 |
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
164 |
|
|
165 |
# Now try to open it. |
# Now try to open it. |
166 |
layer = Layer("Empty Layer", shapefilename) |
layer = Layer("Empty Layer", |
167 |
|
self.session.OpenShapefile(shapefilename)) |
168 |
self.assertEquals(layer.BoundingBox(), None) |
self.assertEquals(layer.BoundingBox(), None) |
169 |
self.assertEquals(layer.LatLongBoundingBox(), None) |
self.assertEquals(layer.LatLongBoundingBox(), None) |
170 |
self.assertEquals(layer.NumShapes(), 0) |
self.assertEquals(layer.NumShapes(), 0) |
171 |
|
layer.Destroy() |
172 |
|
|
173 |
|
def test_get_field_type(self): |
174 |
|
"""Test Layer.GetFieldType()""" |
175 |
|
layer = Layer("Test Layer", self.open_shapefile("roads-line.shp")) |
176 |
|
self.assertEquals(layer.GetFieldType("LENGTH"), FIELDTYPE_DOUBLE) |
177 |
|
self.assertEquals(layer.GetFieldType("non existing"), None) |
178 |
|
layer.Destroy() |
179 |
|
|
180 |
|
def test_raster_layer(self): |
181 |
|
filename = self.build_path("island.tif") |
182 |
|
layer = RasterLayer("Test RasterLayer", filename) |
183 |
|
self.assertEquals(layer.GetImageFilename(), filename) |
184 |
|
self.assertFloatSeqEqual(layer.BoundingBox(), |
185 |
|
[-24.5500000, 63.2833330, |
186 |
|
-13.4916670, 66.5666670]) |
187 |
|
self.assertFloatSeqEqual(layer.LatLongBoundingBox(), |
188 |
|
[-24.5500000, 63.2833330, |
189 |
|
-13.4916670, 66.5666670]) |
190 |
|
|
191 |
|
|
192 |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
195 |
""" |
""" |
196 |
|
|
197 |
def setUp(self): |
def setUp(self): |
198 |
"""Clear the list of received messages and create a layer |
"""Clear the list of received messages and create a layer and a session |
199 |
|
|
200 |
The layer is bound to self.layer. |
The layer is bound to self.layer and the session to self.session. |
201 |
""" |
""" |
202 |
self.clear_messages() |
self.clear_messages() |
203 |
|
self.session = Session("Test session for %s" % self.__class__) |
204 |
|
filename = os.path.join("..", "Data", "iceland", "political.shp") |
205 |
self.layer = Layer("Test Layer", |
self.layer = Layer("Test Layer", |
206 |
os.path.join("..", "Data", "iceland", |
self.session.OpenShapefile(filename)) |
|
"political.shp")) |
|
207 |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
208 |
LAYER_LEGEND_CHANGED) |
LAYER_LEGEND_CHANGED) |
209 |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
214 |
"""Clear the list of received messages and explictly destroy self.layer |
"""Clear the list of received messages and explictly destroy self.layer |
215 |
""" |
""" |
216 |
self.layer.Destroy() |
self.layer.Destroy() |
217 |
|
self.layer = None |
218 |
|
self.session.Destroy() |
219 |
|
self.session = None |
220 |
self.clear_messages() |
self.clear_messages() |
221 |
|
|
222 |
def test_initial_settings(self): |
def test_initial_settings(self): |
223 |
"""Test Layer's initial legend attributes""" |
"""Test Layer's initial legend attributes""" |
224 |
# test default settings |
# test default settings |
225 |
self.failIf(self.layer.WasModified()) |
self.failIf(self.layer.WasModified()) |
226 |
self.assertEquals(self.layer.fill, None) |
#self.assertEquals(self.layer.fill, None) |
227 |
self.assertEquals(self.layer.stroke.hex(), "#000000") |
#self.assertEquals(self.layer.stroke.hex(), "#000000") |
228 |
self.assertEquals(self.layer.stroke_width, 1) |
#self.assertEquals(self.layer.stroke_width, 1) |
229 |
self.assertEquals(self.layer.Visible(), 1) |
self.assertEquals(self.layer.Visible(), 1) |
230 |
# no messages should have been produced |
# no messages should have been produced |
231 |
self.check_messages([]) |
self.check_messages([]) |
258 |
|
|
259 |
|
|
260 |
if __name__ == "__main__": |
if __name__ == "__main__": |
261 |
unittest.main() |
support.run_tests() |