/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/renderer.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/renderer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (show annotations)
Mon Feb 4 19:23:30 2002 UTC (23 years, 1 month ago) by bh
Original Path: trunk/thuban/Thuban/UI/renderer.py
File MIME type: text/x-python
File size: 7303 byte(s)
	* Thuban/UI/renderer.py (MapRenderer.draw_shape_layer): Honour the
	stroke_width attribute

1 # Copyright (c) 2001, 2002 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 Thuban for details.
7
8 __version__ = "$Revision$"
9
10 from wxPython.wx import wxPoint, wxColour, wxPen, wxBrush, wxFont, \
11 wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \
12 wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL
13
14 from wxproj import draw_polygon_shape
15
16 from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
17 SHAPETYPE_POINT
18 from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
19 ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE
20
21
22 class MapRenderer:
23
24 """Class to render a map onto a wxDC"""
25
26 honor_visibility = 1
27
28 def __init__(self, dc, scale, offset, resolution = 72.0,
29 honor_visibility = None):
30 """Inititalize the renderer.
31
32 dc -- the wxPython DC to render on
33 scale, offset -- the scale factor and translation to convert
34 between projected coordinates and the DC coordinates
35
36 resolution -- the assumed resolution of the DC. Used to convert
37 absolute lengths like font sizes to DC coordinates
38
39 honor_visibility -- boolean. If true, honor the visibility flag
40 of the layers, otherwise draw all layers. If None, use
41 the renderer's default.
42 """
43 # resolution in pixel/inch
44 self.dc = dc
45 self.scale = scale
46 self.offset = offset
47 if honor_visibility is not None:
48 self.honor_visibility = honor_visibility
49 # store the resolution in pixel/point because it's more useful
50 # later.
51 self.resolution = resolution / 72.0
52
53 def render_map(self, map):
54 self.map = map
55 for layer in map.Layers():
56 # if honor_visibility is true, only draw visible layers,
57 # otherwise draw all layers
58 if not self.honor_visibility or layer.Visible():
59 self.draw_shape_layer(layer)
60 self.draw_label_layer(map.LabelLayer())
61
62 def draw_shape_layer(self, layer):
63 scale = self.scale
64 offx, offy = self.offset
65
66 fill = layer.fill
67 if fill is None:
68 brush = wxTRANSPARENT_BRUSH
69 else:
70 color = wxColour(fill.red * 255,
71 fill.green * 255,
72 fill.blue * 255)
73 brush = wxBrush(color, wxSOLID)
74 stroke = layer.stroke
75 stroke_width = layer.stroke_width
76 if stroke is None:
77 pen = wxTRANSPARENT_PEN
78 else:
79 color = wxColour(stroke.red * 255,
80 stroke.green * 255,
81 stroke.blue * 255)
82 pen = wxPen(color, stroke_width, wxSOLID)
83
84 map_proj = self.map.projection
85 layer_proj = layer.projection
86
87 shapetype = layer.ShapeType()
88
89 if shapetype == SHAPETYPE_POLYGON:
90 for i in range(layer.NumShapes()):
91 self.draw_polygon_shape(layer, i, pen, brush)
92 else:
93 self.dc.SetBrush(brush)
94 self.dc.SetPen(pen)
95 if shapetype == SHAPETYPE_ARC:
96 f = self.draw_arc_shape
97 elif shapetype == SHAPETYPE_POINT:
98 f = self.draw_point_shape
99 for i in range(layer.NumShapes()):
100 f(layer, i)
101
102 def draw_polygon_shape(self, layer, index, pen, brush):
103 offx, offy = self.offset
104 draw_polygon_shape(layer.shapefile.cobject(), index,
105 self.dc, pen, brush,
106 self.map.projection, layer.projection,
107 self.scale, -self.scale, offx, offy)
108
109 def projected_points(self, layer, index):
110 proj = self.map.projection
111 if proj is not None:
112 forward = proj.Forward
113 else:
114 forward = None
115 proj = layer.projection
116 if proj is not None:
117 inverse = proj.Inverse
118 else:
119 inverse = None
120 shape = layer.Shape(index)
121 points = []
122 scale = self.scale
123 offx, offy = self.offset
124 for x, y in shape.Points():
125 if inverse:
126 x, y = inverse(x, y)
127 if forward:
128 x, y = forward(x, y)
129 points.append(wxPoint(x * scale + offx,
130 -y * scale + offy))
131 return points
132
133 def draw_arc_shape(self, layer, index):
134 points = self.projected_points(layer, index)
135 self.dc.DrawLines(points)
136
137 def draw_point_shape(self, layer, index):
138 p = self.projected_points(layer, index)[0]
139 radius = self.resolution * 5
140 self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)
141
142 def draw_label_layer(self, layer):
143 scale = self.scale
144 offx, offy = self.offset
145
146 font = wxFont(self.resolution * 10, wxSWISS, wxNORMAL, wxNORMAL)
147 self.dc.SetFont(font)
148
149 map_proj = self.map.projection
150 if map_proj is not None:
151 forward = map_proj.Forward
152 else:
153 forward = None
154
155 for label in layer.Labels():
156 x = label.x
157 y = label.y
158 text = label.text
159 if forward:
160 x, y = forward(x, y)
161 x = x * scale + offx
162 y = -y * scale + offy
163 width, height = self.dc.GetTextExtent(text)
164 if label.halign == ALIGN_LEFT:
165 # nothing to be done
166 pass
167 elif label.halign == ALIGN_RIGHT:
168 x = x - width
169 elif label.halign == ALIGN_CENTER:
170 x = x - width/2
171 if label.valign == ALIGN_TOP:
172 # nothing to be done
173 pass
174 elif label.valign == ALIGN_BOTTOM:
175 y = y - height
176 elif label.valign == ALIGN_CENTER:
177 y = y - height/2
178 self.dc.DrawText(text, x, y)
179
180
181 class ScreenRenderer(MapRenderer):
182
183 # On the screen we want to see only visible layers by default
184 honor_visibility = 1
185
186 def RenderMap(self, map, selected_layer, selected_shape):
187 self.selected_layer = selected_layer
188 self.selected_shape = selected_shape
189 self.render_map(map)
190
191 def draw_shape_layer(self, layer):
192 MapRenderer.draw_shape_layer(self, layer)
193 if layer is self.selected_layer and self.selected_shape is not None:
194 pen = wxPen(wxBLACK, 3, wxSOLID)
195 brush = wxBrush(wxBLACK, wxCROSS_HATCH)
196
197 shapetype = layer.ShapeType()
198 index = self.selected_shape
199 if shapetype == SHAPETYPE_POLYGON:
200 self.draw_polygon_shape(layer, index, pen, brush)
201 else:
202 self.dc.SetBrush(brush)
203 self.dc.SetPen(pen)
204 if shapetype == SHAPETYPE_ARC:
205 f = self.draw_arc_shape
206 elif shapetype == SHAPETYPE_POINT:
207 f = self.draw_point_shape
208 f(layer, index)
209
210
211 class PrinterRender(MapRenderer):
212
213 # When printing we want to see all layers
214 honor_visibility = 0
215
216 RenderMap = MapRenderer.render_map
217

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26