Ignore:
Timestamp:
29/02/08 16:08:03 (12 years ago)
Author:
mkochan
Message:

Wrote code for generating a hierarchy of KML files (with kml:NetworkLinks) and directories.

File:
1 edited

Legend:

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

    r3542 r3549  
    33from pylab import dates     # a very good date/time module from matplotviz -- allows years < 1970 
    44from KML import * 
    5  
    6 # [TODO] 
    7 #        * Resolve maxDirDepth 
    85 
    96wmsXmlNamespace = 'http://www.opengis.net/wms' 
     
    4744        raise NotImplementedError('Use the wmsLayerFactory() function instead.') 
    4845 
    49     def toKML(self, wmsRequestConfigElement, viewTypes): 
    50         # ignore wmsRequestConfigElement, viewTypess 
    51         kmlLayerFolder = KMLFolder(self.title, [], opened = False, visible = False) 
     46    def toKML(self, wmsRequestConfigElement, viewTypes, parentDir, parentDirUrl): 
     47 
     48        title_ = self.title.replace(' ', '_')  # self.title underscored 
     49        dir = parentDir + '/' + title_ 
     50        dirUrl = parentDirUrl + '/' + title_ 
     51        filename = dir + '.kml' 
     52        fileUrl = dirUrl + '.kml' 
     53 
     54        # create directory currentDir 
     55        os.mkdir(dir) 
     56 
     57        kmlDocument = KMLDocument(self.title, []) 
    5258        for childWmsLayer in self.children: 
    53             kmlLayerFolder.children.append( childWmsLayer.toKML(wmsRequestConfigElement, viewTypes) ) 
    54         return kmlLayerFolder 
     59            kmlDocument.elements.append(  
     60                childWmsLayer.toKML(wmsRequestConfigElement, viewTypes, dir, dirUrl)  
     61                ) 
     62        kmlDocument.save(filename) 
     63     
     64        return KMLNetworkLink(self.title, fileUrl) 
    5565 
    5666class BottomWMSLayer(WMSLayer): 
     
    98108        return self._parseName()[4] 
    99109 
    100     def toKML(self, wmsRequestConfigElement, viewTypes): 
     110    def toKML(self, wmsRequestConfigElement, viewTypes, parentDir, parentDirUrl): 
    101111        ''' 
    102112        @param viewTyps: A list of View classes (but not instances), which define what kinds of view we are going  
    103113                         to use to look at the data. 
     114                         ... 
    104115        @return:         A KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> 
    105116                         elements, each standing for a different time segment. 
     
    343354class WMSLayersConvertor: 
    344355     
    345     def __init__(self, topWmsLayer, wmsRequestConfigElement, baseKmlOutputDirectory, maxDirDepth): 
     356    def __init__(self, topWmsLayer, wmsRequestConfigElement, baseKmlOutputDirectory, baseKmlOutputUrl): 
    346357        self.topWmsLayer = topWmsLayer 
    347358        self.wmsRequestConfigElement = wmsRequestConfigElement 
    348359        self.baseKmlOutputDirectory = baseKmlOutputDirectory 
    349         self.maxDirDepth = maxDirDepth 
     360        self.baseKmlOutputUrl = baseKmlOutputUrl 
    350361         
    351362    def convert(self): 
    352  
    353         def _convertToKML(wmsLayer, parentLayersStack): 
    354             ''' 
    355             @param parentLayersStack: an immutable list (acting as a stack) with the WMSLayer objects that have 
    356             been passed through. 
    357             @return: a KMLFolder object containing a folder structure (including the backtrack from the parentLayersStack) 
    358             with the KML representation of wmsLayer at the bottom. 
    359             ''' 
    360             viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByPeriod] 
    361             layerKmlFolder = wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
    362  
    363             # Backtrack through parentLayersStack in order to include the upper folders in the KML structure as well 
    364             # (that way it is much easier to see in Google Earth where we are): 
    365             if len(parentLayersStack) == 0: 
    366                 return layerKmlFolder 
    367             else: 
    368                 childKmlFolder = layerKmlFolder 
    369                 while len(parentLayersStack) > 0: 
    370                     stackKmlLayer = parentLayersStack.pop() 
    371                     kmlFolder = KMLFolder(stackKmlLayer.title, [], visible = False, opened = False) 
    372                     kmlFolder.children.append(childKmlFolder) 
    373                     childKmlFolder = kmlFolder 
    374                 return kmlFolder 
    375  
    376         def _convertToFile(wmsLayer, parentLayersStack, parentDir): 
    377  
    378             # Convert the current layer into a KML document 
    379             topKmlFolder = _convertToKML(wmsLayer, parentLayersStack) 
    380  
    381             # Create a KML document with no styles 
    382             kmlDocument = KMLDocument(topKmlFolder.name, [])             
    383  
    384             # Replace the uppermost KMLFolder object with a KMLDocument object 
    385             kmlDocument.elements = topKmlFolder.children 
    386  
    387             # Save the KML document 
    388             dockmlFilename = parentDir + '/doc.kml' 
    389             kmzFilename = parentDir + '/' + wmsLayer.title.replace(' ', '_') + '.kmz' 
    390             kmlDocument.save(dockmlFilename) 
    391             quietZipCommand = 'zip -q ' + ' "' + kmzFilename + '" "' + dockmlFilename + '"' 
    392             os.system(quietZipCommand) 
    393             os.remove(dockmlFilename) 
    394             print 'Saved file "%s"' % kmzFilename 
    395  
    396         def _convertToDirectory(wmsLayer, parentLayersStack, parentDir, currentLevel): 
    397             '''recursive''' 
    398             if currentLevel < self.maxDirDepth and not isinstance(wmsLayer, BottomWMSLayer): 
    399                 currentDir = parentDir + '/' + wmsLayer.title.replace(' ', '_') 
    400                 os.mkdir(currentDir) 
    401                 print 'Created directory "%s"' % currentDir 
    402                 for childWmsLayer in wmsLayer.children: 
    403                     _convertToDirectory(childWmsLayer, parentLayersStack + [wmsLayer], currentDir, currentLevel+1) 
    404             elif currentLevel == self.maxDirDepth or isinstance(wmsLayer, BottomWMSLayer): 
    405                 _convertToFile(wmsLayer, parentLayersStack, parentDir) 
    406             else: 
    407                 pass 
    408  
    409         _convertToDirectory(self.topWmsLayer, [], self.baseKmlOutputDirectory, 0) 
     363        viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByPeriod] 
     364        self.topWmsLayer.toKML(self.wmsRequestConfigElement, viewTypes, self.baseKmlOutputDirectory, self.baseKmlOutputUrl) 
Note: See TracChangeset for help on using the changeset viewer.