/[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

trunk/thuban/Thuban/UI/baserenderer.py revision 2552 by jonathan, Fri Jan 28 15:54:00 2005 UTC branches/WIP-pyshapelib-bramz/Thuban/UI/baserenderer.py revision 2755 by dpinte, Thu Apr 12 09:21:58 2007 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 457  class BaseRenderer: Line 454  class BaseRenderer:
454    
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:          #if False:
459              pmin = [0,height]              pmin = [0,height]
460              pmax = [width, 0]              pmax = [width, 0]
# Line 469  class BaseRenderer: Line 466  class BaseRenderer:
466          #print bb          #print bb
467          #print pmin, pmax          #print pmin, pmax
468    
469          fmin = [max(0, min(pmin[0], pmax[0])) - offx,          fmin = [max(0, min(pmin[0], pmax[0])) - offx,
470                  offy - min(height, max(pmin[1], pmax[1]))]                  offy - min(height, max(pmin[1], pmax[1]))]
471    
472          fmax = [min(width, max(pmin[0], pmax[0])) - offx,          fmax = [min(width, max(pmin[0], pmax[0])) - offx,
473                  offy - max(0, min(pmin[1], pmax[1]))]                  offy - max(0, min(pmin[1], pmax[1]))]
474    
475          xmin = fmin[0]/self.scale          xmin = fmin[0]/self.scale
# Line 483  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              options = 0          options = options | layer.MaskType()
             if layer.UseMask(): options = options | 1  
485    
486              project_params = (layer.GetImageFilename(), in_proj, out_proj,          img_data = self.projected_raster_layer(layer, in_proj, out_proj,
487                                (xmin, ymin, xmax, ymax), "", (width, height),                      (xmin,ymin,xmax,ymax), [0,0], (width, height), options)
488                                options)  
489            if img_data is not None:
490              data = (width, height, apply(ProjectRasterFile, project_params))              data = (width, height, img_data)
491                self.draw_raster_data(fmin[0]+offx, offy-fmax[1],
492          except (MemoryError, IOError, AttributeError, ValueError):                                    data, format="RAW", opacity=layer.Opacity())
             # Why does this catch AttributeError and ValueError?  
             # FIXME: The exception should be communicated to the user  
             # better.  
             traceback.print_exc()  
         else:  
             self.draw_raster_data(fmin[0]+offx, offy-fmax[1], data, "RAW")  
493              data = None              data = None
494    
495      def draw_raster_data(self, x, y, data, format="BMP"):      def projected_raster_layer(self, layer, srcProj, dstProj, extents,
496          """Draw the raster image in data onto the DC with the top                                 resolution, dimensions, options):
497            """Return the projected raster image associated with the layer.
498    
499            The returned value will be a tuple of the form
500    
501                (image_data, mask_data, alpha_data)
502    
503            suitable for the data parameter to draw_raster_data.
504    
505            The return value may be None if raster projections are not supported.
506    
507            srcProj     --  a string describing the source projection
508            dstProj     --  a string describing the destination projection
509            extents     --  a tuple of the region to project in map coordinates
510            resolution  --  (currently not used, defaults to [0,0])
511            dimensions  --  a tuple (width, height) for the output image
512            options     --  bit-wise options to pass to the renderer
513    
514            the currently supported values for options are
515    
516                OPTS_MASK        = 1  -- generate a mask
517                OPTS_ALPHA       = 2  -- generate an alpha channel
518                OPTS_INVERT_MASK = 4  -- invert the values in the mask
519                                         (if generated)
520    
521            This method has to be implemented by derived classes.
522            """
523    
524            raise NotImplementedError
525    
526        def draw_raster_data(self, x, y, data, format="BMP", opacity=1.0):
527            """Draw a raster image held in data onto the DC with the top
528          left corner at (x,y)          left corner at (x,y)
529    
530          The raster image data is a tuple of the form          The raster image data is a tuple of the form
531              (width, height, (image_data, mask_data))              (width, height, (image_data, mask_data, alpha_data))
532                    
533          holding the image width, height, image data, and mask data.          holding the image width, height, image data, mask data, and alpha data.
534          mask_data may be None if a mask should not be used. If          mask_data may be None if a mask should not be used. alpha_data may
535            also be None. If both are not None mask overrides alpha. If
536          format is 'RAW' the data will be RGB values and the mask          format is 'RAW' the data will be RGB values and the mask
537          will be in XMB format. Otherwise, both kinds          will be in XMB format. Otherwise, both kinds
538          of data are assumed to be in the format specified in format.          of data are assumed to be in the format specified in format.
# Line 519  class BaseRenderer: Line 541  class BaseRenderer:
541          The following format names should be used:          The following format names should be used:
542    
543            'RAW'  -- an array of RGB values (len=3*width*height)            'RAW'  -- an array of RGB values (len=3*width*height)
544              'PNG'  -- Portable Network Graphic (transparency supported)
545            'BMP'  -- Windows Bitmap            'BMP'  -- Windows Bitmap
546              'TIFF' -- Tagged Image File Format
547              'GIF'  -- GIF Image
548            'JPEG' -- JPEG Image            'JPEG' -- JPEG Image
549    
550          The default format is 'BMP'.          The default format is 'BMP'.

Legend:
Removed from v.2552  
changed lines
  Added in v.2755

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26