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

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

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

revision 2537 by jonathan, Fri Jan 21 14:01:25 2005 UTC revision 2571 by jonathan, Fri Feb 18 14:54:17 2005 UTC
# Line 30  from Thuban.Model.label import ALIGN_CEN Line 30  from Thuban.Model.label import ALIGN_CEN
30    
31  import Thuban.Model.resource  import Thuban.Model.resource
32    
 if Thuban.Model.resource.has_gdal_support():  
     from gdalwarp import ProjectRasterFile  
33    
34    
35  #  #
# Line 192  class BaseRenderer: Line 190  class BaseRenderer:
190                  if isinstance(layer, Layer):                  if isinstance(layer, Layer):
191                      for i in self.draw_shape_layer_incrementally(layer):                      for i in self.draw_shape_layer_incrementally(layer):
192                          yield True                          yield True
193                  elif isinstance(layer, RasterLayer) \                  elif isinstance(layer, RasterLayer):
                     and Thuban.Model.resource.has_gdal_support():  
194                      self.draw_raster_layer(layer)                      self.draw_raster_layer(layer)
195                      yield True                      yield True
196                  else:                  else:
# Line 443  class BaseRenderer: Line 440  class BaseRenderer:
440      def draw_raster_layer(self, layer):      def draw_raster_layer(self, layer):
441          """Draw the raster layer          """Draw the raster layer
442    
443          This implementation does the projection and scaling of the data          This implementation uses self.projected_raster_layer() to project
444          as required by the layer's and map's projections and the scale          and scale the data as required by the layer's and map's projections
445          and offset of the renderer and then hands the transformed data          and the scale and offset of the renderer and then hands the transformed
446          to self.draw_raster_data() which has to be implemented in          data to self.draw_raster_data() which has to be implemented in
447          derived classes.          derived classes.
448          """          """
449          offx, offy = self.offset          offx, offy = self.offset
# Line 458  class BaseRenderer: Line 455  class BaseRenderer:
455          # True  -- warp the image to the size of the whole screen          # True  -- warp the image to the size of the whole screen
456          # False -- only use the bound box of the layer (currently inaccurate)          # False -- only use the bound box of the layer (currently inaccurate)
457          if True:          if True:
458            #if False:
459              pmin = [0,height]              pmin = [0,height]
460              pmax = [width, 0]              pmax = [width, 0]
461          else:          else:
# Line 465  class BaseRenderer: Line 463  class BaseRenderer:
463              bb = [[[bb[0], bb[1]], [bb[2], bb[3]],],]              bb = [[[bb[0], bb[1]], [bb[2], bb[3]],],]
464              pmin, pmax = self.projected_points(layer, bb)[0]              pmin, pmax = self.projected_points(layer, bb)[0]
465    
466          fmin = [max(0, pmin[0]) - offx, offy - min(height, pmin[1])]          #print bb
467          fmax = [min(width, pmax[0]) - offx, offy - max(0, pmax[1])]          #print pmin, pmax
468    
469            fmin = [max(0, min(pmin[0], pmax[0])) - offx,
470                    offy - min(height, max(pmin[1], pmax[1]))]
471    
472            fmax = [min(width, max(pmin[0], pmax[0])) - offx,
473                    offy - max(0, min(pmin[1], pmax[1]))]
474    
475          xmin = fmin[0]/self.scale          xmin = fmin[0]/self.scale
476          ymin = fmin[1]/self.scale          ymin = fmin[1]/self.scale
# Line 476  class BaseRenderer: Line 480  class BaseRenderer:
480          width  = int(min(width,  round(fmax[0] - fmin[0] + 1)))          width  = int(min(width,  round(fmax[0] - fmin[0] + 1)))
481          height = int(min(height, round(fmax[1] - fmin[1] + 1)))          height = int(min(height, round(fmax[1] - fmin[1] + 1)))
482    
483          try:          options = 0
484              data = [width, height,          options = options | layer.MaskType()
485                      ProjectRasterFile(layer.GetImageFilename(),  
486                                       in_proj, out_proj,          img_data = self.projected_raster_layer(layer, in_proj, out_proj,
487                                       (xmin, ymin, xmax, ymax), "",                      (xmin,ymin,xmax,ymax), [0,0], (width, height), options)
488                                       (width, height))  
489                     ]          if img_data is not None:
490          except (IOError, AttributeError, ValueError):              data = (width, height, img_data)
491              # Why does this catch AttributeError and ValueError?              self.draw_raster_data(fmin[0]+offx, offy-fmax[1], data, "RAW")
             # FIXME: The exception should be communicated to the user  
             # better.  
             traceback.print_exc()  
         else:  
             mask = "#030104"  
             #mask = None  
             self.draw_raster_data(fmin[0]+offx, offy-fmax[1], data, "RAW", mask)  
492              data = None              data = None
493    
494      def draw_raster_data(self, x, y, data, format="BMP", mask = None):      def projected_raster_layer(self, layer, srcProj, dstProj, extents,
495                                   resolution, dimensions, options):
496            """Return the projected raster image associated with the layer.
497    
498            The returned value will be a tuple of the form
499    
500                (image_data, mask_data, alpha_data)
501    
502            suitable for the data parameter to draw_raster_data.
503    
504            The return value may be None if raster projections are not supported.
505    
506            srcProj     --  a string describing the source projection
507            dstProj     --  a string describing the destination projection
508            extents     --  a tuple of the region to project in map coordinates
509            resolution  --  (currently not used, defaults to [0,0])
510            dimensions  --  a tuple (width, height) for the output image
511            options     --  bit-wise options to pass to the renderer
512    
513            the currently supported values for options are
514    
515                OPTS_MASK        = 1  -- generate a mask
516                OPTS_ALPHA       = 2  -- generate an alpha channel
517                OPTS_INVERT_MASK = 4  -- invert the values in the mask
518                                         (if generated)
519    
520            This method has to be implemented by derived classes.
521            """
522    
523            raise NotImplementedError
524    
525        def draw_raster_data(self, x, y, data, format="BMP"):
526          """Draw the raster image in data onto the DC with the top          """Draw the raster image in data onto the DC with the top
527          left corner at (x,y)          left corner at (x,y)
528    
529          The raster image data is a list holding the image width, height,          The raster image data is a tuple of the form
530          and data in the format indicated by the format parameter.              (width, height, (image_data, mask_data, alpha_data))
531            
532            holding the image width, height, image data, mask data, and alpha data.
533            mask_data may be None if a mask should not be used. alpha_data may
534            also be None. If both are not None mask overrides alpha. If
535            format is 'RAW' the data will be RGB values and the mask
536            will be in XMB format. Otherwise, both kinds
537            of data are assumed to be in the format specified in format.
538    
539          The format parameter is a string with the name of the format.          The format parameter is a string with the name of the format.
540          The following format names should be used:          The following format names should be used:
# Line 510  class BaseRenderer: Line 545  class BaseRenderer:
545    
546          The default format is 'BMP'.          The default format is 'BMP'.
547    
         The mask parameter determines how a mask (if any) is applied  
         to the image. mask can have the following values:  
           
           o  None -- no mask is used  
           o  Any object accepted by wxBitmap.SetMaskColour()  
           o  A one-bit image the same size as the image data  
   
548          This method has to be implemented by derived classes. The          This method has to be implemented by derived classes. The
549          implementation in the derived class should try to support at          implementation in the derived class should try to support at
550          least the formats specified above and may support more.          least the formats specified above and may support more.

Legend:
Removed from v.2537  
changed lines
  Added in v.2571

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26