23 |
import dbflib |
import dbflib |
24 |
|
|
25 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
26 |
from Thuban.Model.layer import BaseLayer, Layer, RasterLayer, \ |
from Thuban.Model.layer import BaseLayer, Layer, RasterLayer |
27 |
SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
from Thuban.Model.data import 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_SHAPESTORE_REPLACED, LAYER_CHANGED |
LAYER_VISIBILITY_CHANGED, LAYER_SHAPESTORE_REPLACED, LAYER_CHANGED |
|
from Thuban.Model.color import Color |
|
30 |
from Thuban.Model.table import FIELDTYPE_DOUBLE, FIELDTYPE_STRING, MemoryTable |
from Thuban.Model.table import FIELDTYPE_DOUBLE, FIELDTYPE_STRING, MemoryTable |
31 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
32 |
from Thuban.Model.data import DerivedShapeStore |
from Thuban.Model.data import DerivedShapeStore |
33 |
from Thuban.Model.classification import Classification, ClassGroupSingleton |
from Thuban.Model.classification import Classification, ClassGroupSingleton, \ |
34 |
|
ClassGroupRange |
35 |
|
|
36 |
|
import Thuban.Model.resource |
37 |
|
|
38 |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
39 |
support.FloatComparisonMixin): |
support.FloatComparisonMixin): |
40 |
|
|
41 |
"""Test cases for different layer (shape) types""" |
"""Test cases for different layer (shape) types""" |
42 |
|
|
|
def assertFloatTuplesEqual(self, test, value): |
|
|
"""Assert equality of two lists of tuples of float""" |
|
|
for i in range(len(test)): |
|
|
self.assertFloatSeqEqual(test[i], value[i]) |
|
|
|
|
43 |
def setUp(self): |
def setUp(self): |
44 |
"""Create a session self.session and initialize self.layer to None""" |
"""Create a session self.session and initialize self.layer to None""" |
45 |
self.session = Session("Test session for %s" % self.__class__) |
self.session = Session("Test session for %s" % self.__class__) |
72 |
self.failUnless(layer.Visible()) |
self.failUnless(layer.Visible()) |
73 |
|
|
74 |
self.failIf(layer.HasClassification()) |
self.failIf(layer.HasClassification()) |
75 |
|
self.failIf(layer.HasShapes()) |
76 |
|
|
77 |
self.assertEquals(layer.GetProjection(), None) |
self.assertEquals(layer.GetProjection(), None) |
78 |
|
|
79 |
# set/get projection |
# set/get projection |
91 |
"""Test Layer with arc shapes""" |
"""Test Layer with arc shapes""" |
92 |
layer = self.layer = Layer("Test Layer", |
layer = self.layer = Layer("Test Layer", |
93 |
self.open_shapefile("roads-line.shp")) |
self.open_shapefile("roads-line.shp")) |
94 |
|
self.failUnless(layer.HasClassification()) |
95 |
|
self.failUnless(layer.HasShapes()) |
96 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
97 |
self.assertEquals(layer.NumShapes(), 839) |
self.assertEquals(layer.NumShapes(), 839) |
98 |
shape = layer.Shape(32) |
shape = layer.Shape(32) |
99 |
self.assertFloatTuplesEqual(shape.Points(), |
self.assertPointListEquals(shape.Points(), |
100 |
[(-15.082174301147461, 66.27738189697265), |
[[(-15.082174301147461, 66.27738189697265), |
101 |
(-15.026350021362305, 66.27339172363281)]) |
(-15.026350021362305, 66.27339172363281)]]) |
102 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
self.assertFloatSeqEqual(layer.BoundingBox(), |
103 |
[-24.450359344482422, 63.426830291748047, |
[-24.450359344482422, 63.426830291748047, |
104 |
-13.55668830871582, 66.520111083984375]) |
-13.55668830871582, 66.520111083984375]) |
110 |
-15.026350021362305, 66.27738189697265]) |
-15.026350021362305, 66.27738189697265]) |
111 |
|
|
112 |
shape = layer.Shape(33) |
shape = layer.Shape(33) |
113 |
self.assertFloatTuplesEqual(shape.Points(), |
self.assertPointListEquals(shape.Points(), |
114 |
[(-22.248506546020508, 66.30645751953125), |
[[(-22.24850654602050, 66.30645751953125), |
115 |
(-22.232730865478516, 66.294075012207031), |
(-22.23273086547851, 66.29407501220703), |
116 |
(-22.23158073425293, 66.287689208984375), |
(-22.23158073425293, 66.2876892089843), |
117 |
(-22.246318817138672, 66.270065307617188)]) |
(-22.24631881713867, 66.27006530761718)]]) |
118 |
|
|
119 |
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]), |
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]), |
120 |
[-22.248506546020508, 66.270065307617188, |
[-22.248506546020508, 66.270065307617188, |
127 |
"""Test Layer with polygon shapes""" |
"""Test Layer with polygon shapes""" |
128 |
layer = self.layer = Layer("Test Layer", |
layer = self.layer = Layer("Test Layer", |
129 |
self.open_shapefile("political.shp")) |
self.open_shapefile("political.shp")) |
130 |
|
self.failUnless(layer.HasClassification()) |
131 |
|
self.failUnless(layer.HasShapes()) |
132 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
133 |
self.assertEquals(layer.NumShapes(), 156) |
self.assertEquals(layer.NumShapes(), 156) |
134 |
shape = layer.Shape(4) |
shape = layer.Shape(4) |
135 |
self.assertFloatTuplesEqual(shape.Points(), |
self.assertPointListEquals(shape.Points(), |
136 |
[(-22.406391143798828, 64.714111328125), |
[[(-22.40639114379882, 64.714111328125), |
137 |
(-22.41621208190918, 64.71600341796875), |
(-22.41621208190918, 64.7160034179687), |
138 |
(-22.406051635742188, 64.719200134277344), |
(-22.40605163574218, 64.719200134277), |
139 |
(-22.406391143798828, 64.714111328125)]) |
(-22.40639114379882, 64.714111328125)]]) |
140 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
self.assertFloatSeqEqual(layer.BoundingBox(), |
141 |
[-24.546524047851562, 63.286754608154297, |
[-24.546524047851562, 63.286754608154297, |
142 |
-13.495815277099609, 66.563774108886719]) |
-13.495815277099609, 66.563774108886719]) |
147 |
"""Test Layer with point shapes""" |
"""Test Layer with point shapes""" |
148 |
layer = self.layer = Layer("Test Layer", |
layer = self.layer = Layer("Test Layer", |
149 |
self.open_shapefile("cultural_landmark-point.shp")) |
self.open_shapefile("cultural_landmark-point.shp")) |
150 |
|
self.failUnless(layer.HasClassification()) |
151 |
|
self.failUnless(layer.HasShapes()) |
152 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
153 |
self.assertEquals(layer.NumShapes(), 34) |
self.assertEquals(layer.NumShapes(), 34) |
154 |
shape = layer.Shape(0) |
shape = layer.Shape(0) |
155 |
self.assertFloatTuplesEqual(shape.Points(), |
self.assertPointListEquals(shape.Points(), |
156 |
[(-22.711074829101562, 66.36572265625)]) |
[[(-22.711074829101562, 66.36572265625)]]) |
157 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
self.assertFloatSeqEqual(layer.BoundingBox(), |
158 |
[-23.806047439575195, 63.405960083007812, |
[-23.806047439575195, 63.405960083007812, |
159 |
-15.12291431427002, 66.36572265625]) |
-15.12291431427002, 66.36572265625]) |
187 |
self.assertEquals(layer.GetFieldType("non existing"), None) |
self.assertEquals(layer.GetFieldType("non existing"), None) |
188 |
|
|
189 |
def test_raster_layer(self): |
def test_raster_layer(self): |
190 |
|
if not Thuban.Model.resource.has_gdal_support(): |
191 |
|
raise support.SkipTest("No gdal support") |
192 |
|
|
193 |
filename = self.build_path("island.tif") |
filename = self.build_path("island.tif") |
194 |
layer = RasterLayer("Test RasterLayer", filename) |
layer = RasterLayer("Test RasterLayer", filename) |
195 |
|
self.failIf(layer.HasClassification()) |
196 |
|
self.failIf(layer.HasShapes()) |
197 |
self.assertEquals(layer.GetImageFilename(), filename) |
self.assertEquals(layer.GetImageFilename(), filename) |
198 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
self.assertFloatSeqEqual(layer.BoundingBox(), |
199 |
[-24.5500000, 63.2833330, |
[-24.5500000, 63.2833330, |
211 |
derived = DerivedShapeStore(store, store.Table()) |
derived = DerivedShapeStore(store, store.Table()) |
212 |
layer.SetShapeStore(derived) |
layer.SetShapeStore(derived) |
213 |
self.assert_(layer.ShapeStore() is derived) |
self.assert_(layer.ShapeStore() is derived) |
214 |
|
|
215 |
|
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
216 |
|
self.assertEquals(layer.NumShapes(), 839) |
217 |
|
shape = layer.Shape(32) |
218 |
|
self.assertPointListEquals(shape.Points(), |
219 |
|
[[(-15.082174301147, 66.277381896972), |
220 |
|
(-15.026350021362, 66.273391723632)]]) |
221 |
|
self.assertFloatSeqEqual(layer.BoundingBox(), |
222 |
|
[-24.450359344482422, 63.426830291748047, |
223 |
|
-13.55668830871582, 66.520111083984375]) |
224 |
|
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, |
225 |
|
-23.75, 64.25)), |
226 |
|
[613, 726, 838]) |
227 |
|
|
228 |
|
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]), |
229 |
|
[-15.082174301147461, 66.27339172363281, |
230 |
|
-15.026350021362305, 66.27738189697265]) |
231 |
|
|
232 |
finally: |
finally: |
233 |
store = derived = None |
store = derived = None |
234 |
|
|
243 |
"cultural_landmark-point.dbf") |
"cultural_landmark-point.dbf") |
244 |
self.store = self.session.OpenShapefile(self.shapefilename) |
self.store = self.session.OpenShapefile(self.shapefilename) |
245 |
self.layer = Layer("test layer", self.store) |
self.layer = Layer("test layer", self.store) |
246 |
self.classification = Classification(field = "CLPTLABEL") |
self.classification = Classification() |
247 |
self.classification.AppendGroup(ClassGroupSingleton("FARM")) |
self.classification.AppendGroup(ClassGroupSingleton("FARM")) |
248 |
|
self.layer.SetClassificationColumn("CLPTLABEL") |
249 |
self.layer.SetClassification(self.classification) |
self.layer.SetClassification(self.classification) |
250 |
self.layer.UnsetModified() |
self.layer.UnsetModified() |
251 |
self.layer.Subscribe(LAYER_SHAPESTORE_REPLACED, |
self.layer.Subscribe(LAYER_SHAPESTORE_REPLACED, |
267 |
""" |
""" |
268 |
cls = self.layer.GetClassification() |
cls = self.layer.GetClassification() |
269 |
self.assert_(cls is self.classification) |
self.assert_(cls is self.classification) |
270 |
self.assertEquals(cls.GetField(), "CLPTLABEL") |
field = self.layer.GetClassificationColumn() |
271 |
self.assertEquals(cls.GetFieldType(), FIELDTYPE_STRING) |
self.assertEquals(field, "CLPTLABEL") |
272 |
|
self.assertEquals(self.layer.GetFieldType(field), FIELDTYPE_STRING) |
273 |
self.assertEquals(self.layer.GetClassification().GetNumGroups(), 1) |
self.assertEquals(self.layer.GetClassification().GetNumGroups(), 1) |
274 |
self.failIf(self.layer.WasModified()) |
self.failIf(self.layer.WasModified()) |
275 |
|
|
312 |
(self.layer, LAYER_SHAPESTORE_REPLACED)]) |
(self.layer, LAYER_SHAPESTORE_REPLACED)]) |
313 |
|
|
314 |
|
|
315 |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
class TestLayerModification(unittest.TestCase, support.SubscriberMixin): |
316 |
|
|
317 |
"""Test cases for Layer method that modify the layer. |
"""Test cases for Layer method that modify the layer. |
318 |
""" |
""" |
332 |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
333 |
self.subscribe_with_params, |
self.subscribe_with_params, |
334 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
335 |
|
self.layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params, |
336 |
|
LAYER_CHANGED) |
337 |
|
|
338 |
def tearDown(self): |
def tearDown(self): |
339 |
"""Clear the list of received messages and explictly destroy self.layer |
"""Clear the list of received messages and explictly destroy self.layer |
344 |
self.session = None |
self.session = None |
345 |
self.clear_messages() |
self.clear_messages() |
346 |
|
|
347 |
def test_initial_settings(self): |
def test_sanity(self): |
348 |
"""Test Layer's initial legend attributes""" |
"""TestLayerModification Sanity Checks""" |
349 |
# test default settings |
# test default settings |
350 |
self.failIf(self.layer.WasModified()) |
self.failIf(self.layer.WasModified()) |
|
#self.assertEquals(self.layer.fill, None) |
|
|
#self.assertEquals(self.layer.stroke.hex(), "#000000") |
|
|
#self.assertEquals(self.layer.stroke_width, 1) |
|
351 |
self.assertEquals(self.layer.Visible(), 1) |
self.assertEquals(self.layer.Visible(), 1) |
352 |
# no messages should have been produced |
# no messages should have been produced |
353 |
self.check_messages([]) |
self.check_messages([]) |
362 |
# the layer. |
# the layer. |
363 |
self.failIf(self.layer.WasModified()) |
self.failIf(self.layer.WasModified()) |
364 |
|
|
365 |
|
def test_set_classification(self): |
366 |
|
"""Test Layer.SetClassification""" |
367 |
|
classification = Classification() |
368 |
|
classification.AppendGroup(ClassGroupRange((0.0, 0.1))) |
369 |
|
|
370 |
|
self.layer.SetClassification(classification) |
371 |
|
self.layer.SetClassificationColumn("AREA") |
372 |
|
|
373 |
|
self.check_messages([(self.layer, LAYER_CHANGED), |
374 |
|
(self.layer, LAYER_CHANGED)]) |
375 |
|
self.failUnless(self.layer.WasModified()) |
376 |
|
|
377 |
|
self.clear_messages() |
378 |
|
self.layer.UnsetModified() |
379 |
|
|
380 |
|
# change only the classification column. This should issue a |
381 |
|
# LAYER_CHANGED message as well. |
382 |
|
self.layer.SetClassificationColumn("PERIMETER") |
383 |
|
|
384 |
|
self.check_messages([(self.layer, LAYER_CHANGED)]) |
385 |
|
self.failUnless(self.layer.WasModified()) |
386 |
|
|
387 |
|
|
388 |
# |
# |
389 |
# the tree info now contains Color objects which are difficult to test |
# the tree info now contains Color objects which are difficult to test |
390 |
# |
# |