Changeset 3531


Ignore:
Timestamp:
27/02/08 13:49:59 (11 years ago)
Author:
mkochan
Message:

Changed convertor so that KML files now contain full KML folder hierarchy.

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

Legend:

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

    r3520 r3531  
    347347    def convert(self): 
    348348 
    349         def _convertToKML(wmsLayer): 
     349        def _convertToKML(wmsLayer, parentLayersStack): 
     350            ''' 
     351            @param parentLayersStack: an immutable list (acting as a stack) with the WMSLayer objects that have 
     352            been passed through. 
     353            @return: a KMLFolder object containing a folder structure (including the backtrack from the parentLayersStack) 
     354            with the KML representation of wmsLayer at the bottom. 
     355            ''' 
    350356            viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByPeriod] 
    351             return wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
    352  
    353         def _convertToFile(wmsLayer, parentDir): 
    354             kmlDocument = KMLDocument(wmsLayer.title, [])            # Create a KML document with no styles 
    355             kmlDocument.elements = _convertToKML(wmsLayer).children 
     357            layerKmlFolder = wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
     358 
     359            # Backtrack through parentLayersStack in order to include the upper folders in the KML structure as well 
     360            # (that way it is much easier to see in Google Earth where we are): 
     361            if len(parentLayersStack) == 0: 
     362                return layerKmlFolder 
     363            else: 
     364                childKmlFolder = layerKmlFolder 
     365                while len(parentLayersStack) > 0: 
     366                    stackKmlLayer = parentLayersStack.pop() 
     367                    kmlFolder = KMLFolder(stackKmlLayer.title, [], visible = False, opened = False) 
     368                    kmlFolder.children.append(childKmlFolder) 
     369                    childKmlFolder = kmlFolder 
     370                return kmlFolder 
     371 
     372        def _convertToFile(wmsLayer, parentLayersStack, parentDir): 
     373 
     374            # Convert the current layer into a KML document 
     375            topKmlFolder = _convertToKML(wmsLayer, parentLayersStack) 
     376 
     377            # Create a KML document with no styles 
     378            kmlDocument = KMLDocument(topKmlFolder.name, [])             
     379 
     380            # Replace the uppermost KMLFolder object with a KMLDocument object 
     381            kmlDocument.elements = topKmlFolder.children 
     382 
     383            # Save the KML document 
    356384            filename = parentDir + '/' + wmsLayer.title + '.kml' 
    357385            kmlDocument.save(filename) 
    358386            print 'Saved file "%s"' % filename 
    359387 
    360         def _convertToDirectory(wmsLayer, parentDir, currentLevel): 
     388        def _convertToDirectory(wmsLayer, parentLayersStack, parentDir, currentLevel): 
    361389            '''recursive''' 
    362390            if currentLevel < self.maxDirDepth and not isinstance(wmsLayer, BottomWMSLayer): 
     
    365393                print 'Created directory "%s"' % currentDir 
    366394                for childWmsLayer in wmsLayer.children: 
    367                     _convertToDirectory(childWmsLayer, currentDir, currentLevel+1) 
     395                    _convertToDirectory(childWmsLayer, parentLayersStack + [wmsLayer], currentDir, currentLevel+1) 
    368396            elif currentLevel == self.maxDirDepth or isinstance(wmsLayer, BottomWMSLayer): 
    369                 _convertToFile(wmsLayer, parentDir) 
     397                _convertToFile(wmsLayer, parentLayersStack, parentDir) 
    370398            else: 
    371399                pass 
    372400 
    373         _convertToDirectory(self.topWmsLayer, self.baseKmlOutputDirectory, 0) 
     401        _convertToDirectory(self.topWmsLayer, [], self.baseKmlOutputDirectory, 0) 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testWMSCapabilities.py

    r3516 r3531  
    2222 
    2323wmsLayersConvertor = WMSLayersConvertor( 
    24     wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-obs', 0 
     24    wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-obs2', 100 
    2525    ) 
    2626wmsLayersConvertor.convert() 
Note: See TracChangeset for help on using the changeset viewer.