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/KML.py

    r3545 r3549  
     1import os 
     2import sys 
    13from cElementTree import ElementTree, Element, SubElement, XML 
    24 
     
    4648    def save(self, outputFilename): 
    4749        ''' 
    48         Save the document to file <code>outputFilename</code>. 
     50        Save the document to file <code>outputFilename</code> (full path). 
    4951        ''' 
    5052 
    51         # Auxiliary function, indents XML 
    5253        def _indentXML(elem, level=0): 
     54            '''Auxiliary function, indents XML''' 
    5355            i = "\n" + level * "  " 
    5456            if len(elem): 
     
    6567                    elem.tail = i 
    6668 
    67         # Build the Document element 
    68         documentElement = self.build() 
    69  
    70         # Attach the Document element as a subelement of a root 'kml' element 
    71         rootElement=Element('kml', xmlns='http://earth.google.com/kml/2.2') 
    72         rootElement.append(documentElement) 
    73         _indentXML(rootElement) 
    74  
    75         # Write the KML document to a file 
    76         elementTree = ElementTree(rootElement)        
    77         kmlFile = open(outputFilename, 'w') 
    78         elementTree.write(kmlFile) 
    79         kmlFile.close() 
     69        def _save(filename): 
     70            '''Builds and saves the document in the KML format into the file filename.''' 
     71 
     72            # Build the Document element 
     73            documentElement = self.build() 
     74 
     75            # Attach the Document element as a subelement of a root 'kml' element 
     76            rootElement=Element('kml', xmlns='http://earth.google.com/kml/2.2') 
     77            rootElement.append(documentElement) 
     78            _indentXML(rootElement) 
     79 
     80            # Write the KML document to a file 
     81            elementTree = ElementTree(rootElement)        
     82            kmlFile = open(filename, 'w') 
     83            elementTree.write(kmlFile) 
     84            kmlFile.close() 
     85 
     86        # Check the suffix of outputFilename, and depending on suffix, either save the document 
     87        # directly to a KML file, or save it packed in a KMZ file. 
     88        suffix = outputFilename[-3:].lower() 
     89        if suffix == 'kml': 
     90            _save(outputFilename) 
     91        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') 
     98        else: 
     99            raise ValueError('Wrong file suffix, only "kml" and "kmz" allowed.') 
    80100 
    81101class KMLStyle(KMLElement): 
     
    290310 
    291311class KMLNetworkLink(KMLElement): 
    292     def __init__(self): 
    293         pass 
    294  
    295     def build(self): 
    296         pass 
     312 
     313    def __init__(self, name, url, visible = True): 
     314        self.name = name 
     315        self.url = url 
     316        self.visible = visible 
     317 
     318    def build(self): 
     319        networkLinkElement = Element('NetworkLink') 
     320 
     321        SubElement(networkLinkElement, 'name').text = self.name 
     322 
     323        if self.visible: 
     324            SubElement(networkLinkElement, 'visibility').text = '1' 
     325        else: 
     326            SubElement(networkLinkElement, 'visibility').text = '0' 
     327 
     328        linkElement = SubElement(networkLinkElement, 'Link') 
     329        SubElement(linkElement, 'href').text = self.url 
     330 
     331        return networkLinkElement 
Note: See TracChangeset for help on using the changeset viewer.