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

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

File:
1 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.') 
Note: See TracChangeset for help on using the changeset viewer.