source: DPPP/kml/csml2kml/python/convertor.py @ 3219

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/convertor.py@3219
Revision 3219, 3.5 KB checked in by mkochan, 13 years ago (diff)
Line 
1import os
2from cElementTree import ElementTree, Element, SubElement, tostring, dump
3
4import csml.parser
5import kmlfeatures
6from csmlwrappers import GridSeriesFeatureWrapper
7from configuration import Configuration
8
9''' Defines a class which performs conversion of a CSML file to a KML file.'''
10
11# [UNSAFE] No exception handling.
12class Convertor:
13    def __init__(self, csmlFilename, configFilename, kmlFilename):
14        self.csmlFilename = csmlFilename
15        self.kmlFilename = kmlFilename
16        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
17        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
18        self.config = Configuration(configFilename)  # read in the config file (on per CSML file basis)
19
20    # Auxiliary function, indents XML
21    def _indentXML(self, elem, level=0):
22        i = "\n" + level * "  "
23        if len(elem):
24            if not elem.text or not elem.text.strip():
25                elem.text = i + "  "
26            for child in elem:
27                self._indentXML(child, level+1)
28            if not child.tail or not child.tail.strip():
29                child.tail = i
30            if not elem.tail or not elem.tail.strip():
31                elem.tail = i
32        else:
33            if level and (not elem.tail or not elem.tail.strip()):
34                elem.tail = i
35
36    def writeKML(self):
37       
38        # Start with an empty KML document
39        kmlDocName='TEST GridSeries KML'
40        kmlDocumentElement=Element('Document')
41        SubElement(kmlDocumentElement, 'name').text=kmlDocName
42        SubElement(kmlDocumentElement, 'open').text='1'
43
44        datasetName = os.path.split(self.csmlFilename)[1].split('.')[0]
45
46        # Create and populate a separate folder for each view
47        for viewConfig in self.config.getRoot().findall('View'):
48            kmlViewElement = Element('Folder')
49            SubElement(kmlViewElement, 'name').text=view.get('name')
50            SubElement(kmlViewElement, 'open').text='1'
51           
52            # Create a separate KML folder for each feature
53            features = self.ds.featureCollection.featureMembers
54            for feature in features:
55               
56                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
57                gsFeature=GridSeriesFeatureWrapper(feature)
58                description = feature.description.CONTENT   
59                name = feature.name.CONTENT
60                bBox = gsFeature.getBBox()
61                timeSteps = gsFeature.getTimeSteps()
62                timeBounds = gsFeature.getTimeBounds()
63
64                # Instantiate a GridSeriesKML object with this information
65                gsKML = kmlfeatures.GridSeriesKML(
66                    self.config, self.csmlFilename, name, description, bBox, timeSteps, timeBounds
67                    )
68
69                # Generate a populated KML folder containing the feature, append it to the view
70                kmlFeatureElement = gsKML.exportFeature(viewConfig)
71                kmlViewElement.append(kmlFeatureElement)
72           
73            kmlDocumentElement.append(kmlViewElement)
74           
75        # Attach the Document element as a subelement of the root 'kml' element
76        kmlRootElement=Element('kml', xmlns='http://earth.google.com/kml/2.2')
77        kmlRootElement.append(kmlDocumentElement)
78        self._indentXML(kmlRootElement, 0)
79
80        # Write the KML document to a file
81        tree = ElementTree(kmlRootElement)       
82        f = open(self.kmlFilename, 'w')
83        tree.write(f)
84        f.close()
Note: See TracBrowser for help on using the repository browser.