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 |
18 |
|
import os.path |
19 |
|
|
20 |
from math import hypot |
from math import hypot |
21 |
|
|
22 |
from wxPython.wx import wxWindow,\ |
from wxPython.wx import wxWindow,\ |
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, \ |
26 |
|
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, \ |
43 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
44 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
45 |
|
|
46 |
|
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 |
|
|
53 |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
54 |
|
SCALE_CHANGED |
55 |
|
|
56 |
|
|
57 |
# |
# |
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 |
|
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 wx.true |
return wx.true |
275 |
|
|
276 |
|
|
291 |
delegated_methods = {"SelectLayer": "selection", |
delegated_methods = {"SelectLayer": "selection", |
292 |
"SelectShapes": "selection", |
"SelectShapes": "selection", |
293 |
"SelectedLayer": "selection", |
"SelectedLayer": "selection", |
294 |
"HasSelectedLayer": "selection"} |
"HasSelectedLayer": "selection", |
295 |
|
"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) |
375 |
|
|
376 |
def OnPaint(self, event): |
def OnPaint(self, event): |
377 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
378 |
if self.map is not None and self.map.HasLayers(): |
clear = self.map is None or not self.map.HasLayers() |
379 |
self.do_redraw() |
|
380 |
else: |
wxBeginBusyCursor() |
381 |
|
|
382 |
|
if not clear: |
383 |
|
try: |
384 |
|
self.do_redraw() |
385 |
|
except: |
386 |
|
print "Error during drawing:", sys.exc_info()[0] |
387 |
|
clear = True |
388 |
|
|
389 |
|
if clear: |
390 |
# If we've got no map or if the map is empty, simply clear |
# If we've got no map or if the map is empty, simply clear |
391 |
# the screen. |
# the screen. |
392 |
|
|
398 |
dc.Clear() |
dc.Clear() |
399 |
dc.EndDrawing() |
dc.EndDrawing() |
400 |
|
|
401 |
|
wxEndBusyCursor() |
402 |
|
|
403 |
def do_redraw(self): |
def do_redraw(self): |
404 |
# This should only be called if we have a non-empty map. |
# This should only be called if we have a non-empty map. |
405 |
|
|
416 |
dc.BeginDrawing() |
dc.BeginDrawing() |
417 |
|
|
418 |
# clear the background |
# clear the background |
419 |
dc.SetBrush(wx.wxWHITE_BRUSH) |
#dc.SetBrush(wx.wxWHITE_BRUSH) |
420 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
#dc.SetPen(wx.wxTRANSPARENT_PEN) |
421 |
dc.DrawRectangle(0, 0, width, height) |
#dc.DrawRectangle(0, 0, width, height) |
422 |
|
dc.SetBackground(wx.wxWHITE_BRUSH) |
423 |
|
dc.Clear() |
424 |
|
|
425 |
selected_layer = self.selection.SelectedLayer() |
selected_layer = self.selection.SelectedLayer() |
426 |
selected_shapes = self.selection.SelectedShapes() |
selected_shapes = self.selection.SelectedShapes() |
445 |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
446 |
clientdc.EndDrawing() |
clientdc.EndDrawing() |
447 |
|
|
448 |
|
def Export(self): |
449 |
|
if hasattr(self, "export_path"): |
450 |
|
export_path = self.export_path |
451 |
|
else: |
452 |
|
export_path="." |
453 |
|
dlg = wxFileDialog(self, _("Export Map"), export_path, "", |
454 |
|
"Enhanced Metafile (*.wmf)|*.wmf", |
455 |
|
wxSAVE|wxOVERWRITE_PROMPT) |
456 |
|
if dlg.ShowModal() == wxID_OK: |
457 |
|
self.export_path = os.path.dirname(dlg.GetPath()) |
458 |
|
dc = wxMetaFileDC(dlg.GetPath()) |
459 |
|
|
460 |
|
scale, offset, mapregion = OutputTransform(self.scale, |
461 |
|
self.offset, |
462 |
|
self.GetSizeTuple(), |
463 |
|
dc.GetSizeTuple()) |
464 |
|
|
465 |
|
selected_layer = self.selection.SelectedLayer() |
466 |
|
selected_shapes = self.selection.SelectedShapes() |
467 |
|
|
468 |
|
renderer = ExportRenderer(dc, scale, offset) |
469 |
|
|
470 |
|
# Pass the entire bitmap as update region to the renderer. |
471 |
|
# We're redrawing the whole bitmap, after all. |
472 |
|
width, height = self.GetSizeTuple() |
473 |
|
renderer.RenderMap(self.map, |
474 |
|
(0,0, |
475 |
|
(width/self.scale)*scale, |
476 |
|
(height/self.scale)*scale), |
477 |
|
mapregion, |
478 |
|
selected_layer, selected_shapes) |
479 |
|
dc.EndDrawing() |
480 |
|
dc.Close() |
481 |
|
dlg.Destroy() |
482 |
|
|
483 |
def Print(self): |
def Print(self): |
484 |
printer = wx.wxPrinter() |
printer = wx.wxPrinter() |
485 |
printout = MapPrintout(self.map) |
width, height = self.GetSizeTuple() |
486 |
|
selected_layer = self.selection.SelectedLayer() |
487 |
|
selected_shapes = self.selection.SelectedShapes() |
488 |
|
|
489 |
|
printout = MapPrintout(self, self.map, (0, 0, width, height), |
490 |
|
selected_layer, selected_shapes) |
491 |
printer.Print(self, printout, wx.true) |
printer.Print(self, printout, wx.true) |
492 |
printout.Destroy() |
printout.Destroy() |
493 |
|
|
528 |
|
|
529 |
def set_view_transform(self, scale, offset): |
def set_view_transform(self, scale, offset): |
530 |
self.scale = scale |
self.scale = scale |
531 |
|
if self.scale < 0.0001: |
532 |
|
self.scale = 0.0001 |
533 |
|
|
534 |
self.offset = offset |
self.offset = offset |
535 |
self.full_redraw() |
self.full_redraw() |
536 |
|
self.issue(SCALE_CHANGED, scale) |
537 |
|
|
538 |
def proj_to_win(self, x, y): |
def proj_to_win(self, x, y): |
539 |
"""\ |
"""\ |
558 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
559 |
llx, lly, urx, ury = rect |
llx, lly, urx, ury = rect |
560 |
if llx == urx or lly == ury: |
if llx == urx or lly == ury: |
561 |
# zero with or zero height. Do Nothing |
# zero width or zero height. Do Nothing |
562 |
return |
return |
563 |
scalex = width / (urx - llx) |
scalex = width / (urx - llx) |
564 |
scaley = height / (ury - lly) |
scaley = height / (ury - lly) |
565 |
scale = min(scalex, scaley) |
scale = min(scalex, scaley) |
566 |
offx = 0.5 * (width - (urx + llx) * scale) |
offx = 0.5 * (width - (urx + llx) * scale) |
567 |
offy = 0.5 * (height + (ury + lly) * scale) |
offy = 0.5 * (height + (ury + lly) * scale) |
568 |
|
print "scalex:", scalex, "scaley:", scaley |
569 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
570 |
|
|
571 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
578 |
if bbox is not None: |
if bbox is not None: |
579 |
self.FitRectToWindow(bbox) |
self.FitRectToWindow(bbox) |
580 |
|
|
581 |
|
def FitLayerToWindow(self, layer): |
582 |
|
"""Fit the given layer to the window. |
583 |
|
|
584 |
|
Set the scale so that the layer fits exactly into the window and |
585 |
|
center it in the window. |
586 |
|
""" |
587 |
|
|
588 |
|
bbox = layer.LatLongBoundingBox() |
589 |
|
if bbox is not None: |
590 |
|
proj = self.map.GetProjection() |
591 |
|
if proj is not None: |
592 |
|
bbox = proj.ForwardBBox(bbox) |
593 |
|
|
594 |
|
if bbox is not None: |
595 |
|
self.FitRectToWindow(bbox) |
596 |
|
|
597 |
|
def FitSelectedToWindow(self): |
598 |
|
layer = self.selection.SelectedLayer() |
599 |
|
shapes = self.selection.SelectedShapes() |
600 |
|
|
601 |
|
bbox = layer.ShapesBoundingBox(shapes) |
602 |
|
if bbox is not None: |
603 |
|
proj = self.map.GetProjection() |
604 |
|
if proj is not None: |
605 |
|
bbox = proj.ForwardBBox(bbox) |
606 |
|
|
607 |
|
if bbox is not None: |
608 |
|
self.FitRectToWindow(bbox) |
609 |
|
|
610 |
def ZoomFactor(self, factor, center = None): |
def ZoomFactor(self, factor, center = None): |
611 |
"""Multiply the zoom by factor and center on center. |
"""Multiply the zoom by factor and center on center. |
612 |
|
|
670 |
def PanTool(self): |
def PanTool(self): |
671 |
"""Start the pan tool""" |
"""Start the pan tool""" |
672 |
self.SelectTool(PanTool(self)) |
self.SelectTool(PanTool(self)) |
673 |
|
#img = resource.GetImageResource("pan", wxBITMAP_TYPE_XPM) |
674 |
|
#bmp = resource.GetBitmapResource("pan", wxBITMAP_TYPE_XPM) |
675 |
|
#print bmp |
676 |
|
#img = wxImageFromBitmap(bmp) |
677 |
|
#print img |
678 |
|
#cur = wxCursor(img) |
679 |
|
#print cur |
680 |
|
#self.SetCursor(cur) |
681 |
|
|
682 |
def IdentifyTool(self): |
def IdentifyTool(self): |
683 |
"""Start the identify tool""" |
"""Start the identify tool""" |
756 |
# Even when the window becomes larger some parts of the bitmap |
# Even when the window becomes larger some parts of the bitmap |
757 |
# could be reused. |
# could be reused. |
758 |
self.full_redraw() |
self.full_redraw() |
759 |
|
pass |
760 |
|
|
761 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
762 |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
1002 |
valign = ALIGN_CENTER |
valign = ALIGN_CENTER |
1003 |
label_layer.AddLabel(x, y, text, |
label_layer.AddLabel(x, y, text, |
1004 |
halign = halign, valign = valign) |
halign = halign, valign = valign) |
1005 |
|
|
1006 |
|
def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend): |
1007 |
|
"""Calculate dimensions to transform canvas content to output device.""" |
1008 |
|
width, height = device_extend |
1009 |
|
|
1010 |
|
# Only 80 % of the with are available for the map |
1011 |
|
width = width * 0.8 |
1012 |
|
|
1013 |
|
# Define the distance of the map from DC border |
1014 |
|
distance = 20 |
1015 |
|
|
1016 |
|
if height < width: |
1017 |
|
# landscape |
1018 |
|
map_height = height - 2*distance |
1019 |
|
map_width = map_height |
1020 |
|
else: |
1021 |
|
# portrait, recalibrate width (usually the legend width is too |
1022 |
|
# small |
1023 |
|
width = width * 0.9 |
1024 |
|
map_height = width - 2*distance |
1025 |
|
map_width = map_height |
1026 |
|
|
1027 |
|
mapregion = (distance, distance, |
1028 |
|
distance+map_width, distance+map_height) |
1029 |
|
|
1030 |
|
canvas_width, canvas_height = canvas_size |
1031 |
|
|
1032 |
|
scalex = map_width / (canvas_width/canvas_scale) |
1033 |
|
scaley = map_height / (canvas_height/canvas_scale) |
1034 |
|
scale = min(scalex, scaley) |
1035 |
|
canvas_offx, canvas_offy = canvas_offset |
1036 |
|
offx = scale*canvas_offx/canvas_scale |
1037 |
|
offy = scale*canvas_offy/canvas_scale |
1038 |
|
|
1039 |
|
return scale, (offx, offy), mapregion |