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