/[thuban]/branches/WIP-pyshapelib-bramz/test/test_layer.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/test_layer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 723 - (show annotations)
Thu Apr 24 15:31:53 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_layer.py
File MIME type: text/x-python
File size: 7779 byte(s)
First step towards table management. Introduce a simple data
abstraction so that we replace the data a layer uses more easily
in the next step.

* Thuban/Model/data.py: New file with a simple data abstraction
that bundles shapefile and dbffile into one object.

* Thuban/Model/session.py (Session.OpenShapefile): New method to
open shapefiles and return a shape store object

* Thuban/Model/layer.py (Layer.__init__): Pass the data as a store
object instead of a shapefile filename. This introduces a new
instance variable store holding the datastore. For intermediate
backwards compatibility keep the old instance variables.
(open_shapefile): Removed. No longer needed with the shape store.
(Layer.SetShapeStore, Layer.ShapeStore): New methods to set and
get the shape store used by a layer.
(Layer.Destroy): No need to explicitly destroy the shapefile or
table anymore.

* Thuban/UI/mainwindow.py (MainWindow.AddLayer)
(MainWindow.AddLayer): Use the session's OpenShapefile method to
open shapefiles

* Thuban/Model/load.py (ProcessSession.start_layer): Use the
session's OpenShapefile method to open shapefiles

* test/test_classification.py
(TestClassification.test_classification): Use the session's
OpenShapefile method to open shapefiles and build the filename in
a more platform independed way

* test/test_layer.py (TestLayer.setUp, TestLayer.tearDown):
Implement to have a session to use in the tests
(TestLayer.test_arc_layer, TestLayer.test_polygon_layer)
(TestLayer.test_point_layer, TestLayer.test_empty_layer): Use the
session's OpenShapefile method to open shapefiles
(TestLayerLegend.setUp): Instantiate a session so that we can use
it to open shapefiles.
(TestLayerLegend.tearDown): Make sure that all references to
layers and session are removed otherwise we may get a resource
leak

* test/test_map.py (TestMapAddLayer.test_add_layer)
(TestMapWithContents.setUp): Instantiate a session so that we can
use it to open shapefiles.
(TestMapWithContents.tearDown): Make sure that all references to
layers, maps and sessions are removed otherwise we may get a
resource leak
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_save.py (SaveSessionTest.testSingleLayer): Use the
session's OpenShapefile method to open shapefiles
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_selection.py (TestSelection.tearDown): Make sure that
all references to the session and the selection are removed
otherwise we may get a resource leak
(TestSelection.get_layer): Instantiate a session so that we can
use it to open shapefiles.
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_session.py (TestSessionBase.tearDown)
(TestSessionWithContent.tearDown): Make sure that all references
to the session and layers are removed otherwise we may get a
resource leak
(TestSessionWithContent.setUp): Use the session's OpenShapefile
method to open shapefiles

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26