source: qesdi/geoplot/trunk/lib/geoplot/layer_drawer_coastline.py @ 5735

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/layer_drawer_coastline.py@5735
Revision 5735, 3.1 KB checked in by pnorton, 10 years ago (diff)

Added the ability to change the coastline colour in the coastline layer drawer. Also fixed a minor problem with the colour bar that was causing problems when unicode is used rather than strings.

Line 
1"""
2A layer drawer that instead of drawing the data to an image just draws the
3coastline (and optionally the rivers) at a specified resolution.
4"""
5
6import logging
7
8import matplotlib.colors
9
10from geoplot.layer_drawer import LayerDrawerBase
11
12log = logging.getLogger(__name__)
13
14class LayerDrawerCoastlines(LayerDrawerBase):
15    """
16    Creates a layer image of the coastline for a given projection and resolution.
17    """
18   
19    def __init__(self, projection='latlon', resolution='i', transparent=False, drawRivers=False):
20       
21        LayerDrawerBase.__init__(self, transparent=transparent,
22                                 projection=projection,
23                                 resolution=resolution)
24       
25        self._coastlineColour = 'black'
26       
27        # must be done after the base constructor or the _mapFactory won't be
28        # created
29        self._mapFactory.drawRivers = drawRivers
30
31    def _drawToAxes(self, axes, xLimits, yLimits):
32        """
33        Draws the coastline form the map onto the axes.
34        """
35       
36        map = self._getMap(xLimits, yLimits)
37       
38        #draw the map onto the axes
39        map.basemap.drawcoastlines(ax = axes, 
40                                   linewidth=0.5,
41                                   color=self.coastlineColour,
42                                   antialiased=0,                                       
43                                   xLimits=xLimits, yLimits=yLimits)
44
45        if map.drawRivers:
46            map.basemap.drawrivers(ax = axes, color='b', linewidth=0.3,
47                                    antialiased=0,                                   
48                                    xLimits=xLimits, yLimits=yLimits)
49                   
50       
51        #map.basemap.fillcontinents(color='coral',lake_color=None, ax=axes)
52       #map.basemap.drawparallels([10,20,30,40],ax=axes)
53        #map.basemap.bluemarble(ax=axes)
54
55    def _getMatplotlibColour(self, value):
56        """
57        Makes sure that value is a valid matplotlib colour, will convert form a float
58        to a string if neccesary. Will raise a ValueError if the value is not a valid colour.
59        """
60        if type(value) == float:
61            val = str(value)
62        else:
63            val = value
64           
65        try:
66            matplotlib.colors.colorConverter.to_rgb(val)
67        except:
68            raise
69        else:
70            return val
71
72    def __set_drawRivers(self, value):
73        self._mapFactory.drawRivers = value
74       
75    def __get_drawRivers(self):
76        return self._mapFactory.drawRivers
77   
78    drawRivers = property(__get_drawRivers, __set_drawRivers)   
79   
80   
81    def __set_coastlineColour(self, value):
82       
83        try:
84            newCoastColour = self._getMatplotlibColour(value)
85        except ValueError, e:
86            log.warning("Error occurred getting matplotlib colour form %s, Exception:%s" % (value, e))
87        else:
88            self._coastlineColour = newCoastColour
89               
90    def __get_coastlineColour(self):
91        return self._coastlineColour
92   
93    coastlineColour = property(__get_coastlineColour, __set_coastlineColour)
94   
95       
Note: See TracBrowser for help on using the repository browser.