source: DPPP/ukcip02_server/trunk/ukcip_server/ukcip_server/model/pywms/render_imp.py @ 3568

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/ukcip02_server/trunk/ukcip_server/ukcip_server/model/pywms/render_imp.py@3568
Revision 3568, 4.0 KB checked in by spascoe, 14 years ago (diff)

Some changes to work behind a proxy and other misc. stuff.

Line 
1"""
2Implementation of grid rendering classes.
3
4@author: Stephen Pascoe
5"""
6
7from view import GridRenderer
8import ImageColor, Image
9import Numeric as N
10import logging
11from matplotlib import cm, colors
12
13logger = logging.getLogger(__name__)
14
15class RGBARenderer(GridRenderer):
16    """Creates an RGBA PNG with a selectable matplotlib colour scale.
17    """
18
19    mimeType = 'image/png'
20
21    def __init__(self, varmin, varmax):
22        self.varmin = varmin
23        self.varmax = varmax
24        self._norm = colors.normalize(varmin, varmax)
25
26    def renderColourbar(self, width, height, cmap, isVertical=True):
27        a = N.arrayrange(0, 1.0, 1.0/256)
28        if isVertical:
29            a = a[-1::-1]
30            shape = (1, 256)
31        else:
32            shape = (256, 1)
33
34        cbar = (cmap(a) * 255).astype('b').tostring()
35        img = Image.frombuffer("RGBA", shape, cbar, "raw", "RGBA", 0, 1)
36        img = img.resize((width, height))
37
38        return img
39
40    def renderGrid(self, grid, bbox, width, height, cmap):
41        cmap.set_bad('#ffffff', 0.0)
42
43        logger.debug('grid %s, %s, %s, %s, %s, %s %s, %s' %
44                    (grid.x0, grid.y0, grid.dx, grid.dy, grid.nx, grid.ny,
45                     grid.ix, grid.iy))
46        logger.debug('bbox %s' % (bbox,))
47
48        # Get a pixel = grid-box image for the grid
49        img = self._grid2Img(grid, cmap)
50
51        # Calculate the pixel-size of a grid box
52        pxPerDeg_x = float(width) / (bbox[2] - bbox[0])
53        pxPerDeg_y = float(height) / (bbox[3] - bbox[1])
54        pxPerGrid_x = grid.dx * pxPerDeg_x
55        pxPerGrid_y = grid.dy * pxPerDeg_y
56
57        # Scale Img to the right size
58        img = img.resize(((abs(pxPerGrid_x * grid.nx),
59                           abs(pxPerGrid_y * grid.ny))))
60
61        # We assume the grid points represent the centre of the grid boxes
62        # therefore we can calculate the upper-left position of the image
63
64        # Find top-left corner of grid
65        if grid.dx > 0:
66            leftX = grid.x0
67        else:
68            leftX = grid.x0 + (grid.dx * (grid.nx - 1))
69        if grid.dy > 0:
70            topY = grid.y0 + (grid.dy * (grid.ny - 1))
71        else:
72            topY = grid.y0
73
74        logger.debug('top-left = %s x, %s y' % (leftX, topY))
75
76        # Find pixel position of top left centre grid box
77        cx = width * ((leftX - bbox[0])/ (bbox[2] - bbox[0]))
78        cy = height * ((bbox[3] - topY) / (bbox[3] - bbox[1]))
79        logger.debug('top-left centre pixel = %s x, %s y' % (cx, cy))
80
81        # Apply half width of grid box
82        ox = int(cx - pxPerGrid_x / 2)
83        oy = int(cy - pxPerGrid_y / 2)
84        logger.debug('Offset: %s x, %s y' % (ox, oy))
85
86
87        # Paste the grid image into the tile image
88        tileImg = Image.new('RGBA', (width, height))
89        logger.debug('Pasting image img%s into tileImg%s at (%s,%s)' % (img.size, tileImg.size, ox, oy))
90        tileImg.paste(img, (ox, oy))
91
92        return tileImg
93
94
95    def _grid2Img(self, grid, cmap):
96        """Returns the grid as an image where each pixel is one grid box.
97
98        """
99        a = self._norm(grid.value)
100        img_buf = (cmap(a) * 255).astype('b')
101
102        # This code assumes the axis ordering is either (y, x, time) or (x, y, time)
103        if min(grid.iy, grid.ix) != 0 and max(grid.iy, grid.ix) != 1:
104            raise ValueError("X and Y must be the first 2 dimensions!")
105
106        if grid.iy < grid.ix:
107            yxOrdering = True
108        else:
109            yxOrdering = False
110   
111        img = Image.frombuffer("RGBA", img_buf.shape[1::-1], img_buf.tostring(), "raw", "RGBA", 0, 1)
112        img.save('raw1.png')
113
114        # Rotate if axis order is x, y
115        if not yxOrdering:
116            img = img.transpose(Image.ROTATE_90)
117
118        # Flip if x or y are ordered the wrong way
119        if grid.dy > 0:
120            logger.debug('Flipping y')
121            img = img.transpose(Image.FLIP_TOP_BOTTOM)
122        if grid.dx < 0:
123            logger.debug('Flipping x')
124            img = img.transpose(Image.FLIP_LEFT_RIGHT)
125
126
127        return img
128
129
130#---------------------------------------------------------------------------------
131
Note: See TracBrowser for help on using the repository browser.