1 |
bh |
1537 |
# Copyright (C) 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 the software for details. |
7 |
|
|
|
8 |
|
|
"""Test cases for ShapefileStore""" |
9 |
|
|
|
10 |
|
|
__version__ = "$Revision$" |
11 |
|
|
# $Source$ |
12 |
|
|
# $Id$ |
13 |
|
|
|
14 |
|
|
import os |
15 |
|
|
import unittest |
16 |
|
|
|
17 |
|
|
import support |
18 |
|
|
support.initthuban() |
19 |
|
|
|
20 |
|
|
from Thuban.Model.data import ShapefileStore |
21 |
|
|
from Thuban.Model.session import Session |
22 |
|
|
from Thuban.Model.data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
class TestShapefileStore(unittest.TestCase): |
26 |
|
|
|
27 |
|
|
"""Test cases for ShapefileStore""" |
28 |
|
|
|
29 |
|
|
def setUp(self): |
30 |
|
|
"""Initialize self.session""" |
31 |
|
|
self.session = Session("Test Session") |
32 |
|
|
self.filename = os.path.join("..", "Data", "iceland", |
33 |
|
|
"roads-line.shp") |
34 |
|
|
self.store = ShapefileStore(self.session, self.filename) |
35 |
|
|
|
36 |
|
|
def tearDown(self): |
37 |
|
|
"""Call self.session.Destroy() and reset self.session to None""" |
38 |
|
|
self.session.Destroy() |
39 |
|
|
self.session = None |
40 |
|
|
|
41 |
|
|
def test_accessors(self): |
42 |
|
|
"""Test ShapefileStore accessors""" |
43 |
|
|
self.assertEquals(self.store.FileName(), |
44 |
|
|
os.path.abspath(self.filename)) |
45 |
|
|
|
46 |
|
|
# The filetype of a shapefile is "shapefile" |
47 |
|
|
self.assertEquals(self.store.FileType(), "shapefile") |
48 |
|
|
|
49 |
|
|
def test_dependencies(self): |
50 |
|
|
"""Test ShapefileStore and ShapeTable dependencies""" |
51 |
|
|
# The shapestore itself depends on nothing else |
52 |
|
|
self.assertEquals(self.store.Dependencies(), ()) |
53 |
|
|
|
54 |
|
|
# The shapestore's table depends on the shapestore |
55 |
|
|
self.assertEquals(self.store.Table().Dependencies(), (self.store,)) |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
def test_orig_shapestore(self): |
59 |
|
|
"""Test ShapefileStore.OrigShapeStore()""" |
60 |
|
|
self.assertEquals(self.store.OrigShapeStore(), None) |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
class ShapefileStoreTests(unittest.TestCase, support.FloatComparisonMixin): |
65 |
|
|
|
66 |
bh |
1551 |
def assertPointListEquals(self, test, value): |
67 |
|
|
"""Assert equality of two lists of lists of tuples of float""" |
68 |
bh |
1537 |
for i in range(len(test)): |
69 |
bh |
1551 |
self.assertEquals(len(test[i]), len(value[i])) |
70 |
|
|
for j in range(len(test[i])): |
71 |
|
|
self.assertFloatSeqEqual(test[i][j], value[i][j]) |
72 |
bh |
1537 |
|
73 |
|
|
class TestShapefileStoreArc(ShapefileStoreTests): |
74 |
|
|
|
75 |
|
|
"""Test cases for ShapefileStore with arc shapes""" |
76 |
|
|
|
77 |
|
|
def setUp(self): |
78 |
|
|
"""Initialize self.session""" |
79 |
|
|
self.session = Session("Test Session") |
80 |
|
|
self.filename = os.path.join("..", "Data", "iceland", |
81 |
|
|
"roads-line.shp") |
82 |
|
|
self.store = ShapefileStore(self.session, self.filename) |
83 |
|
|
|
84 |
|
|
def tearDown(self): |
85 |
|
|
"""Call self.session.Destroy() and reset self.session to None""" |
86 |
|
|
self.session.Destroy() |
87 |
|
|
self.session = None |
88 |
|
|
|
89 |
|
|
def test_shape_type(self): |
90 |
|
|
"""Test ShapefileStore.ShapeType() with arc shapes""" |
91 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_ARC) |
92 |
|
|
|
93 |
|
|
def test_boundingbox(self): |
94 |
|
|
"""Test ShapefileStore.BoundingBox() with arc shapes""" |
95 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
96 |
|
|
[-24.450359344482422, 63.426830291748047, |
97 |
|
|
-13.55668830871582, 66.520111083984375]) |
98 |
|
|
|
99 |
|
|
def test_num_shapes(self): |
100 |
|
|
"""Test ShapefileStore.NumShapes() with arc shapes""" |
101 |
|
|
self.assertEquals(self.store.NumShapes(), 839) |
102 |
|
|
|
103 |
|
|
def test_shapes_in_region(self): |
104 |
|
|
"""Test ShapefileStore.ShapesInRegion() with arc shapes""" |
105 |
|
|
self.assertEquals(self.store.ShapesInRegion((-24.0, 64.0, |
106 |
|
|
-23.75, 64.25)), |
107 |
|
|
[613, 726, 838]) |
108 |
|
|
|
109 |
|
|
def test_shape(self): |
110 |
|
|
"""Test ShapefileStore.Shape() with arc shapes""" |
111 |
bh |
1551 |
self.assertPointListEquals(self.store.Shape(32).Points(), |
112 |
|
|
[[(-15.0821743011474, 66.2773818969726), |
113 |
|
|
(-15.0263500213623, 66.2733917236328)]]) |
114 |
bh |
1537 |
|
115 |
|
|
|
116 |
|
|
class TestShapefileStorePolygon(ShapefileStoreTests): |
117 |
|
|
|
118 |
|
|
"""Test cases for ShapefileStore with plygon shapes""" |
119 |
|
|
|
120 |
|
|
def setUp(self): |
121 |
|
|
"""Initialize self.session""" |
122 |
|
|
self.session = Session("Test Session") |
123 |
|
|
"""Test ShapeStore with polygon shapes""" |
124 |
|
|
self.filename = os.path.join("..", "Data", "iceland", |
125 |
|
|
"political.shp") |
126 |
|
|
self.store = ShapefileStore(self.session, self.filename) |
127 |
|
|
|
128 |
|
|
def tearDown(self): |
129 |
|
|
"""Call self.session.Destroy() and reset self.session to None""" |
130 |
|
|
self.session.Destroy() |
131 |
|
|
self.session = None |
132 |
|
|
|
133 |
|
|
def test_shape_type(self): |
134 |
|
|
"""Test ShapeStore.ShapeType() with polygon shapes""" |
135 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POLYGON) |
136 |
|
|
|
137 |
|
|
def test_boundingbox(self): |
138 |
|
|
"""Test ShapefileStore.BoundingBox() with polygon shapes""" |
139 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
140 |
|
|
[-24.546524047851562, 63.286754608154297, |
141 |
|
|
-13.495815277099609, 66.563774108886719]) |
142 |
|
|
|
143 |
|
|
def test_num_shapes(self): |
144 |
|
|
"""Test ShapefileStore.NumShapes() with polygon shapes""" |
145 |
|
|
self.assertEquals(self.store.NumShapes(), 156) |
146 |
|
|
|
147 |
|
|
def test_shapes_in_region(self): |
148 |
|
|
"""Test ShapefileStore.ShapesInRegion() with polygon shapes""" |
149 |
|
|
self.assertEquals(self.store.ShapesInRegion((-24.0, 64.0, |
150 |
|
|
-23.9, 64.1)), |
151 |
|
|
[91, 92, 144, 146, 148, 150, 152, 153]) |
152 |
|
|
|
153 |
|
|
def test_shape(self): |
154 |
|
|
"""Test ShapefileStore.Shape() with polygon shapes""" |
155 |
bh |
1551 |
self.assertPointListEquals(self.store.Shape(4).Points(), |
156 |
|
|
[[(-22.40639114379882, 64.714111328125), |
157 |
|
|
(-22.41621208190918, 64.716003417968), |
158 |
|
|
(-22.40605163574218, 64.719200134277), |
159 |
|
|
(-22.40639114379882, 64.714111328125)]]) |
160 |
bh |
1537 |
|
161 |
|
|
class TestShapefileStorePoint(ShapefileStoreTests): |
162 |
|
|
|
163 |
|
|
"""Test cases for ShapefileStore with plygon shapes""" |
164 |
|
|
|
165 |
|
|
def setUp(self): |
166 |
|
|
"""Initialize self.session""" |
167 |
|
|
self.session = Session("Test Session") |
168 |
|
|
"""Test ShapeStore with point shapes""" |
169 |
|
|
self.filename = os.path.join("..", "Data", "iceland", |
170 |
|
|
"cultural_landmark-point.shp") |
171 |
|
|
self.store = ShapefileStore(self.session, self.filename) |
172 |
|
|
|
173 |
|
|
def tearDown(self): |
174 |
|
|
"""Call self.session.Destroy() and reset self.session to None""" |
175 |
|
|
self.session.Destroy() |
176 |
|
|
self.session = None |
177 |
|
|
|
178 |
|
|
def test_shape_type(self): |
179 |
|
|
"""Test ShapeStore.ShapeType() with point shapes""" |
180 |
|
|
self.assertEquals(self.store.ShapeType(), SHAPETYPE_POINT) |
181 |
|
|
|
182 |
|
|
def test_boundingbox(self): |
183 |
|
|
"""Test ShapefileStore.BoundingBox() with point shapes""" |
184 |
|
|
self.assertFloatSeqEqual(self.store.BoundingBox(), |
185 |
|
|
[-23.806047439575195, 63.405960083007812, |
186 |
|
|
-15.12291431427002, 66.36572265625]) |
187 |
|
|
|
188 |
|
|
def test_num_shapes(self): |
189 |
|
|
"""Test ShapefileStore.NumShapes() with point shapes""" |
190 |
|
|
self.assertEquals(self.store.NumShapes(), 34) |
191 |
|
|
|
192 |
|
|
def test_shapes_in_region(self): |
193 |
|
|
"""Test ShapefileStore.ShapesInRegion() with point shapes""" |
194 |
|
|
self.assertEquals(self.store.ShapesInRegion((-24.0, 64.0, |
195 |
|
|
-23.80, 64.1)), |
196 |
|
|
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
197 |
|
|
|
198 |
|
|
def test_shape(self): |
199 |
|
|
"""Test ShapefileStore.Shape() with point shapes""" |
200 |
bh |
1551 |
self.assertPointListEquals(self.store.Shape(0).Points(), |
201 |
|
|
[[(-22.711074829101562, 66.36572265625)]]) |