Changeset 3497 for DPPP


Ignore:
Timestamp:
21/02/08 15:36:03 (11 years ago)
Author:
mkochan
Message:

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

Location:
DPPP/kml/csml2kml/python
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/WMSCapabilities.py

    r3479 r3497  
     1from pylab import dates     # a very good date/time module from matplotviz -- allows years < 1970 
     2 
    13wmsXmlNamespace = 'http://www.opengis.net/wms' 
    24 
     
    1618        return WMSLayer(name, title, abstract, childWmsLayers) 
    1719    else: 
    18         return BottomWMSLayer(name, title, abstract) 
     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) 
    1925 
    2026class WMSLayer: 
     
    2935        self.children = children 
    3036 
    31         def parseXML(self, layerElement): 
    32             raise NotImplementedError('Use the wmsLayerFactory() function instead.') 
     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 
    3354 
    3455    def _parseName(self): 
     
    4566        return (modelName, scenarioName, sampleTimespan) 
    4667 
    47     def __repr__(self): 
    48         return str(vars(self)) 
    49  
    50 class 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  
    6168    def getModelName(self): 
    6269        return self._parseName()[0] 
     
    6774    def getSampleTimespan(self): 
    6875        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  
    6980 
    7081class WMSCapabilities: 
     
    8192    def __repr__(self): 
    8293        if self.topWmsLayer: 
    83             return repr(self.topWmsLayer) 
     94            return '--- WMSCapabilities object with top layer as follows): ' + repr(self.topWmsLayer) + ' ---' 
    8495        else: 
    85             return '(WMSCapabilities object with no top layer)' 
     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) 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/kmlfeatures.py

    r3459 r3497  
    124124        It will be a KMLFolder object, containing KMLGroundOverlay objects -- each of them stands for  
    125125        a grid for a single time step. 
    126         [MORE EXPLANATION] 
    127         [RESOLVE VISIBILITY] 
    128126        ''' 
    129127        if self.description: 
     
    176174        return kmlFeatureFolder 
    177175 
    178     def exportFeature(self, viewConfig): 
    179         ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element ''' 
    180  
    181         featureElement=Element('Folder') 
    182         if self.description: 
    183             SubElement(featureElement, 'name').text=self.description  # come up with a feature folder name 
    184         else: 
    185             SubElement(featureElement, 'name').text=self.name 
    186         SubElement(featureElement, 'open').text='0' 
    187         SubElement(featureElement, 'visibility').text='0' 
    188  
    189         if self.splitTimeStepsBy == None: 
    190  
    191             for timeStep in self.timeSteps: 
    192                 goElement = self.buildGroundOverlayElement(timeStep) 
    193                 featureElement.append(goElement) 
    194  
    195         else: 
    196              
    197             def getCategory(timeStep): 
    198                 (year, month, day) = self.getYMD(timeStep) 
    199                 if self.splitTimeStepsBy == None: 
    200                     return timeStep 
    201                 elif self.splitTimeStepsBy == 'year': 
    202                     return year 
    203                 elif self.splitTimeStepsBy == 'month': 
    204                     return month 
    205  
    206             dict = {} 
    207  
    208             for timeStep in self.timeSteps: 
    209                 category = getCategory(timeStep) 
    210                 if not dict.has_key(category): 
    211                     dict[category] = [] 
    212                 dict[category].append(timeStep) 
    213             dictKeys = dict.keys() 
    214             dictKeys.sort() 
    215  
    216             for category in dictKeys:             
    217                 categoryElement=Element('Folder') 
    218  
    219                 if self.categoryNamingPattern == None: 
    220                     categoryName = 'Category ' + str(category) 
    221                 else: 
    222                     categoryName = self.categoryNamingPattern.replace('#VERBATIM#', str(category)) 
    223                     if (category >= 1 and category <= 12): 
    224                         categoryName = categoryName.replace('#MONTH#', getMonthCommonName(category)) 
    225                  
    226                 SubElement(categoryElement, 'name').text=categoryName 
    227                 SubElement(categoryElement, 'open').text='0' 
    228                 SubElement(categoryElement, 'visibility').text='0'         
    229                 for timeStep in dict[category]: 
    230                     goElement = self.buildGroundOverlayElement(timeStep) 
    231                     categoryElement.append(goElement) 
    232                 featureElement.append(categoryElement) 
    233  
    234         return featureElement 
    235  
    236176    def buildKmlGroundOverlay(self, timeStep): 
    237         ''' [VISIBILITY?]''' 
    238177 
    239178        (startDateTime, endDateTime) = self.getLogicalTimeSpan(timeStep) 
     
    248187            ) 
    249188 
    250     def buildGroundOverlayElement(self, timeStep): 
    251  
    252         goElement=Element('GroundOverlay')       
    253         SubElement(goElement, 'name').text=self.name + ' ' + timeStep 
    254         SubElement(goElement, 'open').text='0' 
    255         SubElement(goElement, 'visibility').text='0' 
    256  
    257         timespanElement=SubElement(goElement, 'TimeSpan') 
    258         (startDateTime, endDateTime) = self.getLogicalTimeSpan(timeStep) 
    259         SubElement(timespanElement, 'begin').text = ('%04d-%02d-%02d') % (startDateTime.utctimetuple()[0:3]) 
    260         SubElement(timespanElement, 'end').text = ('%04d-%02d-%02d') % (endDateTime.utctimetuple()[0:3]) 
    261  
    262         # Include the WMS service call address 
    263         iconElement=SubElement(goElement,'icon') 
    264         SubElement(iconElement, 'href').text=self.buildWMSRequest(timeStep) 
    265         SubElement(iconElement, 'refreshMode').text='onExpire' 
    266  
    267         latlonboxElement=SubElement(goElement, 'LatLonBox') 
    268         SubElement(latlonboxElement, 'north').text=str(self.bBox.north) 
    269         SubElement(latlonboxElement, 'south').text=str(self.bBox.south) 
    270         SubElement(latlonboxElement, 'east' ).text=str(self.bBox.east) 
    271         SubElement(latlonboxElement, 'west' ).text=str(self.bBox.west) 
    272  
    273         return goElement 
    274  
    275189# ------------------------- Logical date/time transforms (defined as plain functions) -------------------------- 
    276190 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testWMSCapabilities.py

    r3479 r3497  
    88wmsCapabilities = WMSCapabilities() 
    99wmsCapabilitiesElement = etree.getroot() 
    10 print wmsCapabilitiesElement 
     10 
    1111wmsCapabilities.parseXML(wmsCapabilitiesElement) 
    1212print wmsCapabilities 
     13print 
     14 
     15#wmsLayersConvertor = WMSLayersConvertor(wmsCapabilities.topWmsLayer, None, '../../../../output/wms-ar4') 
     16#wmsLayersConvertor.convert() 
  • DPPP/kml/csml2kml/python/notes.txt

    r3479 r3497  
    66 
    77---------------------------------------------------------------------------------------------------------------------------- 
    8 || GridSeriesConvertor ___/ 
     8|| WMSLayersConvertor ___/ 
    99 
    10 [O] Create a version of GridSeriesConvertor that takes in the Layers XML and a config file and spits out 
    11     either a single KML file or a directory structure with KML files as leaves. 
     10[O] Create an active class that takes in the Layers XML and a config file and spits out either a single KML file 
     11    or a directory structure with KML files as leaves. 
     12[o] Consider whether it is appropriate for WMSLayersConvertor to be in the "csml2kml" package, given that it 
     13     does *not* involve CSML along the way (possibly CSML could later get involved, so better to keep it as an example). 
    1214 
    1315---------------------------------------------------------------------------------------------------------------------------- 
Note: See TracChangeset for help on using the changeset viewer.