1 |
bh |
1536 |
# 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 |
|
|
"""Tests for DerivedShapeStore""" |
9 |
|
|
|
10 |
|
|
__version__ = "$Revision$" |
11 |
|
|
# $Source$ |
12 |
|
|
# $Id$ |
13 |
|
|
|
14 |
|
|
import os |
15 |
|
|
|
16 |
|
|
import unittest |
17 |
|
|
|
18 |
|
|
import support |
19 |
|
|
support.initthuban() |
20 |
|
|
|
21 |
bh |
1564 |
from Thuban.Model.data import DerivedShapeStore, ShapefileStore, \ |
22 |
|
|
SHAPETYPE_ARC, RAW_SHAPEFILE |
23 |
bh |
1593 |
|
24 |
bh |
1536 |
from Thuban.Model.session import Session |
25 |
|
|
from Thuban.Model.table import MemoryTable, \ |
26 |
|
|
FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
class TestDerivedShapeStore(unittest.TestCase, support.FloatComparisonMixin): |
30 |
|
|
|
31 |
|
|
def setUp(self): |
32 |
|
|
"""Initialize self.session""" |
33 |
|
|
self.session = Session("Test Session") |
34 |
|
|
self.filename = os.path.join("..", "Data", "iceland", |
35 |
|
|
"roads-line.shp") |
36 |
|
|
self.store = ShapefileStore(self.session, self.filename) |
37 |
|
|
|
38 |
|
|
self.table = MemoryTable([("type", FIELDTYPE_STRING), |
39 |
|
|
("value", FIELDTYPE_DOUBLE), |
40 |
|
|
("code", FIELDTYPE_INT)], |
41 |
|
|
[("UNKNOWN", 0.0, 0)] * 839) |
42 |
|
|
self.derived = DerivedShapeStore(self.store, self.table) |
43 |
|
|
|
44 |
|
|
def tearDown(self): |
45 |
|
|
"""Call self.session.Destroy() and reset self.session to None""" |
46 |
|
|
self.session.Destroy() |
47 |
|
|
self.session = None |
48 |
|
|
|
49 |
|
|
def test_dependencies(self): |
50 |
|
|
"""Test DerivedShapeStore dependencies""" |
51 |
|
|
# The shapestore itself depends on nothing else |
52 |
|
|
self.assertEquals(self.derived.Dependencies(), |
53 |
|
|
(self.store, self.table)) |
54 |
|
|
|
55 |
|
|
def test_orig_shapestore(self): |
56 |
|
|
"""Test DerivedShapeStore.OrigShapeStore()""" |
57 |
|
|
self.assertEquals(self.derived.OrigShapeStore(), self.store) |
58 |
|
|
|
59 |
|
|
def test_shape_type(self): |
60 |
|
|
"""Test DerivedShapeStore.ShapeType() with arc shapes""" |
61 |
|
|
self.assertEquals(self.derived.ShapeType(), SHAPETYPE_ARC) |
62 |
|
|
|
63 |
bh |
1564 |
def test_raw_format(self): |
64 |
|
|
"""Test DerivedShapeStore.RawShapeFormat() with shapefiles""" |
65 |
|
|
self.assertEquals(self.derived.RawShapeFormat(), RAW_SHAPEFILE) |
66 |
|
|
|
67 |
bh |
1536 |
def test_boundingbox(self): |
68 |
|
|
"""Test DerivedShapeStore.BoundingBox() with arc shapes""" |
69 |
|
|
self.assertFloatSeqEqual(self.derived.BoundingBox(), |
70 |
|
|
[-24.450359344482422, 63.426830291748047, |
71 |
|
|
-13.55668830871582, 66.520111083984375]) |
72 |
|
|
|
73 |
|
|
def test_num_shapes(self): |
74 |
|
|
"""Test DerivedShapeStore.NumShapes() with arc shapes""" |
75 |
|
|
self.assertEquals(self.derived.NumShapes(), 839) |
76 |
|
|
|
77 |
|
|
def test_shapes_in_region(self): |
78 |
|
|
"""Test DerivedShapeStore.ShapesInRegion() with arc shapes""" |
79 |
bh |
1593 |
shapes = self.derived.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)) |
80 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
81 |
bh |
1536 |
[613, 726, 838]) |
82 |
|
|
|
83 |
bh |
1593 |
def test_all_shapes(self): |
84 |
|
|
"""Test DerivedShapeStore.AllShapes()""" |
85 |
|
|
self.assertEquals([s.ShapeID() for s in self.store.AllShapes()], |
86 |
|
|
range(self.store.NumShapes())) |
87 |
|
|
|
88 |
bh |
1536 |
def test_shape(self): |
89 |
|
|
"""Test DerivedShapeStore.Shape() with arc shapes""" |
90 |
bh |
1551 |
self.assertPointListEquals(self.derived.Shape(32).Points(), |
91 |
bh |
1593 |
[[(-15.08217430114746, 66.2773818969726), |
92 |
|
|
(-15.02635002136230, 66.2733917236328)]]) |
93 |
|
|
def test_shape_shapeid(self): |
94 |
|
|
"""Test DerivedShapeStore.Shape(i).ShapeID()""" |
95 |
|
|
self.assertEquals(self.store.Shape(5).ShapeID(), 5) |
96 |
bh |
1536 |
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
class TestDerivedShapeStoreExceptions(unittest.TestCase): |
100 |
|
|
|
101 |
|
|
"""Test DerivedShapeStore exceptions""" |
102 |
|
|
|
103 |
bh |
1784 |
def tearDown(self): |
104 |
|
|
if hasattr(self, "session"): |
105 |
|
|
self.session.Destroy() |
106 |
|
|
self.session = None |
107 |
|
|
|
108 |
bh |
1536 |
def test_table_with_wrong_size(self): |
109 |
|
|
"""Test DerivedShapeStore() with a table with the wrong number of lines |
110 |
|
|
""" |
111 |
|
|
filename = os.path.join("..", "Data", "iceland", "roads-line.shp") |
112 |
bh |
1682 |
session = self.session = Session("TestDerivedShapeStore Session") |
113 |
bh |
1536 |
store = session.OpenShapefile(filename) |
114 |
|
|
|
115 |
|
|
table = MemoryTable([("type", FIELDTYPE_STRING), |
116 |
|
|
("value", FIELDTYPE_DOUBLE), |
117 |
|
|
("code", FIELDTYPE_INT)], |
118 |
|
|
[("UNKNOWN", 0.0, 0), |
119 |
|
|
("Foo", 0.5, -1), |
120 |
|
|
("Foo", 0.25, 100), |
121 |
|
|
("bar", 1e10, 17)]) |
122 |
|
|
|
123 |
|
|
# Trying to create a DerivedShapeStore where the number of lines |
124 |
|
|
# in the table is not the same as the number of shapes in the |
125 |
|
|
# shapefile raises a ValueError |
126 |
|
|
self.assertRaises(ValueError, DerivedShapeStore, store, table) |
127 |
bh |
1682 |
|
128 |
|
|
|
129 |
|
|
if __name__ == "__main__": |
130 |
|
|
support.run_tests() |