/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/view.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/view.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 125 - (hide annotations)
Thu May 2 18:55:33 2002 UTC (22 years, 10 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/view.py
File MIME type: text/x-python
File size: 24632 byte(s)
	* Thuban/UI/view.py: Keep the temporary bitmap used during drawing
	around to speed up most redraws:
	(MapCanvas.__init__): New instance variable bitmap which holds the
	bitmap
	(MapCanvas.do_redraw): Redraw self.bitmap if necessary. Use
	self.bitmap to draw.
	(MapCanvas.full_redraw): New method to force a full redraw
	including the bitmap
	(MapCanvas.SetMap): Subscribe full_redraw instead of redraw to
	make sure the bitmap is redrawn.
	(MapCanvas.projection_changed, MapCanvas.set_view_transform,
	MapCanvas.shape_selected): Call full_redraw instead of readraw to
	make sure the bitmap is redrawn.
	(MapCanvas.OnSize): New method to handle size events so that the
	bitmap can be redrawn.

1 bh 78 # Copyright (c) 2001, 2002 by Intevation GmbH
2 bh 6 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Classes for display of a map and interaction with it
10     """
11    
12     __version__ = "$Revision$"
13    
14     from math import hypot
15    
16     from wxPython.wx import wxWindow,\
17     wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
18 bh 122 EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW
19 bh 6
20    
21     from wxPython import wx
22    
23     from wxproj import point_in_polygon_shape, shape_centroid
24    
25    
26     from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \
27     LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED
28     from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
29     SHAPETYPE_POINT
30     from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
31     ALIGN_LEFT, ALIGN_RIGHT
32 bh 122 from Thuban.Lib.connector import Publisher
33 bh 6
34     from renderer import ScreenRenderer, PrinterRender
35    
36     import labeldialog
37    
38 bh 122 from messages import SELECTED_SHAPE, VIEW_POSITION
39 bh 6
40    
41     #
42     # The tools
43     #
44    
45     class Tool:
46    
47     """
48     Base class for the interactive tools
49     """
50    
51     def __init__(self, view):
52     """Intitialize the tool. The view is the canvas displaying the map"""
53     self.view = view
54     self.start = self.current = None
55     self.dragging = 0
56     self.drawn = 0
57    
58     def Name(self):
59     """Return the tool's name"""
60     return ''
61    
62     def drag_start(self, x, y):
63     self.start = self.current = x, y
64     self.dragging = 1
65    
66     def drag_move(self, x, y):
67     self.current = x, y
68    
69     def drag_stop(self, x, y):
70     self.current = x, y
71     self.dragging = 0
72    
73     def Show(self, dc):
74     if not self.drawn:
75     self.draw(dc)
76     self.drawn = 1
77    
78     def Hide(self, dc):
79     if self.drawn:
80     self.draw(dc)
81     self.drawn = 0
82    
83     def draw(self, dc):
84     pass
85    
86     def MouseDown(self, event):
87     self.drag_start(event.m_x, event.m_y)
88    
89     def MouseMove(self, event):
90     if self.dragging:
91     self.drag_move(event.m_x, event.m_y)
92    
93     def MouseUp(self, event):
94     if self.dragging:
95     self.drag_move(event.m_x, event.m_y)
96    
97     def Cancel(self):
98     self.dragging = 0
99    
100    
101     class RectTool(Tool):
102    
103     """Base class for tools that draw rectangles while dragging"""
104    
105     def draw(self, dc):
106     sx, sy = self.start
107     cx, cy = self.current
108     dc.DrawRectangle(sx, sy, cx - sx, cy - sy)
109    
110     class ZoomInTool(RectTool):
111    
112     """The Zoom-In Tool"""
113    
114     def Name(self):
115     return "ZoomInTool"
116    
117     def proj_rect(self):
118     """return the rectangle given by start and current in projected
119     coordinates"""
120     sx, sy = self.start
121     cx, cy = self.current
122     left, top = self.view.win_to_proj(sx, sy)
123     right, bottom = self.view.win_to_proj(cx, cy)
124     return (min(left, right), min(top, bottom),
125     max(left, right), max(top, bottom))
126    
127     def MouseUp(self, event):
128     if self.dragging:
129     Tool.MouseUp(self, event)
130 bh 57 sx, sy = self.start
131     cx, cy = self.current
132     if sx == cx and sy == cy:
133     # Just a mouse click. Simply zoom in by a factor of two
134     self.view.ZoomFactor(2, center = (cx, cy))
135     else:
136     # A drag. Zoom in to the rectangle
137     self.view.FitRectToWindow(self.proj_rect())
138 bh 6
139    
140     class ZoomOutTool(RectTool):
141    
142     """The Zoom-Out Tool"""
143    
144     def Name(self):
145     return "ZoomOutTool"
146    
147     def MouseUp(self, event):
148     if self.dragging:
149     Tool.MouseUp(self, event)
150     sx, sy = self.start
151     cx, cy = self.current
152 bh 57 if sx == cx and sy == cy:
153     # Just a mouse click. Simply zoom out by a factor of two
154     self.view.ZoomFactor(0.5, center = (cy, cy))
155     else:
156     # A drag. Zoom out to the rectangle
157     self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),
158     max(sx, cx), max(sy, cy)))
159 bh 6
160    
161     class PanTool(Tool):
162    
163     """The Pan Tool"""
164    
165     def Name(self):
166     return "PanTool"
167    
168     def MouseMove(self, event):
169     if self.dragging:
170     x0, y0 = self.current
171     Tool.MouseMove(self, event)
172     x, y = self.current
173     width, height = self.view.GetSizeTuple()
174     dc = self.view.drag_dc
175     dc.Blit(0, 0, width, height, dc, x0 - x, y0 - y)
176    
177     def MouseUp(self, event):
178     if self.dragging:
179     Tool.MouseUp(self, event)
180     sx, sy = self.start
181     cx, cy = self.current
182     self.view.Translate(cx - sx, cy - sy)
183    
184     class IdentifyTool(Tool):
185    
186     """The "Identify" Tool"""
187    
188     def Name(self):
189     return "IdentifyTool"
190    
191     def MouseUp(self, event):
192     self.view.SelectShapeAt(event.m_x, event.m_y)
193    
194    
195     class LabelTool(Tool):
196    
197     """The "Label" Tool"""
198    
199     def Name(self):
200     return "LabelTool"
201    
202     def MouseUp(self, event):
203     self.view.LabelShapeAt(event.m_x, event.m_y)
204    
205    
206    
207    
208     class MapPrintout(wx.wxPrintout):
209    
210     """
211     wxPrintout class for printing Thuban maps
212     """
213    
214     def __init__(self, map):
215     wx.wxPrintout.__init__(self)
216     self.map = map
217    
218     def GetPageInfo(self):
219     return (1, 1, 1, 1)
220    
221     def HasPage(self, pagenum):
222     return pagenum == 1
223    
224     def OnPrintPage(self, pagenum):
225     if pagenum == 1:
226     self.draw_on_dc(self.GetDC())
227    
228     def draw_on_dc(self, dc):
229     width, height = self.GetPageSizePixels()
230     llx, lly, urx, ury = self.map.ProjectedBoundingBox()
231     scalex = width / (urx - llx)
232     scaley = height / (ury - lly)
233     scale = min(scalex, scaley)
234     offx = 0.5 * (width - (urx + llx) * scale)
235     offy = 0.5 * (height + (ury + lly) * scale)
236    
237     resx, resy = self.GetPPIPrinter()
238     renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)
239     renderer.RenderMap(self.map)
240     return wx.true
241    
242    
243 bh 122 class MapCanvas(wxWindow, Publisher):
244 bh 6
245     """A widget that displays a map and offers some interaction"""
246    
247 bh 23 def __init__(self, parent, winid, interactor):
248 bh 6 wxWindow.__init__(self, parent, winid)
249     self.SetBackgroundColour(wxColour(255, 255, 255))
250 bh 125
251     # the map displayed in this canvas. Set with SetMap()
252 bh 6 self.map = None
253 bh 125
254     # scale and offset describe the transformation from projected
255     # coordinates to window coordinates.
256 bh 6 self.scale = 1.0
257     self.offset = (0, 0)
258 bh 125
259     # whether the user is currently dragging the mouse, i.e. moving
260     # the mouse while pressing a mouse button
261 bh 6 self.dragging = 0
262 bh 125
263     # the currently active tool
264 bh 6 self.tool = None
265 bh 125
266     # The current mouse position of the last OnMotion event or None
267     # if the mouse is outside the window.
268     self.current_position = None
269    
270    
271     # If true, OnIdle will call do_redraw to do the actual
272     # redrawing. Set by OnPaint to avoid some unnecessary redraws.
273     # To force a redraw call full_redraw().
274 bh 6 self.redraw_on_idle = 0
275 bh 125
276     # the bitmap serving as backing store
277     self.bitmap = None
278    
279     # the interactor
280     self.interactor = interactor
281     self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)
282    
283     # subscribe the WX events we're interested in
284 bh 6 EVT_PAINT(self, self.OnPaint)
285     EVT_LEFT_DOWN(self, self.OnLeftDown)
286     EVT_LEFT_UP(self, self.OnLeftUp)
287     EVT_MOTION(self, self.OnMotion)
288 bh 122 EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)
289 bh 125 wx.EVT_SIZE(self, self.OnSize)
290 bh 6 wx.EVT_IDLE(self, self.OnIdle)
291    
292 bh 122 def __del__(self):
293     wxWindow.__del__(self)
294     Publisher.__del__(self)
295    
296 bh 6 def OnPaint(self, event):
297     dc = wxPaintDC(self)
298 bh 57 if self.map is not None and self.map.HasLayers():
299     # We have a non-empty map. Redraw it in idle time
300     self.redraw_on_idle = 1
301     else:
302     # If we've got no map or if the map is empty, simply clear
303     # the screen.
304    
305     # XXX it's probably possible to get rid of this. The
306     # background color of the window is already white and the
307     # only thing we may have to do is to call self.Refresh()
308     # with a true argument in the right places.
309     dc.BeginDrawing()
310     dc.Clear()
311     dc.EndDrawing()
312 bh 6
313     def do_redraw(self):
314 bh 57 # This should only be called if we have a non-empty map. We draw
315     # it into a memory DC and then blit it to the screen.
316 bh 125
317 bh 6 width, height = self.GetSizeTuple()
318    
319 bh 125 # If self.bitmap's still there, reuse it. Otherwise redraw it
320     if self.bitmap is not None:
321     bitmap = self.bitmap
322 bh 6 else:
323 bh 125 bitmap = wx.wxEmptyBitmap(width, height)
324     dc = wx.wxMemoryDC()
325     dc.SelectObject(bitmap)
326     dc.BeginDrawing()
327 bh 57
328 bh 125 # clear the background
329     dc.SetBrush(wx.wxWHITE_BRUSH)
330     dc.SetPen(wx.wxTRANSPARENT_PEN)
331     dc.DrawRectangle(0, 0, width, height)
332 bh 6
333 bh 125 if 1: #self.interactor.selected_map is self.map:
334     selected_layer = self.interactor.selected_layer
335     selected_shape = self.interactor.selected_shape
336     else:
337     selected_layer = None
338     selected_shape = None
339 bh 57
340 bh 125 # draw the map into the bitmap
341     renderer = ScreenRenderer(dc, self.scale, self.offset)
342     renderer.RenderMap(self.map, selected_layer, selected_shape)
343    
344     dc.EndDrawing()
345     dc.SelectObject(wx.wxNullBitmap)
346     self.bitmap = bitmap
347    
348 bh 57 # blit the bitmap to the screen
349 bh 125 dc = wx.wxMemoryDC()
350     dc.SelectObject(bitmap)
351 bh 6 clientdc = wxClientDC(self)
352     clientdc.BeginDrawing()
353     clientdc.Blit(0, 0, width, height, dc, 0, 0)
354     clientdc.EndDrawing()
355    
356     def Print(self):
357     printer = wx.wxPrinter()
358     printout = MapPrintout(self.map)
359     printer.Print(self, printout, wx.true)
360     printout.Destroy()
361    
362     def SetMap(self, map):
363     redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED,
364     LAYER_VISIBILITY_CHANGED)
365     if self.map is not None:
366     for channel in redraw_channels:
367 bh 125 self.map.Unsubscribe(channel, self.full_redraw)
368 bh 6 self.map.Unsubscribe(MAP_PROJECTION_CHANGED,
369     self.projection_changed)
370     self.map = map
371     if self.map is not None:
372     for channel in redraw_channels:
373 bh 125 self.map.Subscribe(channel, self.full_redraw)
374 bh 6 self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)
375     self.FitMapToWindow()
376 bh 57 # force a redraw. If map is not empty, it's already been called
377     # by FitMapToWindow but if map is empty it hasn't been called
378     # yet so we have to explicitly call it.
379 bh 125 self.full_redraw()
380 bh 6
381     def Map(self):
382     return self.map
383    
384     def redraw(self, *args):
385     self.Refresh(0)
386    
387 bh 125 def full_redraw(self, *args):
388     self.bitmap = None
389     self.redraw()
390    
391 bh 6 def projection_changed(self, *args):
392     self.FitMapToWindow()
393 bh 125 self.full_redraw()
394 bh 6
395     def set_view_transform(self, scale, offset):
396     self.scale = scale
397     self.offset = offset
398 bh 125 self.full_redraw()
399 bh 6
400     def proj_to_win(self, x, y):
401     """\
402     Return the point in window coords given by projected coordinates x y
403     """
404     offx, offy = self.offset
405     return (self.scale * x + offx, -self.scale * y + offy)
406    
407     def win_to_proj(self, x, y):
408     """\
409     Return the point in projected coordinates given by window coords x y
410     """
411     offx, offy = self.offset
412     return ((x - offx) / self.scale, (offy - y) / self.scale)
413    
414     def FitRectToWindow(self, rect):
415     width, height = self.GetSizeTuple()
416     llx, lly, urx, ury = rect
417 bh 45 if llx == urx or lly == ury:
418     # zero with or zero height. Do Nothing
419     return
420 bh 6 scalex = width / (urx - llx)
421     scaley = height / (ury - lly)
422     scale = min(scalex, scaley)
423     offx = 0.5 * (width - (urx + llx) * scale)
424     offy = 0.5 * (height + (ury + lly) * scale)
425     self.set_view_transform(scale, (offx, offy))
426    
427     def FitMapToWindow(self):
428     """\
429     Set the scale and offset so that the map is centered in the
430     window
431     """
432     bbox = self.map.ProjectedBoundingBox()
433     if bbox is not None:
434     self.FitRectToWindow(bbox)
435    
436 bh 57 def ZoomFactor(self, factor, center = None):
437     """Multiply the zoom by factor and center on center.
438    
439     The optional parameter center is a point in window coordinates
440     that should be centered. If it is omitted, it defaults to the
441     center of the window
442     """
443 bh 6 width, height = self.GetSizeTuple()
444     scale = self.scale * factor
445     offx, offy = self.offset
446 bh 57 if center is not None:
447     cx, cy = center
448     else:
449     cx = width / 2
450     cy = height / 2
451     offset = (factor * (offx - cx) + width / 2,
452     factor * (offy - cy) + height / 2)
453 bh 6 self.set_view_transform(scale, offset)
454    
455     def ZoomOutToRect(self, rect):
456     # rect is given in window coordinates
457    
458     # determine the bbox of the displayed region in projected
459     # coordinates
460     width, height = self.GetSizeTuple()
461     llx, lly = self.win_to_proj(0, height - 1)
462     urx, ury = self.win_to_proj(width - 1, 0)
463    
464     sx, sy, ex, ey = rect
465     scalex = (ex - sx) / (urx - llx)
466     scaley = (ey - sy) / (ury - lly)
467     scale = min(scalex, scaley)
468    
469     offx = 0.5 * ((ex + sx) - (urx + llx) * scale)
470     offy = 0.5 * ((ey + sy) + (ury + lly) * scale)
471     self.set_view_transform(scale, (offx, offy))
472    
473     def Translate(self, dx, dy):
474     offx, offy = self.offset
475     self.set_view_transform(self.scale, (offx + dx, offy + dy))
476    
477     def ZoomInTool(self):
478     self.tool = ZoomInTool(self)
479    
480     def ZoomOutTool(self):
481     self.tool = ZoomOutTool(self)
482    
483     def PanTool(self):
484     self.tool = PanTool(self)
485    
486     def IdentifyTool(self):
487     self.tool = IdentifyTool(self)
488    
489     def LabelTool(self):
490     self.tool = LabelTool(self)
491    
492     def CurrentTool(self):
493     return self.tool and self.tool.Name() or None
494    
495 bh 122 def CurrentPosition(self):
496     """Return current position of the mouse in projected coordinates.
497    
498     The result is a 2-tuple of floats with the coordinates. If the
499     mouse is not in the window, the result is None.
500     """
501     if self.current_position is not None:
502     x, y = self.current_position
503     return self.win_to_proj(x, y)
504     else:
505     return None
506    
507     def set_current_position(self, event):
508     """Set the current position from event
509    
510     Should be called by all events that contain mouse positions
511     especially EVT_MOTION. The event paramete may be None to
512     indicate the the pointer left the window.
513     """
514     if event is not None:
515     self.current_position = (event.m_x, event.m_y)
516     else:
517     self.current_position = None
518     self.issue(VIEW_POSITION)
519    
520 bh 6 def OnLeftDown(self, event):
521 bh 122 self.set_current_position(event)
522 bh 6 if self.tool is not None:
523     self.drag_dc = wxClientDC(self)
524     self.drag_dc.SetLogicalFunction(wxINVERT)
525     self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)
526     self.CaptureMouse()
527     self.tool.MouseDown(event)
528     self.tool.Show(self.drag_dc)
529     self.dragging = 1
530    
531     def OnLeftUp(self, event):
532     self.ReleaseMouse()
533 bh 122 self.set_current_position(event)
534 bh 6 if self.dragging:
535     self.tool.Hide(self.drag_dc)
536     self.tool.MouseUp(event)
537     self.drag_dc = None
538     self.dragging = 0
539    
540     def OnMotion(self, event):
541 bh 122 self.set_current_position(event)
542 bh 6 if self.dragging:
543     self.tool.Hide(self.drag_dc)
544     self.tool.MouseMove(event)
545     self.tool.Show(self.drag_dc)
546    
547 bh 122 def OnLeaveWindow(self, event):
548     self.set_current_position(None)
549    
550 bh 6 def OnIdle(self, event):
551     if self.redraw_on_idle:
552     self.do_redraw()
553     self.redraw_on_idle = 0
554    
555 bh 125 def OnSize(self, event):
556     # the window's size has changed. We have to get a new bitmap. If
557     # we want to be clever we could try to get by without throwing
558     # everything away. E.g. when the window gets smaller, we could
559     # either keep the bitmap or create the new one from the old one.
560     # Even when the window becomes larger some parts of the bitmap
561     # could be reused.
562     self.full_redraw()
563    
564 bh 6 def shape_selected(self, layer, shape):
565 bh 125 self.full_redraw()
566 bh 6
567 bh 43 def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):
568     """Determine the shape at point px, py in window coords
569    
570     Return the shape and the corresponding layer as a tuple (layer,
571     shape).
572    
573     If the optional parameter select_labels is true (default false)
574     search through the labels. If a label is found return it's index
575     as the shape and None as the layer.
576    
577     If the optional parameter selected_layer is true (default), only
578     search in the currently selected layer.
579     """
580 bh 6 map_proj = self.map.projection
581     if map_proj is not None:
582     forward = map_proj.Forward
583     else:
584     forward = None
585    
586     scale = self.scale
587     offx, offy = self.offset
588    
589     if select_labels:
590     labels = self.map.LabelLayer().Labels()
591    
592     if labels:
593     dc = wxClientDC(self)
594     font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
595     dc.SetFont(font)
596 bh 60 for i in range(len(labels) - 1, -1, -1):
597 bh 6 label = labels[i]
598     x = label.x
599     y = label.y
600     text = label.text
601     if forward:
602     x, y = forward(x, y)
603     x = x * scale + offx
604     y = -y * scale + offy
605     width, height = dc.GetTextExtent(text)
606     if label.halign == ALIGN_LEFT:
607     # nothing to be done
608     pass
609     elif label.halign == ALIGN_RIGHT:
610     x = x - width
611     elif label.halign == ALIGN_CENTER:
612     x = x - width/2
613     if label.valign == ALIGN_TOP:
614     # nothing to be done
615     pass
616     elif label.valign == ALIGN_BOTTOM:
617     y = y - height
618     elif label.valign == ALIGN_CENTER:
619     y = y - height/2
620     if x <= px < x + width and y <= py <= y + height:
621     return None, i
622 bh 43
623     if selected_layer:
624     layer = self.interactor.SelectedLayer()
625     if layer is not None:
626     layers = [layer]
627     else:
628     # no layer selected. Use an empty list to effectively
629     # ignore all layers.
630     layers = []
631     else:
632     layers = self.map.Layers()
633    
634 bh 6 for layer_index in range(len(layers) - 1, -1, -1):
635     layer = layers[layer_index]
636    
637     # search only in visible layers
638     if not layer.Visible():
639     continue
640    
641     filled = layer.fill is not None
642     stroked = layer.stroke is not None
643    
644     layer_proj = layer.projection
645     if layer_proj is not None:
646     inverse = layer_proj.Inverse
647     else:
648     inverse = None
649    
650     shapetype = layer.ShapeType()
651    
652     select_shape = -1
653     if shapetype == SHAPETYPE_POLYGON:
654 bh 60 for i in range(layer.NumShapes() - 1, -1, -1):
655 bh 6 result = point_in_polygon_shape(layer.shapefile.cobject(),
656     i,
657     filled, stroked,
658     map_proj, layer_proj,
659     scale, -scale, offx, offy,
660     px, py)
661     if result:
662     select_shape = i
663     break
664     elif shapetype == SHAPETYPE_ARC:
665 bh 60 for i in range(layer.NumShapes() - 1, -1, -1):
666 bh 6 result = point_in_polygon_shape(layer.shapefile.cobject(),
667     i, 0, 1,
668     map_proj, layer_proj,
669     scale, -scale, offx, offy,
670     px, py)
671     if result < 0:
672     select_shape = i
673     break
674     elif shapetype == SHAPETYPE_POINT:
675 bh 60 for i in range(layer.NumShapes() - 1, -1, -1):
676 bh 6 shape = layer.Shape(i)
677     x, y = shape.Points()[0]
678     if inverse:
679     x, y = inverse(x, y)
680     if forward:
681     x, y = forward(x, y)
682     x = x * scale + offx
683     y = -y * scale + offy
684     if hypot(px - x, py - y) < 5:
685     select_shape = i
686     break
687    
688     if select_shape >= 0:
689     return layer, select_shape
690     return None, None
691    
692     def SelectShapeAt(self, x, y):
693 bh 78 layer, shape = self.find_shape_at(x, y, selected_layer = 0)
694 bh 43 # If layer is None, then shape will also be None. We don't want
695     # to deselect the currently selected layer, so we simply select
696     # the already selected layer again.
697     if layer is None:
698     layer = self.interactor.SelectedLayer()
699 bh 6 self.interactor.SelectLayerAndShape(layer, shape)
700    
701     def LabelShapeAt(self, x, y):
702     ox = x; oy = y
703     label_layer = self.map.LabelLayer()
704     layer, shape_index = self.find_shape_at(x, y, select_labels = 1)
705     if layer is None and shape_index is not None:
706     # a label was selected
707     label_layer.RemoveLabel(shape_index)
708     elif layer is not None:
709     text = labeldialog.run_label_dialog(self, layer.table, shape_index)
710     if text:
711     proj = self.map.projection
712     if proj is not None:
713     map_proj = proj
714     else:
715     map_proj = None
716     proj = layer.projection
717     if proj is not None:
718     layer_proj = proj
719     else:
720     layer_proj = None
721    
722     shapetype = layer.ShapeType()
723     if shapetype == SHAPETYPE_POLYGON:
724     x, y = shape_centroid(layer.shapefile.cobject(),
725     shape_index,
726     map_proj, layer_proj, 1, 1, 0, 0)
727     if map_proj is not None:
728     x, y = map_proj.Inverse(x, y)
729     else:
730     shape = layer.Shape(shape_index)
731     if shapetype == SHAPETYPE_POINT:
732     x, y = shape.Points()[0]
733     else:
734     # assume SHAPETYPE_ARC
735     points = shape.Points()
736     x, y = points[len(points) / 2]
737     if layer_proj is not None:
738     x, y = layer_proj.Inverse(x, y)
739     if shapetype == SHAPETYPE_POINT:
740     halign = ALIGN_LEFT
741     valign = ALIGN_CENTER
742     elif shapetype == SHAPETYPE_POLYGON:
743     halign = ALIGN_CENTER
744     valign = ALIGN_CENTER
745     elif shapetype == SHAPETYPE_ARC:
746     halign = ALIGN_LEFT
747     valign = ALIGN_CENTER
748     label_layer.AddLabel(x, y, text,
749     halign = halign, valign = valign)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26