source: TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/wms_gdal.py @ 3597

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/wms_gdal.py@3597
Revision 3597, 3.2 KB checked in by spascoe, 13 years ago (diff)

Started a general GDAL WMS adapter.

Line 
1"""
2An implementation of ows_common.service.wms_iface that uses GDAL to support
3warping between multiple coordinate reference systems.  This implementation
4relies on a further interface, IGDALDataSource, to provide the data.
5
6"""
7
8from ows_common.service.wms_iface import *
9import logging
10
11log = logging.getLogger(__name__)
12
13class IGDALDataSource(object):
14    """
15    This interface is very similar to ILayer except that it returns GDAL
16    datasets rather than PIL images.  It also doesn't try to handle multiple
17    CRSs as this is handled by GDALLayer. 
18
19    @ivar title: The layer title.  As seen in the Capabilities document.
20    @ivar abstract:  Abstract as seen in the Capabilities document.
21    @ivar dimensions: A mapping of dimension names to IDimension objects.
22    @ivar units: A string describing the units.
23    @ivar crs: The CRS that GDAL datasets will be returned in by
24        self.getDataset()
25
26    @todo: Legend plotting needs support but should probably be done in a
27        seperate interface.
28
29    """
30    dimensions = NotImplemented
31
32    def getWKT(self):
33        """
34        Because mapping between CRS codes and WKT format can be flakey in GDAL
35        this function allows the problem to be solved on a case-by-case basis.
36       
37        @return: the description of self.crs in GDAL well known text format.
38
39        """
40       
41    def getBBox(self):
42        """
43        @return: the bounding box (llx, lly, urx, ury) in self.crs.
44
45        """
46
47    def getDataset(self, dimValues=None, renderOpts={}):
48        """
49        Create the equivilent of ILayerSlab as a GDAL dataset.  The dataset
50        could have 1,3 or 4 bands representing PIL modes 'L', 'RGB' or 'RGBA'.
51       
52        @param dimValues: A mapping of dimension names to dimension values
53            as specified in the IDimension.extent.
54        @param renderOpts: A generic mapping object for passing rendering
55            options.
56        @return: A GDAL Dataset object for this horizontal slice.
57           
58        """
59
60
61class GDALLayer(ILayer):
62    """
63    This implementation of ILayer can warp images from a source CRS to
64    various other CRSs.
65
66    @ivar warpCRSs: A mapping of CRS itentifiers to WKT descriptions of
67       CRSs that are supported for this ILayer via warping.
68
69    """
70
71    def __init__(self, dataSource):
72        """
73        @param dataSource: A IGDALDataSource implementation.
74
75        """
76        self._ds = dataSource
77        self.warpCRSs = {}
78
79        self.title = dataSource.title
80        self.abstract = dataSource.abstract
81        self.dimensions = dataSource.dimensions
82        self.units = dataSource.units
83        #!NOTE: self.crss is implemented as property
84
85    def _getCRSs(self):
86        return [self._ds.crs] + self._warpCRSs.keys()
87    crss = property(_getCRSs)
88
89    def getBBox(self, crs):
90        src_bb = self._ds.getBBox()
91        if self.crs == self._ds.crs:
92            return src_bb
93
94        sr_src = osr.SpatialReference(self._ds.getWKT())
95        sr_dst = osr.SpatialReference(self.warpCRSs[crs])
96
97        ct = osr.CoordinateTransformation(sr_srs, sr_dst)
98
99        llx, lly = ct.TransformPoint(*src_bb[:2])[:2]
100        urx, ury = ct.TransformPoint(*src_bb[2:])[:2]
101        return (llx, lly, urx, ury)
Note: See TracBrowser for help on using the repository browser.