1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
|
# Frank Koormann <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
12 |
|
|
13 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
14 |
|
|
15 |
|
from Thuban import _ |
16 |
|
|
17 |
import sys |
import sys |
18 |
|
import os.path |
19 |
|
|
20 |
from math import hypot |
from math import hypot |
21 |
|
|
23 |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
24 |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \ |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \ |
25 |
wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \ |
wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \ |
26 |
wxImageFromBitmap |
wxImageFromBitmap, wxPlatform |
27 |
|
|
28 |
|
# Export related stuff |
29 |
|
if wxPlatform == '__WXMSW__': |
30 |
|
from wxPython.wx import wxMetaFileDC |
31 |
|
from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK |
32 |
|
|
33 |
from wxPython import wx |
from wxPython import wx |
34 |
|
|
35 |
from wxproj import point_in_polygon_shape, shape_centroid |
from wxproj import point_in_polygon_shape, shape_centroid |
36 |
|
|
|
|
|
37 |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
38 |
MAP_LAYERS_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED |
MAP_LAYERS_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED |
39 |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
46 |
import resource |
import resource |
47 |
|
|
48 |
from selection import Selection |
from selection import Selection |
49 |
from renderer import ScreenRenderer, PrinterRender |
from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer |
50 |
|
|
51 |
import labeldialog |
import labeldialog |
52 |
|
|
231 |
self.view.LabelShapeAt(event.m_x, event.m_y) |
self.view.LabelShapeAt(event.m_x, event.m_y) |
232 |
|
|
233 |
|
|
|
|
|
|
|
|
234 |
class MapPrintout(wx.wxPrintout): |
class MapPrintout(wx.wxPrintout): |
235 |
|
|
236 |
""" |
""" |
237 |
wxPrintout class for printing Thuban maps |
wxPrintout class for printing Thuban maps |
238 |
""" |
""" |
239 |
|
|
240 |
def __init__(self, map): |
def __init__(self, canvas, map, region, selected_layer, selected_shapes): |
241 |
wx.wxPrintout.__init__(self) |
wx.wxPrintout.__init__(self) |
242 |
|
self.canvas = canvas |
243 |
self.map = map |
self.map = map |
244 |
|
self.region = region |
245 |
|
self.selected_layer = selected_layer |
246 |
|
self.selected_shapes = selected_shapes |
247 |
|
|
248 |
def GetPageInfo(self): |
def GetPageInfo(self): |
249 |
return (1, 1, 1, 1) |
return (1, 1, 1, 1) |
257 |
|
|
258 |
def draw_on_dc(self, dc): |
def draw_on_dc(self, dc): |
259 |
width, height = self.GetPageSizePixels() |
width, height = self.GetPageSizePixels() |
260 |
llx, lly, urx, ury = self.map.ProjectedBoundingBox() |
scale, offset, mapregion = OutputTransform(self.canvas.scale, |
261 |
scalex = width / (urx - llx) |
self.canvas.offset, |
262 |
scaley = height / (ury - lly) |
self.canvas.GetSizeTuple(), |
263 |
scale = min(scalex, scaley) |
self.GetPageSizePixels()) |
|
offx = 0.5 * (width - (urx + llx) * scale) |
|
|
offy = 0.5 * (height + (ury + lly) * scale) |
|
|
|
|
264 |
resx, resy = self.GetPPIPrinter() |
resx, resy = self.GetPPIPrinter() |
265 |
renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx) |
renderer = PrinterRenderer(dc, scale, offset, resolution = resy) |
266 |
renderer.RenderMap(self.map) |
x, y, width, height = self.region |
267 |
return wx.true |
canvas_scale = self.canvas.scale |
268 |
|
renderer.RenderMap(self.map, |
269 |
|
(0,0, |
270 |
|
(width/canvas_scale)*scale, |
271 |
|
(height/canvas_scale)*scale), |
272 |
|
mapregion, |
273 |
|
self.selected_layer, self.selected_shapes) |
274 |
|
return True |
275 |
|
|
276 |
|
|
277 |
class MapCanvas(wxWindow, Publisher): |
class MapCanvas(wxWindow, Publisher): |
292 |
"SelectShapes": "selection", |
"SelectShapes": "selection", |
293 |
"SelectedLayer": "selection", |
"SelectedLayer": "selection", |
294 |
"HasSelectedLayer": "selection", |
"HasSelectedLayer": "selection", |
295 |
"HasSelectedShapes": "selection"} |
"HasSelectedShapes": "selection", |
296 |
|
"SelectedShapes": "selection"} |
297 |
|
|
298 |
def __init__(self, parent, winid): |
def __init__(self, parent, winid): |
299 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
377 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
378 |
clear = self.map is None or not self.map.HasLayers() |
clear = self.map is None or not self.map.HasLayers() |
379 |
|
|
380 |
#wxBeginBusyCursor() |
wxBeginBusyCursor() |
381 |
|
try: |
382 |
if not clear: |
if not clear: |
|
try: |
|
383 |
self.do_redraw() |
self.do_redraw() |
384 |
except: |
try: |
385 |
print "Error during drawing:", sys.exc_info()[0] |
pass |
386 |
clear = True |
except: |
387 |
|
print "Error during drawing:", sys.exc_info()[0] |
388 |
if clear: |
clear = True |
389 |
# If we've got no map or if the map is empty, simply clear |
|
390 |
# the screen. |
if clear: |
391 |
|
# If we've got no map or if the map is empty, simply clear |
392 |
# XXX it's probably possible to get rid of this. The |
# the screen. |
393 |
# background color of the window is already white and the |
|
394 |
# only thing we may have to do is to call self.Refresh() |
# XXX it's probably possible to get rid of this. The |
395 |
# with a true argument in the right places. |
# background color of the window is already white and the |
396 |
dc.BeginDrawing() |
# only thing we may have to do is to call self.Refresh() |
397 |
dc.Clear() |
# with a true argument in the right places. |
398 |
dc.EndDrawing() |
dc.BeginDrawing() |
399 |
|
dc.Clear() |
400 |
#wxEndBusyCursor() |
dc.EndDrawing() |
401 |
|
finally: |
402 |
|
wxEndBusyCursor() |
403 |
|
|
404 |
def do_redraw(self): |
def do_redraw(self): |
405 |
# This should only be called if we have a non-empty map. |
# This should only be called if we have a non-empty map. |
446 |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
447 |
clientdc.EndDrawing() |
clientdc.EndDrawing() |
448 |
|
|
449 |
|
def Export(self): |
450 |
|
if self.scale == 0: |
451 |
|
return |
452 |
|
|
453 |
|
if hasattr(self, "export_path"): |
454 |
|
export_path = self.export_path |
455 |
|
else: |
456 |
|
export_path="." |
457 |
|
dlg = wxFileDialog(self, _("Export Map"), export_path, "", |
458 |
|
"Enhanced Metafile (*.wmf)|*.wmf", |
459 |
|
wxSAVE|wxOVERWRITE_PROMPT) |
460 |
|
if dlg.ShowModal() == wxID_OK: |
461 |
|
self.export_path = os.path.dirname(dlg.GetPath()) |
462 |
|
dc = wxMetaFileDC(dlg.GetPath()) |
463 |
|
|
464 |
|
scale, offset, mapregion = OutputTransform(self.scale, |
465 |
|
self.offset, |
466 |
|
self.GetSizeTuple(), |
467 |
|
dc.GetSizeTuple()) |
468 |
|
|
469 |
|
selected_layer = self.selection.SelectedLayer() |
470 |
|
selected_shapes = self.selection.SelectedShapes() |
471 |
|
|
472 |
|
renderer = ExportRenderer(dc, scale, offset) |
473 |
|
|
474 |
|
# Pass the entire bitmap as update region to the renderer. |
475 |
|
# We're redrawing the whole bitmap, after all. |
476 |
|
width, height = self.GetSizeTuple() |
477 |
|
renderer.RenderMap(self.map, |
478 |
|
(0,0, |
479 |
|
(width/self.scale)*scale, |
480 |
|
(height/self.scale)*scale), |
481 |
|
mapregion, |
482 |
|
selected_layer, selected_shapes) |
483 |
|
dc.EndDrawing() |
484 |
|
dc.Close() |
485 |
|
dlg.Destroy() |
486 |
|
|
487 |
def Print(self): |
def Print(self): |
488 |
printer = wx.wxPrinter() |
printer = wx.wxPrinter() |
489 |
printout = MapPrintout(self.map) |
width, height = self.GetSizeTuple() |
490 |
printer.Print(self, printout, wx.true) |
selected_layer = self.selection.SelectedLayer() |
491 |
|
selected_shapes = self.selection.SelectedShapes() |
492 |
|
|
493 |
|
printout = MapPrintout(self, self.map, (0, 0, width, height), |
494 |
|
selected_layer, selected_shapes) |
495 |
|
printer.Print(self, printout, True) |
496 |
printout.Destroy() |
printout.Destroy() |
497 |
|
|
498 |
def SetMap(self, map): |
def SetMap(self, map): |
531 |
self.full_redraw() |
self.full_redraw() |
532 |
|
|
533 |
def set_view_transform(self, scale, offset): |
def set_view_transform(self, scale, offset): |
534 |
|
# width/height of the projected bbox |
535 |
|
llx, lly, urx, ury = bbox = self.map.ProjectedBoundingBox() |
536 |
|
pwidth = float(urx - llx) |
537 |
|
pheight = float(ury - lly) |
538 |
|
|
539 |
|
# width/height of the window |
540 |
|
wwidth, wheight = self.GetSizeTuple() |
541 |
|
|
542 |
|
# The window's center in projected coordinates assuming the new |
543 |
|
# scale/offset |
544 |
|
pcenterx = (wwidth/2 - offset[0]) / scale |
545 |
|
pcentery = (offset[1] - wheight/2) / scale |
546 |
|
|
547 |
|
# The window coordinates used when drawing the shapes must fit |
548 |
|
# into 16bit signed integers. |
549 |
|
max_len = max(pwidth, pheight) |
550 |
|
if max_len: |
551 |
|
max_scale = 32000.0 / max_len |
552 |
|
else: |
553 |
|
# FIXME: What to do in this case? The bbox is effectively |
554 |
|
# empty so any scale should work. |
555 |
|
max_scale = scale |
556 |
|
|
557 |
|
# The minimal scale is somewhat arbitrarily set to half that of |
558 |
|
# the bbox fit into the window |
559 |
|
scales = [] |
560 |
|
if pwidth: |
561 |
|
scales.append(wwidth / pwidth) |
562 |
|
if pheight: |
563 |
|
scales.append(wheight / pheight) |
564 |
|
if scales: |
565 |
|
min_scale = 0.5 * min(scales) |
566 |
|
else: |
567 |
|
min_scale = scale |
568 |
|
|
569 |
|
if scale > max_scale: |
570 |
|
scale = max_scale |
571 |
|
elif scale < min_scale: |
572 |
|
scale = min_scale |
573 |
|
|
574 |
self.scale = scale |
self.scale = scale |
575 |
self.offset = offset |
|
576 |
|
# determine new offset to preserve the center |
577 |
|
self.offset = (wwidth/2 - scale * pcenterx, |
578 |
|
wheight/2 + scale * pcentery) |
579 |
self.full_redraw() |
self.full_redraw() |
580 |
self.issue(SCALE_CHANGED, scale) |
self.issue(SCALE_CHANGED, scale) |
581 |
|
|
583 |
"""\ |
"""\ |
584 |
Return the point in window coords given by projected coordinates x y |
Return the point in window coords given by projected coordinates x y |
585 |
""" |
""" |
586 |
|
if self.scale == 0: |
587 |
|
return (0, 0) |
588 |
|
|
589 |
offx, offy = self.offset |
offx, offy = self.offset |
590 |
return (self.scale * x + offx, -self.scale * y + offy) |
return (self.scale * x + offx, -self.scale * y + offy) |
591 |
|
|
593 |
"""\ |
"""\ |
594 |
Return the point in projected coordinates given by window coords x y |
Return the point in projected coordinates given by window coords x y |
595 |
""" |
""" |
596 |
|
if self.scale == 0: |
597 |
|
return (0, 0) |
598 |
|
|
599 |
offx, offy = self.offset |
offx, offy = self.offset |
600 |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
601 |
|
|
654 |
bbox = proj.ForwardBBox(bbox) |
bbox = proj.ForwardBBox(bbox) |
655 |
|
|
656 |
if bbox is not None: |
if bbox is not None: |
657 |
self.FitRectToWindow(bbox) |
if len(shapes) == 1 and layer.ShapeType() == SHAPETYPE_POINT: |
658 |
|
self.ZoomFactor(1, self.proj_to_win(bbox[0], bbox[1])) |
659 |
|
else: |
660 |
|
self.FitRectToWindow(bbox) |
661 |
|
|
662 |
def ZoomFactor(self, factor, center = None): |
def ZoomFactor(self, factor, center = None): |
663 |
"""Multiply the zoom by factor and center on center. |
"""Multiply the zoom by factor and center on center. |
666 |
that should be centered. If it is omitted, it defaults to the |
that should be centered. If it is omitted, it defaults to the |
667 |
center of the window |
center of the window |
668 |
""" |
""" |
669 |
width, height = self.GetSizeTuple() |
if self.scale > 0: |
670 |
scale = self.scale * factor |
width, height = self.GetSizeTuple() |
671 |
offx, offy = self.offset |
scale = self.scale * factor |
672 |
if center is not None: |
offx, offy = self.offset |
673 |
cx, cy = center |
if center is not None: |
674 |
else: |
cx, cy = center |
675 |
cx = width / 2 |
else: |
676 |
cy = height / 2 |
cx = width / 2 |
677 |
offset = (factor * (offx - cx) + width / 2, |
cy = height / 2 |
678 |
factor * (offy - cy) + height / 2) |
offset = (factor * (offx - cx) + width / 2, |
679 |
self.set_view_transform(scale, offset) |
factor * (offy - cy) + height / 2) |
680 |
|
self.set_view_transform(scale, offset) |
681 |
|
|
682 |
def ZoomOutToRect(self, rect): |
def ZoomOutToRect(self, rect): |
683 |
"""Zoom out to fit the currently visible region into rect. |
"""Zoom out to fit the currently visible region into rect. |
863 |
forward = None |
forward = None |
864 |
|
|
865 |
scale = self.scale |
scale = self.scale |
866 |
|
|
867 |
|
if scale == 0: |
868 |
|
return None, None |
869 |
|
|
870 |
offx, offy = self.offset |
offx, offy = self.offset |
871 |
|
|
872 |
if select_labels: |
if select_labels: |
1059 |
valign = ALIGN_CENTER |
valign = ALIGN_CENTER |
1060 |
label_layer.AddLabel(x, y, text, |
label_layer.AddLabel(x, y, text, |
1061 |
halign = halign, valign = valign) |
halign = halign, valign = valign) |
1062 |
|
|
1063 |
|
def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend): |
1064 |
|
"""Calculate dimensions to transform canvas content to output device.""" |
1065 |
|
width, height = device_extend |
1066 |
|
|
1067 |
|
# Only 80 % of the with are available for the map |
1068 |
|
width = width * 0.8 |
1069 |
|
|
1070 |
|
# Define the distance of the map from DC border |
1071 |
|
distance = 20 |
1072 |
|
|
1073 |
|
if height < width: |
1074 |
|
# landscape |
1075 |
|
map_height = height - 2*distance |
1076 |
|
map_width = map_height |
1077 |
|
else: |
1078 |
|
# portrait, recalibrate width (usually the legend width is too |
1079 |
|
# small |
1080 |
|
width = width * 0.9 |
1081 |
|
map_height = width - 2*distance |
1082 |
|
map_width = map_height |
1083 |
|
|
1084 |
|
mapregion = (distance, distance, |
1085 |
|
distance+map_width, distance+map_height) |
1086 |
|
|
1087 |
|
canvas_width, canvas_height = canvas_size |
1088 |
|
|
1089 |
|
scalex = map_width / (canvas_width/canvas_scale) |
1090 |
|
scaley = map_height / (canvas_height/canvas_scale) |
1091 |
|
scale = min(scalex, scaley) |
1092 |
|
canvas_offx, canvas_offy = canvas_offset |
1093 |
|
offx = scale*canvas_offx/canvas_scale |
1094 |
|
offy = scale*canvas_offy/canvas_scale |
1095 |
|
|
1096 |
|
return scale, (offx, offy), mapregion |