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

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

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

revision 676 by jonathan, Tue Apr 15 19:20:40 2003 UTC revision 1467 by jonathan, Tue Jul 22 14:02:57 2003 UTC
# Line 2  Line 2 
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  # Jonathan Coles <[email protected]>  # Jonathan Coles <[email protected]>
5    # Frank Koormann <[email protected]>
6  #  #
7  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
8  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
9    
10  __version__ = "$Revision$"  __version__ = "$Revision$"
11    
12  from wxPython.wx import wxPoint, wxPen, wxBrush, wxFont, \  import cStringIO
13       wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \  
14       wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL  from Thuban import _
15    
16    from wxPython.wx import wxMemoryDC, wxEmptyBitmap, \
17        wxPoint, wxRect, wxPen, wxBrush, wxFont, \
18        wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \
19        wxBLACK_PEN, wxRED_PEN, wxBLACK, \
20        wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL, \
21        wxBitmap, wxImageFromBitmap, wxBitmapFromImage, \
22        wxImageFromStream, wxBITMAP_TYPE_BMP
23    
24  from wxproj import draw_polygon_shape, draw_polygon_init  from wxproj import draw_polygon_shape, draw_polygon_init
25    
26  from Thuban import _  from Thuban.UI.common import Color2wxColour
27  from Thuban.UI.common import *  from Thuban.UI.classifier import ClassDataPreviewer
28    from Thuban.UI.scalebar import ScaleBar
29    
30  from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \  from Thuban.Model.layer import Layer, RasterLayer, \
31       SHAPETYPE_POINT       SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT
32  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
33       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE
34    
35  from Thuban.Model.classification import Classification  from Thuban.Model.classification import Classification
36  from Thuban.Model.color import Color  from Thuban.Model.color import Transparent
37    import Thuban.Model.resource
 import time  
38    
39    if Thuban.Model.resource.has_gdal_support():
40        from gdalwarp import ProjectRasterFile
41    
42  class MapRenderer:  class MapRenderer:
43    
# Line 51  class MapRenderer: Line 62  class MapRenderer:
62          """          """
63          # resolution in pixel/inch          # resolution in pixel/inch
64    
         assert scale > 0  
   
65          self.dc = dc          self.dc = dc
66          self.scale = scale          self.scale = scale
67          self.offset = offset          self.offset = offset
# Line 64  class MapRenderer: Line 73  class MapRenderer:
73    
74      def render_map(self, map):      def render_map(self, map):
75          self.map = map          self.map = map
76            seenRaster = True
77    
78            self.dc.BeginDrawing()
79    
80            #
81            # This is only a good optimization if there is only one
82            # raster layer and the image covers the entire window (as
83            # it currently does). We note if there is a raster layer
84            # and only begin drawing layers once we have drawn it.
85            # That way we avoid drawing layers that won't be seen.
86            #
87            if Thuban.Model.resource.has_gdal_support():
88                for layer in map.Layers():
89                    if isinstance(layer, RasterLayer) and layer.Visible():
90                        seenRaster = False
91                        break
92    
93          for layer in map.Layers():          for layer in map.Layers():
94              # if honor_visibility is true, only draw visible layers,              # if honor_visibility is true, only draw visible layers,
95              # otherwise draw all layers              # otherwise draw all layers
96              if not self.honor_visibility or layer.Visible():              if not self.honor_visibility or layer.Visible():
97                  self.draw_shape_layer(layer)                  if isinstance(layer, Layer) and seenRaster:
98                        self.draw_shape_layer(layer)
99                    elif isinstance(layer, RasterLayer) \
100                        and Thuban.Model.resource.has_gdal_support():
101                        self.draw_raster_layer(layer)
102                        seenRaster = True
103    
104          self.draw_label_layer(map.LabelLayer())          self.draw_label_layer(map.LabelLayer())
105    
106            self.dc.EndDrawing()
107    
108      def draw_shape_layer(self, layer):      def draw_shape_layer(self, layer):
109          scale = self.scale          scale = self.scale
110          offx, offy = self.offset          offx, offy = self.offset
# Line 84  class MapRenderer: Line 118  class MapRenderer:
118          pen   = wxTRANSPARENT_PEN          pen   = wxTRANSPARENT_PEN
119    
120          old_prop = None          old_prop = None
121            old_group = None
122          lc = layer.GetClassification()          lc = layer.GetClassification()
123          field = lc.GetField()          field = layer.GetClassificationColumn()
124            defaultGroup = lc.GetDefaultGroup()
125    
         #print "drawing layer: ", layer.Title(),  
         #start = time.clock()  
         #count = 0  
   
         if shapetype != SHAPETYPE_POINT:  
             offx, offy = self.offset          
             draw_polygon_init(layer.shapefile.cobject(),  
                               self.dc,  
                               self.map.projection, layer.projection,  
                               self.scale, -self.scale, offx, offy)  
126    
         for i in self.layer_ids(layer):  
             value = None  
127    
128              if field is not None:          if shapetype == SHAPETYPE_POINT:
129                  try:              draw_func = self.draw_point_shape
130                      record = layer.table.read_record(i)              draw_func_param = layer
131                      if record is not None:          else:
132                          value = record[field]              draw_func = draw_polygon_shape
133                  except:              draw_func_param = self.polygon_render_param(layer)
                     pass  
   
                 #  
                 # if the above statements fail 'value' should  
                 # be null, at which point this call will  
                 # at least retreive the NullData  
                 #  
134    
135                  group = lc.FindGroup(value)          table = layer.ShapeStore().Table()
136            for i in self.layer_ids(layer):
137    
138                  #prop = lc.GetProperties(value)              if field is None:
139                    group = defaultGroup
140              else:              else:
141                  group = lc.GetDefaultGroup()                  record = table.ReadRowAsDict(i)
142                    assert record is not None
143                    group = lc.FindGroup(record[field])
144    
145    
146              if not group.IsVisible():              if not group.IsVisible():
147                  continue                  continue
148    
             prop = group.GetProperties()  
149    
150              # don't recreate new objects if they are the same as before              # don't recreate new objects if they are the same as before
151              if prop != old_prop:              if group is not old_group:
152                  old_prop = prop                  old_group = group
153    
154                  if shapetype == SHAPETYPE_ARC:                  prop = group.GetProperties()
                     fill = Color.Transparent  
                 else:  
                     fill = prop.GetFill()  
155    
156                        if prop != old_prop:
157                  if fill is Color.Transparent:                      old_prop = prop
                     brush = wxTRANSPARENT_BRUSH  
                 else:  
                     color = Color2wxColour(fill)  
                     brush = wxBrush(color, wxSOLID)  
       
                 stroke = prop.GetLineColor()  
                 stroke_width = prop.GetLineWidth()  
                 if stroke is Color.Transparent:  
                     pen = wxTRANSPARENT_PEN  
                 else:  
                     color = Color2wxColour(stroke)  
                     pen = wxPen(color, stroke_width, wxSOLID)  
158    
159                  self.dc.SetBrush(brush)                      if shapetype == SHAPETYPE_ARC:
160                  self.dc.SetPen(pen)                          fill = Transparent
161                            else:
162              if shapetype == SHAPETYPE_POINT:                          fill = prop.GetFill()
163                  self.draw_point_shape(layer, i)  
164              else:  
165                  self.draw_polygon_shape(layer, i, pen, brush)                      if fill is Transparent:
166                            brush = wxTRANSPARENT_BRUSH
167                        else:
168                            color = Color2wxColour(fill)
169                            brush = wxBrush(color, wxSOLID)
170    
171                        stroke = prop.GetLineColor()
172                        stroke_width = prop.GetLineWidth()
173                        if stroke is Transparent:
174                            pen = wxTRANSPARENT_PEN
175                        else:
176                            color = Color2wxColour(stroke)
177                            pen = wxPen(color, stroke_width, wxSOLID)
178    
179              #count += 1              draw_func(draw_func_param, i, pen, brush)
180    
181        def draw_raster_layer(self, layer):
182            data = None
183            offx, offy = self.offset
184            width, height = self.dc.GetSizeTuple()
185    
186            inProj = ""
187            proj = layer.GetProjection()
188            if proj is not None:
189                for p in proj.GetAllParameters():
190                    inProj += "+" + p + " "
191    
192          #end = time.clock()          outProj = ""
193          #print (end-start), "seconds -- ", count          proj = self.map.GetProjection()
194            if proj is not None:
195                for p in proj.GetAllParameters():
196                    outProj += "+" + p + " "
197    
198            xmin = (0 - offx) / self.scale
199            ymin = (offy - height) / self.scale
200            xmax = (width - offx) / self.scale
201            ymax = (offy - 0) / self.scale
202    
203            try:
204                data = ProjectRasterFile(
205                    layer.GetImageFilename(),
206                    inProj,
207                    outProj,
208                    (xmin, ymin, xmax, ymax),
209                    "", (width, height))
210            except IOError, (strerr):
211                print strerr
212            except (AttributeError, ValueError):
213                pass
214            else:
215                if data is not None:
216                    stream = cStringIO.StringIO(data)
217                    image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP)
218                    bitmap = wxBitmapFromImage(image)
219                    self.dc.DrawBitmap(bitmap, 0, 0)
220    
221      def layer_ids(self, layer):      def layer_ids(self, layer):
222          """Return the shape ids of the given layer that have to be drawn.          """Return the shape ids of the given layer that have to be drawn.
223            
224          The default implementation simply returns all ids in the layer.          The default implementation simply returns all ids in the layer.
225          Override in derived classes to be more precise.          Override in derived classes to be more precise.
226          """          """
227          return range(layer.NumShapes())          return range(layer.NumShapes())
228    
229      def draw_polygon_shape(self, layer, index, pen, brush):      def polygon_render_param(self, layer):
230          draw_polygon_shape(index, pen, brush)          """Return the low-lever render parameter for the layer"""
231            offx, offy = self.offset
232            return draw_polygon_init(layer.ShapeStore().Shapefile(), self.dc,
233                                     self.map.projection,
234                                     layer.projection,
235                                     self.scale, -self.scale,
236                                     offx, offy)
237    
238        def draw_polygon_shape(self, draw_polygon_info, index, pen, brush):
239            draw_polygon_shape(draw_polygon_info, index, pen, brush)
240    
241      def projected_points(self, layer, index):      def projected_points(self, layer, index):
242          proj = self.map.projection          proj = self.map.GetProjection()
243          if proj is not None:          if proj is not None:
244              forward = proj.Forward              forward = proj.Forward
245          else:          else:
246              forward = None              forward = None
247          proj = layer.projection          proj = layer.GetProjection()
248          if proj is not None:          if proj is not None:
249              inverse = proj.Inverse              inverse = proj.Inverse
250          else:          else:
# Line 204  class MapRenderer: Line 266  class MapRenderer:
266          points = self.projected_points(layer, index)          points = self.projected_points(layer, index)
267          self.dc.DrawLines(points)          self.dc.DrawLines(points)
268    
269      def draw_point_shape(self, layer, index):      def draw_point_shape(self, layer, index, pen, brush):
270          pp = self.projected_points(layer, index)          pp = self.projected_points(layer, index)
271    
272          if len(pp) == 0: return # ignore Null Shapes which have no points          if len(pp) == 0: return # ignore Null Shapes which have no points
273    
274          p = pp[0]          p = pp[0]
275          radius = self.resolution * 5          radius = self.resolution * 5
276            self.dc.SetBrush(brush)
277            self.dc.SetPen(pen)
278          self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)          self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)
279    
280      def draw_label_layer(self, layer):      def draw_label_layer(self, layer):
# Line 278  class ScreenRenderer(MapRenderer): Line 342  class ScreenRenderer(MapRenderer):
342    
343              shapetype = layer.ShapeType()              shapetype = layer.ShapeType()
344              if shapetype == SHAPETYPE_POLYGON:              if shapetype == SHAPETYPE_POLYGON:
345                    offx, offy = self.offset
346                    renderparam = self.polygon_render_param(layer)
347                  func = self.draw_polygon_shape                  func = self.draw_polygon_shape
348                  args = (pen, brush)                  args = (pen, brush)
349              elif shapetype == SHAPETYPE_ARC:              elif shapetype == SHAPETYPE_ARC:
350                    renderparam = self.polygon_render_param(layer)
351                  func = self.draw_polygon_shape                  func = self.draw_polygon_shape
352                  args = (pen, None)                  args = (pen, None)
353              elif shapetype == SHAPETYPE_POINT:              elif shapetype == SHAPETYPE_POINT:
354                    renderparam = layer
355                  self.dc.SetBrush(brush)                  self.dc.SetBrush(brush)
356                  self.dc.SetPen(pen)                  self.dc.SetPen(pen)
357                  func = self.draw_point_shape                  func = self.draw_point_shape
358                  args = ()                  args = (pen, brush)
359              else:              else:
360                  raise TypeError(_("Unhandled shape type %s") % shapetype)                  raise TypeError(_("Unhandled shape type %s") % shapetype)
361    
362              for index in self.selected_shapes:              for index in self.selected_shapes:
363                  func(layer, index, *args)                  func(renderparam, index, *args)
364    
365      def layer_ids(self, layer):      def layer_ids(self, layer):
366          """Return the shapeids covered by the region that has to be redrawn          """Return the shapeids covered by the region that has to be redrawn
# Line 332  class ScreenRenderer(MapRenderer): Line 400  class ScreenRenderer(MapRenderer):
400          return layer.ShapesInRegion((left, bottom, right, top))          return layer.ShapesInRegion((left, bottom, right, top))
401    
402    
403  class PrinterRender(MapRenderer):  class ExportRenderer(ScreenRenderer):
404    
405      # When printing we want to see all layers      honor_visibility = 1
     honor_visibility = 0  
   
     RenderMap = MapRenderer.render_map  
406            
407        def RenderMap(self, map, region, mapregion,
408                      selected_layer, selected_shapes ):
409            """Render the map.
410    
411            The rendering device has been specified during initialisation.
412            The device border distance was set in Thuban.UI.view.OutputTranform().
413            
414            RenderMap renders a frame set (one page frame, one around
415            legend/scalebar and one around the map), the map, the legend and the
416            scalebar on the given DC. The map is rendered with the region displayed
417            in the canvas view, centered on the area available for map display.
418            """
419            
420            self.update_region = region
421            self.selected_layer = selected_layer
422            self.selected_shapes = selected_shapes
423    
424            # Get some dimensions
425            llx, lly, urx, ury = region
426            self.mapregion = mapregion
427            mminx, mminy, mmaxx, mmaxy = self.mapregion
428    
429            # Manipulate the offset to position the map
430            offx, offy = self.offset
431            # 1. Shift to corner of map drawing area
432            offx = offx + mminx
433            offy = offy + mminy
434    
435            # 2. Center the map on the map drawing area:
436            # region identifies the region on the canvas view:
437            # center of map drawing area - half the size of region: rendering origin
438            self.shiftx = (mmaxx - mminx)*0.5 - (urx - llx)*0.5
439            self.shifty = (mmaxy - mminy)*0.5 - (ury - lly)*0.5
440    
441            self.offset = (offx+self.shiftx, offy+self.shifty)
442    
443            # Draw the map
444            self.dc.BeginDrawing()
445            self.dc.DestroyClippingRegion()
446            self.dc.SetClippingRegion(mminx+self.shiftx, mminy+self.shifty,
447                                      urx, ury)
448            self.render_map(map)
449            self.dc.EndDrawing()
450    
451            # Draw the rest (frames, legend, scalebar)
452            self.dc.BeginDrawing()
453            self.dc.DestroyClippingRegion()
454    
455            # Force the font for Legend drawing
456            font = wxFont(self.resolution * 10, wxSWISS, wxNORMAL, wxNORMAL)
457            self.dc.SetFont(font)
458    
459            self.render_frame(region)
460            self.render_legend(map)
461            self.render_scalebar(map)
462            self.dc.EndDrawing()
463    
464        def render_frame(self, region):
465            """Render the frames for map and legend/scalebar."""
466    
467            dc = self.dc
468            dc.SetPen(wxBLACK_PEN)
469            dc.SetBrush(wxTRANSPARENT_BRUSH)
470    
471            # Dimension stuff
472            width, height = dc.GetSizeTuple()
473            mminx, mminy, mmaxx, mmaxy = self.mapregion
474    
475            # Page Frame
476            dc.DrawRectangle(15,15,width-30, (mmaxy-mminy)+10)
477            
478            # Map Frame
479            llx, lly, urx, ury = region
480            dc.DrawRectangle(mminx + self.shiftx, mminy + self.shifty, urx, ury)
481            
482            # Legend Frame
483            dc.DrawRectangle(mmaxx+10,mminy,(width-20) - (mmaxx+10), mmaxy-mminy)
484    
485            dc.DestroyClippingRegion()
486            dc.SetClippingRegion(mmaxx+10,mminy,
487                                 (width-20) - (mmaxx+10), mmaxy-mminy)
488    
489        def render_legend(self, map):
490            """Render the legend on the Map."""
491    
492            previewer = ClassDataPreviewer()
493            dc = self.dc
494            dc.SetPen(wxBLACK_PEN)
495            dc.SetBrush(wxTRANSPARENT_BRUSH)
496    
497            # Dimension stuff
498            width, height = dc.GetSizeTuple()
499            mminx, mminy, mmaxx, mmaxy = self.mapregion
500            textwidth, textheight = dc.GetTextExtent("0")
501            iconwidth  = textheight
502            iconheight = textheight
503            stepy = textheight+3
504            dx = 10
505            posx = mmaxx + 10 + 5   # 10 pix distance mapframe/legend frame,
506                                    # 5 pix inside legend frame
507            posy = mminy + 5        # 5 pix inside legend frame
508            
509            # Render the legend
510            dc.SetTextForeground(wxBLACK)
511            if map.HasLayers():
512                layers = map.Layers()
513                layers.reverse()
514                for l in layers:
515                    if l.Visible():
516                        # Render title
517                        dc.DrawText(l.Title(), posx, posy)
518                        posy+=stepy
519                        if l.HasClassification():
520                            # Render classification
521                            clazz = l.GetClassification()
522                            shapeType = l.ShapeType()
523                            for g in clazz:
524                                if g.IsVisible():
525                                    previewer.Draw(dc,
526                                        wxRect(posx+dx, posy,
527                                               iconwidth, iconheight),
528                                        g.GetProperties(), shapeType)
529                                    dc.DrawText(g.GetDisplayText(),
530                                                posx+2*dx+iconwidth, posy)
531                                    posy+=stepy
532            
533        def render_scalebar(self, map):
534            """Render the scalebar."""
535    
536            scalebar = ScaleBar(map)
537    
538            # Dimension stuff
539            width, height = self.dc.GetSizeTuple()
540            mminx, mminy, mmaxx, mmaxy = self.mapregion
541          
542            # Render the scalebar
543            scalebar.DrawScaleBar(self.scale, self.dc,
544                                 (mmaxx+10+5, mmaxy-25),
545                                 ((width-15-5) - (mmaxx+10+5),20)
546                                )
547            # 10 pix between map and legend frame, 5 pix inside legend frame
548            # 25 pix from the legend frame bottom line
549            # Width: 15 pix from DC border, 5 pix inside frame, 10, 5 as above
550            # Height: 20
551    
552    class PrinterRenderer(ExportRenderer):
553    
554        # Printing as well as Export / Screen display only the visible layer.
555        honor_visibility = 1
556    

Legend:
Removed from v.676  
changed lines
  Added in v.1467

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26