1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> |
20 |
# $Source$ |
# $Source$ |
21 |
# $Id$ |
# $Id$ |
22 |
|
|
23 |
|
import sys |
24 |
import traceback |
import traceback |
25 |
|
|
26 |
from Thuban.Model.layer import Layer, RasterLayer |
from Thuban.Model.layer import Layer, RasterLayer |
34 |
from gdalwarp import ProjectRasterFile |
from gdalwarp import ProjectRasterFile |
35 |
|
|
36 |
|
|
37 |
|
# |
38 |
|
# Renderer Extensions |
39 |
|
# |
40 |
|
# The renderer extensions provide a way to render layer types defined in |
41 |
|
# Thuban extensions. The renderer extensions are stored as a list with |
42 |
|
# (layer_class, draw_function) pairs. If the renderer has to draw a |
43 |
|
# non-builtin layer type, i.e. a layer that is not a subclass of Layer |
44 |
|
# or RasterLayer, it iterates through that list, tests whether the layer |
45 |
|
# to be drawn is an instance of layer_class and if so calls |
46 |
|
# draw_function with the renderer and the layer as arguments. Since |
47 |
|
# drawing is done incrementally, the draw_function should return an |
48 |
|
# iterable. The easiest way is to simply implement the draw_function as |
49 |
|
# a generator and to yield in suitable places, or to return the empty |
50 |
|
# tuple. |
51 |
|
# |
52 |
|
# New renderer extensions should be added with add_renderer_extension(). |
53 |
|
# If necessary the extensions list can be reset with |
54 |
|
# init_renderer_extensions(). |
55 |
|
|
56 |
|
_renderer_extensions = [] |
57 |
|
|
58 |
|
def add_renderer_extension(layer_class, function): |
59 |
|
"""Add a renderer extension for the layer class layer_class |
60 |
|
|
61 |
|
When an instance of layer_class is to be drawn by the renderer the |
62 |
|
renderer will call function with the renderer and the layer_class |
63 |
|
instance as arguments. Since drawing is done incrementally, the |
64 |
|
function should return an iterable. The easiest way is to simply |
65 |
|
implement the draw_function as a generator and to yield True in |
66 |
|
suitable places, or to return the empty tuple if it's not possible |
67 |
|
to do the rendering incrementally. |
68 |
|
""" |
69 |
|
_renderer_extensions.append((layer_class, function)) |
70 |
|
|
71 |
|
def init_renderer_extensions(): |
72 |
|
"""(Re)initialize the list of renderer extensions |
73 |
|
|
74 |
|
Calling this function outside of the test suite is probably not |
75 |
|
useful. |
76 |
|
""" |
77 |
|
del _renderer_extensions[:] |
78 |
|
|
79 |
|
def proj_params_to_str(proj): |
80 |
|
"Build a string suitable for GDAL describing the given projection" |
81 |
|
str = "" |
82 |
|
if proj is not None: |
83 |
|
for p in proj.GetAllParameters(): |
84 |
|
str += "+" + p + " " |
85 |
|
return str |
86 |
|
|
87 |
|
# |
88 |
|
# Base Renderer |
89 |
|
# |
90 |
|
|
91 |
class BaseRenderer: |
class BaseRenderer: |
92 |
|
|
93 |
"""Basic Renderer Infrastructure for Thuban Maps |
"""Basic Renderer Infrastructure for Thuban Maps |
184 |
that methods especially in derived classes have access to the |
that methods especially in derived classes have access to the |
185 |
map if necessary. |
map if necessary. |
186 |
""" |
""" |
|
# Whether the raster layer has already been drawn. See below for |
|
|
# the optimization this is used for |
|
|
seenRaster = True |
|
|
|
|
|
# |
|
|
# This is only a good optimization if there is only one |
|
|
# raster layer and the image covers the entire window (as |
|
|
# it currently does). We note if there is a raster layer |
|
|
# and only begin drawing layers once we have drawn it. |
|
|
# That way we avoid drawing layers that won't be seen. |
|
|
# |
|
|
if Thuban.Model.resource.has_gdal_support(): |
|
|
for layer in self.map.Layers(): |
|
|
if isinstance(layer, RasterLayer) and layer.Visible(): |
|
|
seenRaster = False |
|
|
break |
|
187 |
|
|
188 |
for layer in self.map.Layers(): |
for layer in self.map.Layers(): |
189 |
# if honor_visibility is true, only draw visible layers, |
# if honor_visibility is true, only draw visible layers, |
190 |
# otherwise draw all layers |
# otherwise draw all layers |
191 |
if not self.honor_visibility or layer.Visible(): |
if not self.honor_visibility or layer.Visible(): |
192 |
if isinstance(layer, Layer) and seenRaster: |
if isinstance(layer, Layer): |
193 |
for i in self.draw_shape_layer_incrementally(layer): |
for i in self.draw_shape_layer_incrementally(layer): |
194 |
yield True |
yield True |
195 |
elif isinstance(layer, RasterLayer) \ |
elif isinstance(layer, RasterLayer) \ |
196 |
and Thuban.Model.resource.has_gdal_support(): |
and Thuban.Model.resource.has_gdal_support(): |
197 |
self.draw_raster_layer(layer) |
self.draw_raster_layer(layer) |
|
seenRaster = True |
|
198 |
yield True |
yield True |
199 |
|
else: |
200 |
|
# look it up in the renderer extensions |
201 |
|
for cls, func in _renderer_extensions: |
202 |
|
if isinstance(layer, cls): |
203 |
|
for i in func(self, layer): |
204 |
|
yield True |
205 |
|
break |
206 |
|
else: |
207 |
|
# No renderer found. Print a message about it |
208 |
|
print >>sys.stderr, ("Drawing layer %r not supported" |
209 |
|
% layer) |
210 |
|
yield True |
211 |
|
|
212 |
self.draw_label_layer(self.map.LabelLayer()) |
self.draw_label_layer(self.map.LabelLayer()) |
213 |
yield False |
yield False |
244 |
# Determine which render function to use. |
# Determine which render function to use. |
245 |
useraw, draw_func, draw_func_param = self.low_level_renderer(layer) |
useraw, draw_func, draw_func_param = self.low_level_renderer(layer) |
246 |
|
|
247 |
|
# |
248 |
# Iterate through all shapes that have to be drawn. |
# Iterate through all shapes that have to be drawn. |
249 |
|
# |
250 |
|
|
251 |
|
# Count the shapes drawn so that we can yield every few hundred |
252 |
|
# shapes |
253 |
count = 0 |
count = 0 |
254 |
|
|
255 |
|
# Cache the tools (pens and brushes) for the classification |
256 |
|
# groups. This is a mapping from the group's ids to the a tuple |
257 |
|
# (pen, brush) |
258 |
|
tool_cache = {} |
259 |
|
|
260 |
for shape in self.layer_shapes(layer): |
for shape in self.layer_shapes(layer): |
261 |
count += 1 |
count += 1 |
262 |
if field is None: |
if field is None: |
263 |
group = defaultGroup |
group = defaultGroup |
264 |
else: |
else: |
265 |
record = table.ReadRowAsDict(shape.ShapeID()) |
value = table.ReadValue(shape.ShapeID(), field) |
266 |
assert record is not None |
group = lc.FindGroup(value) |
|
group = lc.FindGroup(record[field]) |
|
267 |
|
|
268 |
if not group.IsVisible(): |
if not group.IsVisible(): |
269 |
continue |
continue |
270 |
|
|
271 |
# don't recreate new objects if they are the same as before |
try: |
272 |
if group is not old_group: |
pen, brush = tool_cache[id(group)] |
273 |
old_group = group |
except KeyError: |
274 |
|
pen, brush = tool_cache[id(group)] \ |
275 |
prop = group.GetProperties() |
= self.tools_for_property(group.GetProperties()) |
|
|
|
|
if prop != old_prop: |
|
|
pen, brush = self.tools_for_property(prop) |
|
276 |
|
|
277 |
if useraw: |
if useraw: |
278 |
data = shape.RawData() |
data = shape.RawData() |
279 |
else: |
else: |
280 |
data = shape.Points() |
data = shape.Points() |
281 |
draw_func(draw_func_param, data, pen, brush) |
if draw_func == self.draw_point_shape: |
282 |
|
draw_func(draw_func_param, data, pen, brush, |
283 |
|
size = group.GetProperties().GetSize()) |
284 |
|
else: |
285 |
|
draw_func(draw_func_param, data, pen, brush) |
286 |
if count % 500 == 0: |
if count % 500 == 0: |
287 |
yield True |
yield True |
288 |
|
|
359 |
scale = self.scale |
scale = self.scale |
360 |
offx, offy = self.offset |
offx, offy = self.offset |
361 |
make_point = self.make_point |
make_point = self.make_point |
362 |
|
|
363 |
for part in points: |
for part in points: |
364 |
result.append([]) |
result.append([]) |
365 |
for x, y in part: |
for x, y in part: |
378 |
value of the shape's Points() method. The coordinates in the |
value of the shape's Points() method. The coordinates in the |
379 |
DC's coordinate system are determined with |
DC's coordinate system are determined with |
380 |
self.projected_points. |
self.projected_points. |
381 |
|
|
382 |
|
For a description of the algorithm look in wxproj.cpp. |
383 |
""" |
""" |
384 |
points = self.projected_points(layer, points) |
points = self.projected_points(layer, points) |
385 |
|
|
388 |
for part in points: |
for part in points: |
389 |
polygon.extend(part) |
polygon.extend(part) |
390 |
|
|
391 |
|
# missing back vertices for correct filling. |
392 |
insert_index = len(polygon) |
insert_index = len(polygon) |
393 |
for part in points[:-1]: |
for part in points[:-1]: |
394 |
polygon.insert(insert_index, part[0]) |
polygon.insert(insert_index, part[0]) |
418 |
for part in points: |
for part in points: |
419 |
self.dc.DrawLines(part) |
self.dc.DrawLines(part) |
420 |
|
|
421 |
def draw_point_shape(self, layer, points, pen, brush): |
def draw_point_shape(self, layer, points, pen, brush, size = 5): |
422 |
"""Draw a point shape from layer with the given brush and pen |
"""Draw a point shape from layer with the given brush and pen |
423 |
|
|
424 |
The shape is given by points argument which is a the return |
The shape is given by points argument which is a the return |
432 |
if not points: |
if not points: |
433 |
return |
return |
434 |
|
|
435 |
radius = self.resolution * 5 |
radius = int(round(self.resolution * size)) |
436 |
self.dc.SetBrush(brush) |
self.dc.SetBrush(brush) |
437 |
self.dc.SetPen(pen) |
self.dc.SetPen(pen) |
438 |
for part in points: |
for part in points: |
452 |
offx, offy = self.offset |
offx, offy = self.offset |
453 |
width, height = self.dc.GetSizeTuple() |
width, height = self.dc.GetSizeTuple() |
454 |
|
|
455 |
in_proj = "" |
in_proj = proj_params_to_str(layer.GetProjection()) |
456 |
proj = layer.GetProjection() |
out_proj = proj_params_to_str(self.map.GetProjection()) |
|
if proj is not None: |
|
|
for p in proj.GetAllParameters(): |
|
|
in_proj += "+" + p + " " |
|
457 |
|
|
458 |
out_proj = "" |
# True -- warp the image to the size of the whole screen |
459 |
proj = self.map.GetProjection() |
# False -- only use the bound box of the layer (currently inaccurate) |
460 |
if proj is not None: |
if True: |
461 |
for p in proj.GetAllParameters(): |
pmin = [0,height] |
462 |
out_proj += "+" + p + " " |
pmax = [width, 0] |
463 |
|
else: |
464 |
|
bb = layer.LatLongBoundingBox() |
465 |
|
bb = [[[bb[0], bb[1]], [bb[2], bb[3]],],] |
466 |
|
pmin, pmax = self.projected_points(layer, bb)[0] |
467 |
|
|
468 |
|
fmin = [max(0, pmin[0]) - offx, offy - min(height, pmin[1])] |
469 |
|
fmax = [min(width, pmax[0]) - offx, offy - max(0, pmax[1])] |
470 |
|
|
471 |
|
xmin = fmin[0]/self.scale |
472 |
|
ymin = fmin[1]/self.scale |
473 |
|
xmax = fmax[0]/self.scale |
474 |
|
ymax = fmax[1]/self.scale |
475 |
|
|
476 |
xmin = (0 - offx) / self.scale |
width = int(min(width, round(fmax[0] - fmin[0] + 1))) |
477 |
ymin = (offy - height) / self.scale |
height = int(min(height, round(fmax[1] - fmin[1] + 1))) |
|
xmax = (width - offx) / self.scale |
|
|
ymax = (offy - 0) / self.scale |
|
478 |
|
|
479 |
try: |
try: |
480 |
data = ProjectRasterFile(layer.GetImageFilename(), |
data = [width, height, |
481 |
|
ProjectRasterFile(layer.GetImageFilename(), |
482 |
in_proj, out_proj, |
in_proj, out_proj, |
483 |
(xmin, ymin, xmax, ymax), "", |
(xmin, ymin, xmax, ymax), "", |
484 |
(width, height)) |
(width, height)) |
485 |
|
] |
486 |
except (IOError, AttributeError, ValueError): |
except (IOError, AttributeError, ValueError): |
487 |
# Why does this catch AttributeError and ValueError? |
# Why does this catch AttributeError and ValueError? |
488 |
# FIXME: The exception should be communicated to the user |
# FIXME: The exception should be communicated to the user |
489 |
# better. |
# better. |
490 |
traceback.print_exc() |
traceback.print_exc() |
491 |
else: |
else: |
492 |
self.draw_raster_data(data) |
mask = "#030104" |
493 |
|
#mask = None |
494 |
def draw_raster_data(self, data): |
self.draw_raster_data(fmin[0]+offx, offy-fmax[1], data, "RAW", mask) |
495 |
"""Draw the raster image in data onto the DC |
data = None |
496 |
|
|
497 |
The raster image data is a string holding the data in BMP |
def draw_raster_data(self, x, y, data, format="BMP", mask = None): |
498 |
format. The data is exactly the size of the dc and covers it |
"""Draw the raster image in data onto the DC with the top |
499 |
completely. |
left corner at (x,y) |
500 |
|
|
501 |
This method has to be implemented by derived classes. |
The raster image data is a list holding the image width, height, |
502 |
|
and data in the format indicated by the format parameter. |
503 |
|
|
504 |
|
The format parameter is a string with the name of the format. |
505 |
|
The following format names should be used: |
506 |
|
|
507 |
|
'RAW' -- an array of RGB values (len=3*width*height) |
508 |
|
'BMP' -- Windows Bitmap |
509 |
|
'JPEG' -- JPEG Image |
510 |
|
|
511 |
|
The default format is 'BMP'. |
512 |
|
|
513 |
|
The mask parameter determines how a mask (if any) is applied |
514 |
|
to the image. mask can have the following values: |
515 |
|
|
516 |
|
o None -- no mask is used |
517 |
|
o Any object accepted by wxBitmap.SetMaskColour() |
518 |
|
o A one-bit image the same size as the image data |
519 |
|
|
520 |
|
This method has to be implemented by derived classes. The |
521 |
|
implementation in the derived class should try to support at |
522 |
|
least the formats specified above and may support more. |
523 |
""" |
""" |
524 |
raise NotImplementedError |
raise NotImplementedError |
525 |
|
|
549 |
text = label.text |
text = label.text |
550 |
if forward: |
if forward: |
551 |
x, y = forward(x, y) |
x, y = forward(x, y) |
552 |
x = x * scale + offx |
x = int(round(x * scale + offx)) |
553 |
y = -y * scale + offy |
y = int(round(-y * scale + offy)) |
554 |
width, height = self.dc.GetTextExtent(text) |
width, height = self.dc.GetTextExtent(text) |
555 |
if label.halign == ALIGN_LEFT: |
if label.halign == ALIGN_LEFT: |
556 |
# nothing to be done |
# nothing to be done |