source: DPPP/ukcip02_server/trunk/ukcip_server/ukcip_server/model/pywms/impl.py @ 3565

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

Uses new ows_common interface. Works with Cadcorp (at least partially).

Line 
1"""
2Implements the WMS interfaces for simple CDMS variables
3
4This implementation is for backward compatibility between the old interface and
5the new.  Only one CRS per layer is supported.
6
7"""
8from ows_common.service.wms_iface import ILayer, ILayerSlab, IDimension
9from wms_cdms import SimpleCdmsLayer, CdmsTimeDimension, CdmsGrid
10import cdtime
11from render_imp import RGBARenderer
12from matplotlib.cm import get_cmap
13
14class CdmsLayer(ILayer):
15    """
16    The ILayer interface has superceded model.Layer and therefore CdmsLayer
17    does not inherit from model.Layer or SimpleCdmsLayer.  Instead encapsulation
18    is used.
19
20    """
21    def __init__(self, cdmsVar, GridClass=CdmsGrid):
22        try:
23            self.title = cdmsVar.long_name
24        except AttributeError:
25            self.title = cdmsVar.id
26           
27        self._layer = SimpleCdmsLayer(cdmsVar, title, GridClass=GridClass)
28        self.abstract = None
29        self.crss = [GridClass.crs]
30        self.units = self._layer.units
31
32        dims = {}
33        for dimName, dim in self._layer.dimensions.items():
34            dims[dimName] = Dimension(dim)
35        self.dimensions = dims
36
37       
38
39
40    def getBBox(self, crs):
41        # Get BBOX from pywms layer
42        var = self._layer.var
43        xAxis = var.getAxis(var.getAxisIndex('eastings'))
44        yAxis = var.getAxis(var.getAxisIndex('northings'))
45        #!NOTE order specific code
46        return (xAxis[0], yAxis[-1], xAxis[-1], yAxis[0])
47
48    def getSlab(self, crs, dimValues=None, renderOpts={}):
49        if crs != self.crss[0]:
50            raise ValueError("CRS %s not supported on this layer" % crs)
51       
52        return CdmsLayerSlab(crs, dimValues, renderOpts, self)
53
54    def getCacheKey(self, crs, dimValues=None, renderOpts={}):
55        return '%s:%s:%s' % (crs, dimValues, renderOpts)
56
57class CdmsLayerSlab(ILayerSlab):
58    def __init__(self, crs, dimValues, renderOpts, layer):
59        self.crs = crs
60        self.dimValues = dimValues
61        self.renderOpts = renderOpts
62        self.layer = layer
63        self.bbox = layer.getBBox(crs)
64
65        self._renderer = RGBARenderer(renderOpts['minValue'], renderOpts['maxValue'])
66
67    def getImage(self, bbox, width, height):
68        grid = self.layer._layer.selectGrid(bbox, self.dimValues)
69        cmap = get_cmap(self.renderOpts.get('cmap', 'jet'))
70
71        return self._renderer.renderGrid(grid, bbox, width, height, cmap)
72
73    def getCacheKey(self):
74        raise NotImplementedError
75   
76
77class Dimension(IDimension):
78    """
79    Wrapper to fix some interface differences.
80
81    """
82    def __init__(self, pywmsDim):
83        self.units = pywmsDim.units
84        self.extent = pywmsDim.extent.split(',')
Note: See TracBrowser for help on using the repository browser.