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

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

Added support for storing datetimes in the WMSCapabilities.BottomWMSLayer class. Added a convertor class WMSCapabilities.WmsLayersConvertor? for conversion into KML.

Line 
1from pylab import dates     # a very good date/time module from matplotviz -- allows years < 1970
2
3wmsXmlNamespace = 'http://www.opengis.net/wms'
4
5def wmsLayerFactory(layerElement):
6    '''
7    [DOC]
8    '''
9    name = layerElement.find('{%s}Name' % wmsXmlNamespace).text
10    title = layerElement.find('{%s}Title' % wmsXmlNamespace).text
11    abstract = layerElement.find('{%s}Abstract' % wmsXmlNamespace).text
12    childElements = layerElement.findall('{%s}Layer' % wmsXmlNamespace)
13    childWmsLayers = []
14    for childElement in childElements:
15        childWmsLayer = wmsLayerFactory(childElement)
16        childWmsLayers.append(childWmsLayer)
17    if childElements != []:
18        return WMSLayer(name, title, abstract, childWmsLayers)
19    else:
20        dimensionElements = layerElement.findall('{%s}Dimension' % wmsXmlNamespace)
21        for dimensionElement in dimensionElements:
22            if dimensionElement.get('name') == 'time':
23                times = map( dates.dateutil.parser.parse, dimensionElement.text.split(',') )
24        return BottomWMSLayer(name, title, abstract, times)
25
26class WMSLayer:
27    '''
28    [DOC]
29    '''
30
31    def __init__(self, name, title, abstract, children):
32        self.name = name
33        self.title = title
34        self.abstract = abstract
35        self.children = children
36
37    def parseXML(self, layerElement):
38        raise NotImplementedError('Use the wmsLayerFactory() function instead.')
39
40    def __repr__(self):
41        return str(vars(self))
42
43class BottomWMSLayer(WMSLayer):
44
45    '''[DOC]'''
46   
47    def __init__(self, name, title, abstract, times):
48
49        self.name = name
50        self.title = title
51        self.abstract = abstract
52        # but no self.children
53        self.times = times
54
55    def _parseName(self):
56        (modelName, scenarioName, rest) = self.name.split(':')
57        timespanDesignator = rest.split('/')[0]
58
59        if timespanDesignator == 'clim_20' or timespanDesignator == 'change_20':
60            sampleTimespan = 20
61        elif timespanDesignator == 'clim_30' or timespanDesignator == 'change_30':
62            sampleTimespan = 30
63        else:
64            raise ValueError('Sample timespan designation is incorrect')
65
66        return (modelName, scenarioName, sampleTimespan)
67
68    def getModelName(self):
69        return self._parseName()[0]
70
71    def getScenarioName(self):
72        return self._parseName()[1]
73
74    def getSampleTimespan(self):
75        return self._parseName()[2]
76
77    def toKML(self):
78        # Return a KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> elements,
79        # each standing for a different
80
81class WMSCapabilities:
82
83    '''[DOC]'''
84
85    def __init__(self):
86        self.topWmsLayer = None
87
88    def parseXML(self, wmsCapabilitiesElement):
89        topLayerElement = wmsCapabilitiesElement.find('{%s}Capability/{%s}Layer' % (wmsXmlNamespace, wmsXmlNamespace))
90        self.topWmsLayer = wmsLayerFactory(topLayerElement)
91
92    def __repr__(self):
93        if self.topWmsLayer:
94            return '--- WMSCapabilities object with top layer as follows): ' + repr(self.topWmsLayer) + ' ---'
95        else:
96            return '--- WMSCapabilities object with no top layer ---'
97
98class WMSLayersConvertor:
99   
100    def __init__(self, topWmsLayer, config, baseKmlOutputDirectory):
101        self.topWmsLayer = topWmsLayer
102        self.config = config
103        self.baseKmlOutputDirectory = baseKmlOutputDirectory
104        self.maxDirDepth = 10
105        # [a:D,r:DEBUG] self.maxDirDepth = self.config(...)
106       
107    def convert(self):
108
109        def _convertToKML(wmsLayer):
110            return kmlElement
111       
112        def _convertToDirectory(wmsLayer, parentDir, currentLevel):
113           
114            if currentLevel < self.maxDirDepth:
115                currentDir = parentDir + '/' + wmsLayer.name
116                ###os.mkdir(currentDir)
117                print 'Created directory "%s"' % currentDir
118                if not isinstance(wmsLayer, BottomWMSLayer):
119                    for child in wmsLayer.children:
120                        _convertToDirectory(child, currentDir, currentLevel+1)
121            elif currentLevel == self.maxDirDepth:
122                # Create a KML document with no styles
123                ###kmlDocument = KMLDocument(wmsLayer.name, [])   # [?] dir name??
124                ###kmlDocument.elements.append(_convertToKML(wmsLayer))
125                filename = parentDir + '/' + wmsLayer.name + '.kml'
126                ###kmlDocument.save(filename)
127                print 'Saved file "%s"' % filename
128            else:
129                pass
130
131        #### topWmsLayer = wmsLayerFactory(topLayerElement)
132        _convertToDirectory(self.topWmsLayer, self.baseKmlOutputDirectory, 0)
Note: See TracBrowser for help on using the repository browser.