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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/convertor.py@3234
Revision 3234, 3.5 KB checked in by mkochan, 13 years ago (diff)

Finished CSML-to-KML conversion for the climatology dataset. Changed naming of dataset to use CSML name and naming of meteorological features to use feature description preferentially over feature (short)name.

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        kmlDocumentElement=Element('Document')
40        SubElement(kmlDocumentElement, 'name').text=self.ds.name.CONTENT
41        SubElement(kmlDocumentElement, 'open').text='1'
42
43        #!!datasetName = os.path.split(self.csmlFilename)[1].split('.')[0]
44
45        # Create and populate a separate folder for each view
46        for viewConfig in self.config.getRoot().findall('View'):
47            kmlViewElement = Element('Folder')
48            SubElement(kmlViewElement, 'name').text=viewConfig.get('name')
49            SubElement(kmlViewElement, 'open').text='1'
50           
51            # Create a separate KML folder for each feature
52            features = self.ds.featureCollection.featureMembers
53            for feature in features:
54               
55                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
56                gsFeature=GridSeriesFeatureWrapper(feature)
57                description = feature.description.CONTENT   
58                name = feature.name.CONTENT
59                bBox = gsFeature.getBBox()
60                timeSteps = gsFeature.getTimeSteps()
61                timeBounds = gsFeature.getTimeBounds()
62
63                # Instantiate a GridSeriesKML object with this information
64                gsKML = kmlfeatures.GridSeriesKML(
65                    self.config, viewConfig, self.csmlFilename, 
66                    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.