22 |
wxImageFromStream, wxBITMAP_TYPE_BMP |
wxImageFromStream, wxBITMAP_TYPE_BMP |
23 |
|
|
24 |
from wxproj import draw_polygon_shape, draw_polygon_init |
from wxproj import draw_polygon_shape, draw_polygon_init |
|
from gdalwarp import ProjectRasterFile |
|
25 |
|
|
26 |
from Thuban.UI.common import Color2wxColour |
from Thuban.UI.common import Color2wxColour |
27 |
from Thuban.UI.classifier import ClassDataPreviewer |
from Thuban.UI.classifier import ClassDataPreviewer |
33 |
ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE |
ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE |
34 |
|
|
35 |
from Thuban.Model.classification import Classification |
from Thuban.Model.classification import Classification |
36 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Transparent |
37 |
|
import Thuban.Model.resource |
38 |
|
|
39 |
|
if Thuban.Model.resource.has_gdal_support(): |
40 |
|
from gdalwarp import ProjectRasterFile |
41 |
|
|
42 |
class MapRenderer: |
class MapRenderer: |
43 |
|
|
75 |
self.map = map |
self.map = map |
76 |
seenRaster = True |
seenRaster = True |
77 |
|
|
78 |
|
self.dc.BeginDrawing() |
79 |
|
|
80 |
# |
# |
81 |
# This is only a good optimization if there is only one |
# This is only a good optimization if there is only one |
82 |
# raster layer and the image covers the entire window (as |
# raster layer and the image covers the entire window (as |
83 |
# it currently does). |
# it currently does). We note if there is a raster layer |
84 |
|
# and only begin drawing layers once we have drawn it. |
85 |
|
# That way we avoid drawing layers that won't be seen. |
86 |
# |
# |
87 |
for layer in map.Layers(): |
if Thuban.Model.resource.has_gdal_support(): |
88 |
if isinstance(layer, RasterLayer): |
for layer in map.Layers(): |
89 |
seenRaster = False |
if isinstance(layer, RasterLayer) and layer.Visible(): |
90 |
break |
seenRaster = False |
91 |
|
break |
92 |
|
|
93 |
for layer in map.Layers(): |
for layer in map.Layers(): |
94 |
# if honor_visibility is true, only draw visible layers, |
# if honor_visibility is true, only draw visible layers, |
96 |
if not self.honor_visibility or layer.Visible(): |
if not self.honor_visibility or layer.Visible(): |
97 |
if isinstance(layer, Layer) and seenRaster: |
if isinstance(layer, Layer) and seenRaster: |
98 |
self.draw_shape_layer(layer) |
self.draw_shape_layer(layer) |
99 |
elif isinstance(layer, RasterLayer): |
elif isinstance(layer, RasterLayer) \ |
100 |
|
and Thuban.Model.resource.has_gdal_support(): |
101 |
self.draw_raster_layer(layer) |
self.draw_raster_layer(layer) |
102 |
seenRaster = True |
seenRaster = True |
103 |
|
|
104 |
self.draw_label_layer(map.LabelLayer()) |
self.draw_label_layer(map.LabelLayer()) |
105 |
|
|
106 |
|
self.dc.EndDrawing() |
107 |
|
|
108 |
def draw_shape_layer(self, layer): |
def draw_shape_layer(self, layer): |
109 |
scale = self.scale |
scale = self.scale |
110 |
offx, offy = self.offset |
offx, offy = self.offset |
120 |
old_prop = None |
old_prop = None |
121 |
old_group = None |
old_group = None |
122 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
123 |
field = lc.GetField() |
field = layer.GetClassificationColumn() |
124 |
defaultGroup = lc.GetDefaultGroup() |
defaultGroup = lc.GetDefaultGroup() |
125 |
|
|
126 |
|
|
|
if shapetype != SHAPETYPE_POINT: |
|
|
polygon_render_param = self.polygon_render_param(layer) |
|
127 |
|
|
128 |
if shapetype == SHAPETYPE_POINT: |
if shapetype == SHAPETYPE_POINT: |
129 |
draw_func = lambda i: \ |
draw_func = self.draw_point_shape |
130 |
self.draw_point_shape(layer, i) |
draw_func_param = layer |
131 |
else: |
else: |
132 |
draw_func = lambda i: \ |
draw_func = draw_polygon_shape |
133 |
self.draw_polygon_shape(polygon_render_param, i, pen, brush) |
draw_func_param = self.polygon_render_param(layer) |
134 |
|
|
135 |
|
table = layer.ShapeStore().Table() |
136 |
for i in self.layer_ids(layer): |
for i in self.layer_ids(layer): |
137 |
|
|
138 |
if field is None: |
if field is None: |
139 |
group = defaultGroup |
group = defaultGroup |
140 |
else: |
else: |
141 |
record = layer.table.ReadRowAsDict(i) |
record = table.ReadRowAsDict(i) |
142 |
assert record is not None |
assert record is not None |
143 |
group = lc.FindGroup(record[field]) |
group = lc.FindGroup(record[field]) |
144 |
|
|
157 |
old_prop = prop |
old_prop = prop |
158 |
|
|
159 |
if shapetype == SHAPETYPE_ARC: |
if shapetype == SHAPETYPE_ARC: |
160 |
fill = Color.Transparent |
fill = Transparent |
161 |
else: |
else: |
162 |
fill = prop.GetFill() |
fill = prop.GetFill() |
163 |
|
|
164 |
|
|
165 |
if fill is Color.Transparent: |
if fill is Transparent: |
166 |
brush = wxTRANSPARENT_BRUSH |
brush = wxTRANSPARENT_BRUSH |
167 |
else: |
else: |
168 |
color = Color2wxColour(fill) |
color = Color2wxColour(fill) |
170 |
|
|
171 |
stroke = prop.GetLineColor() |
stroke = prop.GetLineColor() |
172 |
stroke_width = prop.GetLineWidth() |
stroke_width = prop.GetLineWidth() |
173 |
if stroke is Color.Transparent: |
if stroke is Transparent: |
174 |
pen = wxTRANSPARENT_PEN |
pen = wxTRANSPARENT_PEN |
175 |
else: |
else: |
176 |
color = Color2wxColour(stroke) |
color = Color2wxColour(stroke) |
177 |
pen = wxPen(color, stroke_width, wxSOLID) |
pen = wxPen(color, stroke_width, wxSOLID) |
178 |
|
|
179 |
if shapetype == SHAPETYPE_POINT: |
draw_func(draw_func_param, i, pen, brush) |
|
self.dc.SetBrush(brush) |
|
|
self.dc.SetPen(pen) |
|
|
|
|
|
draw_func(i) |
|
180 |
|
|
181 |
def draw_raster_layer(self, layer): |
def draw_raster_layer(self, layer): |
182 |
data = None |
data = None |
195 |
for p in proj.GetAllParameters(): |
for p in proj.GetAllParameters(): |
196 |
outProj += "+" + p + " " |
outProj += "+" + p + " " |
197 |
|
|
|
print "self.scale: ", self.scale, offx, offy, width, height |
|
198 |
xmin = (0 - offx) / self.scale |
xmin = (0 - offx) / self.scale |
199 |
ymin = (offy - height) / self.scale |
ymin = (offy - height) / self.scale |
200 |
xmax = (width - offx) / self.scale |
xmax = (width - offx) / self.scale |
205 |
layer.GetImageFilename(), |
layer.GetImageFilename(), |
206 |
inProj, |
inProj, |
207 |
outProj, |
outProj, |
208 |
str(xmin), str(ymin), str(xmax), str(ymax), |
(xmin, ymin, xmax, ymax), |
209 |
"", str(width), str(height)); |
"", (width, height)) |
210 |
except (AttributeError, IOError, ValueError): |
except IOError, (strerr): |
211 |
|
print strerr |
212 |
|
except (AttributeError, ValueError): |
213 |
pass |
pass |
214 |
else: |
else: |
215 |
if data is not None: |
if data is not None: |
216 |
stream = cStringIO.StringIO(data) |
stream = cStringIO.StringIO(data) |
217 |
image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP) |
image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP) |
218 |
bitmap = wxBitmapFromImage(image) |
bitmap = wxBitmapFromImage(image) |
|
self.dc.BeginDrawing() |
|
219 |
self.dc.DrawBitmap(bitmap, 0, 0) |
self.dc.DrawBitmap(bitmap, 0, 0) |
|
self.dc.EndDrawing() |
|
220 |
|
|
221 |
def layer_ids(self, layer): |
def layer_ids(self, layer): |
222 |
"""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. |
229 |
def polygon_render_param(self, layer): |
def polygon_render_param(self, layer): |
230 |
"""Return the low-lever render parameter for the layer""" |
"""Return the low-lever render parameter for the layer""" |
231 |
offx, offy = self.offset |
offx, offy = self.offset |
232 |
return draw_polygon_init(layer.shapefile, self.dc, |
return draw_polygon_init(layer.ShapeStore().Shapefile(), self.dc, |
233 |
self.map.projection, |
self.map.projection, |
234 |
layer.projection, |
layer.projection, |
235 |
self.scale, -self.scale, |
self.scale, -self.scale, |
266 |
points = self.projected_points(layer, index) |
points = self.projected_points(layer, index) |
267 |
self.dc.DrawLines(points) |
self.dc.DrawLines(points) |
268 |
|
|
269 |
def draw_point_shape(self, layer, index): |
def draw_point_shape(self, layer, index, pen, brush): |
270 |
pp = self.projected_points(layer, index) |
pp = self.projected_points(layer, index) |
271 |
|
|
272 |
if len(pp) == 0: return # ignore Null Shapes which have no points |
if len(pp) == 0: return # ignore Null Shapes which have no points |
273 |
|
|
274 |
p = pp[0] |
p = pp[0] |
275 |
radius = self.resolution * 5 |
radius = self.resolution * 5 |
276 |
|
self.dc.SetBrush(brush) |
277 |
|
self.dc.SetPen(pen) |
278 |
self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius) |
self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius) |
279 |
|
|
280 |
def draw_label_layer(self, layer): |
def draw_label_layer(self, layer): |
355 |
self.dc.SetBrush(brush) |
self.dc.SetBrush(brush) |
356 |
self.dc.SetPen(pen) |
self.dc.SetPen(pen) |
357 |
func = self.draw_point_shape |
func = self.draw_point_shape |
358 |
args = () |
args = (pen, brush) |
359 |
else: |
else: |
360 |
raise TypeError(_("Unhandled shape type %s") % shapetype) |
raise TypeError(_("Unhandled shape type %s") % shapetype) |
361 |
|
|
362 |
for index in self.selected_shapes: |
for index in self.selected_shapes: |
363 |
func(renderparam, index, *args) |
func(renderparam, index, *args) |
364 |
|
|
|
|
|
365 |
def layer_ids(self, layer): |
def layer_ids(self, layer): |
366 |
"""Return the shapeids covered by the region that has to be redrawn |
"""Return the shapeids covered by the region that has to be redrawn |
367 |
|
|
509 |
# Render the legend |
# Render the legend |
510 |
dc.SetTextForeground(wxBLACK) |
dc.SetTextForeground(wxBLACK) |
511 |
if map.HasLayers(): |
if map.HasLayers(): |
512 |
for l in map.Layers(): |
layers = map.Layers() |
513 |
|
layers.reverse() |
514 |
|
for l in layers: |
515 |
if l.Visible(): |
if l.Visible(): |
516 |
# Render title |
# Render title |
517 |
dc.DrawText(l.Title(), posx, posy) |
dc.DrawText(l.Title(), posx, posy) |
518 |
posy+=stepy |
posy+=stepy |
519 |
# Render classification |
if l.HasClassification(): |
520 |
clazz = l.GetClassification() |
# Render classification |
521 |
shapeType = l.ShapeType() |
clazz = l.GetClassification() |
522 |
for g in clazz: |
shapeType = l.ShapeType() |
523 |
if g.IsVisible(): |
for g in clazz: |
524 |
previewer.Draw(dc, |
if g.IsVisible(): |
525 |
wxRect(posx+dx, posy, iconwidth, iconheight), |
previewer.Draw(dc, |
526 |
g.GetProperties(), shapeType) |
wxRect(posx+dx, posy, |
527 |
dc.DrawText(g.GetDisplayText(), |
iconwidth, iconheight), |
528 |
posx+2*dx+iconwidth, posy) |
g.GetProperties(), shapeType) |
529 |
posy+=stepy |
dc.DrawText(g.GetDisplayText(), |
530 |
|
posx+2*dx+iconwidth, posy) |
531 |
|
posy+=stepy |
532 |
|
|
533 |
def render_scalebar(self, map): |
def render_scalebar(self, map): |
534 |
"""Render the scalebar.""" |
"""Render the scalebar.""" |