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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 940 by jonathan, Tue May 20 15:25:33 2003 UTC revision 1277 by jonathan, Fri Jun 20 17:46:45 2003 UTC
# Line 19  import os.path Line 19  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, wxYield,\
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, wxCursor, wxImageFromBitmap, wxPlatform
      wxImageFromBitmap, wxPlatform  
26    
27  # Export related stuff  # Export related stuff
28  if wxPlatform == '__WXMSW__':  if wxPlatform == '__WXMSW__':
# Line 35  from wxPython import wx Line 34  from wxPython import wx
34  from wxproj import point_in_polygon_shape, shape_centroid  from wxproj import point_in_polygon_shape, shape_centroid
35    
36  from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \  from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \
37         LAYER_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, \
40       SHAPETYPE_POINT       SHAPETYPE_POINT
# Line 43  from Thuban.Model.label import ALIGN_CEN Line 43  from Thuban.Model.label import ALIGN_CEN
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    from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor
47    
48  import resource  import resource
49    
50  from selection import Selection  from selection import Selection
# Line 271  class MapPrintout(wx.wxPrintout): Line 273  class MapPrintout(wx.wxPrintout):
273                                  (height/canvas_scale)*scale),                                  (height/canvas_scale)*scale),
274                                  mapregion,                                  mapregion,
275                             self.selected_layer, self.selected_shapes)                             self.selected_layer, self.selected_shapes)
276          return wx.true          return True
   
277    
278  class MapCanvas(wxWindow, Publisher):  class MapCanvas(wxWindow, Publisher):
279    
# Line 302  class MapCanvas(wxWindow, Publisher): Line 303  class MapCanvas(wxWindow, Publisher):
303          # the map displayed in this canvas. Set with SetMap()          # the map displayed in this canvas. Set with SetMap()
304          self.map = None          self.map = None
305    
306            # current map projection. should only differ from map.projection
307            # when the map's projection is changing and we need access to the
308            # old projection.
309            self.current_map_proj = None
310    
311          # scale and offset describe the transformation from projected          # scale and offset describe the transformation from projected
312          # coordinates to window coordinates.          # coordinates to window coordinates.
313          self.scale = 1.0          self.scale = 1.0
# Line 377  class MapCanvas(wxWindow, Publisher): Line 383  class MapCanvas(wxWindow, Publisher):
383          dc = wxPaintDC(self)          dc = wxPaintDC(self)
384          clear = self.map is None or not self.map.HasLayers()          clear = self.map is None or not self.map.HasLayers()
385    
386          wxBeginBusyCursor()          ThubanBeginBusyCursor()
387            try:
388          if not clear:              if not clear:
             try:  
389                  self.do_redraw()                  self.do_redraw()
390              except:                  try:
391                  print "Error during drawing:", sys.exc_info()[0]                      pass
392                  clear = True                  except:
393                        print "Error during drawing:", sys.exc_info()[0]
394          if clear:                      clear = True
395              # If we've got no map or if the map is empty, simply clear  
396              # the screen.              if clear:
397                    # If we've got no map or if the map is empty, simply clear
398              # XXX it's probably possible to get rid of this. The                  # the screen.
399              # background color of the window is already white and the  
400              # only thing we may have to do is to call self.Refresh()                  # XXX it's probably possible to get rid of this. The
401              # with a true argument in the right places.                  # background color of the window is already white and the
402              dc.BeginDrawing()                  # only thing we may have to do is to call self.Refresh()
403              dc.Clear()                  # with a true argument in the right places.
404              dc.EndDrawing()                  dc.BeginDrawing()
405                    dc.Clear()
406          wxEndBusyCursor()                  dc.EndDrawing()
407            finally:
408                ThubanEndBusyCursor()
409    
410      def do_redraw(self):      def do_redraw(self):
411          # This should only be called if we have a non-empty map.          # This should only be called if we have a non-empty map.
# Line 446  class MapCanvas(wxWindow, Publisher): Line 453  class MapCanvas(wxWindow, Publisher):
453          clientdc.EndDrawing()          clientdc.EndDrawing()
454    
455      def Export(self):      def Export(self):
456            if self.scale == 0:
457                return
458    
459          if hasattr(self, "export_path"):          if hasattr(self, "export_path"):
460              export_path = self.export_path              export_path = self.export_path
461          else:          else:
# Line 488  class MapCanvas(wxWindow, Publisher): Line 498  class MapCanvas(wxWindow, Publisher):
498                    
499          printout = MapPrintout(self, self.map, (0, 0, width, height),          printout = MapPrintout(self, self.map, (0, 0, width, height),
500                                 selected_layer, selected_shapes)                                 selected_layer, selected_shapes)
501          printer.Print(self, printout, wx.true)          printer.Print(self, printout, True)
502          printout.Destroy()          printout.Destroy()
503    
504      def SetMap(self, map):      def SetMap(self, map):
# Line 498  class MapCanvas(wxWindow, Publisher): Line 508  class MapCanvas(wxWindow, Publisher):
508              for channel in redraw_channels:              for channel in redraw_channels:
509                  self.map.Unsubscribe(channel, self.full_redraw)                  self.map.Unsubscribe(channel, self.full_redraw)
510              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,
511                                   self.projection_changed)                                   self.map_projection_changed)
512                self.map.Unsubscribe(LAYER_PROJECTION_CHANGED,
513                                     self.layer_projection_changed)
514          self.map = map          self.map = map
515            self.current_map_proj = self.map.GetProjection()
516          self.selection.ClearSelection()          self.selection.ClearSelection()
517          if self.map is not None:          if self.map is not None:
518              for channel in redraw_channels:              for channel in redraw_channels:
519                  self.map.Subscribe(channel, self.full_redraw)                  self.map.Subscribe(channel, self.full_redraw)
520              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.map_projection_changed)
521                self.map.Subscribe(LAYER_PROJECTION_CHANGED, self.layer_projection_changed)
522          self.FitMapToWindow()          self.FitMapToWindow()
523          # force a redraw. If map is not empty, it's already been called          # force a redraw. If map is not empty, it's already been called
524          # by FitMapToWindow but if map is empty it hasn't been called          # by FitMapToWindow but if map is empty it hasn't been called
# Line 522  class MapCanvas(wxWindow, Publisher): Line 536  class MapCanvas(wxWindow, Publisher):
536          self.bitmap = None          self.bitmap = None
537          self.redraw()          self.redraw()
538    
539      def projection_changed(self, *args):      def map_projection_changed(self, *args):
540          self.FitMapToWindow()  
541            proj = self.current_map_proj
542            self.current_map_proj = self.map.GetProjection()
543    
544            bbox = None
545    
546            if proj is not None and self.current_map_proj is not None:
547                width, height = self.GetSizeTuple()
548                llx, lly = self.win_to_proj(0, height)
549                urx, ury = self.win_to_proj(width, 0)
550                bbox = proj.Inverse(llx, lly) + proj.Inverse(urx, ury)
551                bbox = self.current_map_proj.ForwardBBox(bbox)
552    
553            if bbox is not None:
554                self.FitRectToWindow(bbox)
555            else:
556                self.FitMapToWindow()
557    
558            self.full_redraw()
559    
560        def layer_projection_changed(self, *args):
561          self.full_redraw()          self.full_redraw()
562    
563      def set_view_transform(self, scale, offset):      def set_view_transform(self, scale, offset):
564            # width/height of the projected bbox
565            llx, lly, urx, ury = bbox = self.map.ProjectedBoundingBox()
566            pwidth = float(urx - llx)
567            pheight = float(ury - lly)
568    
569            # width/height of the window
570            wwidth, wheight = self.GetSizeTuple()
571    
572            # The window's center in projected coordinates assuming the new
573            # scale/offset
574            pcenterx = (wwidth/2 - offset[0]) / scale
575            pcentery = (offset[1] - wheight/2) / scale
576    
577            # The window coordinates used when drawing the shapes must fit
578            # into 16bit signed integers.
579            max_len = max(pwidth, pheight)
580            if max_len:
581                max_scale = 32000.0 / max_len
582            else:
583                # FIXME: What to do in this case? The bbox is effectively
584                # empty so any scale should work.
585                max_scale = scale
586    
587            # The minimal scale is somewhat arbitrarily set to half that of
588            # the bbox fit into the window
589            scales = []
590            if pwidth:
591                scales.append(wwidth / pwidth)
592            if pheight:
593                scales.append(wheight / pheight)
594            if scales:
595                min_scale = 0.5 * min(scales)
596            else:
597                min_scale = scale
598    
599            if scale > max_scale:
600                scale = max_scale
601            elif scale < min_scale:
602                scale = min_scale
603    
604          self.scale = scale          self.scale = scale
         if self.scale < 0.0001:  
             self.scale = 0.0001  
605    
606          self.offset = offset          # determine new offset to preserve the center
607            self.offset = (wwidth/2 - scale * pcenterx,
608                           wheight/2 + scale * pcentery)
609          self.full_redraw()          self.full_redraw()
610          self.issue(SCALE_CHANGED, scale)          self.issue(SCALE_CHANGED, scale)
611    
# Line 539  class MapCanvas(wxWindow, Publisher): Line 613  class MapCanvas(wxWindow, Publisher):
613          """\          """\
614          Return the point in  window coords given by projected coordinates x y          Return the point in  window coords given by projected coordinates x y
615          """          """
616            if self.scale == 0:
617                return (0, 0)
618    
619          offx, offy = self.offset          offx, offy = self.offset
620          return (self.scale * x + offx, -self.scale * y + offy)          return (self.scale * x + offx, -self.scale * y + offy)
621    
# Line 546  class MapCanvas(wxWindow, Publisher): Line 623  class MapCanvas(wxWindow, Publisher):
623          """\          """\
624          Return the point in projected coordinates given by window coords x y          Return the point in projected coordinates given by window coords x y
625          """          """
626            if self.scale == 0:
627                return (0, 0)
628    
629          offx, offy = self.offset          offx, offy = self.offset
630          return ((x - offx) / self.scale, (offy - y) / self.scale)          return ((x - offx) / self.scale, (offy - y) / self.scale)
631    
# Line 565  class MapCanvas(wxWindow, Publisher): Line 645  class MapCanvas(wxWindow, Publisher):
645          scale = min(scalex, scaley)          scale = min(scalex, scaley)
646          offx = 0.5 * (width - (urx + llx) * scale)          offx = 0.5 * (width - (urx + llx) * scale)
647          offy = 0.5 * (height + (ury + lly) * scale)          offy = 0.5 * (height + (ury + lly) * scale)
         print "scalex:", scalex, "scaley:", scaley  
648          self.set_view_transform(scale, (offx, offy))          self.set_view_transform(scale, (offx, offy))
649    
650      def FitMapToWindow(self):      def FitMapToWindow(self):
# Line 574  class MapCanvas(wxWindow, Publisher): Line 653  class MapCanvas(wxWindow, Publisher):
653          Set the scale so that the map fits exactly into the window and          Set the scale so that the map fits exactly into the window and
654          center it in the window.          center it in the window.
655          """          """
656          bbox = self.map.ProjectedBoundingBox()          if self.map is not None:
657          if bbox is not None:              bbox = self.map.ProjectedBoundingBox()
658              self.FitRectToWindow(bbox)              if bbox is not None:
659                    self.FitRectToWindow(bbox)
660    
661      def FitLayerToWindow(self, layer):      def FitLayerToWindow(self, layer):
662          """Fit the given layer to the window.          """Fit the given layer to the window.
# Line 605  class MapCanvas(wxWindow, Publisher): Line 685  class MapCanvas(wxWindow, Publisher):
685                  bbox = proj.ForwardBBox(bbox)                  bbox = proj.ForwardBBox(bbox)
686    
687              if bbox is not None:              if bbox is not None:
688                  self.FitRectToWindow(bbox)                  if len(shapes) == 1 and layer.ShapeType() == SHAPETYPE_POINT:
689                        self.ZoomFactor(1, self.proj_to_win(bbox[0], bbox[1]))
690                    else:
691                        self.FitRectToWindow(bbox)
692    
693      def ZoomFactor(self, factor, center = None):      def ZoomFactor(self, factor, center = None):
694          """Multiply the zoom by factor and center on center.          """Multiply the zoom by factor and center on center.
# Line 614  class MapCanvas(wxWindow, Publisher): Line 697  class MapCanvas(wxWindow, Publisher):
697          that should be centered. If it is omitted, it defaults to the          that should be centered. If it is omitted, it defaults to the
698          center of the window          center of the window
699          """          """
700          width, height = self.GetSizeTuple()          if self.scale > 0:
701          scale = self.scale * factor              width, height = self.GetSizeTuple()
702          offx, offy = self.offset              scale = self.scale * factor
703          if center is not None:              offx, offy = self.offset
704              cx, cy = center              if center is not None:
705          else:                  cx, cy = center
706              cx = width / 2              else:
707              cy = height / 2                  cx = width / 2
708          offset = (factor * (offx - cx) + width / 2,                  cy = height / 2
709                    factor * (offy - cy) + height / 2)              offset = (factor * (offx - cx) + width / 2,
710          self.set_view_transform(scale, offset)                      factor * (offy - cy) + height / 2)
711                self.set_view_transform(scale, offset)
712    
713      def ZoomOutToRect(self, rect):      def ZoomOutToRect(self, rect):
714          """Zoom out to fit the currently visible region into rect.          """Zoom out to fit the currently visible region into rect.
# Line 810  class MapCanvas(wxWindow, Publisher): Line 894  class MapCanvas(wxWindow, Publisher):
894              forward = None              forward = None
895    
896          scale = self.scale          scale = self.scale
897    
898            if scale == 0:
899                return None, None
900    
901          offx, offy = self.offset          offx, offy = self.offset
902    
903          if select_labels:          if select_labels:
# Line 855  class MapCanvas(wxWindow, Publisher): Line 943  class MapCanvas(wxWindow, Publisher):
943              layer = layers[layer_index]              layer = layers[layer_index]
944    
945              # search only in visible layers              # search only in visible layers
946              if not layer.Visible():              if not layer.Visible() or not layer.HasShapes():
947                  continue                  continue
948    
949              filled = layer.GetClassification().GetDefaultFill() \              filled = layer.GetClassification().GetDefaultFill() \
# Line 888  class MapCanvas(wxWindow, Publisher): Line 976  class MapCanvas(wxWindow, Publisher):
976    
977              if shapetype == SHAPETYPE_POLYGON:              if shapetype == SHAPETYPE_POLYGON:
978                  for i in shape_ids:                  for i in shape_ids:
979                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      shapefile = layer.ShapeStore().Shapefile().cobject()
980                                                      i,                      result = point_in_polygon_shape(shapefile, i,
981                                                      filled, stroked,                                                      filled, stroked,
982                                                      map_proj, layer_proj,                                                      map_proj, layer_proj,
983                                                      scale, -scale, offx, offy,                                                      scale, -scale, offx, offy,
# Line 899  class MapCanvas(wxWindow, Publisher): Line 987  class MapCanvas(wxWindow, Publisher):
987                          break                          break
988              elif shapetype == SHAPETYPE_ARC:              elif shapetype == SHAPETYPE_ARC:
989                  for i in shape_ids:                  for i in shape_ids:
990                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      shapefile = layer.ShapeStore().Shapefile().cobject()
991                        result = point_in_polygon_shape(shapefile,
992                                                      i, 0, 1,                                                      i, 0, 1,
993                                                      map_proj, layer_proj,                                                      map_proj, layer_proj,
994                                                      scale, -scale, offx, offy,                                                      scale, -scale, offx, offy,
# Line 961  class MapCanvas(wxWindow, Publisher): Line 1050  class MapCanvas(wxWindow, Publisher):
1050              # a label was selected              # a label was selected
1051              label_layer.RemoveLabel(shape_index)              label_layer.RemoveLabel(shape_index)
1052          elif layer is not None:          elif layer is not None:
1053              text = labeldialog.run_label_dialog(self, layer.table, shape_index)              text = labeldialog.run_label_dialog(self,
1054                                                    layer.ShapeStore().Table(),
1055                                                    shape_index)
1056              if text:              if text:
1057                  proj = self.map.projection                  proj = self.map.projection
1058                  if proj is not None:                  if proj is not None:
# Line 976  class MapCanvas(wxWindow, Publisher): Line 1067  class MapCanvas(wxWindow, Publisher):
1067    
1068                  shapetype = layer.ShapeType()                  shapetype = layer.ShapeType()
1069                  if shapetype == SHAPETYPE_POLYGON:                  if shapetype == SHAPETYPE_POLYGON:
1070                      x, y = shape_centroid(layer.shapefile.cobject(),                      shapefile = layer.ShapeStore().Shapefile().cobject()
1071                                            shape_index,                      x, y = shape_centroid(shapefile, shape_index,
1072                                            map_proj, layer_proj, 1, 1, 0, 0)                                            map_proj, layer_proj, 1, 1, 0, 0)
1073                      if map_proj is not None:                      if map_proj is not None:
1074                          x, y = map_proj.Inverse(x, y)                          x, y = map_proj.Inverse(x, y)

Legend:
Removed from v.940  
changed lines
  Added in v.1277

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26