Changeset 3555


Ignore:
Timestamp:
05/03/08 16:26:22 (11 years ago)
Author:
mkochan
Message:

Corrected bug in saving KML files, working on saving KMZ files.

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

Legend:

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

    r3549 r3555  
    22import sys 
    33from cElementTree import ElementTree, Element, SubElement, XML 
     4from zipfile import ZipFile 
     5from StringIO import StringIO 
    46 
    57class KMLElement: 
     
    6769                    elem.tail = i 
    6870 
    69         def _save(filename): 
    70             '''Builds and saves the document in the KML format into the file filename.''' 
     71        def _save(file): 
     72            ''' 
     73            Builds and saves the document in the KML format into a file. 
     74            @param file: An open file-like object to write to. 
     75            ''' 
    7176 
    7277            # Build the Document element 
     
    8085            # Write the KML document to a file 
    8186            elementTree = ElementTree(rootElement)        
     87            elementTree.write(file) 
     88 
     89        def _saveKml(filename): 
     90            ''' 
     91            Builds and saves the document in the KML format into a file named filename. 
     92            @param filename: Name of the KML output file. 
     93            ''' 
    8294            kmlFile = open(filename, 'w') 
    83             elementTree.write(kmlFile) 
     95            _save(kmlFile) 
    8496            kmlFile.close() 
     97 
     98        def _saveKmz(filename): 
     99            ''' 
     100            Builds and saves the document into a KMZ file, i.e. using KML format with ZIP compression. 
     101            @param filename: Name of the KMZ output file. 
     102            ''' 
     103            # Write the KMZ archive 
     104            buf = StringIO() 
     105            _save(buf) 
     106            kmzFile = ZipFile(filename, 'w') 
     107            kmzFile.writestr('doc.kml', buf.read()) 
     108            kmzFile.close() 
     109            buf.close() 
    85110 
    86111        # Check the suffix of outputFilename, and depending on suffix, either save the document 
     
    88113        suffix = outputFilename[-3:].lower() 
    89114        if suffix == 'kml': 
    90             _save(outputFilename) 
     115            _saveKml(outputFilename) 
    91116        elif suffix == 'kmz': 
    92             raise NotImplementError('Needs re-testing') 
    93             ##shortOutputFilename = outputFilename.split('/')[-1]          # separate out the short filename 
    94             ##outputDir = outputFilename[:-(len(shortOutputFilename)+1)]   # separate out the directory name 
    95             ##_save(outputDir + '/doc.kml') 
    96             ##os.system('zip -q %s %s/doc.kml' % (outputFilename, outputDir)) 
    97             ##os.remove(outputDir + '/doc.kml') 
     117            #shortOutputFilename = outputFilename.split('/')[-1]          # separate out the short filename 
     118            #outputDir = outputFilename[:-(len(shortOutputFilename)+1)]   # separate out the directory name 
     119            #_save(outputDir + '/doc.kml') 
     120            #os.system('zip -q %s %s/doc.kml' % (outputFilename, outputDir)) 
     121            #os.remove(outputDir + '/doc.kml') 
     122            _saveKmz(outputFilename) 
    98123        else: 
    99124            raise ValueError('Wrong file suffix, only "kml" and "kmz" allowed.') 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/WMSCapabilities.py

    r3549 r3555  
    4646    def toKML(self, wmsRequestConfigElement, viewTypes, parentDir, parentDirUrl): 
    4747 
    48         title_ = self.title.replace(' ', '_')  # self.title underscored 
     48        title_ = self.title.replace(' ', '_').replace('/', '_').replace('\\', '_')  # self.title underscored 
     49 
    4950        dir = parentDir + '/' + title_ 
    5051        dirUrl = parentDirUrl + '/' + title_ 
    51         filename = dir + '.kml' 
    52         fileUrl = dirUrl + '.kml' 
    53  
    54         # create directory currentDir 
     52        filename = dir + '.kmz' 
     53        fileUrl = dirUrl + '.kmz' 
     54 
     55        # create directory dir 
    5556        os.mkdir(dir) 
     57        print 'Created directory "%s".' % dir 
    5658 
    5759        kmlDocument = KMLDocument(self.title, []) 
     
    6163                ) 
    6264        kmlDocument.save(filename) 
    63      
    64         return KMLNetworkLink(self.title, fileUrl) 
     65        print 'Saved file "%s".' % filename 
     66     
     67        return KMLNetworkLink(self.title, fileUrl, visible = False) 
    6568 
    6669class BottomWMSLayer(WMSLayer): 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testWMSCapabilities.py

    r3549 r3555  
    99wmsRequestConfigElement = configElementTree.getroot().find('WMSRequest') 
    1010 
    11 ###etree = ElementTree() 
    12 ###etree.parse('../../../../testdata/ar4-shortened.xml') 
    13 ###wmsCapabilities = WMSCapabilities() 
    14 ###wmsCapabilitiesElement = etree.getroot() 
    15 ###print wmsCapabilities 
    16  
    1711wmsCapabilitiesUrl = wmsRequestConfigElement.find('URL').text + '?request=GetCapabilities' 
    1812wmsCapabilitiesElement = XML( utils.wget(wmsCapabilitiesUrl) ) 
     
    2115 
    2216wmsLayersConvertor = WMSLayersConvertor( 
    23     wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-obs2', 'http://bond.badc.rl.ac.uk' 
     17    wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-obs2', 'http://bond.badc.rl.ac.uk:8089' 
    2418    ) 
    2519wmsLayersConvertor.convert() 
Note: See TracChangeset for help on using the changeset viewer.