jonathan: thuban/Thuban/UI baserenderer.py, 1.13, 1.14 renderer.py, 1.53, 1.54

cvs@intevation.de cvs at intevation.de
Fri Jan 21 15:01:27 CET 2005


Author: jonathan

Update of /thubanrepository/thuban/Thuban/UI
In directory doto:/tmp/cvs-serv21080/Thuban/UI

Modified Files:
	baserenderer.py renderer.py 
Log Message:
Improved rendering raster layers by changing the return format of
the ProjectRasterFile function.


Index: baserenderer.py
===================================================================
RCS file: /thubanrepository/thuban/Thuban/UI/baserenderer.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- baserenderer.py	22 Nov 2004 11:16:35 -0000	1.13
+++ baserenderer.py	21 Jan 2005 14:01:25 -0000	1.14
@@ -76,6 +76,13 @@
     """
     del _renderer_extensions[:]
 
+def proj_params_to_str(proj):
+    "Build a string suitable for GDAL describing the given projection"
+    str = ""
+    if proj is not None:
+        for p in proj.GetAllParameters():
+            str += "+" + p + " "
+    return str
 
 #
 #       Base Renderer
@@ -177,35 +184,17 @@
         that methods especially in derived classes have access to the
         map if necessary.
         """
-        # Whether the raster layer has already been drawn. See below for
-        # the optimization this is used for
-        seenRaster = True
-
-        #
-        # This is only a good optimization if there is only one
-        # raster layer and the image covers the entire window (as
-        # it currently does). We note if there is a raster layer
-        # and only begin drawing layers once we have drawn it.
-        # That way we avoid drawing layers that won't be seen.
-        #
-        if Thuban.Model.resource.has_gdal_support():
-            for layer in self.map.Layers():
-                if isinstance(layer, RasterLayer) and layer.Visible():
-                    seenRaster = False
-                    break
 
         for layer in self.map.Layers():
             # if honor_visibility is true, only draw visible layers,
             # otherwise draw all layers
             if not self.honor_visibility or layer.Visible():
                 if isinstance(layer, Layer):
-                    if seenRaster:
-                        for i in self.draw_shape_layer_incrementally(layer):
-                            yield True
+                    for i in self.draw_shape_layer_incrementally(layer):
+                        yield True
                 elif isinstance(layer, RasterLayer) \
                     and Thuban.Model.resource.has_gdal_support():
                     self.draw_raster_layer(layer)
-                    seenRaster = True
                     yield True
                 else:
                     # look it up in the renderer extensions
@@ -370,6 +359,7 @@
         scale = self.scale
         offx, offy = self.offset
         make_point = self.make_point
+
         for part in points:
             result.append([])
             for x, y in part:
@@ -462,50 +452,70 @@
         offx, offy = self.offset
         width, height = self.dc.GetSizeTuple()
 
-        in_proj = ""
-        proj = layer.GetProjection()
-        if proj is not None:
-            for p in proj.GetAllParameters():
-                in_proj += "+" + p + " "
+        in_proj  = proj_params_to_str(layer.GetProjection())
+        out_proj = proj_params_to_str(self.map.GetProjection())
 
-        out_proj = ""
-        proj = self.map.GetProjection()
-        if proj is not None:
-            for p in proj.GetAllParameters():
-                out_proj += "+" + p + " "
+        # True  -- warp the image to the size of the whole screen
+        # False -- only use the bound box of the layer (currently inaccurate)
+        if True: 
+            pmin = [0,height]
+            pmax = [width, 0]
+        else:
+            bb = layer.LatLongBoundingBox()
+            bb = [[[bb[0], bb[1]], [bb[2], bb[3]],],]
+            pmin, pmax = self.projected_points(layer, bb)[0]
 
-        xmin = (0 - offx) / self.scale
-        ymin = (offy - height) / self.scale
-        xmax = (width - offx) / self.scale
-        ymax = (offy - 0) / self.scale
+        fmin = [max(0, pmin[0]) - offx, offy - min(height, pmin[1])]
+        fmax = [min(width, pmax[0]) - offx, offy - max(0, pmax[1])]
+
+        xmin = fmin[0]/self.scale
+        ymin = fmin[1]/self.scale
+        xmax = fmax[0]/self.scale
+        ymax = fmax[1]/self.scale
+
+        width  = int(min(width,  round(fmax[0] - fmin[0] + 1)))
+        height = int(min(height, round(fmax[1] - fmin[1] + 1)))
 
         try:
-            data = ProjectRasterFile(layer.GetImageFilename(),
+            data = [width, height,
+                    ProjectRasterFile(layer.GetImageFilename(),
                                      in_proj, out_proj,
                                      (xmin, ymin, xmax, ymax), "",
                                      (width, height))
+                   ]
         except (IOError, AttributeError, ValueError):
             # 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(data, "BMP")
+            mask = "#030104"
+            #mask = None
+            self.draw_raster_data(fmin[0]+offx, offy-fmax[1], data, "RAW", mask)
+            data = None
 
-    def draw_raster_data(self, data, format="BMP"):
-        """Draw the raster image in data onto the DC
+    def draw_raster_data(self, x, y, data, format="BMP", mask = None):
+        """Draw the raster image in data onto the DC with the top
+        left corner at (x,y)
 
-        The raster image data is a string holding the data in the format
-        indicated by the format parameter. The image is assumed to be
-        exactly the size of the dc and to cover it completely.
+        The raster image data is a list holding the image width, height,
+        and data in the format indicated by the format parameter. 
 
         The format parameter is a string with the name of the format.
         The following format names should be used:
 
-          'BMP' -- Windows Bitmap
-          'JPEG' -- Jpeg
+          'RAW'  -- an array of RGB values (len=3*width*height)
+          'BMP'  -- Windows Bitmap
+          'JPEG' -- JPEG Image
 
-        The default format is 'bmp'.
+        The default format is 'BMP'.
+
+        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
 
         This method has to be implemented by derived classes. The
         implementation in the derived class should try to support at

Index: renderer.py
===================================================================
RCS file: /thubanrepository/thuban/Thuban/UI/renderer.py,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- renderer.py	3 Jan 2005 16:21:17 -0000	1.53
+++ renderer.py	21 Jan 2005 14:01:25 -0000	1.54
@@ -22,7 +22,8 @@
     wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \
     wxBLACK_PEN, wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL, \
     wxBitmapFromImage, wxImageFromStream, wxBITMAP_TYPE_BMP, \
-    wxBITMAP_TYPE_JPEG, wxBITMAP_TYPE_PNG, wxBITMAP_TYPE_TIF, wxBITMAP_TYPE_GIF
+    wxBITMAP_TYPE_JPEG, wxBITMAP_TYPE_PNG, wxBITMAP_TYPE_TIF, \
+    wxBITMAP_TYPE_GIF, wxEmptyImage
 
 from wxproj import draw_polygon_shape, draw_polygon_init
 
@@ -38,6 +39,10 @@
 
 from baserenderer import BaseRenderer
 
+from math import floor
+
+from types import StringType
+
 
 # Map the strings used for the format parameter of the draw_raster_data
 # method to the appropriate wxWindows constants
@@ -99,11 +104,27 @@
         return wxFont(int(round(self.resolution * 10)), wxSWISS, wxNORMAL,
                       wxNORMAL)
 
-    def draw_raster_data(self, data, format = 'BMP'):
-        stream = cStringIO.StringIO(data)
-        image = wxImageFromStream(stream, raster_format_map[format])
+    def draw_raster_data(self, x,y, data, format = 'BMP', mask = None):
+        if format == 'RAW':
+            image = wxEmptyImage(data[0], data[1])
+            image.SetData(data[2])
+        else:
+            stream = cStringIO.StringIO(data[2])
+            image = wxImageFromStream(stream, raster_format_map[format])
+
         bitmap = wxBitmapFromImage(image)
-        self.dc.DrawBitmap(bitmap, 0, 0)
+
+        if mask is None:
+            self.dc.DrawBitmap(bitmap, int(round(x)), int(round(y)), False)
+        else:
+            # if we are given a mask object, try to pass it to SetMaskColour,
+            # otherwise assume it's a mask image
+            try:
+                bitmap.SetMaskColour(mask);
+                self.dc.DrawBitmap(bitmap, int(round(x)), int(round(y)), True)
+            except (TypeError):
+                # implement using a mask image
+                raise NotImplementedError
 
 
 class ScreenRenderer(MapRenderer):





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)