/[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 1219 by bh, Mon Jun 16 17:42:54 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    from gdalwarp import ProjectRasterFile
26    
27  from Thuban.UI.common import Color2wxColour  from Thuban.UI.common import Color2wxColour
28  from Thuban.UI.classifier import ClassDataPreviewer  from Thuban.UI.classifier import ClassDataPreviewer
29  from Thuban.UI.scalebar import ScaleBar  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    
36  from Thuban.Model.classification import Classification  from Thuban.Model.classification import Classification
37  from Thuban.Model.color import Color  from Thuban.Model.color import Color
38    import Thuban.Model.resource
39    
40  class MapRenderer:  class MapRenderer:
41    
# Line 64  class MapRenderer: Line 71  class MapRenderer:
71    
72      def render_map(self, map):      def render_map(self, map):
73          self.map = map          self.map = map
74            seenRaster = True
75    
76            if self.scale == 0:
77                return
78    
79            #
80            # This is only a good optimization if there is only one
81            # raster layer and the image covers the entire window (as
82            # it currently does). We note if there is a raster layer
83            # and only begin drawing layers once we have drawn it.
84            # That way we avoid drawing layers that won't be seen.
85            #
86            for layer in map.Layers():
87                if isinstance(layer, RasterLayer) and layer.Visible():
88                    seenRaster = False
89                    break
90    
91          for layer in map.Layers():          for layer in map.Layers():
92              # if honor_visibility is true, only draw visible layers,              # if honor_visibility is true, only draw visible layers,
93              # otherwise draw all layers              # otherwise draw all layers
94              if not self.honor_visibility or layer.Visible():              if not self.honor_visibility or layer.Visible():
95                  self.draw_shape_layer(layer)                  if isinstance(layer, Layer) and seenRaster:
96                        self.draw_shape_layer(layer)
97                    elif isinstance(layer, RasterLayer) \
98                        and Thuban.Model.resource.has_gdal_support():
99                        self.draw_raster_layer(layer)
100                        seenRaster = True
101    
102          self.draw_label_layer(map.LabelLayer())          self.draw_label_layer(map.LabelLayer())
103    
104      def draw_shape_layer(self, layer):      def draw_shape_layer(self, layer):
# Line 99  class MapRenderer: Line 129  class MapRenderer:
129          else:          else:
130              draw_func = lambda i: \              draw_func = lambda i: \
131                     self.draw_polygon_shape(polygon_render_param, i, pen, brush)                     self.draw_polygon_shape(polygon_render_param, i, pen, brush)
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 149  class MapRenderer: Line 180  class MapRenderer:
180    
181              draw_func(i)              draw_func(i)
182    
183        def draw_raster_layer(self, layer):
184            data = None
185            offx, offy = self.offset
186            width, height = self.dc.GetSizeTuple()
187    
188            inProj = ""
189            proj = layer.GetProjection()
190            if proj is not None:
191                for p in proj.GetAllParameters():
192                    inProj += "+" + p + " "
193    
194            outProj = ""
195            proj = self.map.GetProjection()
196            if proj is not None:
197                for p in proj.GetAllParameters():
198                    outProj += "+" + p + " "
199    
200            xmin = (0 - offx) / self.scale
201            ymin = (offy - height) / self.scale
202            xmax = (width - offx) / self.scale
203            ymax = (offy - 0) / self.scale
204    
205            try:
206                data = ProjectRasterFile(
207                    layer.GetImageFilename(),
208                    inProj,
209                    outProj,
210                    (xmin, ymin, xmax, ymax),
211                    "", (width, height))
212            except IOError, (strerr):
213                print strerr
214            except (AttributeError, ValueError):
215                pass
216            else:
217                if data is not None:
218                    stream = cStringIO.StringIO(data)
219                    image = wxImageFromStream(stream, wxBITMAP_TYPE_BMP)
220                    bitmap = wxBitmapFromImage(image)
221                    self.dc.BeginDrawing()
222                    self.dc.DrawBitmap(bitmap, 0, 0)
223                    self.dc.EndDrawing()
224    
225      def layer_ids(self, layer):      def layer_ids(self, layer):
226          """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.
227    
# Line 160  class MapRenderer: Line 233  class MapRenderer:
233      def polygon_render_param(self, layer):      def polygon_render_param(self, layer):
234          """Return the low-lever render parameter for the layer"""          """Return the low-lever render parameter for the layer"""
235          offx, offy = self.offset          offx, offy = self.offset
236          return draw_polygon_init(layer.shapefile, self.dc,          return draw_polygon_init(layer.ShapeStore().Shapefile(), self.dc,
237                                   self.map.projection,                                   self.map.projection,
238                                   layer.projection,                                   layer.projection,
239                                   self.scale, -self.scale,                                   self.scale, -self.scale,

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26