/[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 798 by jonathan, Wed Apr 30 17:02:04 2003 UTC revision 938 by jonathan, Tue May 20 15:25:10 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    from gdalwarp import ProjectRasterFile
26    
27  from Thuban import _  from Thuban.UI.common import Color2wxColour
28  from Thuban.UI.common import *  from Thuban.UI.classifier import ClassDataPreviewer
29    from Thuban.UI.scalebar import ScaleBar
30    
31  from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \  from Thuban.Model.layer import Layer, RasterLayer, \
32       SHAPETYPE_POINT       SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT
33  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
34       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE       ALIGN_LEFT, ALIGN_RIGHT, ALIGN_BASELINE
35    
# Line 59  class MapRenderer: Line 70  class MapRenderer:
70    
71      def render_map(self, map):      def render_map(self, map):
72          self.map = map          self.map = map
73            seenRaster = True
74    
75            #
76            # This is only a good optimization if there is only one
77            # raster layer and the image covers the entire window (as
78            # it currently does).
79            #
80            for layer in map.Layers():
81                if isinstance(layer, RasterLayer):
82                    seenRaster = False
83                    break
84    
85          for layer in map.Layers():          for layer in map.Layers():
86              # if honor_visibility is true, only draw visible layers,              # if honor_visibility is true, only draw visible layers,
87              # otherwise draw all layers              # otherwise draw all layers
88              if not self.honor_visibility or layer.Visible():              if not self.honor_visibility or layer.Visible():
89                  self.draw_shape_layer(layer)                  if isinstance(layer, Layer) and seenRaster:
90                        self.draw_shape_layer(layer)
91                    elif isinstance(layer, RasterLayer):
92                        self.draw_raster_layer(layer)
93                        seenRaster = True
94    
95          self.draw_label_layer(map.LabelLayer())          self.draw_label_layer(map.LabelLayer())
96    
97      def draw_shape_layer(self, layer):      def draw_shape_layer(self, layer):
# Line 100  class MapRenderer: Line 128  class MapRenderer:
128              if field is None:              if field is None:
129                  group = defaultGroup                  group = defaultGroup
130              else:              else:
131                  record = layer.table.read_record(i)                  record = layer.table.ReadRowAsDict(i)
132                  assert record is not None                  assert record is not None
133                  group = lc.FindGroup(record[field])                  group = lc.FindGroup(record[field])
134    
# Line 144  class MapRenderer: Line 172  class MapRenderer:
172    
173              draw_func(i)              draw_func(i)
174    
175        def draw_raster_layer(self, layer):
176            data = None
177            offx, offy = self.offset
178            width, height = self.dc.GetSizeTuple()
179    
180            inProj = ""
181            proj = layer.GetProjection()
182            if proj is not None:
183                for p in proj.GetAllParameters():
184                    inProj += "+" + p + " "
185    
186            outProj = ""
187            proj = self.map.GetProjection()
188            if proj is not None:
189                for p in proj.GetAllParameters():
190                    outProj += "+" + p + " "
191    
192            print "self.scale: ", self.scale, offx, offy, width, height
193            xmin = (0 - offx) / self.scale
194            ymin = (offy - height) / self.scale
195            xmax = (width - offx) / self.scale
196            ymax = (offy - 0) / self.scale
197    
198            try:
199                data = ProjectRasterFile(
200                    layer.GetImageFilename(),
201                    inProj,
202                    outProj,
203                    str(xmin), str(ymin), str(xmax), str(ymax),
204                    "", str(width), str(height));
205            except (AttributeError, IOError, ValueError):
206                pass
207            else:
208                if data is not None:
209                    stream = cStringIO.StringIO(data)
210                    image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP)
211                    bitmap = wxBitmapFromImage(image)
212                    self.dc.BeginDrawing()
213                    self.dc.DrawBitmap(bitmap, 0, 0)
214                    self.dc.EndDrawing()
215    
216      def layer_ids(self, layer):      def layer_ids(self, layer):
217          """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.
218    
# Line 325  class ScreenRenderer(MapRenderer): Line 394  class ScreenRenderer(MapRenderer):
394          return layer.ShapesInRegion((left, bottom, right, top))          return layer.ShapesInRegion((left, bottom, right, top))
395    
396    
397  class PrinterRender(MapRenderer):  class ExportRenderer(ScreenRenderer):
398    
399      # When printing we want to see all layers      honor_visibility = 1
     honor_visibility = 0  
   
     RenderMap = MapRenderer.render_map  
400            
401        def RenderMap(self, map, region, mapregion,
402                      selected_layer, selected_shapes ):
403            """Render the map.
404    
405            The rendering device has been specified during initialisation.
406            The device border distance was set in Thuban.UI.view.OutputTranform().
407            
408            RenderMap renders a frame set (one page frame, one around
409            legend/scalebar and one around the map), the map, the legend and the
410            scalebar on the given DC. The map is rendered with the region displayed
411            in the canvas view, centered on the area available for map display.
412            """
413            
414            self.update_region = region
415            self.selected_layer = selected_layer
416            self.selected_shapes = selected_shapes
417    
418            # Get some dimensions
419            llx, lly, urx, ury = region
420            self.mapregion = mapregion
421            mminx, mminy, mmaxx, mmaxy = self.mapregion
422    
423            # Manipulate the offset to position the map
424            offx, offy = self.offset
425            # 1. Shift to corner of map drawing area
426            offx = offx + mminx
427            offy = offy + mminy
428    
429            # 2. Center the map on the map drawing area:
430            # region identifies the region on the canvas view:
431            # center of map drawing area - half the size of region: rendering origin
432            self.shiftx = (mmaxx - mminx)*0.5 - (urx - llx)*0.5
433            self.shifty = (mmaxy - mminy)*0.5 - (ury - lly)*0.5
434    
435            self.offset = (offx+self.shiftx, offy+self.shifty)
436    
437            # Draw the map
438            self.dc.BeginDrawing()
439            self.dc.DestroyClippingRegion()
440            self.dc.SetClippingRegion(mminx+self.shiftx, mminy+self.shifty,
441                                      urx, ury)
442            self.render_map(map)
443            self.dc.EndDrawing()
444    
445            # Draw the rest (frames, legend, scalebar)
446            self.dc.BeginDrawing()
447            self.dc.DestroyClippingRegion()
448    
449            # Force the font for Legend drawing
450            font = wxFont(self.resolution * 10, wxSWISS, wxNORMAL, wxNORMAL)
451            self.dc.SetFont(font)
452    
453            self.render_frame(region)
454            self.render_legend(map)
455            self.render_scalebar(map)
456            self.dc.EndDrawing()
457    
458        def render_frame(self, region):
459            """Render the frames for map and legend/scalebar."""
460    
461            dc = self.dc
462            dc.SetPen(wxBLACK_PEN)
463            dc.SetBrush(wxTRANSPARENT_BRUSH)
464    
465            # Dimension stuff
466            width, height = dc.GetSizeTuple()
467            mminx, mminy, mmaxx, mmaxy = self.mapregion
468    
469            # Page Frame
470            dc.DrawRectangle(15,15,width-30, (mmaxy-mminy)+10)
471            
472            # Map Frame
473            llx, lly, urx, ury = region
474            dc.DrawRectangle(mminx + self.shiftx, mminy + self.shifty, urx, ury)
475            
476            # Legend Frame
477            dc.DrawRectangle(mmaxx+10,mminy,(width-20) - (mmaxx+10), mmaxy-mminy)
478    
479            dc.DestroyClippingRegion()
480            dc.SetClippingRegion(mmaxx+10,mminy,
481                                 (width-20) - (mmaxx+10), mmaxy-mminy)
482    
483        def render_legend(self, map):
484            """Render the legend on the Map."""
485    
486            previewer = ClassDataPreviewer()
487            dc = self.dc
488            dc.SetPen(wxBLACK_PEN)
489            dc.SetBrush(wxTRANSPARENT_BRUSH)
490    
491            # Dimension stuff
492            width, height = dc.GetSizeTuple()
493            mminx, mminy, mmaxx, mmaxy = self.mapregion
494            textwidth, textheight = dc.GetTextExtent("0")
495            iconwidth  = textheight
496            iconheight = textheight
497            stepy = textheight+3
498            dx = 10
499            posx = mmaxx + 10 + 5   # 10 pix distance mapframe/legend frame,
500                                    # 5 pix inside legend frame
501            posy = mminy + 5        # 5 pix inside legend frame
502            
503            # Render the legend
504            dc.SetTextForeground(wxBLACK)
505            if map.HasLayers():
506                for l in map.Layers():
507                    if l.Visible():
508                        # Render title
509                        dc.DrawText(l.Title(), posx, posy)
510                        posy+=stepy
511                        # Render classification
512                        clazz = l.GetClassification()
513                        shapeType = l.ShapeType()
514                        for g in clazz:
515                            if g.IsVisible():
516                                previewer.Draw(dc,
517                                    wxRect(posx+dx, posy, iconwidth, iconheight),
518                                    g.GetProperties(), shapeType)
519                                dc.DrawText(g.GetDisplayText(),
520                                            posx+2*dx+iconwidth, posy)
521                                posy+=stepy
522            
523        def render_scalebar(self, map):
524            """Render the scalebar."""
525    
526            scalebar = ScaleBar(map)
527    
528            # Dimension stuff
529            width, height = self.dc.GetSizeTuple()
530            mminx, mminy, mmaxx, mmaxy = self.mapregion
531          
532            # Render the scalebar
533            scalebar.DrawScaleBar(self.scale, self.dc,
534                                 (mmaxx+10+5, mmaxy-25),
535                                 ((width-15-5) - (mmaxx+10+5),20)
536                                )
537            # 10 pix between map and legend frame, 5 pix inside legend frame
538            # 25 pix from the legend frame bottom line
539            # Width: 15 pix from DC border, 5 pix inside frame, 10, 5 as above
540            # Height: 20
541    
542    class PrinterRenderer(ExportRenderer):
543    
544        # Printing as well as Export / Screen display only the visible layer.
545        honor_visibility = 1
546    

Legend:
Removed from v.798  
changed lines
  Added in v.938

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26