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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations)
Tue Aug 28 15:41:52 2001 UTC (23 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/renderer.py
File MIME type: text/x-python
File size: 7244 byte(s)
import all the source files

1 bh 6 # Copyright (c) 2001 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     if stroke is None:
76     pen = wxTRANSPARENT_PEN
77     else:
78     color = wxColour(stroke.red * 255,
79     stroke.green * 255,
80     stroke.blue * 255)
81     pen = wxPen(color, 1, wxSOLID)
82    
83     map_proj = self.map.projection
84     layer_proj = layer.projection
85    
86     shapetype = layer.ShapeType()
87    
88     if shapetype == SHAPETYPE_POLYGON:
89     for i in range(layer.NumShapes()):
90     self.draw_polygon_shape(layer, i, pen, brush)
91     else:
92     self.dc.SetBrush(brush)
93     self.dc.SetPen(pen)
94     if shapetype == SHAPETYPE_ARC:
95     f = self.draw_arc_shape
96     elif shapetype == SHAPETYPE_POINT:
97     f = self.draw_point_shape
98     for i in range(layer.NumShapes()):
99     f(layer, i)
100    
101     def draw_polygon_shape(self, layer, index, pen, brush):
102     offx, offy = self.offset
103     draw_polygon_shape(layer.shapefile.cobject(), index,
104     self.dc, pen, brush,
105     self.map.projection, layer.projection,
106     self.scale, -self.scale, offx, offy)
107    
108     def projected_points(self, layer, index):
109     proj = self.map.projection
110     if proj is not None:
111     forward = proj.Forward
112     else:
113     forward = None
114     proj = layer.projection
115     if proj is not None:
116     inverse = proj.Inverse
117     else:
118     inverse = None
119     shape = layer.Shape(index)
120     points = []
121     scale = self.scale
122     offx, offy = self.offset
123     for x, y in shape.Points():
124     if inverse:
125     x, y = inverse(x, y)
126     if forward:
127     x, y = forward(x, y)
128     points.append(wxPoint(x * scale + offx,
129     -y * scale + offy))
130     return points
131    
132     def draw_arc_shape(self, layer, index):
133     points = self.projected_points(layer, index)
134     self.dc.DrawLines(points)
135    
136     def draw_point_shape(self, layer, index):
137     p = self.projected_points(layer, index)[0]
138     radius = self.resolution * 5
139     self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)
140    
141     def draw_label_layer(self, layer):
142     scale = self.scale
143     offx, offy = self.offset
144    
145     font = wxFont(self.resolution * 10, wxSWISS, wxNORMAL, wxNORMAL)
146     self.dc.SetFont(font)
147    
148     map_proj = self.map.projection
149     if map_proj is not None:
150     forward = map_proj.Forward
151     else:
152     forward = None
153    
154     for label in layer.Labels():
155     x = label.x
156     y = label.y
157     text = label.text
158     if forward:
159     x, y = forward(x, y)
160     x = x * scale + offx
161     y = -y * scale + offy
162     width, height = self.dc.GetTextExtent(text)
163     if label.halign == ALIGN_LEFT:
164     # nothing to be done
165     pass
166     elif label.halign == ALIGN_RIGHT:
167     x = x - width
168     elif label.halign == ALIGN_CENTER:
169     x = x - width/2
170     if label.valign == ALIGN_TOP:
171     # nothing to be done
172     pass
173     elif label.valign == ALIGN_BOTTOM:
174     y = y - height
175     elif label.valign == ALIGN_CENTER:
176     y = y - height/2
177     self.dc.DrawText(text, x, y)
178    
179    
180     class ScreenRenderer(MapRenderer):
181    
182     # On the screen we want to see only visible layers by default
183     honor_visibility = 1
184    
185     def RenderMap(self, map, selected_layer, selected_shape):
186     self.selected_layer = selected_layer
187     self.selected_shape = selected_shape
188     self.render_map(map)
189    
190     def draw_shape_layer(self, layer):
191     MapRenderer.draw_shape_layer(self, layer)
192     if layer is self.selected_layer and self.selected_shape is not None:
193     pen = wxPen(wxBLACK, 3, wxSOLID)
194     brush = wxBrush(wxBLACK, wxCROSS_HATCH)
195    
196     shapetype = layer.ShapeType()
197     index = self.selected_shape
198     if shapetype == SHAPETYPE_POLYGON:
199     self.draw_polygon_shape(layer, index, pen, brush)
200     else:
201     self.dc.SetBrush(brush)
202     self.dc.SetPen(pen)
203     if shapetype == SHAPETYPE_ARC:
204     f = self.draw_arc_shape
205     elif shapetype == SHAPETYPE_POINT:
206     f = self.draw_point_shape
207     f(layer, index)
208    
209    
210     class PrinterRender(MapRenderer):
211    
212     # When printing we want to see all layers
213     honor_visibility = 0
214    
215     RenderMap = MapRenderer.render_map
216    

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26