1 |
# Copyright (c) 2001-2004 by Intevation GmbH |
# Copyright (c) 2001-2006 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> (2001-2003) |
# Bernhard Herzog <[email protected]> (2001-2003) |
4 |
# Jonathan Coles <[email protected]> (2003) |
# Jonathan Coles <[email protected]> (2003) |
5 |
# Frank Koormann <[email protected]> (2003) |
# Frank Koormann <[email protected]> (2003) |
6 |
# Jan-Oliver Wagner <[email protected]> (2003, 2004) |
# Jan-Oliver Wagner <[email protected]> (2003-2005) |
7 |
# |
# |
8 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
9 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
18 |
|
|
19 |
import array |
import array |
20 |
|
|
21 |
|
import traceback |
22 |
|
|
23 |
from Thuban import _ |
from Thuban import _ |
24 |
|
|
25 |
from wxPython.wx import wxPoint, wxRect, wxPen, wxBrush, wxFont, \ |
import wx |
|
wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \ |
|
|
wxBLACK_PEN, wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL, \ |
|
|
wxBitmapFromImage, wxImageFromStream, wxBITMAP_TYPE_BMP, \ |
|
|
wxBITMAP_TYPE_JPEG, wxBITMAP_TYPE_PNG, wxBITMAP_TYPE_TIF, \ |
|
|
wxBITMAP_TYPE_GIF, wxEmptyImage, wxMask, wxBitmapFromBits |
|
26 |
|
|
27 |
from wxproj import draw_polygon_shape, draw_polygon_init |
from wxproj import draw_polygon_shape, draw_polygon_init |
28 |
|
|
47 |
if Thuban.Model.resource.has_gdal_support(): |
if Thuban.Model.resource.has_gdal_support(): |
48 |
from gdalwarp import ProjectRasterFile |
from gdalwarp import ProjectRasterFile |
49 |
|
|
50 |
|
verbose = 0 # whether to talk more on stdout |
51 |
|
|
52 |
# Map the strings used for the format parameter of the draw_raster_data |
# Map the strings used for the format parameter of the draw_raster_data |
53 |
# method to the appropriate wxWindows constants |
# method to the appropriate wxWindows constants |
54 |
raster_format_map = { |
raster_format_map = { |
55 |
"BMP": wxBITMAP_TYPE_BMP, |
"BMP": wx.BITMAP_TYPE_BMP, |
56 |
"JPEG": wxBITMAP_TYPE_JPEG, |
"JPEG": wx.BITMAP_TYPE_JPEG, |
57 |
"PNG": wxBITMAP_TYPE_PNG, |
"PNG": wx.BITMAP_TYPE_PNG, |
58 |
"TIFF": wxBITMAP_TYPE_TIF, |
"TIFF": wx.BITMAP_TYPE_TIF, |
59 |
"GIF": wxBITMAP_TYPE_GIF, |
"GIF": wx.BITMAP_TYPE_GIF, |
60 |
} |
} |
61 |
|
|
62 |
class MapRenderer(BaseRenderer): |
class MapRenderer(BaseRenderer): |
63 |
|
|
64 |
"""Class to render a map onto a wxDC""" |
"""Class to render a map onto a wxDC""" |
65 |
|
|
66 |
TRANSPARENT_PEN = wxTRANSPARENT_PEN |
TRANSPARENT_PEN = wx.TRANSPARENT_PEN |
67 |
TRANSPARENT_BRUSH = wxTRANSPARENT_BRUSH |
TRANSPARENT_BRUSH = wx.TRANSPARENT_BRUSH |
68 |
|
|
69 |
def make_point(self, x, y): |
def make_point(self, x, y): |
70 |
return wxPoint(int(round(x)), int(round(y))) |
return wx.Point(int(round(x)), int(round(y))) |
71 |
|
|
72 |
def tools_for_property(self, prop): |
def tools_for_property(self, prop): |
73 |
fill = prop.GetFill() |
fill = prop.GetFill() |
74 |
if fill is Transparent: |
if fill is Transparent: |
75 |
brush = self.TRANSPARENT_BRUSH |
brush = self.TRANSPARENT_BRUSH |
76 |
else: |
else: |
77 |
brush = wxBrush(Color2wxColour(fill), wxSOLID) |
brush = wx.Brush(Color2wxColour(fill), wx.SOLID) |
78 |
|
|
79 |
stroke = prop.GetLineColor() |
stroke = prop.GetLineColor() |
80 |
if stroke is Transparent: |
if stroke is Transparent: |
81 |
pen = self.TRANSPARENT_PEN |
pen = self.TRANSPARENT_PEN |
82 |
else: |
else: |
83 |
pen = wxPen(Color2wxColour(stroke), prop.GetLineWidth(), wxSOLID) |
pen = wx.Pen(Color2wxColour(stroke), prop.GetLineWidth(), wx.SOLID) |
84 |
return pen, brush |
return pen, brush |
85 |
|
|
86 |
def low_level_renderer(self, layer): |
def low_level_renderer(self, layer): |
108 |
return BaseRenderer.low_level_renderer(self, layer) |
return BaseRenderer.low_level_renderer(self, layer) |
109 |
|
|
110 |
def label_font(self): |
def label_font(self): |
111 |
return wxFont(int(round(self.resolution * 10)), wxSWISS, wxNORMAL, |
return wx.Font(int(round(self.resolution * 10)), wx.SWISS, wx.NORMAL, |
112 |
wxNORMAL) |
wx.NORMAL) |
113 |
|
|
114 |
def projected_raster_layer(self, layer, srcProj, dstProj, extents, |
def projected_raster_layer(self, layer, srcProj, dstProj, extents, |
115 |
resolution, dimensions, options): |
resolution, dimensions, options): |
116 |
|
"""Returns a raster layer image in projected space |
117 |
|
|
118 |
|
Based on a given filename. This method must be implemented in |
119 |
|
classes derived from BaseRenderer. |
120 |
|
""" |
121 |
|
|
122 |
ret = None |
ret = None |
123 |
|
|
128 |
options = options & ~2 # ALPHA_MASK not supported |
options = options & ~2 # ALPHA_MASK not supported |
129 |
|
|
130 |
try: |
try: |
131 |
ret = ProjectRasterFile(layer.GetImageFilename(), |
if verbose > 0: |
132 |
|
print "doing ProjectRasterFile '%s' -> '%s'" % \ |
133 |
|
(srcProj, dstProj) |
134 |
|
print "extents:", extents, "resolution:", resolution |
135 |
|
print "dimensions:", dimensions, "options:", options |
136 |
|
ret = ProjectRasterFile(layer.GetImageFilename(), |
137 |
srcProj, dstProj, |
srcProj, dstProj, |
138 |
extents, resolution, dimensions, |
extents, resolution, dimensions, |
139 |
options) |
options) |
140 |
except (MemoryError, IOError, AttributeError, ValueError): |
except (MemoryError, IOError, AttributeError, ValueError): |
141 |
# Why does this catch AttributeError and ValueError? |
# Why does this catch AttributeError and ValueError? |
145 |
|
|
146 |
return ret |
return ret |
147 |
|
|
148 |
def draw_raster_data(self, layer, x,y, data, format = 'BMP'): |
def draw_raster_data(self, x,y, data, format = 'BMP', opacity=1.0): |
149 |
|
|
150 |
mask = None |
mask = None |
151 |
alpha = None |
alpha = None |
157 |
alpha_data = None |
alpha_data = None |
158 |
|
|
159 |
if format == 'RAW': |
if format == 'RAW': |
160 |
image = wxEmptyImage(width, height) |
image = wx.EmptyImage(width, height) |
161 |
image.SetData(image_data) |
image.SetData(image_data) |
162 |
if mask_data is not None: |
if mask_data is not None: |
163 |
mask = wxBitmapFromBits(mask_data, width, height, 1) |
mask = wx.BitmapFromBits(mask_data, width, height, 1) |
164 |
mask = wxMask(mask) |
mask = wx.Mask(mask) |
165 |
elif alpha_data is not None: |
elif alpha_data is not None: |
166 |
# alpha_data is already in the right format |
# alpha_data is already in the right format |
167 |
alpha = alpha_data |
alpha = alpha_data |
168 |
|
|
169 |
else: |
else: |
170 |
stream = cStringIO.StringIO(image_data) |
stream = cStringIO.StringIO(image_data) |
171 |
image = wxImageFromStream(stream, raster_format_map[format]) |
image = wx.ImageFromStream(stream, raster_format_map[format]) |
172 |
|
|
173 |
if mask_data is not None: |
if mask_data is not None: |
174 |
stream = cStringIO.StringIO(mask_data) |
stream = cStringIO.StringIO(mask_data) |
175 |
mask = wxImageFromStream(stream, raster_format_map[format]) |
mask = wx.ImageFromStream(stream, raster_format_map[format]) |
176 |
mask = wxMask(wxBitmapFromImage(mask, 1)) |
mask = wx.Mask(wx.BitmapFromImage(mask, 1)) |
177 |
elif alpha_data is not None: |
elif alpha_data is not None: |
178 |
stream = cStringIO.StringIO(alpha_data) |
stream = cStringIO.StringIO(alpha_data) |
179 |
alpha = wxImageFromStream(stream, raster_format_map[format]) |
alpha = wx.ImageFromStream(stream, raster_format_map[format]) |
180 |
alpha = alpha.GetData()[:] # XXX: do we need to copy this? |
alpha = alpha.GetData() #[:] # XXX: do we need to copy this? |
181 |
|
elif image.HasAlpha(): |
182 |
|
alpha = image.GetAlphaData() |
183 |
|
|
184 |
# |
# |
185 |
# if we are using the alpha_data then scale down the alpha values |
# scale down the alpha values the opacity level using a string |
186 |
# by the layer's opacity using a string translation table |
# translation table for efficiency. |
187 |
# |
# |
188 |
if alpha is not None: |
if alpha is not None: |
189 |
lo = layer.Opacity() |
if opacity == 0: |
|
if lo == 0: |
|
190 |
return |
return |
191 |
elif lo == 1: |
elif opacity == 1: |
192 |
a = alpha |
a = alpha |
193 |
else: |
else: |
194 |
tr = [int(i*lo) for i in range(256)] |
tr = [int(i*opacity) for i in range(256)] |
195 |
table = array.array('B', tr).tostring() |
table = array.array('B', tr).tostring() |
196 |
a = alpha.translate(table) |
a = alpha.translate(table) |
197 |
|
|
198 |
image.SetAlphaData(a) |
image.SetAlphaData(a) |
199 |
|
|
200 |
bitmap = wxBitmapFromImage(image) |
bitmap = wx.BitmapFromImage(image) |
201 |
|
|
202 |
if mask is not None: |
if mask is not None: |
203 |
bitmap.SetMask(mask) |
bitmap.SetMask(mask) |
243 |
selected_shapes -- a list of the shape-ids representing the |
selected_shapes -- a list of the shape-ids representing the |
244 |
selected shapes for the given layer. |
selected shapes for the given layer. |
245 |
""" |
""" |
246 |
pen = wxPen(wxBLACK, 3, wxSOLID) |
pen = wx.Pen(wx.BLACK, 3, wx.SOLID) |
247 |
brush = wxBrush(wxBLACK, wxCROSS_HATCH) |
brush = wx.Brush(wx.BLACK, wx.CROSS_HATCH) |
248 |
|
|
249 |
shapetype = layer.ShapeType() |
shapetype = layer.ShapeType() |
250 |
useraw, func, param = self.low_level_renderer(layer) |
useraw, func, param = self.low_level_renderer(layer) |
265 |
|
|
266 |
# Get the size of the specific property for this |
# Get the size of the specific property for this |
267 |
# point |
# point |
268 |
if shapetype == SHAPETYPE_POINT and field is not None: |
if shapetype == SHAPETYPE_POINT: |
269 |
value = table.ReadValue(shape.ShapeID(), field) |
if field is not None: |
270 |
group = lc.FindGroup(value) |
value = table.ReadValue(shape.ShapeID(), field) |
271 |
size = group.GetProperties().GetSize() |
group = lc.FindGroup(value) |
272 |
|
size = group.GetProperties().GetSize() |
273 |
|
else: |
274 |
|
size = lc.GetDefaultGroup().GetProperties().GetSize() |
275 |
args = (pen, brush, size) |
args = (pen, brush, size) |
276 |
|
|
277 |
if useraw: |
if useraw: |
385 |
self.dc.DestroyClippingRegion() |
self.dc.DestroyClippingRegion() |
386 |
|
|
387 |
# Force the font for Legend drawing |
# Force the font for Legend drawing |
388 |
font = wxFont(self.resolution * 10, wxSWISS, wxNORMAL, wxNORMAL) |
font = wx.Font(self.resolution * 10, wx.SWISS, wx.NORMAL, wx.NORMAL) |
389 |
self.dc.SetFont(font) |
self.dc.SetFont(font) |
390 |
|
|
391 |
self.render_frame() |
self.render_frame() |
397 |
"""Render the frames for map and legend/scalebar.""" |
"""Render the frames for map and legend/scalebar.""" |
398 |
|
|
399 |
dc = self.dc |
dc = self.dc |
400 |
dc.SetPen(wxBLACK_PEN) |
dc.SetPen(wx.BLACK_PEN) |
401 |
dc.SetBrush(wxTRANSPARENT_BRUSH) |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
402 |
|
|
403 |
# Dimension stuff |
# Dimension stuff |
404 |
width, height = dc.GetSizeTuple() |
width, height = dc.GetSizeTuple() |
423 |
|
|
424 |
previewer = ClassDataPreviewer() |
previewer = ClassDataPreviewer() |
425 |
dc = self.dc |
dc = self.dc |
426 |
dc.SetPen(wxBLACK_PEN) |
dc.SetPen(wx.BLACK_PEN) |
427 |
dc.SetBrush(wxTRANSPARENT_BRUSH) |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
428 |
|
|
429 |
# Dimension stuff |
# Dimension stuff |
430 |
width, height = dc.GetSizeTuple() |
width, height = dc.GetSizeTuple() |
439 |
posy = mminy + 5 # 5 pix inside legend frame |
posy = mminy + 5 # 5 pix inside legend frame |
440 |
|
|
441 |
# Render the legend |
# Render the legend |
442 |
dc.SetTextForeground(wxBLACK) |
dc.SetTextForeground(wx.BLACK) |
443 |
if self.map.HasLayers(): |
if self.map.HasLayers(): |
444 |
layers = self.map.Layers()[:] |
layers = self.map.Layers()[:] |
445 |
layers.reverse() |
layers.reverse() |
455 |
for g in clazz: |
for g in clazz: |
456 |
if g.IsVisible(): |
if g.IsVisible(): |
457 |
previewer.Draw(dc, |
previewer.Draw(dc, |
458 |
wxRect(posx+dx, posy, |
wx.Rect(posx+dx, posy, |
459 |
iconwidth, iconheight), |
iconwidth, iconheight), |
460 |
g.GetProperties(), shapeType) |
g.GetProperties(), shapeType) |
461 |
dc.DrawText(g.GetDisplayText(), |
dc.DrawText(g.GetDisplayText(), |