source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/WMSCapabilities.py @ 3479

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/WMSCapabilities.py@3479
Revision 3479, 2.5 KB checked in by mkochan, 12 years ago (diff)

Added module WMSCapabilities.py with a wrapper class for the <wms:WMS_Capabilities> tag (returned by the WMS getCapabilities() request), and a wrapper class for the <wms:Layer> tag. (Test successful, but undocumented).

Line 
1wmsXmlNamespace = 'http://www.opengis.net/wms'
2
3def wmsLayerFactory(layerElement):
4    '''
5    [DOC]
6    '''
7    name = layerElement.find('{%s}Name' % wmsXmlNamespace).text
8    title = layerElement.find('{%s}Title' % wmsXmlNamespace).text
9    abstract = layerElement.find('{%s}Abstract' % wmsXmlNamespace).text
10    childElements = layerElement.findall('{%s}Layer' % wmsXmlNamespace)
11    childWmsLayers = []
12    for childElement in childElements:
13        childWmsLayer = wmsLayerFactory(childElement)
14        childWmsLayers.append(childWmsLayer)
15    if childElements != []:
16        return WMSLayer(name, title, abstract, childWmsLayers)
17    else:
18        return BottomWMSLayer(name, title, abstract)
19
20class WMSLayer:
21    '''
22    [DOC]
23    '''
24
25    def __init__(self, name, title, abstract, children):
26        self.name = name
27        self.title = title
28        self.abstract = abstract
29        self.children = children
30
31        def parseXML(self, layerElement):
32            raise NotImplementedError('Use the wmsLayerFactory() function instead.')
33
34    def _parseName(self):
35        (modelName, scenarioName, rest) = self.name.split(':')
36        timespanDesignator = rest.split('/')[0]
37
38        if timespanDesignator == 'clim_20' or timespanDesignator == 'change_20':
39            sampleTimespan = 20
40        elif timespanDesignator == 'clim_30' or timespanDesignator == 'change_30':
41            sampleTimespan = 30
42        else:
43            raise ValueError('Sample timespan designation is incorrect')
44
45        return (modelName, scenarioName, sampleTimespan)
46
47    def __repr__(self):
48        return str(vars(self))
49
50class BottomWMSLayer(WMSLayer):
51
52    '''[DOC]'''
53   
54    def __init__(self, name, title, abstract):
55
56        self.name = name
57        self.title = title
58        self.abstract = abstract
59        # but no self.children
60
61    def getModelName(self):
62        return self._parseName()[0]
63
64    def getScenarioName(self):
65        return self._parseName()[1]
66
67    def getSampleTimespan(self):
68        return self._parseName()[2]
69
70class WMSCapabilities:
71
72    '''[DOC]'''
73
74    def __init__(self):
75        self.topWmsLayer = None
76
77    def parseXML(self, wmsCapabilitiesElement):
78        topLayerElement = wmsCapabilitiesElement.find('{%s}Capability/{%s}Layer' % (wmsXmlNamespace, wmsXmlNamespace))
79        self.topWmsLayer = wmsLayerFactory(topLayerElement)
80
81    def __repr__(self):
82        if self.topWmsLayer:
83            return repr(self.topWmsLayer)
84        else:
85            return '(WMSCapabilities object with no top layer)'
Note: See TracBrowser for help on using the repository browser.