1 |
# Copyright (c) 2001 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_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 |
LAYERS_CHANGED, LAYER_LEGEND_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, \ |
40 |
SHAPETYPE_POINT |
SHAPETYPE_POINT |
41 |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
42 |
ALIGN_LEFT, ALIGN_RIGHT |
ALIGN_LEFT, ALIGN_RIGHT |
43 |
|
from Thuban.Lib.connector import Publisher |
44 |
|
from Thuban.Model.color import Color |
45 |
|
|
46 |
|
import resource |
47 |
|
|
48 |
from renderer import ScreenRenderer, PrinterRender |
from selection import Selection |
49 |
|
from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer |
50 |
|
|
51 |
import labeldialog |
import labeldialog |
52 |
|
|
53 |
from messages import SELECTED_SHAPE |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
54 |
|
SCALE_CHANGED |
55 |
|
|
56 |
|
|
57 |
# |
# |
143 |
def MouseUp(self, event): |
def MouseUp(self, event): |
144 |
if self.dragging: |
if self.dragging: |
145 |
Tool.MouseUp(self, event) |
Tool.MouseUp(self, event) |
146 |
self.view.FitRectToWindow(self.proj_rect()) |
sx, sy = self.start |
147 |
|
cx, cy = self.current |
148 |
|
if sx == cx or sy == cy: |
149 |
|
# Just a mouse click or a degenerate rectangle. Simply |
150 |
|
# zoom in by a factor of two |
151 |
|
# FIXME: For a click this is the desired behavior but should we |
152 |
|
# really do this for degenrate rectagles as well or |
153 |
|
# should we ignore them? |
154 |
|
self.view.ZoomFactor(2, center = (cx, cy)) |
155 |
|
else: |
156 |
|
# A drag. Zoom in to the rectangle |
157 |
|
self.view.FitRectToWindow(self.proj_rect()) |
158 |
|
|
159 |
|
|
160 |
class ZoomOutTool(RectTool): |
class ZoomOutTool(RectTool): |
161 |
|
|
162 |
"""The Zoom-Out Tool""" |
"""The Zoom-Out Tool""" |
163 |
|
|
164 |
def Name(self): |
def Name(self): |
165 |
return "ZoomOutTool" |
return "ZoomOutTool" |
166 |
|
|
169 |
Tool.MouseUp(self, event) |
Tool.MouseUp(self, event) |
170 |
sx, sy = self.start |
sx, sy = self.start |
171 |
cx, cy = self.current |
cx, cy = self.current |
172 |
self.view.ZoomOutToRect((min(sx, cx), min(sy, cy), |
if sx == cx or sy == cy: |
173 |
max(sx, cx), max(sy, cy))) |
# Just a mouse click or a degenerate rectangle. Simply |
174 |
|
# zoom out by a factor of two. |
175 |
|
# FIXME: For a click this is the desired behavior but should we |
176 |
|
# really do this for degenrate rectagles as well or |
177 |
|
# should we ignore them? |
178 |
|
self.view.ZoomFactor(0.5, center = (cx, cy)) |
179 |
|
else: |
180 |
|
# A drag. Zoom out to the rectangle |
181 |
|
self.view.ZoomOutToRect((min(sx, cx), min(sy, cy), |
182 |
|
max(sx, cx), max(sy, cy))) |
183 |
|
|
184 |
|
|
185 |
class PanTool(Tool): |
class PanTool(Tool): |
191 |
|
|
192 |
def MouseMove(self, event): |
def MouseMove(self, event): |
193 |
if self.dragging: |
if self.dragging: |
|
x0, y0 = self.current |
|
194 |
Tool.MouseMove(self, event) |
Tool.MouseMove(self, event) |
195 |
|
sx, sy = self.start |
196 |
x, y = self.current |
x, y = self.current |
197 |
width, height = self.view.GetSizeTuple() |
width, height = self.view.GetSizeTuple() |
198 |
|
|
199 |
|
bitmapdc = wx.wxMemoryDC() |
200 |
|
bitmapdc.SelectObject(self.view.bitmap) |
201 |
|
|
202 |
dc = self.view.drag_dc |
dc = self.view.drag_dc |
203 |
dc.Blit(0, 0, width, height, dc, x0 - x, y0 - y) |
dc.Blit(0, 0, width, height, bitmapdc, sx - x, sy - y) |
204 |
|
|
205 |
def MouseUp(self, event): |
def MouseUp(self, event): |
206 |
if self.dragging: |
if self.dragging: |
208 |
sx, sy = self.start |
sx, sy = self.start |
209 |
cx, cy = self.current |
cx, cy = self.current |
210 |
self.view.Translate(cx - sx, cy - sy) |
self.view.Translate(cx - sx, cy - sy) |
211 |
|
|
212 |
class IdentifyTool(Tool): |
class IdentifyTool(Tool): |
213 |
|
|
214 |
"""The "Identify" Tool""" |
"""The "Identify" Tool""" |
215 |
|
|
216 |
def Name(self): |
def Name(self): |
217 |
return "IdentifyTool" |
return "IdentifyTool" |
218 |
|
|
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 |
class MapCanvas(wxWindow): |
|
277 |
|
class MapCanvas(wxWindow, Publisher): |
278 |
|
|
279 |
"""A widget that displays a map and offers some interaction""" |
"""A widget that displays a map and offers some interaction""" |
280 |
|
|
281 |
def __init__(self, parent, winid, interactor): |
# Some messages that can be subscribed/unsubscribed directly through |
282 |
|
# the MapCanvas come in fact from other objects. This is a dict |
283 |
|
# mapping those messages to the names of the instance variables they |
284 |
|
# actually come from. The delegation is implemented in the Subscribe |
285 |
|
# and Unsubscribe methods |
286 |
|
delegated_messages = {LAYER_SELECTED: "selection", |
287 |
|
SHAPES_SELECTED: "selection"} |
288 |
|
|
289 |
|
# Methods delegated to some instance variables. The delegation is |
290 |
|
# implemented in the __getattr__ method. |
291 |
|
delegated_methods = {"SelectLayer": "selection", |
292 |
|
"SelectShapes": "selection", |
293 |
|
"SelectedLayer": "selection", |
294 |
|
"HasSelectedLayer": "selection", |
295 |
|
"HasSelectedShapes": "selection", |
296 |
|
"SelectedShapes": "selection"} |
297 |
|
|
298 |
|
def __init__(self, parent, winid): |
299 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
300 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
301 |
|
|
302 |
|
# the map displayed in this canvas. Set with SetMap() |
303 |
self.map = None |
self.map = None |
304 |
|
|
305 |
|
# scale and offset describe the transformation from projected |
306 |
|
# coordinates to window coordinates. |
307 |
self.scale = 1.0 |
self.scale = 1.0 |
308 |
self.offset = (0, 0) |
self.offset = (0, 0) |
309 |
|
|
310 |
|
# whether the user is currently dragging the mouse, i.e. moving |
311 |
|
# the mouse while pressing a mouse button |
312 |
self.dragging = 0 |
self.dragging = 0 |
313 |
|
|
314 |
|
# the currently active tool |
315 |
self.tool = None |
self.tool = None |
316 |
self.redraw_on_idle = 0 |
|
317 |
|
# The current mouse position of the last OnMotion event or None |
318 |
|
# if the mouse is outside the window. |
319 |
|
self.current_position = None |
320 |
|
|
321 |
|
# the bitmap serving as backing store |
322 |
|
self.bitmap = None |
323 |
|
|
324 |
|
# the selection |
325 |
|
self.selection = Selection() |
326 |
|
self.selection.Subscribe(SHAPES_SELECTED , self.shape_selected) |
327 |
|
|
328 |
|
# keep track of which layers/shapes are selected to make sure we |
329 |
|
# only redraw when necessary |
330 |
|
self.last_selected_layer = None |
331 |
|
self.last_selected_shape = None |
332 |
|
|
333 |
|
# subscribe the WX events we're interested in |
334 |
EVT_PAINT(self, self.OnPaint) |
EVT_PAINT(self, self.OnPaint) |
335 |
EVT_LEFT_DOWN(self, self.OnLeftDown) |
EVT_LEFT_DOWN(self, self.OnLeftDown) |
336 |
EVT_LEFT_UP(self, self.OnLeftUp) |
EVT_LEFT_UP(self, self.OnLeftUp) |
337 |
EVT_MOTION(self, self.OnMotion) |
EVT_MOTION(self, self.OnMotion) |
338 |
wx.EVT_IDLE(self, self.OnIdle) |
EVT_LEAVE_WINDOW(self, self.OnLeaveWindow) |
339 |
self.interactor = interactor |
wx.EVT_SIZE(self, self.OnSize) |
340 |
self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected) |
|
341 |
|
def __del__(self): |
342 |
|
wxWindow.__del__(self) |
343 |
|
Publisher.__del__(self) |
344 |
|
|
345 |
|
def Subscribe(self, channel, *args): |
346 |
|
"""Extend the inherited method to handle delegated messages. |
347 |
|
|
348 |
|
If channel is one of the delegated messages call the appropriate |
349 |
|
object's Subscribe method. Otherwise just call the inherited |
350 |
|
method. |
351 |
|
""" |
352 |
|
if channel in self.delegated_messages: |
353 |
|
object = getattr(self, self.delegated_messages[channel]) |
354 |
|
object.Subscribe(channel, *args) |
355 |
|
else: |
356 |
|
Publisher.Subscribe(self, channel, *args) |
357 |
|
|
358 |
|
def Unsubscribe(self, channel, *args): |
359 |
|
"""Extend the inherited method to handle delegated messages. |
360 |
|
|
361 |
|
If channel is one of the delegated messages call the appropriate |
362 |
|
object's Unsubscribe method. Otherwise just call the inherited |
363 |
|
method. |
364 |
|
""" |
365 |
|
if channel in self.delegated_messages: |
366 |
|
object = getattr(self, self.delegated_messages[channel]) |
367 |
|
object.Unsubscribe(channel, *args) |
368 |
|
else: |
369 |
|
Publisher.Unsubscribe(self, channel, *args) |
370 |
|
|
371 |
|
def __getattr__(self, attr): |
372 |
|
if attr in self.delegated_methods: |
373 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
374 |
|
raise AttributeError(attr) |
375 |
|
|
376 |
def OnPaint(self, event): |
def OnPaint(self, event): |
377 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
378 |
if self.map is None or not self.map.HasLayers(): |
clear = self.map is None or not self.map.HasLayers() |
|
return |
|
|
self.redraw_on_idle = 1 |
|
379 |
|
|
380 |
def do_redraw(self): |
wxBeginBusyCursor() |
|
width, height = self.GetSizeTuple() |
|
|
bitmap = wx.wxEmptyBitmap(width, height) |
|
381 |
|
|
382 |
dc = wx.wxMemoryDC() |
if not clear: |
383 |
dc.SelectObject(bitmap) |
self.do_redraw() |
384 |
|
try: |
385 |
|
pass |
386 |
|
except: |
387 |
|
print "Error during drawing:", sys.exc_info()[0] |
388 |
|
clear = True |
389 |
|
|
390 |
|
if clear: |
391 |
|
# If we've got no map or if the map is empty, simply clear |
392 |
|
# the screen. |
393 |
|
|
394 |
|
# XXX it's probably possible to get rid of this. The |
395 |
|
# background color of the window is already white and the |
396 |
|
# only thing we may have to do is to call self.Refresh() |
397 |
|
# with a true argument in the right places. |
398 |
|
dc.BeginDrawing() |
399 |
|
dc.Clear() |
400 |
|
dc.EndDrawing() |
401 |
|
|
402 |
|
wxEndBusyCursor() |
403 |
|
|
404 |
dc.BeginDrawing() |
def do_redraw(self): |
405 |
|
# This should only be called if we have a non-empty map. |
406 |
|
|
407 |
dc.SetBrush(wx.wxWHITE_BRUSH) |
# Get the window size. |
408 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
width, height = self.GetSizeTuple() |
409 |
dc.DrawRectangle(0, 0, width, height) |
|
410 |
|
# If self.bitmap's still there, reuse it. Otherwise redraw it |
411 |
if 1: #self.interactor.selected_map is self.map: |
if self.bitmap is not None: |
412 |
selected_layer = self.interactor.selected_layer |
bitmap = self.bitmap |
|
selected_shape = self.interactor.selected_shape |
|
413 |
else: |
else: |
414 |
selected_layer = None |
bitmap = wx.wxEmptyBitmap(width, height) |
415 |
selected_shape = None |
dc = wx.wxMemoryDC() |
416 |
|
dc.SelectObject(bitmap) |
417 |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
dc.BeginDrawing() |
418 |
renderer.RenderMap(self.map, selected_layer, selected_shape) |
|
419 |
|
# clear the background |
420 |
|
#dc.SetBrush(wx.wxWHITE_BRUSH) |
421 |
|
#dc.SetPen(wx.wxTRANSPARENT_PEN) |
422 |
|
#dc.DrawRectangle(0, 0, width, height) |
423 |
|
dc.SetBackground(wx.wxWHITE_BRUSH) |
424 |
|
dc.Clear() |
425 |
|
|
426 |
|
selected_layer = self.selection.SelectedLayer() |
427 |
|
selected_shapes = self.selection.SelectedShapes() |
428 |
|
|
429 |
|
# draw the map into the bitmap |
430 |
|
renderer = ScreenRenderer(dc, self.scale, self.offset) |
431 |
|
|
432 |
|
# Pass the entire bitmap as update region to the renderer. |
433 |
|
# We're redrawing the whole bitmap, after all. |
434 |
|
renderer.RenderMap(self.map, (0, 0, width, height), |
435 |
|
selected_layer, selected_shapes) |
436 |
|
|
437 |
|
dc.EndDrawing() |
438 |
|
dc.SelectObject(wx.wxNullBitmap) |
439 |
|
self.bitmap = bitmap |
440 |
|
|
441 |
|
# blit the bitmap to the screen |
442 |
|
dc = wx.wxMemoryDC() |
443 |
|
dc.SelectObject(bitmap) |
444 |
clientdc = wxClientDC(self) |
clientdc = wxClientDC(self) |
445 |
clientdc.BeginDrawing() |
clientdc.BeginDrawing() |
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 |
printout.Destroy() |
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() |
497 |
|
|
498 |
def SetMap(self, map): |
def SetMap(self, map): |
499 |
redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED, |
500 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
501 |
if self.map is not None: |
if self.map is not None: |
502 |
for channel in redraw_channels: |
for channel in redraw_channels: |
503 |
self.map.Unsubscribe(channel, self.redraw) |
self.map.Unsubscribe(channel, self.full_redraw) |
504 |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
505 |
self.projection_changed) |
self.projection_changed) |
506 |
self.map = map |
self.map = map |
507 |
|
self.selection.ClearSelection() |
508 |
if self.map is not None: |
if self.map is not None: |
509 |
for channel in redraw_channels: |
for channel in redraw_channels: |
510 |
self.map.Subscribe(channel, self.redraw) |
self.map.Subscribe(channel, self.full_redraw) |
511 |
self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed) |
self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed) |
512 |
self.FitMapToWindow() |
self.FitMapToWindow() |
513 |
|
# force a redraw. If map is not empty, it's already been called |
514 |
|
# by FitMapToWindow but if map is empty it hasn't been called |
515 |
|
# yet so we have to explicitly call it. |
516 |
|
self.full_redraw() |
517 |
|
|
518 |
def Map(self): |
def Map(self): |
519 |
|
"""Return the map displayed by this canvas""" |
520 |
return self.map |
return self.map |
521 |
|
|
522 |
def redraw(self, *args): |
def redraw(self, *args): |
523 |
self.Refresh(0) |
self.Refresh(0) |
524 |
|
|
525 |
|
def full_redraw(self, *args): |
526 |
|
self.bitmap = None |
527 |
|
self.redraw() |
528 |
|
|
529 |
def projection_changed(self, *args): |
def projection_changed(self, *args): |
530 |
self.FitMapToWindow() |
self.FitMapToWindow() |
531 |
self.redraw() |
self.full_redraw() |
532 |
|
|
533 |
def set_view_transform(self, scale, offset): |
def set_view_transform(self, scale, offset): |
534 |
self.scale = scale |
self.scale = scale |
535 |
|
|
536 |
self.offset = offset |
self.offset = offset |
537 |
self.redraw() |
self.full_redraw() |
538 |
|
self.issue(SCALE_CHANGED, scale) |
539 |
|
|
540 |
def proj_to_win(self, x, y): |
def proj_to_win(self, x, y): |
541 |
"""\ |
"""\ |
542 |
Return the point in window coords given by projected coordinates x y |
Return the point in window coords given by projected coordinates x y |
543 |
""" |
""" |
544 |
|
if self.scale == 0: |
545 |
|
return (0, 0) |
546 |
|
|
547 |
offx, offy = self.offset |
offx, offy = self.offset |
548 |
return (self.scale * x + offx, -self.scale * y + offy) |
return (self.scale * x + offx, -self.scale * y + offy) |
549 |
|
|
551 |
"""\ |
"""\ |
552 |
Return the point in projected coordinates given by window coords x y |
Return the point in projected coordinates given by window coords x y |
553 |
""" |
""" |
554 |
|
if self.scale == 0: |
555 |
|
return (0, 0) |
556 |
|
|
557 |
offx, offy = self.offset |
offx, offy = self.offset |
558 |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
559 |
|
|
560 |
def FitRectToWindow(self, rect): |
def FitRectToWindow(self, rect): |
561 |
|
"""Fit the rectangular region given by rect into the window. |
562 |
|
|
563 |
|
Set scale so that rect (in projected coordinates) just fits into |
564 |
|
the window and center it. |
565 |
|
""" |
566 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
567 |
llx, lly, urx, ury = rect |
llx, lly, urx, ury = rect |
568 |
|
if llx == urx or lly == ury: |
569 |
|
# zero width or zero height. Do Nothing |
570 |
|
return |
571 |
scalex = width / (urx - llx) |
scalex = width / (urx - llx) |
572 |
scaley = height / (ury - lly) |
scaley = height / (ury - lly) |
573 |
scale = min(scalex, scaley) |
scale = min(scalex, scaley) |
576 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
577 |
|
|
578 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
579 |
"""\ |
"""Fit the map to the window |
580 |
Set the scale and offset so that the map is centered in the |
|
581 |
window |
Set the scale so that the map fits exactly into the window and |
582 |
|
center it in the window. |
583 |
""" |
""" |
584 |
bbox = self.map.ProjectedBoundingBox() |
bbox = self.map.ProjectedBoundingBox() |
585 |
if bbox is not None: |
if bbox is not None: |
586 |
self.FitRectToWindow(bbox) |
self.FitRectToWindow(bbox) |
587 |
|
|
588 |
def ZoomFactor(self, factor): |
def FitLayerToWindow(self, layer): |
589 |
width, height = self.GetSizeTuple() |
"""Fit the given layer to the window. |
590 |
scale = self.scale * factor |
|
591 |
offx, offy = self.offset |
Set the scale so that the layer fits exactly into the window and |
592 |
offset = (factor * (offx - width / 2) + width / 2, |
center it in the window. |
593 |
factor * (offy - height / 2) + height / 2) |
""" |
594 |
self.set_view_transform(scale, offset) |
|
595 |
|
bbox = layer.LatLongBoundingBox() |
596 |
|
if bbox is not None: |
597 |
|
proj = self.map.GetProjection() |
598 |
|
if proj is not None: |
599 |
|
bbox = proj.ForwardBBox(bbox) |
600 |
|
|
601 |
|
if bbox is not None: |
602 |
|
self.FitRectToWindow(bbox) |
603 |
|
|
604 |
|
def FitSelectedToWindow(self): |
605 |
|
layer = self.selection.SelectedLayer() |
606 |
|
shapes = self.selection.SelectedShapes() |
607 |
|
|
608 |
|
bbox = layer.ShapesBoundingBox(shapes) |
609 |
|
if bbox is not None: |
610 |
|
proj = self.map.GetProjection() |
611 |
|
if proj is not None: |
612 |
|
bbox = proj.ForwardBBox(bbox) |
613 |
|
|
614 |
|
if bbox is not None: |
615 |
|
self.FitRectToWindow(bbox) |
616 |
|
|
617 |
|
def ZoomFactor(self, factor, center = None): |
618 |
|
"""Multiply the zoom by factor and center on center. |
619 |
|
|
620 |
|
The optional parameter center is a point in window coordinates |
621 |
|
that should be centered. If it is omitted, it defaults to the |
622 |
|
center of the window |
623 |
|
""" |
624 |
|
if self.scale > 0: |
625 |
|
width, height = self.GetSizeTuple() |
626 |
|
scale = self.scale * factor |
627 |
|
offx, offy = self.offset |
628 |
|
if center is not None: |
629 |
|
cx, cy = center |
630 |
|
else: |
631 |
|
cx = width / 2 |
632 |
|
cy = height / 2 |
633 |
|
offset = (factor * (offx - cx) + width / 2, |
634 |
|
factor * (offy - cy) + height / 2) |
635 |
|
self.set_view_transform(scale, offset) |
636 |
|
|
637 |
def ZoomOutToRect(self, rect): |
def ZoomOutToRect(self, rect): |
638 |
# rect is given in window coordinates |
"""Zoom out to fit the currently visible region into rect. |
639 |
|
|
640 |
|
The rect parameter is given in window coordinates |
641 |
|
""" |
642 |
# determine the bbox of the displayed region in projected |
# determine the bbox of the displayed region in projected |
643 |
# coordinates |
# coordinates |
644 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
655 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
656 |
|
|
657 |
def Translate(self, dx, dy): |
def Translate(self, dx, dy): |
658 |
|
"""Move the map by dx, dy pixels""" |
659 |
offx, offy = self.offset |
offx, offy = self.offset |
660 |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
661 |
|
|
662 |
|
def SelectTool(self, tool): |
663 |
|
"""Make tool the active tool. |
664 |
|
|
665 |
|
The parameter should be an instance of Tool or None to indicate |
666 |
|
that no tool is active. |
667 |
|
""" |
668 |
|
self.tool = tool |
669 |
|
|
670 |
def ZoomInTool(self): |
def ZoomInTool(self): |
671 |
self.tool = ZoomInTool(self) |
"""Start the zoom in tool""" |
672 |
|
self.SelectTool(ZoomInTool(self)) |
673 |
|
|
674 |
def ZoomOutTool(self): |
def ZoomOutTool(self): |
675 |
self.tool = ZoomOutTool(self) |
"""Start the zoom out tool""" |
676 |
|
self.SelectTool(ZoomOutTool(self)) |
677 |
|
|
678 |
def PanTool(self): |
def PanTool(self): |
679 |
self.tool = PanTool(self) |
"""Start the pan tool""" |
680 |
|
self.SelectTool(PanTool(self)) |
681 |
|
#img = resource.GetImageResource("pan", wxBITMAP_TYPE_XPM) |
682 |
|
#bmp = resource.GetBitmapResource("pan", wxBITMAP_TYPE_XPM) |
683 |
|
#print bmp |
684 |
|
#img = wxImageFromBitmap(bmp) |
685 |
|
#print img |
686 |
|
#cur = wxCursor(img) |
687 |
|
#print cur |
688 |
|
#self.SetCursor(cur) |
689 |
|
|
690 |
def IdentifyTool(self): |
def IdentifyTool(self): |
691 |
self.tool = IdentifyTool(self) |
"""Start the identify tool""" |
692 |
|
self.SelectTool(IdentifyTool(self)) |
693 |
|
|
694 |
def LabelTool(self): |
def LabelTool(self): |
695 |
self.tool = LabelTool(self) |
"""Start the label tool""" |
696 |
|
self.SelectTool(LabelTool(self)) |
697 |
|
|
698 |
def CurrentTool(self): |
def CurrentTool(self): |
699 |
|
"""Return the name of the current tool or None if no tool is active""" |
700 |
return self.tool and self.tool.Name() or None |
return self.tool and self.tool.Name() or None |
701 |
|
|
702 |
|
def CurrentPosition(self): |
703 |
|
"""Return current position of the mouse in projected coordinates. |
704 |
|
|
705 |
|
The result is a 2-tuple of floats with the coordinates. If the |
706 |
|
mouse is not in the window, the result is None. |
707 |
|
""" |
708 |
|
if self.current_position is not None: |
709 |
|
x, y = self.current_position |
710 |
|
return self.win_to_proj(x, y) |
711 |
|
else: |
712 |
|
return None |
713 |
|
|
714 |
|
def set_current_position(self, event): |
715 |
|
"""Set the current position from event |
716 |
|
|
717 |
|
Should be called by all events that contain mouse positions |
718 |
|
especially EVT_MOTION. The event paramete may be None to |
719 |
|
indicate the the pointer left the window. |
720 |
|
""" |
721 |
|
if event is not None: |
722 |
|
self.current_position = (event.m_x, event.m_y) |
723 |
|
else: |
724 |
|
self.current_position = None |
725 |
|
self.issue(VIEW_POSITION) |
726 |
|
|
727 |
def OnLeftDown(self, event): |
def OnLeftDown(self, event): |
728 |
|
self.set_current_position(event) |
729 |
if self.tool is not None: |
if self.tool is not None: |
730 |
self.drag_dc = wxClientDC(self) |
self.drag_dc = wxClientDC(self) |
731 |
self.drag_dc.SetLogicalFunction(wxINVERT) |
self.drag_dc.SetLogicalFunction(wxINVERT) |
734 |
self.tool.MouseDown(event) |
self.tool.MouseDown(event) |
735 |
self.tool.Show(self.drag_dc) |
self.tool.Show(self.drag_dc) |
736 |
self.dragging = 1 |
self.dragging = 1 |
737 |
|
|
738 |
def OnLeftUp(self, event): |
def OnLeftUp(self, event): |
739 |
self.ReleaseMouse() |
self.set_current_position(event) |
740 |
if self.dragging: |
if self.dragging: |
741 |
self.tool.Hide(self.drag_dc) |
self.ReleaseMouse() |
742 |
self.tool.MouseUp(event) |
try: |
743 |
self.drag_dc = None |
self.tool.Hide(self.drag_dc) |
744 |
self.dragging = 0 |
self.tool.MouseUp(event) |
745 |
|
finally: |
746 |
|
self.drag_dc = None |
747 |
|
self.dragging = 0 |
748 |
|
|
749 |
def OnMotion(self, event): |
def OnMotion(self, event): |
750 |
|
self.set_current_position(event) |
751 |
if self.dragging: |
if self.dragging: |
752 |
self.tool.Hide(self.drag_dc) |
self.tool.Hide(self.drag_dc) |
753 |
self.tool.MouseMove(event) |
self.tool.MouseMove(event) |
754 |
self.tool.Show(self.drag_dc) |
self.tool.Show(self.drag_dc) |
755 |
|
|
756 |
def OnIdle(self, event): |
def OnLeaveWindow(self, event): |
757 |
if self.redraw_on_idle: |
self.set_current_position(None) |
758 |
self.do_redraw() |
|
759 |
self.redraw_on_idle = 0 |
def OnSize(self, event): |
760 |
|
# the window's size has changed. We have to get a new bitmap. If |
761 |
|
# we want to be clever we could try to get by without throwing |
762 |
|
# everything away. E.g. when the window gets smaller, we could |
763 |
|
# either keep the bitmap or create the new one from the old one. |
764 |
|
# Even when the window becomes larger some parts of the bitmap |
765 |
|
# could be reused. |
766 |
|
self.full_redraw() |
767 |
|
pass |
768 |
|
|
769 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
770 |
self.redraw() |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
771 |
|
# The selection object takes care that it only issues |
772 |
|
# SHAPES_SELECTED messages when the set of selected shapes has |
773 |
|
# actually changed, so we can do a full redraw unconditionally. |
774 |
|
# FIXME: We should perhaps try to limit the redraw to the are |
775 |
|
# actually covered by the shapes before and after the selection |
776 |
|
# change. |
777 |
|
self.full_redraw() |
778 |
|
|
779 |
|
def unprojected_rect_around_point(self, x, y, dist): |
780 |
|
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
781 |
|
|
782 |
def find_shape_at(self, px, py, select_labels = 0): |
The return value is a tuple (minx, miny, maxx, maxy) suitable a |
783 |
"""Return a tuple shape at point px, py in window coords.""" |
parameter to a layer's ShapesInRegion method. |
784 |
|
""" |
785 |
|
map_proj = self.map.projection |
786 |
|
if map_proj is not None: |
787 |
|
inverse = map_proj.Inverse |
788 |
|
else: |
789 |
|
inverse = None |
790 |
|
|
791 |
|
xs = [] |
792 |
|
ys = [] |
793 |
|
for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)): |
794 |
|
px, py = self.win_to_proj(x + dist * dx, y + dist * dy) |
795 |
|
if inverse: |
796 |
|
px, py = inverse(px, py) |
797 |
|
xs.append(px) |
798 |
|
ys.append(py) |
799 |
|
return (min(xs), min(ys), max(xs), max(ys)) |
800 |
|
|
801 |
|
def find_shape_at(self, px, py, select_labels = 0, searched_layer = None): |
802 |
|
"""Determine the shape at point px, py in window coords |
803 |
|
|
804 |
|
Return the shape and the corresponding layer as a tuple (layer, |
805 |
|
shape). |
806 |
|
|
807 |
|
If the optional parameter select_labels is true (default false) |
808 |
|
search through the labels. If a label is found return it's index |
809 |
|
as the shape and None as the layer. |
810 |
|
|
811 |
|
If the optional parameter searched_layer is given (or not None |
812 |
|
which it defaults to), only search in that layer. |
813 |
|
""" |
814 |
map_proj = self.map.projection |
map_proj = self.map.projection |
815 |
if map_proj is not None: |
if map_proj is not None: |
816 |
forward = map_proj.Forward |
forward = map_proj.Forward |
818 |
forward = None |
forward = None |
819 |
|
|
820 |
scale = self.scale |
scale = self.scale |
821 |
|
|
822 |
|
if scale == 0: |
823 |
|
return None, None |
824 |
|
|
825 |
offx, offy = self.offset |
offx, offy = self.offset |
826 |
|
|
827 |
if select_labels: |
if select_labels: |
828 |
labels = self.map.LabelLayer().Labels() |
labels = self.map.LabelLayer().Labels() |
829 |
|
|
830 |
if labels: |
if labels: |
831 |
dc = wxClientDC(self) |
dc = wxClientDC(self) |
832 |
font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL) |
font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL) |
833 |
dc.SetFont(font) |
dc.SetFont(font) |
834 |
for i in range(len(labels)): |
for i in range(len(labels) - 1, -1, -1): |
835 |
label = labels[i] |
label = labels[i] |
836 |
x = label.x |
x = label.x |
837 |
y = label.y |
y = label.y |
857 |
y = y - height/2 |
y = y - height/2 |
858 |
if x <= px < x + width and y <= py <= y + height: |
if x <= px < x + width and y <= py <= y + height: |
859 |
return None, i |
return None, i |
860 |
|
|
861 |
layers = self.map.Layers() |
if searched_layer: |
862 |
|
layers = [searched_layer] |
863 |
|
else: |
864 |
|
layers = self.map.Layers() |
865 |
|
|
866 |
for layer_index in range(len(layers) - 1, -1, -1): |
for layer_index in range(len(layers) - 1, -1, -1): |
867 |
layer = layers[layer_index] |
layer = layers[layer_index] |
868 |
|
|
870 |
if not layer.Visible(): |
if not layer.Visible(): |
871 |
continue |
continue |
872 |
|
|
873 |
filled = layer.fill is not None |
filled = layer.GetClassification().GetDefaultFill() \ |
874 |
stroked = layer.stroke is not None |
is not Color.Transparent |
875 |
|
stroked = layer.GetClassification().GetDefaultLineColor() \ |
876 |
|
is not Color.Transparent |
877 |
|
|
878 |
layer_proj = layer.projection |
layer_proj = layer.projection |
879 |
if layer_proj is not None: |
if layer_proj is not None: |
880 |
inverse = layer_proj.Inverse |
inverse = layer_proj.Inverse |
881 |
else: |
else: |
882 |
inverse = None |
inverse = None |
883 |
|
|
884 |
shapetype = layer.ShapeType() |
shapetype = layer.ShapeType() |
885 |
|
|
886 |
select_shape = -1 |
select_shape = -1 |
887 |
|
|
888 |
|
# Determine the ids of the shapes that overlap a tiny area |
889 |
|
# around the point. For layers containing points we have to |
890 |
|
# choose a larger size of the box we're testing agains so |
891 |
|
# that we take the size of the markers into account |
892 |
|
# FIXME: Once the markers are more flexible this part has to |
893 |
|
# become more flexible too, of course |
894 |
|
if shapetype == SHAPETYPE_POINT: |
895 |
|
box = self.unprojected_rect_around_point(px, py, 5) |
896 |
|
else: |
897 |
|
box = self.unprojected_rect_around_point(px, py, 1) |
898 |
|
shape_ids = layer.ShapesInRegion(box) |
899 |
|
shape_ids.reverse() |
900 |
|
|
901 |
if shapetype == SHAPETYPE_POLYGON: |
if shapetype == SHAPETYPE_POLYGON: |
902 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
903 |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
904 |
i, |
i, |
905 |
filled, stroked, |
filled, stroked, |
910 |
select_shape = i |
select_shape = i |
911 |
break |
break |
912 |
elif shapetype == SHAPETYPE_ARC: |
elif shapetype == SHAPETYPE_ARC: |
913 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
914 |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
915 |
i, 0, 1, |
i, 0, 1, |
916 |
map_proj, layer_proj, |
map_proj, layer_proj, |
920 |
select_shape = i |
select_shape = i |
921 |
break |
break |
922 |
elif shapetype == SHAPETYPE_POINT: |
elif shapetype == SHAPETYPE_POINT: |
923 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
924 |
shape = layer.Shape(i) |
shape = layer.Shape(i) |
925 |
x, y = shape.Points()[0] |
x, y = shape.Points()[0] |
926 |
if inverse: |
if inverse: |
937 |
return layer, select_shape |
return layer, select_shape |
938 |
return None, None |
return None, None |
939 |
|
|
940 |
def SelectShapeAt(self, x, y): |
def SelectShapeAt(self, x, y, layer = None): |
941 |
layer, shape = self.find_shape_at(x, y) |
"""\ |
942 |
self.interactor.SelectLayerAndShape(layer, shape) |
Select and return the shape and its layer at window position (x, y) |
943 |
|
|
944 |
|
If layer is given, only search in that layer. If no layer is |
945 |
|
given, search through all layers. |
946 |
|
|
947 |
|
Return a tuple (layer, shapeid). If no shape is found, return |
948 |
|
(None, None). |
949 |
|
""" |
950 |
|
layer, shape = result = self.find_shape_at(x, y, searched_layer=layer) |
951 |
|
# If layer is None, then shape will also be None. We don't want |
952 |
|
# to deselect the currently selected layer, so we simply select |
953 |
|
# the already selected layer again. |
954 |
|
if layer is None: |
955 |
|
layer = self.selection.SelectedLayer() |
956 |
|
shapes = [] |
957 |
|
else: |
958 |
|
shapes = [shape] |
959 |
|
self.selection.SelectShapes(layer, shapes) |
960 |
|
return result |
961 |
|
|
962 |
def LabelShapeAt(self, x, y): |
def LabelShapeAt(self, x, y): |
963 |
|
"""Add or remove a label at window position x, y. |
964 |
|
|
965 |
|
If there's a label at the given position, remove it. Otherwise |
966 |
|
determine the shape at the position, run the label dialog and |
967 |
|
unless the user cancels the dialog, add a laber. |
968 |
|
""" |
969 |
ox = x; oy = y |
ox = x; oy = y |
970 |
label_layer = self.map.LabelLayer() |
label_layer = self.map.LabelLayer() |
971 |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
1014 |
valign = ALIGN_CENTER |
valign = ALIGN_CENTER |
1015 |
label_layer.AddLabel(x, y, text, |
label_layer.AddLabel(x, y, text, |
1016 |
halign = halign, valign = valign) |
halign = halign, valign = valign) |
1017 |
|
|
1018 |
|
def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend): |
1019 |
|
"""Calculate dimensions to transform canvas content to output device.""" |
1020 |
|
width, height = device_extend |
1021 |
|
|
1022 |
|
# Only 80 % of the with are available for the map |
1023 |
|
width = width * 0.8 |
1024 |
|
|
1025 |
|
# Define the distance of the map from DC border |
1026 |
|
distance = 20 |
1027 |
|
|
1028 |
|
if height < width: |
1029 |
|
# landscape |
1030 |
|
map_height = height - 2*distance |
1031 |
|
map_width = map_height |
1032 |
|
else: |
1033 |
|
# portrait, recalibrate width (usually the legend width is too |
1034 |
|
# small |
1035 |
|
width = width * 0.9 |
1036 |
|
map_height = width - 2*distance |
1037 |
|
map_width = map_height |
1038 |
|
|
1039 |
|
mapregion = (distance, distance, |
1040 |
|
distance+map_width, distance+map_height) |
1041 |
|
|
1042 |
|
canvas_width, canvas_height = canvas_size |
1043 |
|
|
1044 |
|
scalex = map_width / (canvas_width/canvas_scale) |
1045 |
|
scaley = map_height / (canvas_height/canvas_scale) |
1046 |
|
scale = min(scalex, scaley) |
1047 |
|
canvas_offx, canvas_offy = canvas_offset |
1048 |
|
offx = scale*canvas_offx/canvas_scale |
1049 |
|
offy = scale*canvas_offy/canvas_scale |
1050 |
|
|
1051 |
|
return scale, (offx, offy), mapregion |