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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (show annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 4362 byte(s)
made a copy
1 # 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 """Mock geometric/geographic objects"""
9
10 from __future__ import generators
11
12 __version__ = "$Revision$"
13 # $Source$
14 # $Id$
15
16
17 class SimpleShape:
18
19 def __init__(self, shapeid, points):
20 self.shapeid = shapeid
21 self.points = points
22
23 def Points(self):
24 return self.points
25
26 def ShapeID(self):
27 return self.shapeid
28
29 def RawData(self):
30 return self.points
31
32 def compute_bbox(self):
33 xs = []
34 ys = []
35 for part in self.Points():
36 for x, y in part:
37 xs.append(x)
38 ys.append(y)
39 return (min(xs), min(ys), max(xs), max(ys))
40
41
42 class SimpleShapeStore:
43
44 """A simple shapestore object which holds its data in memory"""
45
46 def __init__(self, shapetype, shapes, table):
47 """Initialize the simple shapestore object.
48
49 The shapetype should be one of the predefined SHAPETYPE_*
50 constants. shapes is a list of shape definitions. Each
51 definitions is a list of lists of tuples as returned by the
52 Shape's Points() method. The table argument should be an object
53 implementing the table interface and contain with one row for
54 each shape.
55 """
56 self.shapetype = shapetype
57 self.shapes = shapes
58 self.table = table
59 assert table.NumRows() == len(shapes)
60
61 def ShapeType(self):
62 return self.shapetype
63
64 def Table(self):
65 return self.table
66
67 def NumShapes(self):
68 return len(self.shapes)
69
70 def Shape(self, index):
71 return SimpleShape(index, self.shapes[index])
72
73 def BoundingBox(self):
74 xs = []
75 ys = []
76 for shape in self.shapes:
77 for part in shape:
78 for x, y in part:
79 xs.append(x)
80 ys.append(y)
81 return (min(xs), min(ys), max(xs), max(ys))
82
83 def ShapesInRegion(self, bbox):
84 left, bottom, right, top = bbox
85 if left > right:
86 left, right = right, left
87 if bottom > top:
88 bottom, top = top, bottom
89 for i in xrange(len(self.shapes)):
90 shape = SimpleShape(i, self.shapes[i])
91 sleft, sbottom, sright, stop = shape.compute_bbox()
92 if (left <= sright and right >= sleft
93 and top >= sbottom and bottom <= stop):
94 yield shape
95
96 def AllShapes(self):
97 for i in xrange(len(self.shapes)):
98 yield SimpleShape(i, self.shapes[i])
99
100
101 class AffineProjection:
102
103 """Projection-like object implemented with an affine transformation
104
105 The transformation matrix is defined by a list of six floats:
106
107 [m11, m21, m12, m22, v1, v2]
108
109 This list is essentially in the same form as used in PostScript.
110
111 This interpreted as the following transformation of (x, y):
112
113 / x \ / m11 m12 \ / x \ / v1 \
114 T * | | = | | | | + | |
115 \ y / \ m21 m22 / \ y / \ v2 /
116
117 or, in homogeneous coordinates:
118
119 / m11 m12 v1 \ / x \
120 | | | |
121 ^= | m21 m22 v2 | | y |
122 | | | |
123 \ 0 0 1 / \ 1 /
124
125 Obviously this is not a real geographic projection, but it's useful
126 in test cases because it's simple and the result is easily computed
127 in advance.
128 """
129
130 def __init__(self, coeff):
131 self.coeff = coeff
132
133 # determine the inverse transformation right away. We trust that
134 # an inverse exist for the transformations used in the Thuban
135 # test suite.
136 m11, m21, m12, m22, v1, v2 = coeff
137 det = float(m11 * m22 - m12 * m21)
138 n11 = m22 / det
139 n12 = -m12 / det
140 n21 = -m21 / det
141 n22 = m11 / det
142 self.inv = [n11, n21, n12, n22, -n11*v1 - n12*v2, -n21*v1 - n22*v2]
143
144 def _apply(self, matrix, x, y):
145 m11, m21, m12, m22, v1, v2 = matrix
146 return (m11 * x + m12 * y + v1), (m21 * x + m22 * y + v2)
147
148 def Forward(self, x, y):
149 return self._apply(self.coeff, x, y)
150
151 def Inverse(self, x, y):
152 return self._apply(self.inv, x, y)

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26