source: qesdi/geoplot/trunk/lib/geoplot/layer_drawer_grid.py @ 5612

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/layer_drawer_grid.py@5612
Revision 5612, 3.4 KB checked in by pnorton, 11 years ago (diff)

Have added some code that allows the grid drawer to decide if it should use imshow or pcolormesh to render the grid. This was done to get arround the problem of the grid cells having outlines when pcolormesh is used.

Also implemented a map function that chooses which basemap resolution to use if one isn't set.

Line 
1"""
2An object to draw just the layer (map + grid) from the plot,
3can draw to a file, a string or create an Image object.
4"""
5
6
7import logging
8import Image
9import StringIO
10import thread
11
12import numpy as N
13
14from matplotlib.patches import Rectangle
15from matplotlib.figure import Figure
16from matplotlib.backends.backend_agg import FigureCanvasAgg
17from matplotlib.lines import Line2D
18
19from geoplot.grid_builder_lat_lon import GridBuilderLatLon
20from geoplot.grid_builder_national import GridBuilderNational
21from geoplot.grid_builder_rotated import GridBuilderRotated
22
23from geoplot.map_lat_lon import MapLatLon
24
25from geoplot.grid_drawer import GridDrawer
26import geoplot.utils as geoplot_utils
27
28from geoplot.layer_drawer import LayerDrawerBase
29
30from matplotlib.colors import Normalize
31
32log = logging.getLogger(__name__)
33
34VALID_GRID_TYPES = ['latlon', 'national', 'rotated']
35
36class LayerDrawerGrid(LayerDrawerBase):
37    "Draw only the layer section of the plot"
38   
39    def __init__(self, cdmsVar, gridType='latlon', showGridLines=False, cmap=None, cmapRange=(None,None), outline=False,
40                 transparent=False):
41       
42        self._cmap = None
43        self._gridDrawer = GridDrawer()
44       
45        log.debug("cmapRange = %s" % (cmapRange,))
46       
47        if cmapRange[0] > cmapRange[1]:
48            log.warning("cmapRange[0] > cmapRange[1], swapping values")
49            cmapRange = (cmapRange[1], cmapRange[0])       
50       
51        self.cdmsVar = cdmsVar
52        self.cmap = cmap
53        self.cmapRange = cmapRange
54        self.showGridLines = showGridLines
55        self.outline = outline
56       
57        LayerDrawerBase.__init__(self, gridType, transparent)
58       
59    def _drawToAxes(self, axes, xLimits, yLimits):
60        """
61        Creates a new grid object and draws it onto the axis.
62        """
63        #create a new grid, this grid will be in terms of lat/lon
64        grid = self._buildGrid(self.cdmsVar, xLimits, yLimits)
65       
66        # if the projection is latlon then there is no need to transform
67        # the data on the grid.
68        if self.projection == 'latlon':
69            basemap = None
70        else:
71            map = self._getMap(xLimits, yLimits)
72            basemap = map.basemap       
73       
74        norm = Normalize(self.cmapRange[0], self.cmapRange[1])
75       
76        #draw the grid on the axis
77        self._gridDrawer.draw(axes, grid,
78                              basemap=basemap, 
79                              limits=(xLimits, yLimits),
80                              norm=norm,
81                              cmap=self.cmap)
82                             
83   
84    ### properties ###
85                       
86    def __set_showGridLines(self, value):
87        self._gridDrawer.showGridLines = value
88   
89    def __get_showGridLines(self):
90        return self._gridDrawer.showGridLines
91   
92    showGridLines = property(__get_showGridLines, __set_showGridLines) 
93   
94    def __set_cmap(self, value):
95        self._cmap = value
96       
97    def __get_cmap(self):
98        return self._cmap
99   
100    cmap = property(__get_cmap, __set_cmap)
101   
102    def __set_cmapRange(self, value):
103        self._cmapRange = value
104       
105    def __get_cmapRange(self):
106        return self._cmapRange
107   
108    cmapRange = property(__get_cmapRange, __set_cmapRange)
109   
110    def __set_outline(self, value):
111        self._gridDrawer.outline = value
112       
113    def __get_outline(self):
114        return self._gridDrawer.outline
115   
116    outline = property(__get_outline, __set_outline)
117 
Note: See TracBrowser for help on using the repository browser.