8 |
|
|
9 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
10 |
|
|
11 |
|
from Thuban import _ |
12 |
|
|
13 |
from wxPython.wx import wxPoint, wxPen, wxBrush, wxFont, \ |
from wxPython.wx import wxPoint, wxPen, wxBrush, wxFont, \ |
14 |
wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \ |
wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \ |
15 |
wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL |
wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL |
16 |
|
|
17 |
from wxproj import draw_polygon_shape |
from wxproj import draw_polygon_shape, draw_polygon_init |
18 |
|
|
19 |
from Thuban import _ |
from Thuban.UI.common import Color2wxColour |
|
from Thuban.UI.common import * |
|
20 |
|
|
21 |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
22 |
SHAPETYPE_POINT |
SHAPETYPE_POINT |
26 |
from Thuban.Model.classification import Classification |
from Thuban.Model.classification import Classification |
27 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
28 |
|
|
|
|
|
29 |
class MapRenderer: |
class MapRenderer: |
30 |
|
|
31 |
"""Class to render a map onto a wxDC""" |
"""Class to render a map onto a wxDC""" |
49 |
""" |
""" |
50 |
# resolution in pixel/inch |
# resolution in pixel/inch |
51 |
|
|
|
assert scale > 0 |
|
|
|
|
52 |
self.dc = dc |
self.dc = dc |
53 |
self.scale = scale |
self.scale = scale |
54 |
self.offset = offset |
self.offset = offset |
80 |
pen = wxTRANSPARENT_PEN |
pen = wxTRANSPARENT_PEN |
81 |
|
|
82 |
old_prop = None |
old_prop = None |
83 |
|
old_group = None |
84 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
85 |
field = lc.GetField() |
field = lc.GetField() |
86 |
|
defaultGroup = lc.GetDefaultGroup() |
87 |
|
|
|
for i in self.layer_ids(layer): |
|
|
value = None |
|
88 |
|
|
89 |
if field is not None: |
if shapetype != SHAPETYPE_POINT: |
90 |
try: |
polygon_render_param = self.polygon_render_param(layer) |
|
record = layer.table.read_record(i) |
|
|
if record is not None: |
|
|
value = record[field] |
|
|
except: |
|
|
pass |
|
|
|
|
|
# |
|
|
# if the above statements fail 'value' should |
|
|
# be null, at which point this call will |
|
|
# at least retreive the NullData |
|
|
# |
|
91 |
|
|
92 |
group = lc.FindGroup(value) |
if shapetype == SHAPETYPE_POINT: |
93 |
|
draw_func = lambda i: \ |
94 |
|
self.draw_point_shape(layer, i) |
95 |
|
else: |
96 |
|
draw_func = lambda i: \ |
97 |
|
self.draw_polygon_shape(polygon_render_param, i, pen, brush) |
98 |
|
|
99 |
|
for i in self.layer_ids(layer): |
100 |
|
|
101 |
#prop = lc.GetProperties(value) |
if field is None: |
102 |
|
group = defaultGroup |
103 |
else: |
else: |
104 |
group = lc.GetDefaultGroup() |
record = layer.table.ReadRowAsDict(i) |
105 |
|
assert record is not None |
106 |
|
group = lc.FindGroup(record[field]) |
107 |
|
|
|
prop = group.GetProperties() |
|
108 |
|
|
109 |
if not group.IsVisible(): |
if not group.IsVisible(): |
110 |
continue |
continue |
111 |
|
|
112 |
|
|
113 |
# don't recreate new objects if they are the same as before |
# don't recreate new objects if they are the same as before |
114 |
if prop != old_prop: |
if group is not old_group: |
115 |
old_prop = prop |
old_group = group |
116 |
|
|
117 |
if shapetype == SHAPETYPE_ARC: |
prop = group.GetProperties() |
118 |
fill = Color.Transparent |
|
119 |
else: |
if prop != old_prop: |
120 |
fill = prop.GetFill() |
old_prop = prop |
121 |
|
|
122 |
if fill is Color.Transparent: |
if shapetype == SHAPETYPE_ARC: |
123 |
brush = wxTRANSPARENT_BRUSH |
fill = Color.Transparent |
124 |
else: |
else: |
125 |
color = Color2wxColour(fill) |
fill = prop.GetFill() |
126 |
brush = wxBrush(color, wxSOLID) |
|
127 |
|
|
128 |
stroke = prop.GetLineColor() |
if fill is Color.Transparent: |
129 |
stroke_width = prop.GetLineWidth() |
brush = wxTRANSPARENT_BRUSH |
130 |
if stroke is Color.Transparent: |
else: |
131 |
pen = wxTRANSPARENT_PEN |
color = Color2wxColour(fill) |
132 |
else: |
brush = wxBrush(color, wxSOLID) |
133 |
color = Color2wxColour(stroke) |
|
134 |
pen = wxPen(color, stroke_width, wxSOLID) |
stroke = prop.GetLineColor() |
135 |
|
stroke_width = prop.GetLineWidth() |
136 |
if shapetype == SHAPETYPE_POINT: |
if stroke is Color.Transparent: |
137 |
self.dc.SetBrush(brush) |
pen = wxTRANSPARENT_PEN |
138 |
self.dc.SetPen(pen) |
else: |
139 |
self.draw_point_shape(layer, i) |
color = Color2wxColour(stroke) |
140 |
else: |
pen = wxPen(color, stroke_width, wxSOLID) |
141 |
self.draw_polygon_shape(layer, i, pen, brush) |
|
142 |
|
if shapetype == SHAPETYPE_POINT: |
143 |
|
self.dc.SetBrush(brush) |
144 |
|
self.dc.SetPen(pen) |
145 |
|
|
146 |
|
draw_func(i) |
147 |
|
|
148 |
def layer_ids(self, layer): |
def layer_ids(self, layer): |
149 |
"""Return the shape ids of the given layer that have to be drawn. |
"""Return the shape ids of the given layer that have to be drawn. |
150 |
|
|
151 |
The default implementation simply returns all ids in the layer. |
The default implementation simply returns all ids in the layer. |
152 |
Override in derived classes to be more precise. |
Override in derived classes to be more precise. |
153 |
""" |
""" |
154 |
return range(layer.NumShapes()) |
return range(layer.NumShapes()) |
155 |
|
|
156 |
def draw_polygon_shape(self, layer, index, pen, brush): |
def polygon_render_param(self, layer): |
157 |
offx, offy = self.offset |
"""Return the low-lever render parameter for the layer""" |
158 |
draw_polygon_shape(layer.shapefile.cobject(), index, |
offx, offy = self.offset |
159 |
self.dc, pen, brush, |
return draw_polygon_init(layer.shapefile, self.dc, |
160 |
self.map.projection, layer.projection, |
self.map.projection, |
161 |
self.scale, -self.scale, offx, offy) |
layer.projection, |
162 |
|
self.scale, -self.scale, |
163 |
|
offx, offy) |
164 |
|
|
165 |
|
def draw_polygon_shape(self, draw_polygon_info, index, pen, brush): |
166 |
|
draw_polygon_shape(draw_polygon_info, index, pen, brush) |
167 |
|
|
168 |
def projected_points(self, layer, index): |
def projected_points(self, layer, index): |
169 |
proj = self.map.projection |
proj = self.map.GetProjection() |
170 |
if proj is not None: |
if proj is not None: |
171 |
forward = proj.Forward |
forward = proj.Forward |
172 |
else: |
else: |
173 |
forward = None |
forward = None |
174 |
proj = layer.projection |
proj = layer.GetProjection() |
175 |
if proj is not None: |
if proj is not None: |
176 |
inverse = proj.Inverse |
inverse = proj.Inverse |
177 |
else: |
else: |
267 |
|
|
268 |
shapetype = layer.ShapeType() |
shapetype = layer.ShapeType() |
269 |
if shapetype == SHAPETYPE_POLYGON: |
if shapetype == SHAPETYPE_POLYGON: |
270 |
|
offx, offy = self.offset |
271 |
|
renderparam = self.polygon_render_param(layer) |
272 |
func = self.draw_polygon_shape |
func = self.draw_polygon_shape |
273 |
args = (pen, brush) |
args = (pen, brush) |
274 |
elif shapetype == SHAPETYPE_ARC: |
elif shapetype == SHAPETYPE_ARC: |
275 |
|
renderparam = self.polygon_render_param(layer) |
276 |
func = self.draw_polygon_shape |
func = self.draw_polygon_shape |
277 |
args = (pen, None) |
args = (pen, None) |
278 |
elif shapetype == SHAPETYPE_POINT: |
elif shapetype == SHAPETYPE_POINT: |
279 |
|
renderparam = layer |
280 |
self.dc.SetBrush(brush) |
self.dc.SetBrush(brush) |
281 |
self.dc.SetPen(pen) |
self.dc.SetPen(pen) |
282 |
func = self.draw_point_shape |
func = self.draw_point_shape |
285 |
raise TypeError(_("Unhandled shape type %s") % shapetype) |
raise TypeError(_("Unhandled shape type %s") % shapetype) |
286 |
|
|
287 |
for index in self.selected_shapes: |
for index in self.selected_shapes: |
288 |
func(layer, index, *args) |
func(renderparam, index, *args) |
289 |
|
|
290 |
|
|
291 |
def layer_ids(self, layer): |
def layer_ids(self, layer): |
292 |
"""Return the shapeids covered by the region that has to be redrawn |
"""Return the shapeids covered by the region that has to be redrawn |