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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1585 - (show annotations)
Fri Aug 15 10:26:40 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_baserenderer.py
File MIME type: text/x-python
File size: 16230 byte(s)
Move some of the mock objects in test_baserenderer into their own
module so they can easily be used from other tests

* test/mockgeo.py: New test helper module with some mock objects
for geometry related things like shapes, shapestores and
projections.

* test/test_mockgeo.py: New. Tests for the new helper module

* test/test_baserenderer.py: Some of the mock-objects are in mockgeo now.

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 """Test Thuban.Model.baserenderer"""
9
10 __version__ = "$Revision$"
11 # $Source$
12 # $Id$
13
14 import os
15 import binascii
16 import unittest
17
18 from mockgeo import SimpleShapeStore
19 import support
20 support.initthuban()
21
22 from Thuban.Model.color import Transparent, Color
23 from Thuban.Model.data import SHAPETYPE_ARC, SHAPETYPE_POLYGON, SHAPETYPE_POINT
24 from Thuban.Model.map import Map
25 from Thuban.Model.layer import Layer, RasterLayer
26 from Thuban.Model.table import MemoryTable, \
27 FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING
28 from Thuban.Model.classification import ClassGroupSingleton
29 import Thuban.Model.resource
30
31
32 from Thuban.UI.baserenderer import BaseRenderer
33
34
35 class MockDC:
36
37 def __init__(self, size = None):
38 self.calls = []
39 self.size = size
40
41 def GetSizeTuple(self):
42 return self.size
43
44 def __getattr__(self, attr):
45 def method(*args):
46 self.calls.append((attr,) + args)
47 return method
48
49 class P:
50
51 """A simple point"""
52
53 def __init__(self, x, y):
54 self.x = x
55 self.y = y
56
57 def __eq__(self, other):
58 return self.x == other.x and self.y == other.y
59
60 def __ne__(self, other):
61 return self.x != other.x and self.y != other.y
62
63 def __repr__(self):
64 return "P(%r, %r)" % (self.x, self.y)
65
66
67 class SimpleRenderer(BaseRenderer):
68
69 TRANSPARENT_PEN = ("pen", Transparent)
70 TRANSPARENT_BRUSH = ("brush", Transparent)
71
72 def make_point(self, x, y):
73 return P(x, y)
74
75 def tools_for_property(self, prop):
76 fill = prop.GetFill()
77 brush = ("brush", fill)
78
79 stroke = prop.GetLineColor()
80 stroke_width = prop.GetLineWidth()
81 if stroke is Transparent:
82 pen = ("pen", Transparent)
83 else:
84 pen = ("pen", stroke, stroke_width)
85
86 return pen, brush
87
88 def label_font(self):
89 return "label font"
90
91 def draw_raster_data(self, data):
92 self.raster_data = data
93
94
95 class MockProjection:
96
97 """Objects that look like projections but simply apply non-uniform scalings
98 """
99
100 def __init__(self, xscale, yscale):
101 self.xscale = float(xscale)
102 self.yscale = float(yscale)
103
104 def Forward(self, x, y):
105 return (x * self.xscale, y * self.yscale)
106
107 def Inverse(self, x, y):
108 return (x / self.xscale, y / self.yscale)
109
110
111 class TestBaseRenderer(unittest.TestCase):
112
113 def setUp(self):
114 """Set self.to_destroy to an empty list
115
116 Test should put all objects whose Destroy should be called atunittest.main
117 the end into this list so that they're destroyed in tearDown
118 """
119 self.to_destroy = []
120
121 def tearDown(self):
122 for obj in self.to_destroy:
123 obj.Destroy()
124
125 def test_arc_no_projection(self):
126 """Test BaseRenderer with arc layer and no projections"""
127 table = MemoryTable([("type", FIELDTYPE_STRING),
128 ("value", FIELDTYPE_DOUBLE),
129 ("code", FIELDTYPE_INT)],
130 [("UNKNOWN", 0.0, 0)])
131 shapes = [[[(0, 0), (10, 10)]]]
132 store = SimpleShapeStore(SHAPETYPE_ARC, shapes, table)
133
134 map = Map("TestBaseRenderer")
135 self.to_destroy.append(map)
136 layer = Layer("arc layer", store)
137 map.AddLayer(layer)
138
139 dc = MockDC()
140 renderer = SimpleRenderer(dc, 2, (10, 10))
141
142 renderer.render_map(map)
143
144 self.assertEquals(dc.calls,
145 [('BeginDrawing',),
146 ('SetBrush', ('brush', Transparent)),
147 ('SetPen', ('pen', Color(0, 0, 0), 1)),
148 ('DrawLines', [P(10, 10), P(30, -10)]),
149 ('SetFont', "label font"),
150 ('EndDrawing',)])
151
152 def test_polygon_no_projection(self):
153 """Test BaseRenderer with polygon layer and no projections"""
154 table = MemoryTable([("type", FIELDTYPE_STRING),
155 ("value", FIELDTYPE_DOUBLE),
156 ("code", FIELDTYPE_INT)],
157 [("UNKNOWN", 0.0, 0)])
158 shapes = [[[(0, 0), (10, 10), (10, 0), (0, 0)]]]
159 store = SimpleShapeStore(SHAPETYPE_POLYGON, shapes, table)
160
161 map = Map("TestBaseRenderer")
162 layer = Layer("polygon layer", store)
163 prop = layer.GetClassification().GetDefaultGroup().GetProperties()
164 prop.SetFill(Color(1, 1, 1))
165
166 map.AddLayer(layer)
167 self.to_destroy.append(map)
168
169 dc = MockDC()
170 renderer = SimpleRenderer(dc, 2, (10, 10))
171
172 renderer.render_map(map)
173
174 self.assertEquals(dc.calls,
175 [('BeginDrawing',),
176 ('SetBrush', ('brush', Color(1, 1, 1))),
177 ('SetPen', ('pen', Transparent)),
178 ('DrawPolygon', [P(10, 10), P(30, -10), P(30, 10),
179 P(10, 10)]),
180 ('SetBrush', ('brush', Transparent)),
181 ('SetPen', ('pen', Color(0, 0, 0), 1)),
182 ('DrawLines', [P(10, 10), P(30, -10), P(30, 10),
183 P(10, 10)]),
184 ('SetFont', "label font"),
185 ('EndDrawing',)])
186
187 def test_complex_polygon(self):
188 """Test BaseRenderer with complex polygon and no projections"""
189 # A square inside a sqare. This has to be drawn with at least a
190 # draw polygon call and two draw lines calls.
191 shapes = [[[(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
192 [(2, 2), (8, 2), (8, 8), (2, 8), (2, 2)]]]
193
194 table = MemoryTable([("type", FIELDTYPE_STRING),
195 ("value", FIELDTYPE_DOUBLE),
196 ("code", FIELDTYPE_INT)],
197 [("UNKNOWN", 0.0, 0)])
198 store = SimpleShapeStore(SHAPETYPE_POLYGON, shapes, table)
199
200 map = Map("TestBaseRenderer")
201 layer = Layer("polygon layer", store)
202 prop = layer.GetClassification().GetDefaultGroup().GetProperties()
203 prop.SetFill(Color(1, 1, 1))
204
205 map.AddLayer(layer)
206 self.to_destroy.append(map)
207
208 dc = MockDC()
209 renderer = SimpleRenderer(dc, 2, (10, 10))
210
211 renderer.render_map(map)
212
213 self.assertEquals(dc.calls,
214 [('BeginDrawing',),
215 ('SetBrush', ('brush', Color(1, 1, 1))),
216 ('SetPen', ('pen', Transparent)),
217 ('DrawPolygon',
218 [P(10, 10), P(10, -10), P(30, -10), P(30, 10),
219 P(10, 10),
220 P(14, 6), P(26, 6), P(26, -6), P(14, -6),
221 P(14, 6),
222 P(10, 10)]),
223 ('SetBrush', ('brush', Transparent)),
224 ('SetPen', ('pen', Color(0, 0, 0), 1)),
225 ('DrawLines', [P(10, 10), P(10, -10), P(30, -10),
226 P(30, 10), P(10, 10)]),
227 ('DrawLines', [P(14, 6), P(26, 6), P(26, -6),
228 P(14, -6), P(14, 6)]),
229 ('SetFont', "label font"),
230 ('EndDrawing',)])
231
232 def test_point_no_projection(self):
233 """Test BaseRenderer with point layer and no projections"""
234 table = MemoryTable([("type", FIELDTYPE_STRING),
235 ("value", FIELDTYPE_DOUBLE),
236 ("code", FIELDTYPE_INT)],
237 [("UNKNOWN", 0.0, 0),
238 ("UNKNOWN", 0.0, 1)])
239 shapes = [[[(0, 0)]], [[(10, 10)]]]
240 store = SimpleShapeStore(SHAPETYPE_POINT, shapes, table)
241
242 map = Map("TestBaseRenderer")
243 layer = Layer("point layer", store)
244 map.AddLayer(layer)
245 self.to_destroy.append(map)
246
247 dc = MockDC()
248 renderer = SimpleRenderer(dc, 2, (10, 10))
249
250 renderer.render_map(map)
251
252 self.assertEquals(dc.calls,
253 [('BeginDrawing',),
254 ('SetBrush', ('brush', Transparent)),
255 ('SetPen', ('pen', Color(0, 0, 0), 1)),
256 ('DrawEllipse', 5, 5, 10, 10),
257 ('SetBrush', ('brush', Transparent)),
258 ('SetPen', ('pen', Color(0, 0, 0), 1)),
259 ('DrawEllipse', 25, -15, 10, 10),
260 ('SetFont', "label font"),
261 ('EndDrawing',)])
262
263 def test_raster_no_projection(self):
264 """Test BaseRenderer with raster layer and no projections
265
266 This test is very simple minded and perhaps can easily fail due
267 to round-off errors. It simply compares the complete BMP file
268 returned by gdalwarp.ProjectRasterFile to a BMP file data.
269 """
270 if not Thuban.Model.resource.has_gdal_support():
271 raise support.SkipTest("No gdal support")
272
273 map = Map("TestBaseRenderer")
274
275 layer = RasterLayer("raster layer",
276 os.path.join("..", "Data", "iceland",
277 "island.tif"))
278 map.AddLayer(layer)
279 self.to_destroy.append(map)
280
281 dc = MockDC(size = (20, 20))
282 renderer = SimpleRenderer(dc, 34, (800, 2250))
283
284 renderer.render_map(map)
285
286 # The following commented out code block can be used to generate
287 # the base64 coded reference image data
288 #hexed = binascii.b2a_base64(renderer.raster_data)
289 #while hexed:
290 # print repr(hexed[:65])
291 # hexed = hexed[65:]
292
293 # The reference data as a base64 coded BMP image
294 raw_data = binascii.a2b_base64(
295 'Qk3GBQAAAAAAADYEAAAoAAAAFAAAABQAAAABAAgAAAAAAJABAAAAAAAAAAAAAAABA'
296 'AAAAAAAApYOAALGAgAGfjoAHmZyACZ2egAujo4AArICAE66GgACngIA5mZSAJqONg'
297 'ACzgIAAoIyABZqZgAO4uYAAtICAAKqAgAScloAAtYCADKepgAS2t4AAooiAALaAgA'
298 'CtgIAHsbOAAp2TgACogIAFtbaACqOigAidnoAAuICADKaogACfjoAAr4CAAKSFgAm'
299 'fnoAAo4eABrS1gAibnoAHsbKAAp6SgACmg4AGs7SACLCxgAqioIAAoYqAAZ6RgACm'
300 'goAKrK6AALmAgAC3gIAApIaABZqagACngYAAo4iAAKmAgAivsYAJoJ6AALCAgACyg'
301 'IAAq4CAAKWEgAOclYALpqeAAK6AgACgjYAEm5eAAKKKgAGekIAHmp6ABpmcgAChi4'
302 'ALpaaACJyegAClhYAEnJeAAZ+QgAqhoIADnZSAB5mdgACiiYAJnp6ACqGegAqrrYA'
303 'GmpuAB5megACkh4ALqqyAA52VgAulpYAAoI6AAZ+PgASbmIALpKWAA7m5gAWbmYAG'
304 'mpyAC6SjgAqioYADnZOAA7q6gAianoALqauABpqagAqgnoAEnJWAAp6RgAWbmIACu'
305 '7uACqGfgAqiooAMqauAAby8gAmusIAMp6qAC6WngAyoqoABvb2AC6SkgAS3uIAJra'
306 '+AB7KzgAynqIALq62ADKirgAC+voAGsrSABLi4gAG8vYADubqAC6qtgAuprIABvb6'
307 'ACLCygAW2t4AKra+AAru8gAS4uYACuruAAry8gAG+voAAEBAAFhkZABAQEAABp6fA'
308 'EBACAAAgPwAAPu/AJE9CgBACAAALj1BAEAICAAGPAAAQAgAAAY8+gBACL8AJTxXAA'
309 'gIQAAAPEAAAAgIAAY8QABACAgAAGQAAABAAAAALpoAAEBAAAAALgAAAEAABkAGAEA'
310 'IQAD2AEAAvwAIAJAu2ABAQEAALmQ5AEBAQAAAnp8AAEAIAAD4+gAAv78An5rDAEBA'
311 'QAAuhAcAQEBAAAb8AABAvwAAAGQKAABAAABpLksAQEAIAC4ACwBAAAAAAPkGAAC/Q'
312 'AD2APoAvwC/AJ0umgBAQEAAAGRkAABAQAAGnp8AQEAIAPcA/AC/AL8A7D0GAEAIQA'
313 'D4hAAAv0AAAAD8QAAAvwgA92T6AL9AvwDsLlcAQEBAAC4AQABAAAgA+EAAAL8IAAA'
314 '8AAAACAAAAJ8uVgBAQEAAAGQ5AABAQAAAnpcAAEBAAPYAQAC/AAgAnQsGAEAAQAAA'
315 'hG0AAEBAAB38PQAIv0AA9mT6AL9AvwAJLmwAZUBAAB0AQAAIAAgAHUAAAAgIAAAAA'
316 'AAAAAAAAACzbAAAQEAA//k5AH+/QAAGb5cAQEBAAACy5AAAQAgAAIpAAABACAAAAA'
317 'AAAAAAAAkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk'
318 'JCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJ'
319 'CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJC'
320 'QkJCQkJCQkJCQkJCQkJCQkJCQkyEi8aQCEJCQkJCQkJCQkJCQkJCTI7OwgILzsyCg'
321 'kJCQkJCQkJCQkJCzcJFggvADwGEDc3EhYAMgkJCQkJEjcVJDohGj0LGgYAPT0hCT0'
322 'LDyI3CQoBLwAaFgkyEC9AJAE8OgsIMjoABi8kCx4JCQkJCQkeGko8KTcLCxIyNwkJ'
323 'CQkWEjwWNUAQPCEAMwgJCQkJCQkSQBcvEkAPAQkyN0AMCQkJCQhBFyEvNy89JCIkM'
324 'yItGQwJCQo9RxozIgkyCQoPFxAtDBkgIgkJCQkJCQkJMRoQECJQNi9EIAAgCQkJCQ'
325 'kSUA88UAYeBjELICA8HiI=\n')
326 self.assertEquals(renderer.raster_data, raw_data)
327
328 self.assertEquals(dc.calls,
329 [('BeginDrawing',),
330 ('SetFont', "label font"),
331 ('EndDrawing',)])
332
333 def test_point_map_projection(self):
334 """Test BaseRenderer with point layer and map projection"""
335 table = MemoryTable([("type", FIELDTYPE_STRING),
336 ("value", FIELDTYPE_DOUBLE),
337 ("code", FIELDTYPE_INT)],
338 [("UNKNOWN", 0.0, 0)])
339 shapes = [[[(10, 10)]]]
340 store = SimpleShapeStore(SHAPETYPE_POINT, shapes, table)
341
342 map = Map("TestBaseRenderer")
343 map.SetProjection(MockProjection(-3, 3))
344 layer = Layer("point layer", store)
345 map.AddLayer(layer)
346 self.to_destroy.append(map)
347
348 dc = MockDC()
349 renderer = SimpleRenderer(dc, 2, (10, 10))
350
351 renderer.render_map(map)
352
353 self.assertEquals(dc.calls,
354 [('BeginDrawing',),
355 ('SetBrush', ('brush', Transparent)),
356 ('SetPen', ('pen', Color(0, 0, 0), 1)),
357 ('DrawEllipse', -55, -55, 10, 10),
358 ('SetFont', "label font"),
359 ('EndDrawing',)])
360
361 def test_point_layer_projection(self):
362 """Test BaseRenderer with point layer and layer projection"""
363 table = MemoryTable([("type", FIELDTYPE_STRING),
364 ("value", FIELDTYPE_DOUBLE),
365 ("code", FIELDTYPE_INT)],
366 [("UNKNOWN", 0.0, 0)])
367 shapes = [[[(9, 9)]]]
368 store = SimpleShapeStore(SHAPETYPE_POINT, shapes, table)
369
370 map = Map("TestBaseRenderer")
371 layer = Layer("point layer", store)
372 layer.SetProjection(MockProjection(3, -3))
373 map.AddLayer(layer)
374 self.to_destroy.append(map)
375
376 dc = MockDC()
377 renderer = SimpleRenderer(dc, 2, (10, 10))
378
379 renderer.render_map(map)
380
381 self.assertEquals(dc.calls,
382 [('BeginDrawing',),
383 ('SetBrush', ('brush', Transparent)),
384 ('SetPen', ('pen', Color(0, 0, 0), 1)),
385 ('DrawEllipse', 11, 11, 10, 10),
386 ('SetFont', "label font"),
387 ('EndDrawing',)])
388
389 def test_point_layer_and_map_projection(self):
390 """Test BaseRenderer with point layer and layer and map projection"""
391 table = MemoryTable([("type", FIELDTYPE_STRING),
392 ("value", FIELDTYPE_DOUBLE),
393 ("code", FIELDTYPE_INT)],
394 [("UNKNOWN", 0.0, 0)])
395 shapes = [[[(9, 9)]]]
396 store = SimpleShapeStore(SHAPETYPE_POINT, shapes, table)
397
398 map = Map("TestBaseRenderer")
399 map.SetProjection(MockProjection(-3, 3))
400 layer = Layer("point layer", store)
401 layer.SetProjection(MockProjection(3, -3))
402 map.AddLayer(layer)
403 self.to_destroy.append(map)
404
405 dc = MockDC()
406 renderer = SimpleRenderer(dc, 2, (10, 10))
407
408 renderer.render_map(map)
409
410 self.assertEquals(dc.calls,
411 [('BeginDrawing',),
412 ('SetBrush', ('brush', Transparent)),
413 ('SetPen', ('pen', Color(0, 0, 0), 1)),
414 ('DrawEllipse', -13, 23, 10, 10),
415 ('SetFont', "label font"),
416 ('EndDrawing',)])
417
418
419
420 if __name__ == "__main__":
421 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