/[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 909 by frank, Fri May 16 16:23:12 2003 UTC revision 1419 by bh, Tue Jul 15 09:29:18 2003 UTC
# Line 9  Line 9 
9    
10  __version__ = "$Revision$"  __version__ = "$Revision$"
11    
12    import cStringIO
13    
14  from Thuban import _  from Thuban import _
15    
16  from wxPython.wx import wxMemoryDC, wxEmptyBitmap, \  from wxPython.wx import wxMemoryDC, wxEmptyBitmap, \
17      wxPoint, wxRect, wxPen, wxBrush, wxFont, \      wxPoint, wxRect, wxPen, wxBrush, wxFont, \
18      wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \      wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \
19      wxBLACK_PEN, wxRED_PEN, wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL      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    
# Line 22  from Thuban.UI.common import Color2wxCol Line 27  from Thuban.UI.common import Color2wxCol
27  from Thuban.UI.classifier import ClassDataPreviewer  from Thuban.UI.classifier import ClassDataPreviewer
28  from Thuban.UI.scalebar import ScaleBar  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
38    
39    if Thuban.Model.resource.has_gdal_support():
40        from gdalwarp import ProjectRasterFile
41    
42  class MapRenderer:  class MapRenderer:
43    
# 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            if self.scale == 0:
79                return
80    
81            #
82            # This is only a good optimization if there is only one
83            # raster layer and the image covers the entire window (as
84            # it currently does). We note if there is a raster layer
85            # and only begin drawing layers once we have drawn it.
86            # That way we avoid drawing layers that won't be seen.
87            #
88            if Thuban.Model.resource.has_gdal_support():
89                for layer in map.Layers():
90                    if isinstance(layer, RasterLayer) and layer.Visible():
91                        seenRaster = False
92                        break
93    
94          for layer in map.Layers():          for layer in map.Layers():
95              # if honor_visibility is true, only draw visible layers,              # if honor_visibility is true, only draw visible layers,
96              # otherwise draw all layers              # otherwise draw all layers
97              if not self.honor_visibility or layer.Visible():              if not self.honor_visibility or layer.Visible():
98                  self.draw_shape_layer(layer)                  if isinstance(layer, Layer) and seenRaster:
99                        self.draw_shape_layer(layer)
100                    elif isinstance(layer, RasterLayer) \
101                        and Thuban.Model.resource.has_gdal_support():
102                        self.draw_raster_layer(layer)
103                        seenRaster = True
104    
105          self.draw_label_layer(map.LabelLayer())          self.draw_label_layer(map.LabelLayer())
106    
107      def draw_shape_layer(self, layer):      def draw_shape_layer(self, layer):
# Line 90  class MapRenderer: Line 123  class MapRenderer:
123          defaultGroup = lc.GetDefaultGroup()          defaultGroup = lc.GetDefaultGroup()
124    
125    
         if shapetype != SHAPETYPE_POINT:  
             polygon_render_param = self.polygon_render_param(layer)  
   
126          if shapetype == SHAPETYPE_POINT:          if shapetype == SHAPETYPE_POINT:
127              draw_func = lambda i: \              draw_func = self.draw_point_shape
128                     self.draw_point_shape(layer, i)              draw_func_param = layer
129          else:          else:
130              draw_func = lambda i: \              draw_func = draw_polygon_shape
131                     self.draw_polygon_shape(polygon_render_param, i, pen, brush)              draw_func_param = self.polygon_render_param(layer)
132                
133            table = layer.ShapeStore().Table()
134          for i in self.layer_ids(layer):          for i in self.layer_ids(layer):
135    
136              if field is None:              if field is None:
137                  group = defaultGroup                  group = defaultGroup
138              else:              else:
139                  record = layer.table.ReadRowAsDict(i)                  record = table.ReadRowAsDict(i)
140                  assert record is not None                  assert record is not None
141                  group = lc.FindGroup(record[field])                  group = lc.FindGroup(record[field])
142    
# Line 124  class MapRenderer: Line 155  class MapRenderer:
155                      old_prop = prop                      old_prop = prop
156    
157                      if shapetype == SHAPETYPE_ARC:                      if shapetype == SHAPETYPE_ARC:
158                          fill = Color.Transparent                          fill = Transparent
159                      else:                      else:
160                          fill = prop.GetFill()                          fill = prop.GetFill()
161    
162    
163                      if fill is Color.Transparent:                      if fill is Transparent:
164                          brush = wxTRANSPARENT_BRUSH                          brush = wxTRANSPARENT_BRUSH
165                      else:                      else:
166                          color = Color2wxColour(fill)                          color = Color2wxColour(fill)
# Line 137  class MapRenderer: Line 168  class MapRenderer:
168    
169                      stroke = prop.GetLineColor()                      stroke = prop.GetLineColor()
170                      stroke_width = prop.GetLineWidth()                      stroke_width = prop.GetLineWidth()
171                      if stroke is Color.Transparent:                      if stroke is Transparent:
172                          pen = wxTRANSPARENT_PEN                          pen = wxTRANSPARENT_PEN
173                      else:                      else:
174                          color = Color2wxColour(stroke)                          color = Color2wxColour(stroke)
175                          pen = wxPen(color, stroke_width, wxSOLID)                          pen = wxPen(color, stroke_width, wxSOLID)
176    
177                      if shapetype == SHAPETYPE_POINT:              draw_func(draw_func_param, i, pen, brush)
                         self.dc.SetBrush(brush)  
                         self.dc.SetPen(pen)  
178    
179              draw_func(i)      def draw_raster_layer(self, layer):
180            data = None
181            offx, offy = self.offset
182            width, height = self.dc.GetSizeTuple()
183    
184            inProj = ""
185            proj = layer.GetProjection()
186            if proj is not None:
187                for p in proj.GetAllParameters():
188                    inProj += "+" + p + " "
189    
190            outProj = ""
191            proj = self.map.GetProjection()
192            if proj is not None:
193                for p in proj.GetAllParameters():
194                    outProj += "+" + p + " "
195    
196            xmin = (0 - offx) / self.scale
197            ymin = (offy - height) / self.scale
198            xmax = (width - offx) / self.scale
199            ymax = (offy - 0) / self.scale
200    
201            try:
202                data = ProjectRasterFile(
203                    layer.GetImageFilename(),
204                    inProj,
205                    outProj,
206                    (xmin, ymin, xmax, ymax),
207                    "", (width, height))
208            except IOError, (strerr):
209                print strerr
210            except (AttributeError, ValueError):
211                pass
212            else:
213                if data is not None:
214                    stream = cStringIO.StringIO(data)
215                    image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP)
216                    bitmap = wxBitmapFromImage(image)
217                    self.dc.BeginDrawing()
218                    self.dc.DrawBitmap(bitmap, 0, 0)
219                    self.dc.EndDrawing()
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.
# Line 160  class MapRenderer: Line 229  class MapRenderer:
229      def polygon_render_param(self, layer):      def polygon_render_param(self, layer):
230          """Return the low-lever render parameter for the layer"""          """Return the low-lever render parameter for the layer"""
231          offx, offy = self.offset          offx, offy = self.offset
232          return draw_polygon_init(layer.shapefile, self.dc,          return draw_polygon_init(layer.ShapeStore().Shapefile(), self.dc,
233                                   self.map.projection,                                   self.map.projection,
234                                   layer.projection,                                   layer.projection,
235                                   self.scale, -self.scale,                                   self.scale, -self.scale,
# Line 197  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 439  class ExportRenderer(ScreenRenderer): Line 510  class ExportRenderer(ScreenRenderer):
510          # Render the legend          # Render the legend
511          dc.SetTextForeground(wxBLACK)          dc.SetTextForeground(wxBLACK)
512          if map.HasLayers():          if map.HasLayers():
513              for l in map.Layers():              layers = map.Layers()
514                layers.reverse()
515                for l in layers:
516                  if l.Visible():                  if l.Visible():
517                      # Render title                      # Render title
518                      dc.DrawText(l.Title(), posx, posy)                      dc.DrawText(l.Title(), posx, posy)
519                      posy+=stepy                      posy+=stepy
520                      # Render classification                      if l.HasClassification():
521                      clazz = l.GetClassification()                          # Render classification
522                      shapeType = l.ShapeType()                          clazz = l.GetClassification()
523                      for g in clazz:                          shapeType = l.ShapeType()
524                          if g.IsVisible():                          for g in clazz:
525                              previewer.Draw(dc,                              if g.IsVisible():
526                                  wxRect(posx+dx, posy, iconwidth, iconheight),                                  previewer.Draw(dc,
527                                  g.GetProperties(), shapeType)                                      wxRect(posx+dx, posy,
528                              dc.DrawText(g.GetDisplayText(),                                             iconwidth, iconheight),
529                                          posx+2*dx+iconwidth, posy)                                      g.GetProperties(), shapeType)
530                              posy+=stepy                                  dc.DrawText(g.GetDisplayText(),
531                                                posx+2*dx+iconwidth, posy)
532                                    posy+=stepy
533                    
534      def render_scalebar(self, map):      def render_scalebar(self, map):
535          """Render the scalebar."""          """Render the scalebar."""

Legend:
Removed from v.909  
changed lines
  Added in v.1419

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26